-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldView.java
More file actions
62 lines (52 loc) · 2.04 KB
/
WorldView.java
File metadata and controls
62 lines (52 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import processing.core.PApplet;
import processing.core.PImage;
import java.util.Optional;
public final class WorldView {
private final PApplet screen;
private final WorldModel world;
private final int tileWidth;
private final int tileHeight;
private final Viewport viewport;
public WorldView(int numRows, int numCols, PApplet screen, WorldModel world, int tileWidth, int tileHeight) {
this.screen = screen;
this.world = world;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.viewport = new Viewport(numRows, numCols);
}
public void drawBackground() {
for (int row = 0; row < viewport.getNumRows(); row++) {
for (int col = 0; col < viewport.getNumCols(); col++) {
Point worldPoint = viewport.viewportToWorld(col, row);
Optional<PImage> image = world.getBackgroundImage(worldPoint);
if (image.isPresent()) {
screen.image(image.get(), col * tileWidth, row * tileHeight);
}
}
}
}
public void drawEntities() {
for (Entity entity : world.getEntities()) {
Point pos = entity.getPosition();
if (viewport.contains(pos)) {
Point viewPoint = viewport.worldToViewport(pos.x, pos.y);
screen.image(entity.getCurrentImage(), viewPoint.x * tileWidth, viewPoint.y * tileHeight);
}
}
}
public void drawViewport() {
this.drawBackground();
this.drawEntities();
}
public void shiftView(int colDelta, int rowDelta) {
int newCol = clamp(viewport.getCol() + colDelta, 0, world.getNumCols() - viewport.getNumCols());
int newRow = clamp(viewport.getRow() + rowDelta, 0, world.getNumRows() - viewport.getNumRows());
viewport.shift(newCol, newRow);
}
private static int clamp(int value, int low, int high) {
return Math.min(high, Math.max(value, low));
}
public Viewport getViewport() {
return viewport;
}
}