Skip to content

Commit

Permalink
Fix blinking enemies
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
Jupi007 committed Oct 2, 2021
1 parent 8cd9c1e commit 589bf58
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 47 deletions.
1 change: 1 addition & 0 deletions build/Enemies.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Enemy extends MovingBox {
this.hasViewportCollision = true;
this.hasBricksCollisions = true;
this.requirePassBetweenBoxHelper = false;
this.isKilledAnimationFinished = false;
this.killedSprites[1] = SpriteLoader.load("./sprites/png/killed1.png");
this.killedSprites[2] = SpriteLoader.load("./sprites/png/killed2.png");
this.invincibleObserver = new StateObserver(false);
Expand Down
26 changes: 15 additions & 11 deletions build/EnemyManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ export class EnemyManager {
callback(enemy);
});
}
removeEnemy(enemy) {
const enemyIndex = this.enemies.indexOf(enemy);
if (enemyIndex > -1) {
this.enemies.splice(enemyIndex, 1);
}
if (this.Game.EnemyManager.enemies.length <= 0) {
this.Game.Player.increaseScore();
}
enemy.dropItem();
}
aiThinking() {
this.loopEnemies((enemy) => {
enemy.aiThinking();
Expand Down Expand Up @@ -64,7 +54,7 @@ export class EnemyManager {
this.Game.Viewport.currentScene.drawImage(enemy.killedSprites[2], enemy.x, enemy.y, enemy.width, enemy.height);
}
else {
this.Game.EnemyManager.removeEnemy(enemy);
enemy.isKilledAnimationFinished = true;
}
return;
}
Expand All @@ -80,6 +70,20 @@ export class EnemyManager {
enemy.spritesAnimation.update(this.Game.dt);
});
}
removeKilled(forceRemoving = false) {
this.loopEnemies((enemy) => {
if (enemy.isKilledAnimationFinished || (forceRemoving && enemy.state.is(EnemyState.Killed))) {
const enemyIndex = this.enemies.indexOf(enemy);
if (enemyIndex > -1) {
this.enemies.splice(enemyIndex, 1);
}
if (this.Game.EnemyManager.enemies.length <= 0) {
this.Game.Player.increaseScore();
}
enemy.dropItem();
}
});
}
updateObservers() {
this.loopEnemies((enemy) => {
enemy.state.update(this.Game.dt);
Expand Down
1 change: 1 addition & 0 deletions build/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export class Game {
this.EnemyManager.move();
this.ProjectileManager.move();
this.drawGame();
this.EnemyManager.removeKilled();
this.Player.updateObservers();
this.EnemyManager.updateObservers();
this.ProjectileManager.updateObservers();
Expand Down
13 changes: 2 additions & 11 deletions build/Viewport.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GameState } from "./Game.js";
import { Direction } from "./Libraries/Direction.js";
import { Collisions } from "./Libraries/Collisions.js";
import { EnemyState } from "./Enemies.js";
import { EnemyManager } from "./EnemyManager.js";
export class Viewport {
constructor(game) {
Expand Down Expand Up @@ -174,11 +173,7 @@ export class Viewport {
this.currentScene.permanentItems = this.Game.ItemManager.permanentItems;
this.currentScene = this.nextScene;
this.nextScene = null;
this.Game.EnemyManager.loopEnemies((enemy) => {
if (enemy.state.is(EnemyState.Killed)) {
this.Game.EnemyManager.removeEnemy(enemy);
}
});
this.Game.EnemyManager.removeKilled(true);
this.Game.EnemyManager = new EnemyManager(this.Game);
this.Game.ProjectileManager.deleteAllProjectiles();
this.Game.ItemManager.deleteAllItems();
Expand Down Expand Up @@ -225,11 +220,7 @@ export class Viewport {
this.currentScene = this.nextScene;
this.nextWorld = null;
this.nextScene = null;
this.Game.EnemyManager.loopEnemies((enemy) => {
if (enemy.state.is(EnemyState.Killed)) {
this.Game.EnemyManager.removeEnemy(enemy);
}
});
this.Game.EnemyManager.removeKilled(true);
this.Game.EnemyManager = new EnemyManager(this.Game);
this.Game.ProjectileManager.deleteAllProjectiles();
this.Game.ItemManager.deleteAllItems();
Expand Down
4 changes: 4 additions & 0 deletions src/ts/Enemies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export class Enemy extends MovingBox {
speed: number;
damage: number;

isKilledAnimationFinished: boolean;

hasPlayerCollision: boolean;
hasViewportCollision: boolean;
hasBricksCollisions: boolean;
Expand All @@ -45,6 +47,8 @@ export class Enemy extends MovingBox {
this.hasBricksCollisions = true;
this.requirePassBetweenBoxHelper = false;

this.isKilledAnimationFinished = false;

this.killedSprites[1] = SpriteLoader.load("./sprites/png/killed1.png");
this.killedSprites[2] = SpriteLoader.load("./sprites/png/killed2.png");

Expand Down
34 changes: 19 additions & 15 deletions src/ts/EnemyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ export class EnemyManager {
});
}

removeEnemy(enemy: Enemy): void {
const enemyIndex = this.enemies.indexOf(enemy);

if (enemyIndex > -1) {
this.enemies.splice(enemyIndex, 1);
}

if (this.Game.EnemyManager.enemies.length <= 0) {
this.Game.Player.increaseScore();
}

enemy.dropItem();
}

aiThinking(): void {
this.loopEnemies((enemy: Enemy) => {
enemy.aiThinking();
Expand Down Expand Up @@ -95,7 +81,7 @@ export class EnemyManager {
);
}
else {
this.Game.EnemyManager.removeEnemy(enemy);
enemy.isKilledAnimationFinished = true;
}
return;
}
Expand All @@ -112,6 +98,24 @@ export class EnemyManager {
});
}

removeKilled(forceRemoving: boolean = false): void {
this.loopEnemies((enemy: Enemy) => {
if (enemy.isKilledAnimationFinished || (forceRemoving && enemy.state.is(EnemyState.Killed))) {
const enemyIndex = this.enemies.indexOf(enemy);

if (enemyIndex > -1) {
this.enemies.splice(enemyIndex, 1);
}

if (this.Game.EnemyManager.enemies.length <= 0) {
this.Game.Player.increaseScore();
}

enemy.dropItem();
}
});
}

updateObservers(): void {
this.loopEnemies((enemy: Enemy) => {
enemy.state.update(this.Game.dt);
Expand Down
2 changes: 2 additions & 0 deletions src/ts/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ export class Game {

this.drawGame();

this.EnemyManager.removeKilled();

this.Player.updateObservers();
this.EnemyManager.updateObservers();
this.ProjectileManager.updateObservers();
Expand Down
12 changes: 2 additions & 10 deletions src/ts/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,7 @@ export class Viewport {
this.currentScene = this.nextScene;
this.nextScene = null;

this.Game.EnemyManager.loopEnemies((enemy: Enemy) => {
if (enemy.state.is(EnemyState.Killed)) {
this.Game.EnemyManager.removeEnemy(enemy);
}
})
this.Game.EnemyManager.removeKilled(true);
this.Game.EnemyManager = new EnemyManager(this.Game);
this.Game.ProjectileManager.deleteAllProjectiles();
this.Game.ItemManager.deleteAllItems();
Expand Down Expand Up @@ -340,11 +336,7 @@ export class Viewport {
this.nextWorld = null;
this.nextScene = null;

this.Game.EnemyManager.loopEnemies((enemy: Enemy) => {
if (enemy.state.is(EnemyState.Killed)) {
this.Game.EnemyManager.removeEnemy(enemy);
}
});
this.Game.EnemyManager.removeKilled(true);

this.Game.EnemyManager = new EnemyManager(this.Game);
this.Game.ProjectileManager.deleteAllProjectiles();
Expand Down

0 comments on commit 589bf58

Please sign in to comment.