-
Notifications
You must be signed in to change notification settings - Fork 0
/
Person.js
108 lines (86 loc) · 2.96 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class Person extends GameObject{
constructor(config){
super(config)
this.movementProgressRemaining = 0
this.isStanding = false
this.intentPosition = null //[x,y]
this.isPlayerControlled = config.isPlayerControlled || false
this.directionUpdate = {
"up": ["y",-1],
"down": ["y",1],
"left": ["x",-1],
"right": ["x",1],
}
}
update(state){
if (this.movementProgressRemaining > 0){
this.updatePosition()
} else {
//More cases for starting to walk
//Case: Keyboard ready and have arrow pressed
if (!state.map.isCutscenePlaying && this.isPlayerControlled && state.arrow){
this.startBehavior(state, {
type:"walk",
direction: state.arrow
})
}
this.updateSprite(state)
}
// console.log(this.x/16,this.y/16)
}
startBehavior(state, behavior){
if (!this.isMounted) {
return
}
//Set character direction to behavior
this.direction = behavior.direction
if (behavior.type === "walk"){
//Stop if space not here
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.movementProgressRemaining = 16
//Add next position
const intentPosition = utils.nextPosition(this.x,this.y,this.direction)
this.intentPosition = [
intentPosition.x,
intentPosition.y
]
this.updateSprite(state)
}
if (behavior.type === "stand"){
this.isStanding = true
setTimeout(() => {
//We finished the walk!
this.intentPosition = null
utils.emitEvent("PersonStandComplete", {
whoId: this.id
})
this.isStanding = false
}, behavior.time)
}
}
updatePosition() {
const [property, change] = this.directionUpdate[this.direction]
this[property] += change
this.movementProgressRemaining -= 1
if (this.movementProgressRemaining === 0){
//We finished walk
utils.emitEvent("PersonWalkingComplete", {
whoId: this.id
})
}
}
updateSprite(state){
if (this.movementProgressRemaining > 0){
this.sprite.setAnimation("walk-"+this.direction)
return
}
this.sprite.setAnimation("idle-"+this.direction)
}
}