2024-07-30 18:56:24 +00:00
|
|
|
class Person extends GameObject {
|
|
|
|
constructor(config) {
|
|
|
|
super(config);
|
|
|
|
this.movementProgressRemaining = 0;
|
2024-08-05 16:14:13 +00:00
|
|
|
this.isStanding = false;
|
2024-07-30 18:56:24 +00:00
|
|
|
|
|
|
|
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 come here
|
|
|
|
|
|
|
|
// case: keyboard ready and arrow pressed
|
2024-08-02 14:01:51 +00:00
|
|
|
if (state.map.isCutscenePlaying == false && this.isPlayerControlled && state.arrow) {
|
2024-07-30 18:56:24 +00:00
|
|
|
this.startBehavior(state, {
|
|
|
|
type: "walk",
|
|
|
|
direction: state.arrow
|
|
|
|
})
|
|
|
|
}
|
|
|
|
this.updateSprite(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startBehavior(state, behavior) {
|
|
|
|
// set character direction to behavior
|
|
|
|
this.direction = behavior.direction;
|
|
|
|
if (behavior.type === "walk") {
|
|
|
|
// stop if space is not free
|
|
|
|
if (state.map.isSpaceTaken(this.x, this.y, this.direction)) {
|
2024-08-02 14:01:51 +00:00
|
|
|
behavior.retry && setTimeout(() => {
|
|
|
|
this.startBehavior(state, behavior);
|
|
|
|
}, 10);
|
2024-07-30 18:56:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ready to walk
|
|
|
|
state.map.moveWall(this.x, this.y, this.direction);
|
|
|
|
this.movementProgressRemaining = 16;
|
2024-08-02 14:01:51 +00:00
|
|
|
this.updateSprite(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (behavior.type = "stand") {
|
2024-08-05 16:14:13 +00:00
|
|
|
this.isStanding = true;
|
2024-08-02 14:01:51 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
utils.emitEvent("PersonStandComplete", {
|
|
|
|
whoId: this.id
|
|
|
|
})
|
2024-08-05 16:14:13 +00:00
|
|
|
this.isStanding = false;
|
2024-08-02 14:01:51 +00:00
|
|
|
}, behavior.time);
|
2024-07-30 18:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePosition() {
|
|
|
|
const [property, change] = this.directionUpdate[this.direction];
|
|
|
|
this[property] += change;
|
|
|
|
this.movementProgressRemaining -= 1;
|
2024-08-02 14:01:51 +00:00
|
|
|
|
|
|
|
if (this.movementProgressRemaining === 0) {
|
|
|
|
utils.emitEvent("PersonWalkingComplete", {
|
|
|
|
whoId: this.id
|
|
|
|
})
|
|
|
|
}
|
2024-07-30 18:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateSprite() {
|
|
|
|
if (this.movementProgressRemaining > 0) {
|
|
|
|
this.sprite.setAnimation("walk-" + this.direction);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.sprite.setAnimation("idle-" + this.direction);
|
|
|
|
}
|
|
|
|
}
|