-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldView.java
65 lines (54 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
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 shiftView(int colDelta, int rowDelta) {
int newCol = clamp( this.viewport.getCol() + colDelta, 0,
world.getNumCols() - this.viewport.getNumCols() );
int newRow = clamp( this.viewport.getRow() + rowDelta, 0,
world.getNumRows() - this.viewport.getNumRows() );
viewport.shift( newCol, newRow );
}
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 = world.getBackgroundImage( worldPoint );
if (image.isPresent()) {
screen.image( image.get(), col * tileWidth,
row * tileHeight );
}
}
}
}
public void drawEntities() {
for (Entity entity : world.entities()) {
Point pos = entity.getPosition();
if (viewport.contains( pos )) {
Point viewPoint = this.viewport.worldToViewport( pos.x, pos.y );
screen.image( entity.getCurrentImage(),
viewPoint.x * tileWidth, viewPoint.y * tileHeight );
}
}
}
public void drawViewport() {
drawBackground();
drawEntities();
}
public int clamp(int value, int low, int high) {
return Math.min( high, Math.max( value, low ) );
}
}