-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.js
More file actions
104 lines (86 loc) · 3.03 KB
/
Copy pathPerson.js
File metadata and controls
104 lines (86 loc) · 3.03 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Person extends GameObject {
constructor(config) {
super(config);
this.movingProgressRemaining = 0;
this.isStanding = false;
this.intentPosition = null; //either null or [x,y] coordinate
this.isPlayerControlled = config.isPlayerControlled || false;
this.directionUpdate = {
"up": ["y", -2],
"down": ["y", 2],
"left": ["x", -2],
"right": ["x", 2],
};
}
update(state) {
if (this.movingProgressRemaining > 0) {
this.updatePosition();
} else {
//Placeholder for more cases for starting to walk
//Basically the user is allowed to provide input and has an arrow pressed and no cutscene playing
if(state.map.isCutscenePlaying === false && this.isPlayerControlled === true && state.arrow) {
//move in that direction
this.startBehaviour(state, {
type: "walk",
direction: state.arrow
})
}
this.updateSprite(state);
}
}
startBehaviour(state, behaviour) {
if (!this.isMounted) {
return;
}
//setting the character direction to whatever behaviour has
this.direction = behaviour.direction;
if (behaviour.type === "walk") {
//stop here if space is not free
if (state.map.isSpaceTaken(this.x, this.y, this.direction)) {
behaviour.retry && setTimeout(() => {
this.startBehaviour(state, behaviour)
}, 10)
return;
}
// console.log(state.map.isSpaceTaken(this.x, this.y, this.direction));
console.log(this.x, this.y, this.direction);
//ready to walk
this.movingProgressRemaining = 16;
//Add next position intent
const intentPosition = utils.nextPosition(this.x, this.y, this.direction)
this.intentPosition = [
intentPosition.x,
intentPosition.y,
]
this.updateSprite(state)
}
if(behaviour.type === "stand") {
this.isStanding = true;
setTimeout(() => {
utils.emitEvent("PersonStandingComplete", {
whoId: this.id
})
this.isStanding = false;
}, behaviour.time)
}
}
updatePosition() {
const [property, change] = this.directionUpdate[this.direction];
this[property] += change;
this.movingProgressRemaining -= 1;
if(this.movingProgressRemaining === 0) {
//The player has finished the walk
this.intentPosition = null;
utils.emitEvent("PersonWalkingComplete", {
whoId: this.id
})
}
}
updateSprite() {
if (this.movingProgressRemaining > 0) {
this.sprite.setAnimation("walk-" + this.direction);
return;
}
this.sprite.setAnimation("idle-" + this.direction);
}
}