-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlThread.java
More file actions
113 lines (94 loc) · 3.09 KB
/
ControlThread.java
File metadata and controls
113 lines (94 loc) · 3.09 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package control;
import main.Main;
import org.lwjgl.input.Keyboard;
import camera.CameraUtils;
import thread.GameThread;
import assets.AssetContainer;
import assets.entities.Monster;
import assets.entities.Projectile;
/**
* First stab at a thread for handling input for player.
*/
public class ControlThread extends GameThread {
private boolean leftPressed = false;
private boolean rightPressed = false;
private boolean upPressed = false;
private boolean downPressed = false;
/** The time of the last iteration, to calculate the time delta. */
private long lastIterationTime;
public ControlThread(AssetContainer assContainer, int threadDelay, Main mainApplication) {
super(assContainer, threadDelay, mainApplication);
// Initialise the delta.
getIterationDelta();
}
// TODO: Should we be doing something with the getEventNanoseconds method?
// I'm confused about how this works given that we're taking events off a
// buffer - what if, since the last iteration, I've pressed left for 1
// millisecond and right for 5 milliseconds? I should end up turning
// slightly right, but I think I'll actually just continue forwards.
public void gameLoop() {
// Iterate over the key events buffer.
while (Keyboard.next()) {
// Identify which button(s) have been pressed / released.
if (Keyboard.getEventKey() == Keyboard.KEY_LEFT) {
leftPressed = Keyboard.getEventKeyState();
}
if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT) {
rightPressed = Keyboard.getEventKeyState();
}
if (Keyboard.getEventKey() == Keyboard.KEY_UP) {
upPressed = Keyboard.getEventKeyState();
}
if (Keyboard.getEventKey() == Keyboard.KEY_DOWN) {
downPressed = Keyboard.getEventKeyState();
}
if (Keyboard.getEventKey() == Keyboard.KEY_SPACE && Keyboard.getEventKeyState()) {
assContainer.addProjectile(assContainer.getPlayer().fireProjectile());
}
}
int timeDelta = getIterationDelta();
// All the time that either left or right are pressed, increase the
// rotation speed.
if (leftPressed || rightPressed) {
assContainer.getPlayer().accelerateRotation();
if (leftPressed) {
assContainer.getPlayer().rotateCCW(timeDelta);
} else {
assContainer.getPlayer().rotateCW(timeDelta);
}
} else {
assContainer.getPlayer().resetRotationSpeed();
}
if (downPressed || upPressed) {
if (downPressed) {
assContainer.getPlayer().decelerateMovement();
} else {
assContainer.getPlayer().accelerateMovement();
}
}
moveEntities(timeDelta);
}
private void moveEntities(int timeDelta) {
assContainer.getPlayer().move(timeDelta);
for (Monster m : assContainer.getMonsters()) {
if (m != null) {
m.rotate(timeDelta);
m.moveRandom();
}
}
for (Projectile projectile : assContainer.getProjectiles()) {
projectile.move(timeDelta);
}
}
/**
* Calculate the time delta between now and the previous iteration.
*
* @return Milliseconds since the last iteration.
*/
private int getIterationDelta() {
long time = CameraUtils.getTime();
int delta = (int) (time - lastIterationTime);
lastIterationTime = time;
return delta;
}
}