-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerson.js
83 lines (70 loc) · 3.03 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
class Person extends GameObject {
constructor(config) {
super(config); // Constructor for GameObject class
this.movingProgressRemaining = 0; // Person can't exist in-between cells, they have to be in a cell
this.isStanding = false;
this.isPlayerControlled = config.isPlayerControlled || false; // Only player controlled Persons will move with keyboard input
this.directionUpdate = { // What to do in each direction
"up": ["y", -1],
"down": ["y", 1],
"left": ["x", -1],
"right": ["x", 1]
}
}
update(state) {
if(this.movingProgressRemaining > 0) { // If we are in-between cells, continue finishing that move
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) {
// Setting character direction to behavior
this.direction = behavior.direction;
if(behavior.type === "walk") {
// If space is taken, don't move
if(state.map.isSpaceTaken(this.x, this.y, this.direction)) {
// If retry = true, try again in 10ms
behavior.retry && setTimeout(() => {
this.startBehavior(state, behavior)
}, 10)
return;
}
// Ready to walk!
state.map.moveWall(this.x, this.y, this.direction); // Move the "wall" that is on the character as character moves
this.movingProgressRemaining = 16;
this.updateSprite(state); // Play the animation frame also
}
if(behavior.type === "stand") {
this.isStanding = true; // So that standing actions can't overlap
setTimeout(() => {
utils.emitEvent("PersonStandComplete", { whoId: this.id })
this.isStanding = false;
}, behavior.time)
}
}
updatePosition() {
const [property, change] = this.directionUpdate[this.direction]; // If direction is "down", propery will be "y" and dir will be 1
this[property] += change;
this.movingProgressRemaining -= 1;
if(this.movingProgressRemaining === 0) {
// We finished the walk
utils.emitEvent("PersonWalkingComplete", { whoId: this.id })
}
}
updateSprite() { // Changing the animation depending on the direction we are looking towards
if (this.movingProgressRemaining > 0) { // If moving
this.sprite.setAnimation("walk-" + this.direction);
return;
}
this.sprite.setAnimation("idle-" + this.direction); // If not moving -> idle
}
}