-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGravityActor.java
70 lines (57 loc) · 1.79 KB
/
GravityActor.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
import mayflower.*;
// Actor thing that falls i guess
public class GravityActor extends Actor {
private float gravity = 0.2f;
private float velY;
final int TERMINAL = 5;
private boolean jumping = false;
public void act() {
// go downwards by velY
if (isFalling() || jumping) {
velY += gravity;
setLocation(getX(), getY() + velY);
}
// dont move when isblocked
if (isBlocked()) {
setLocation(getX(), getY() - 1);
jumping = false;
velY = 0;
}
if (velY >= TERMINAL)
velY = TERMINAL;
// jumping
if (Mayflower.isKeyPressed(Keyboard.KEY_SPACE) && !isFalling()) {
setLocation(getX(), getY() - 5);
jumping = true;
velY = -7;
}
// climbing
if (isTouching(Ladder.class)) {
velY = 0;
jumping = false;
if (Mayflower.isKeyDown(Keyboard.KEY_SPACE) || Mayflower.isKeyDown(Keyboard.KEY_UP)) {
setLocation(getX(), getY() - 1);
}
if (Mayflower.isKeyDown(Keyboard.KEY_DOWN)){
setLocation(getX(), getY() + 1);
if (isTouching(Block.class))
setLocation(getX(), getY() - 1);
}
}
}
// blocked and falling checks
private boolean isBlocked() {
return isTouching(Block.class);
}
public boolean isFalling() {
boolean ret;
setLocation(getX(), getY() + 1);
ret = isTouching(Block.class) || isTouching(Ladder.class) ;
setLocation(getX(), getY() - 1);
return !ret;
}
public boolean isJumping() {
return velY < 0;
}
public boolean isClimbing() { return isTouching(Ladder.class); }
}