-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.java
More file actions
81 lines (73 loc) · 2.05 KB
/
Copy pathCamera.java
File metadata and controls
81 lines (73 loc) · 2.05 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
81
import java.util.ArrayList;
public class Camera implements Position {
private double x;
private double y;
private int speed;
private int goalX;
private int goalY;
private ArrayList<int[]> goals;
private double zoomFactor;
public Camera(int initialX, int initialY) {
x = goalX = initialX;
y = goalY = initialY;
zoomFactor = 1;
speed = 1;
goals = new ArrayList<int[]>();
}
//moves camera toward given goal by speed
public void updateCamera(int gX, int gY) {
goalX = gX;
goalY = gY;
if (Math.sqrt(Math.pow(x - goalX, 2) + Math.pow(y - goalY, 2)) <= speed) {
x = goalX;
y = goalY;
}
else {
double angle = Math.atan((goalY - y) / (goalX - x));
double changeX = (goalX > x) ? speed * Math.cos(angle) : -speed * Math.cos(angle);
double changeY = (goalY > y) ? speed * Math.sin(angle) : -speed * Math.sin(angle);
if (gX > x) {
changeX = Math.abs(changeX);
}
else {
changeX = -Math.abs(changeX);
}
if (gY > y) {
changeY = Math.abs(changeY);
}
else {
changeY = -Math.abs(changeY);
}
x += changeX;
y += changeY;
}
}
//moves camera toward existing goal by speed
public void updateCamera() {
updateCamera(goalX, goalY);
}
public void addGoal(int gX, int gY) {
goals.add(new int[] {gX, gY});
}
public void averageGoals() {
if (goals.size() > 0) {
int sumX = 0;
int sumY = 0;
for (int i = 0; i < goals.size(); i++) {
sumX += goals.get(i)[0];
sumY += goals.get(i)[1];
}
goalX = sumX / goals.size();
goalY = sumY / goals.size();
goals.clear();
}
}
//returns camera's current center coordinates
public int[] getCoords() {
return new int[] {(int) x, (int) y};
}
//returns camera's zoom level
public double getZoom() {
return zoomFactor;
}
}