-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewport.java
More file actions
80 lines (71 loc) · 2.49 KB
/
Viewport.java
File metadata and controls
80 lines (71 loc) · 2.49 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* The window with which we are viewing the world. The WorldModel
* is larger than the window we are using to look into the world.
* The Viewport has the row and column for where we are to start viewing
* and then the number of rows and columns we wish to currently view from
* that starting point.
*/
public final class Viewport {
private int row;
private int col;
private final int numRows;
private final int numCols;
public Viewport(int numRows, int numCols) {
this.numRows = numRows;
this.numCols = numCols;
}
/**
* Checks if a point is in the current view of the world
* we have.
* @param p - the location we are checking
* @return - true if the location is in the viewport, and false otherwise
*/
public boolean contains(Point p) {
return p.y >= row && p.y < row + numRows && p.x >= col && p.x < col + numCols;
}
/**
* Shift our camera / viewport so we see a different part of the world
* @param col - the column we want to move our camera / viewport to
* @param row - the row we want to move our camera / viewport to
*/
public void shift(int col, int row) {
this.col = col;
this.row = row;
}
/**
* The viewport is a smaller grid withing our larger WorldModel grid.
* This function does the math to take a location in our WorldModel and
* transform it to be the proper location within our current view.
* @param col - column from the world model
* @param row - row from the world model
* @return - location in the context of our viewport instead of world model
*/
public Point worldToViewport(int col, int row) {
return new Point(col - this.col, row - this.row);
}
/**
* The viewport is a smaller grid withing our larger WorldModel grid.
* This function does the math to take a location in our Viewport
* (e.g. current view) and transforms it to be the proper location
* within our WorldModel.
*
* @param col - column from the viewport
* @param row - row from the viewport
* @return - location in the context of our world model, not viewport
*/
public Point viewportToWorld(int col, int row) {
return new Point(col + this.col, row + this.row);
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public int getNumRows() {
return numRows;
}
public int getNumCols() {
return numCols;
}
}