From e3d38cd730407b5703907b5b975389cbd9e9706e Mon Sep 17 00:00:00 2001 From: Nikita Iryupin Date: Fri, 4 Jun 2021 21:31:09 +0200 Subject: [PATCH] Fixed bug and added check on destroyed eagle --- src/Blocks/types/eagle.js | 6 +++++- src/GameLogic/collisionManager.js | 6 +++--- src/GameLogic/gameField.js | 21 +++++++++++++-------- src/Graphics/Map/map.js | 10 ++++++++++ src/Graphics/Map/mapParser.js | 6 ++++-- src/Tanks/aiTankManager.js | 6 +++++- src/game.js | 2 +- 7 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/Blocks/types/eagle.js b/src/Blocks/types/eagle.js index 7a81a7f..91ed49c 100644 --- a/src/Blocks/types/eagle.js +++ b/src/Blocks/types/eagle.js @@ -10,8 +10,12 @@ export class Eagle extends Block { } resolveAttack(box, direction) { - this._isDestroyed = true; this._tile = this._destroyedTile; + this._isDestroyed = true; + } + + isDestroyed() { + return this._isDestroyed; } diff --git a/src/GameLogic/collisionManager.js b/src/GameLogic/collisionManager.js index a57da27..7e0a861 100644 --- a/src/GameLogic/collisionManager.js +++ b/src/GameLogic/collisionManager.js @@ -33,8 +33,8 @@ export class CollisionManager { this._aiTanks.push(tank); } - deleteFromAITank() { - this._aiTanks = this._aiTanks.filter((e) => !e.canBeErased()); + deleteFromAITank(tank) { + this._aiTanks = this._aiTanks.filter(t => tank.x != t.x && tank.y != t.y); } checkCollisionForTank(xMin, xMax, yMin, yMax, tank) { @@ -65,7 +65,7 @@ export class CollisionManager { let collTank = this._checkTankCollision(xMin, xMax, yMin, yMax, tank); - if (collTank) { + if (collTank && collTank.id != tank.id) { collTank.isDestroyed = true; return true; } diff --git a/src/GameLogic/gameField.js b/src/GameLogic/gameField.js index ab473b4..be09a61 100644 --- a/src/GameLogic/gameField.js +++ b/src/GameLogic/gameField.js @@ -62,7 +62,8 @@ export class GameField { await this.tileset.loadAll(); } - async drawField() { + + async gameLoop() { if (!this.tileset) { await this.setupTileset(); } @@ -75,8 +76,18 @@ export class GameField { this.aiTanks.setCount(this.map._enemiesCount); } - let lateDraw = []; + if (this.map.getEagle().isDestroyed()) return; + if (this.aiTanks.isAllTanksDied()) return; + if (this.tank.canBeErased()) return; + this.drawField(); + + requestAnimationFrame(() => this.gameLoop()); + } + + drawField() { + + let lateDraw = []; for (let x = 0; x < this.map.width; x += 1) { for (let y = 0; y < this.map.height; y += 1) { @@ -102,12 +113,6 @@ export class GameField { el.element.draw(this.ctx, el.x * Constants.TILE_SIZE.getValue(), el.y * Constants.TILE_SIZE.getValue()); } - if (this.collisionManager.deleteFromAITank()); - - if (this.aiTanks.isAllTanksDied()) return; - if (this.tank.canBeErased()) return; - - requestAnimationFrame(() => this.drawField()); } } \ No newline at end of file diff --git a/src/Graphics/Map/map.js b/src/Graphics/Map/map.js index d22f172..e66bc86 100644 --- a/src/Graphics/Map/map.js +++ b/src/Graphics/Map/map.js @@ -4,12 +4,22 @@ export class Map { this.height = height; this._map = []; this._enemiesCount = enemiesCount; + + this._eagle = null; } pushElement(element) { this._map.push(element); } + getEagle() { + return this._eagle; + } + + setEagle(eagle) { + this._eagle = eagle; + } + getElement(x, y){ return this._map[x + y*this.width]; } diff --git a/src/Graphics/Map/mapParser.js b/src/Graphics/Map/mapParser.js index 33fa6cf..cd68386 100644 --- a/src/Graphics/Map/mapParser.js +++ b/src/Graphics/Map/mapParser.js @@ -62,8 +62,10 @@ export class MapParser { map.pushElement(builder.buildBrick(tileset.getImageOf(BlockTypeEnum.Brick))); break; case "E": - map.pushElement(builder.buildEagle(tileset.getImageOf(BlockTypeEnum.Eagle), - tileset.getImageOf(BlockTypeEnum.DestroyedEagle))) + let el = builder.buildEagle(tileset.getImageOf(BlockTypeEnum.Eagle), + tileset.getImageOf(BlockTypeEnum.DestroyedEagle)); + map.pushElement(el) + map.setEagle(el); break; default: map.pushElement(builder.buildEmpty()); diff --git a/src/Tanks/aiTankManager.js b/src/Tanks/aiTankManager.js index 5df058d..63606fb 100644 --- a/src/Tanks/aiTankManager.js +++ b/src/Tanks/aiTankManager.js @@ -34,7 +34,8 @@ export class AITankManager { if (this.checkIfCanSpawn()){ this._count--; - this._collision.addNewAITank(this.spawn()); + let tank = this.spawn(); + this._collision.addNewAITank(tank); } if (this._activeTanks.length >= this.MAX_TANKS_ON_FIELD) @@ -47,6 +48,7 @@ export class AITankManager { destroy(tank) { this._activeTanks = this._activeTanks.filter(t => tank.x != t.x && tank.y != t.y); + this._collision.deleteFromAITank(tank); this._canSpawn = true; } @@ -84,6 +86,8 @@ export class AITankManager { return tank; } } + + return null; } diff --git a/src/game.js b/src/game.js index e36b662..4416abc 100644 --- a/src/game.js +++ b/src/game.js @@ -43,6 +43,6 @@ export class Game { addEventListener("keyup", (ev) => { if (prevEv && prevEv.key == ev.key) prevEv = undefined; }); - requestAnimationFrame(() => this.gameField.drawField()); + requestAnimationFrame(() => this.gameField.gameLoop()); } } \ No newline at end of file