-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerson.js
92 lines (80 loc) · 2.17 KB
/
Person.js
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
class Person extends GameObject {
constructor(config) {
super(config);
this.movingProgressRemaining = 0;
this.isStanding = false;
this.isPlayerControlled = config.isPlayerControlled || false;
this.directionUpdate = {
up: ["y", -1],
down: ["y", 1],
left: ["x", -1],
right: ["x", 1],
};
}
update(state) {
if (this.movingProgressRemaining > 0) {
this.updatePosition();
} else {
//More cases for starting to walk will come here
//
//
//Case: We're keyboard ready and have an arrow pressed
if (
!state.map.isCutscenePlaying &&
this.isPlayerControlled &&
state.arrow
) {
this.startBehavior(state, {
type: "walk",
direction: state.arrow,
});
}
this.updateSprite(state);
}
}
startBehavior(state, behavior) {
//Set character direction to whatever behavior has
this.direction = behavior.direction;
if (behavior.type === "walk") {
//Stop here if space is not free
if (state.map.isSpaceTaken(this.x, this.y, this.direction)) {
behavior.retry &&
setTimeout(() => {
this.startBehavior(state, behavior);
}, 10);
return;
}
//Ready to walk!
state.map.moveWall(this.x, this.y, this.direction);
this.movingProgressRemaining = 16;
this.updateSprite(state);
}
if (behavior.type === "stand") {
this.isStanding = true;
setTimeout(() => {
utils.emitEvent("PersonStandComplete", {
whoId: this.id,
});
this.isStanding = false;
}, behavior.time);
}
}
updatePosition() {
const [property, change] = this.directionUpdate[this.direction];
this[property] += change;
this.movingProgressRemaining -= 1;
if (this.movingProgressRemaining === 0) {
//We finished the walk!
utils.emitEvent("PersonWalkingComplete", {
whoId: this.id,
});
}
}
updateSprite() {
if (this.movingProgressRemaining > 0) {
this.sprite.setAnimation("walk-" + this.direction);
return;
}
this.sprite.setAnimation("idle-" + this.direction);
}
}