-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera.java
104 lines (84 loc) · 1.91 KB
/
Camera.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Camera
{
// World Coordinates of the Camera
private int x;
private int y;
// Camera Movement
private int xOffset = 0;
private int yOffset = 0;
private double dx;
private double dy;
// Target sprite to follow (ex. Player)
private Sprite target;
Camera()
{
}
Camera(int x, int y)
{
target = new Sprite(x, y);
}
Camera(Sprite target)
{
this.target = target;
}
public void setTarget(Sprite target)
{
this.target = target;
}
public void setTarget(int x, int y)
{
this.target.setX(x);
this.target.setY(y);
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public Sprite getTarget()
{
return this.target;
}
public void shake()
{
shake(30, 30);
}
public void shake(int strength)
{
shake(strength, strength);
}
public void shake(int strengthX, int strengthY)
{
if(Math.abs(this.dx) < 1 && Math.abs(this.dy) < 1)
{
this.dx = strengthX;
this.dy = strengthY;
}
}
public void update()
{
this.x = (int)(this.target.getX() + dx);
this.y = (int)(this.target.getY() + dy);
// Reduce dx and dy gradually
this.dx *= -0.9;
this.dy *= -0.9;
// Reset offset
if(Math.abs(this.dx) < 1)
{
this.dx = 0.0;
}
if(Math.abs(this.dy) < 1)
{
this.dy = 0.0;
}
if(Math.abs(this.dx) < 1 && Math.abs(this.dy) < 1)
{
this.x = (int)target.getX();
this.y = (int)target.getY();
}
// System.out.println(this.dx + " " + this.dy);
}
}