-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovementController.java
More file actions
84 lines (72 loc) · 2.34 KB
/
MovementController.java
File metadata and controls
84 lines (72 loc) · 2.34 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
public class MovementController {
private static final double MOVEMENT_SPEED = 15; // blocks per second
private Game game;
private Vector3 velocity;
public MovementController(Game game) {
this.game = game;
this.velocity = new Vector3(0, 0, 0);
}
public void moveForward() {
if (velocity.getX() == 1) {
return;
}
velocity = velocity
.add(new Vector3(1, 0, 0));
}
public void moveBackward() {
if (velocity.getX() == -1) {
return;
}
velocity = velocity
.add(new Vector3(-1, 0, 0));
}
public void moveLeft() {
if (velocity.getZ() == 1) {
return;
}
velocity = velocity
.add(new Vector3(0, 0, 1));
}
public void moveRight() {
if (velocity.getZ() == -1) {
return;
}
velocity = velocity
.add(new Vector3(0, 0, -1));
}
public void moveUp() {
if (velocity.getY() == 1) {
return;
}
velocity = velocity
.add(new Vector3(0, 1, 0));
}
public void moveDown() {
if (velocity.getY() == -1) {
return;
}
velocity = velocity
.add(new Vector3(0, -1, 0));
}
public void update(double timeElapsed) {
double verticalRotation = game.getPlayer().getCamera().getVerticalRotation();
Vector3 v = velocity.normalise().multiply(MOVEMENT_SPEED * timeElapsed * game.getPlayer().getSpeed());
v = new Vector3(v.getX() * Math.cos(verticalRotation) - v.getZ() * Math.sin(verticalRotation),
v.getY(), v.getZ() * Math.cos(verticalRotation) + v.getX() * Math.sin(verticalRotation));
// Vector3 collisionVector = game.getPlayer().getCollisionVector(v);
setPlayerPosition(v);// .add(collisionVector));
}
private void setPlayerPosition(Vector3 offset) {
game.getPlayer().setPosition(game.getPlayer().getPosition().add(offset));
// Recalculate chunk coordinates
// More efficient than checking if the player is in a new chunk every tick
int newChunkX = (int) Math.floor(game.getPlayer().getPosition().getX() / Chunk.CHUNK_SIZE);
int newChunkZ = (int) Math.floor(game.getPlayer().getPosition().getZ() / Chunk.CHUNK_SIZE);
game.getLevel().updateChunksAfterCameraMovement(offset, game.getPlayer().getChunkX(),
game.getPlayer().getChunkZ(),
newChunkX,
newChunkZ);
game.getPlayer().setChunkX(newChunkX);
game.getPlayer().setChunkZ(newChunkZ);
}
}