Skip to content
Snippets Groups Projects
Commit bdc2fdf9 authored by Paolo.Brasolin's avatar Paolo.Brasolin
Browse files

feat: #fe clean up critter destroy logic

parent 75f13f7c
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,14 @@ const WALK_VELOCITY = 100; ...@@ -7,7 +7,14 @@ const WALK_VELOCITY = 100;
const FLEE_VELOCITY = -200; const FLEE_VELOCITY = -200;
const ESCAPE_VELOCITY = 300; const ESCAPE_VELOCITY = 300;
enum CritterState {
Moving,
Escaping,
Fleeing,
}
class Critter extends Phaser.Physics.Arcade.Sprite { class Critter extends Phaser.Physics.Arcade.Sprite {
state: CritterState;
clue: Clue; clue: Clue;
scene: FightScene; scene: FightScene;
species: string; species: string;
...@@ -34,34 +41,52 @@ class Critter extends Phaser.Physics.Arcade.Sprite { ...@@ -34,34 +41,52 @@ class Critter extends Phaser.Physics.Arcade.Sprite {
this.scene.physics.add.collider(this, this.scene.ground); this.scene.physics.add.collider(this, this.scene.ground);
this.setScale(scale); this.setScale(scale);
// this.setInteractive(true);
this.flipX = true;
this.play({ key: this.species + "_walk", repeat: -1 }); this.move();
// TODO: bring animal below grass
this.body.setVelocity(WALK_VELOCITY, 0); this.scene.physics.add.overlap(this.scene.player, this, () => {
this.escape();
this.clue.delete();
});
}
preUpdate(time, delta) {
super.preUpdate(time, delta);
if (
(this.state === CritterState.Escaping && this.isOutsideRightBound()) ||
(this.state === CritterState.Fleeing && this.isOutsideLeftBound())
) {
this.destroy();
}
}
isOutsideRightBound() {
return this.x - this.width * 0.5 > this.scene.cameras.main.width;
}
// here to implement health isOutsideLeftBound() {
this.scene.physics.add.overlap( return this.x + this.width * 0.5 < 0;
this.scene.player, }
this,
(_player, _enemy) => { move() {
this.play(this.species + "_run"); this.state = CritterState.Moving;
this.body.setVelocity(ESCAPE_VELOCITY, 0); this.flipX = true;
this.clue.delete(); this.play({ key: this.species + "_walk", repeat: -1 });
setTimeout(() => { this.body.setVelocity(WALK_VELOCITY, 0);
this.destroy();
}, 2000);
},
);
} }
flee() { flee() {
this.play(this.species + "_run"); this.state = CritterState.Fleeing;
this.flipX = false; this.flipX = false;
this.play(this.species + "_run");
this.body.setVelocity(FLEE_VELOCITY, 0); this.body.setVelocity(FLEE_VELOCITY, 0);
setTimeout(() => this.destroy(), 2000); // TODO: disappear offscreen }
escape() {
this.state = CritterState.Escaping;
this.flipX = true;
this.play(this.species + "_run");
this.body.setVelocity(ESCAPE_VELOCITY, 0);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment