Skip to content

Commit

Permalink
feat: apple transforms in a wall after 10 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Aug 23, 2022
1 parent a8572a9 commit 6f4d1f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Objects/Snake/Apple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@ export default class Apple extends Phaser.GameObjects.Sprite {
scene.add.existing(this);

this.isAlive = true;
this.isWall = false;
this.spriteName = sprite;

this.animation = scene.tweens.add({
targets: [this],
duration: 10000,
alpha: { from: 1, to: 0.7 },
onComplete: () => {
this.setTint(0xff0000);
this.isWall = true;
}
});
}

kill() {
this.animation.stop();
this.animation.remove();
this.isAlive = false;
this.destroy();
}
Expand Down
7 changes: 7 additions & 0 deletions src/Scenes/GamePlay/SnakeAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export default class SnakeAll extends Phaser.Scene {
EventSystem.removeListener(EVENTS_NAMES.gameOver);
EventSystem.on(EVENTS_NAMES.gameOver, (value) => {
this.isAlive = false;
this.player.isAlive = false;
this.newAppleTimer.paused = true;
this.tweens.pauseAll();
});

EventSystem.removeListener(EVENTS_NAMES.restartGame);
Expand Down Expand Up @@ -98,6 +100,11 @@ export default class SnakeAll extends Phaser.Scene {

this.apples.forEach(apple => {
if (this.player.head.x === apple.x && this.player.head.y === apple.y && apple.isAlive) {
if (apple.isWall) {
EventSystem.emit(EVENTS_NAMES.gameOver);
apple.setDepth(100000);
return;
}
this.player.grow(apple.spriteName);
apple.kill();
EventSystem.emit(EVENTS_NAMES.increaseScore, 1, apple, apple.spriteName);
Expand Down

0 comments on commit 6f4d1f8

Please sign in to comment.