-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActor.pde
106 lines (90 loc) · 1.77 KB
/
Actor.pde
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
class Actor extends Entity {
boolean goingUp, goingDown, goingLeft, goingRight;
boolean facingRight;
boolean onGround;
boolean jumping;
float speed;
float xVelocity;
float yVelocity;
int jumpTimer;
int jumpMax;
Actor() {
this(0, 0);
}
Actor(float x, float y) {
super(x, y);
speed = 1;
facingRight = false;
onGround = false;
jumping = false;
}
boolean hasGravity() {
return true;
}
void setVelocity() {
xVelocity = 0;
if (goingLeft) {
xVelocity -= speed;
if (!goingRight) {
facingRight = false;
}
}
if (goingRight) {
xVelocity += speed;
if (!goingLeft) {
facingRight = true;
}
}
if (hasGravity()) {
if (jumping && onGround) {
onGround = false;
jumpTimer = jumpMax;
}
if (jumpTimer > 0 && !onGround && jumping) {
jumpTimer--;
applyGravity(false);
}
else {
jumpTimer = 0;
applyGravity();
}
}
else {
yVelocity = 0;
if (goingUp) {
yVelocity -= speed;
}
if (goingDown) {
yVelocity += speed;
}
}
}
void applyGravity() {
applyGravity(true);
}
void applyGravity(boolean down) {
if (down) {
onGround = false;
if (yVelocity < speed * 2) {
yVelocity += speed / 10;
}
}
else {
if (yVelocity > speed * -2) {
yVelocity -= speed / 5;
}
}
}
void update() {
super.update();
setVelocity();
x += xVelocity;
y += yVelocity;
}
void handleTileCollision() {
// Do nothing, this is for overriding.
}
void handleEntityCollision(Entity other) {
// Do nothing, this is also for overriding.
}
}