-
Notifications
You must be signed in to change notification settings - Fork 3
/
WorldView.java
74 lines (63 loc) · 2.08 KB
/
WorldView.java
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
63
64
65
66
67
68
69
70
71
72
73
74
import processing.core.PApplet;
import processing.core.PImage;
import java.util.Optional;
final class WorldView
{
private PApplet screen;
private WorldModel world;
private int tileWidth;
private int tileHeight;
private 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 drawEntities()
{
for (Entity entity : this.world.getEntities())
{
Point pos = entity.getPosition();
if (this.viewport.contains(pos))
{
Point viewPoint = this.viewport.worldToViewport(pos.getX(), pos.getY());
this.screen.image(entity.getCurrentImage(),
viewPoint.getX() * this.tileWidth, viewPoint.getY() * this.tileHeight);
}
}
}
public void drawViewport()
{
drawBackground();
this.drawEntities();
}
public void drawBackground() {
for (int row = 0; row < this.viewport.getNumRows(); row++) {
for (int col = 0; col < this.viewport.getNumCols(); col++) {
Point worldPoint = this.viewport.viewportToWorld(col, row);
Optional<PImage> image = this.world.getBackgroundImage(
worldPoint);
if (image.isPresent()) {
this.screen.image(image.get(), col * this.tileWidth,
row * this.tileHeight);
}
}
}
}
public void shiftView(int colDelta, int rowDelta)
{
int newCol = clamp(this.viewport.getCol() + colDelta, 0,
this.world.getNumCols() - this.viewport.getNumCols());
int newRow = clamp(this.viewport.getRow() + rowDelta, 0,
this.world.getNumRows() - this.viewport.getNumRows());
this.viewport.shift(newCol, newRow);
}
public static int clamp(int value, int low, int high)
{
return Math.min(high, Math.max(value, low));
}
}