Fixed bug and added check on destroyed eagle

This commit is contained in:
Nikita Iryupin 2021-06-04 21:31:09 +02:00
parent 03e90daf35
commit e3d38cd730
7 changed files with 41 additions and 16 deletions

View File

@ -10,8 +10,12 @@ export class Eagle extends Block {
} }
resolveAttack(box, direction) { resolveAttack(box, direction) {
this._isDestroyed = true;
this._tile = this._destroyedTile; this._tile = this._destroyedTile;
this._isDestroyed = true;
}
isDestroyed() {
return this._isDestroyed;
} }

View File

@ -33,8 +33,8 @@ export class CollisionManager {
this._aiTanks.push(tank); this._aiTanks.push(tank);
} }
deleteFromAITank() { deleteFromAITank(tank) {
this._aiTanks = this._aiTanks.filter((e) => !e.canBeErased()); this._aiTanks = this._aiTanks.filter(t => tank.x != t.x && tank.y != t.y);
} }
checkCollisionForTank(xMin, xMax, yMin, yMax, tank) { checkCollisionForTank(xMin, xMax, yMin, yMax, tank) {
@ -65,7 +65,7 @@ export class CollisionManager {
let collTank = this._checkTankCollision(xMin, xMax, yMin, yMax, tank); let collTank = this._checkTankCollision(xMin, xMax, yMin, yMax, tank);
if (collTank) { if (collTank && collTank.id != tank.id) {
collTank.isDestroyed = true; collTank.isDestroyed = true;
return true; return true;
} }

View File

@ -62,7 +62,8 @@ export class GameField {
await this.tileset.loadAll(); await this.tileset.loadAll();
} }
async drawField() {
async gameLoop() {
if (!this.tileset) { if (!this.tileset) {
await this.setupTileset(); await this.setupTileset();
} }
@ -75,8 +76,18 @@ export class GameField {
this.aiTanks.setCount(this.map._enemiesCount); 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 x = 0; x < this.map.width; x += 1) {
for (let y = 0; y < this.map.height; y += 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()); 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());
} }
} }

View File

@ -4,12 +4,22 @@ export class Map {
this.height = height; this.height = height;
this._map = []; this._map = [];
this._enemiesCount = enemiesCount; this._enemiesCount = enemiesCount;
this._eagle = null;
} }
pushElement(element) { pushElement(element) {
this._map.push(element); this._map.push(element);
} }
getEagle() {
return this._eagle;
}
setEagle(eagle) {
this._eagle = eagle;
}
getElement(x, y){ getElement(x, y){
return this._map[x + y*this.width]; return this._map[x + y*this.width];
} }

View File

@ -62,8 +62,10 @@ export class MapParser {
map.pushElement(builder.buildBrick(tileset.getImageOf(BlockTypeEnum.Brick))); map.pushElement(builder.buildBrick(tileset.getImageOf(BlockTypeEnum.Brick)));
break; break;
case "E": case "E":
map.pushElement(builder.buildEagle(tileset.getImageOf(BlockTypeEnum.Eagle), let el = builder.buildEagle(tileset.getImageOf(BlockTypeEnum.Eagle),
tileset.getImageOf(BlockTypeEnum.DestroyedEagle))) tileset.getImageOf(BlockTypeEnum.DestroyedEagle));
map.pushElement(el)
map.setEagle(el);
break; break;
default: default:
map.pushElement(builder.buildEmpty()); map.pushElement(builder.buildEmpty());

View File

@ -34,7 +34,8 @@ export class AITankManager {
if (this.checkIfCanSpawn()){ if (this.checkIfCanSpawn()){
this._count--; this._count--;
this._collision.addNewAITank(this.spawn()); let tank = this.spawn();
this._collision.addNewAITank(tank);
} }
if (this._activeTanks.length >= this.MAX_TANKS_ON_FIELD) if (this._activeTanks.length >= this.MAX_TANKS_ON_FIELD)
@ -47,6 +48,7 @@ export class AITankManager {
destroy(tank) { destroy(tank) {
this._activeTanks = this._activeTanks.filter(t => tank.x != t.x && tank.y != t.y); this._activeTanks = this._activeTanks.filter(t => tank.x != t.x && tank.y != t.y);
this._collision.deleteFromAITank(tank);
this._canSpawn = true; this._canSpawn = true;
} }
@ -84,6 +86,8 @@ export class AITankManager {
return tank; return tank;
} }
} }
return null;
} }

View File

@ -43,6 +43,6 @@ export class Game {
addEventListener("keyup", (ev) => { addEventListener("keyup", (ev) => {
if (prevEv && prevEv.key == ev.key) prevEv = undefined; if (prevEv && prevEv.key == ev.key) prevEv = undefined;
}); });
requestAnimationFrame(() => this.gameField.drawField()); requestAnimationFrame(() => this.gameField.gameLoop());
} }
} }