diff --git a/public/stages/stage-1.json b/public/stages/stage-1.json index 0406486..9666f58 100644 --- a/public/stages/stage-1.json +++ b/public/stages/stage-1.json @@ -15,10 +15,7 @@ "X Bf X Bf X B8 Bc B4 X Bf X Bf X ", "X X X X X Ba Ef B5 X X X X X " ], - "bots": [ - "18*basic", - "2*fast" - ], + "bots": 20, "size": { "width": 13, "height": 13 diff --git a/public/stages/stage-2.json b/public/stages/stage-2.json index 8e8e08b..cfaa2cd 100644 --- a/public/stages/stage-2.json +++ b/public/stages/stage-2.json @@ -15,11 +15,7 @@ "X BF X X X B8 BC B4 X BF X BF X ", "X BF X BF X BA Ef B5 X BF BF BF X " ], - "bots": [ - "2*armor", - "4*fast", - "14*basic" - ], + "bots": 20, "size": { "width": 13, "height": 13 diff --git a/public/stages/stage-3.json b/public/stages/stage-3.json index c7df28e..adf50ee 100644 --- a/public/stages/stage-3.json +++ b/public/stages/stage-3.json @@ -15,11 +15,7 @@ "BF BF X T5 X B8 BC B4 X Ff Ff Ff X ", "TF BF BF X X BA Ef B5 X BF X X X " ], - "bots": [ - "14*basic", - "4*fast", - "2*armor" - ], + "bots": 20, "size": { "width": 13, "height": 13 diff --git a/public/stages/stage-4.json b/public/stages/stage-4.json index 7644382..068dfaf 100644 --- a/public/stages/stage-4.json +++ b/public/stages/stage-4.json @@ -15,12 +15,7 @@ "Ff X B3 B3 X B8 Bc B4 B3 B3 X Ff Ff ", "Tf Ff X X X Ba Ef B5 X X Ff Ff Tf " ], - "bots": [ - "10*power", - "5*fast", - "2*basic", - "3*armor" - ], + "bots": 20, "size": { "width": 13, "height": 13 diff --git a/src/Blocks/types/block.js b/src/Blocks/types/block.js index 288b538..e5034d4 100644 --- a/src/Blocks/types/block.js +++ b/src/Blocks/types/block.js @@ -11,6 +11,11 @@ export class Block { constructor(boxes, tile) { this._boxes = boxes; this._tile = tile; + this._type = null; + } + + getType(){ + return this._type; } draw(ctx, x, y){ diff --git a/src/Blocks/types/brick.js b/src/Blocks/types/brick.js index 0c98ecf..3e6e3fa 100644 --- a/src/Blocks/types/brick.js +++ b/src/Blocks/types/brick.js @@ -1,10 +1,12 @@ import { DirectionEnum } from "../../Tanks/directionEnum"; +import { BlockTypeEnum } from "../blockTypeEnum"; import { Block, Box } from "./block" export class Brick extends Block { constructor(boxes, tile) { super(boxes, tile); + this._type = BlockTypeEnum.Brick; } /** diff --git a/src/Blocks/types/eagle.js b/src/Blocks/types/eagle.js index 42b7a21..7a81a7f 100644 --- a/src/Blocks/types/eagle.js +++ b/src/Blocks/types/eagle.js @@ -1,11 +1,18 @@ +import { BlockTypeEnum } from "../blockTypeEnum"; import {Block} from "./block" export class Eagle extends Block { constructor(boxes, tile, destroyedTile) { super(boxes, tile); + this._type = BlockTypeEnum.Eagle; this._destroyedTile = destroyedTile; this._isDestroyed = false; } + resolveAttack(box, direction) { + this._isDestroyed = true; + this._tile = this._destroyedTile; + } + } \ No newline at end of file diff --git a/src/Blocks/types/forest.js b/src/Blocks/types/forest.js index 9df966f..6987ad5 100644 --- a/src/Blocks/types/forest.js +++ b/src/Blocks/types/forest.js @@ -1,8 +1,10 @@ +import { BlockTypeEnum } from "../blockTypeEnum"; import { Block } from "./block" export class Forest extends Block { constructor(boxes, tile){ super(boxes, tile); + this._type = BlockTypeEnum.Forest; } getAllBoxes(){ diff --git a/src/Blocks/types/iron.js b/src/Blocks/types/iron.js index 2c1b72c..30f2f37 100644 --- a/src/Blocks/types/iron.js +++ b/src/Blocks/types/iron.js @@ -1,8 +1,10 @@ +import { BlockTypeEnum } from "../blockTypeEnum"; import {Block} from "./block" export class Iron extends Block { constructor(boxes, tile) { super(boxes, tile); + this._type = BlockTypeEnum.Iron; } diff --git a/src/Blocks/types/river.js b/src/Blocks/types/river.js index 9d4c727..e2c1838 100644 --- a/src/Blocks/types/river.js +++ b/src/Blocks/types/river.js @@ -1,8 +1,10 @@ -import {Block} from "./block" +import { BlockTypeEnum } from "../blockTypeEnum"; +import { Block } from "./block" export class River extends Block { constructor(boxes, tile) { super(boxes, tile); + this._type = BlockTypeEnum.River; } diff --git a/src/GameLogic/collisionManager.js b/src/GameLogic/collisionManager.js index 32b6bf8..01f781c 100644 --- a/src/GameLogic/collisionManager.js +++ b/src/GameLogic/collisionManager.js @@ -1,3 +1,4 @@ +import { BlockTypeEnum } from "../Blocks/blockTypeEnum"; import { Constants } from "../constants"; import { Map } from "../Graphics/Map/map"; @@ -20,7 +21,7 @@ export class CollisionManager { return (this._checkTankCollision(xMin, xMax, yMin, yMax) != null) || (this._checkBoundaries(xMin, xMax, yMin, yMax) != null) || - (this._checkBlockCollision(xMin, xMax, yMin, yMax) != null); + (this._checkBlockCollision(xMin, xMax, yMin, yMax, false) != null); } @@ -28,10 +29,10 @@ export class CollisionManager { if (!this._map) return false; - let blockColl = this._checkBlockCollision(xMin, xMax, yMin, yMax); + let blockColl = this._checkBlockCollision(xMin, xMax, yMin, yMax, true); if (blockColl) { - for (let blockInfo of blockColl) { + for (let blockInfo of Array.from(blockColl)) { blockInfo.block.resolveAttack(blockInfo.boxId, direction); } return true; @@ -54,12 +55,15 @@ export class CollisionManager { return null; } - _checkBlockCollision(xMin, xMax, yMin, yMax) { + _checkBlockCollision(xMin, xMax, yMin, yMax, isShot) { const blocksToCheck = this._getAllElementsOfMapOn(xMin, xMax, yMin, yMax); const tilesize = Constants.TILE_SIZE.getValue(); let blockColl = new Set(); for (let i = 0; i < blocksToCheck.length; i++) { + if (isShot && blocksToCheck[i].block.getType() == BlockTypeEnum.River) + continue; + let boxes = blocksToCheck[i].block.getAllBoxes(); let x = blocksToCheck[i].xPos; let y = blocksToCheck[i].yPos; diff --git a/src/GameLogic/collisionType.js b/src/GameLogic/collisionType.js deleted file mode 100644 index 5ded716..0000000 --- a/src/GameLogic/collisionType.js +++ /dev/null @@ -1,6 +0,0 @@ -export class CollisionTypeEnum { - static Block = new CollisionTypeEnum(); - static Tank = new CollisionTypeEnum(); - static Shot = new CollisionTypeEnum(); - static Edge = new CollisionTypeEnum(); -} \ No newline at end of file diff --git a/src/GameLogic/gameField.js b/src/GameLogic/gameField.js index d59169a..edc9ced 100644 --- a/src/GameLogic/gameField.js +++ b/src/GameLogic/gameField.js @@ -1,10 +1,11 @@ import { Tileset } from "../Graphics/Tileset/tileset" -import { PlayerTank } from "../Tanks/playerTank"; +import { Tank } from "../Tanks/tank"; import { Constants } from "../constants" import { BlockTypeEnum } from "../Blocks/blockTypeEnum" import { MapParser } from "../Graphics/Map/mapParser"; import { CollisionManager } from "./collisionManager"; -import { Forest } from "./../Blocks/types/forest"; +import { AITankManager } from "./../Tanks/aiTankManager"; +import { DirectionEnum } from "../Tanks/directionEnum"; export class GameField { constructor(width = 512, height = 512, ctx, canvas) { @@ -13,7 +14,8 @@ export class GameField { this.ctx = ctx; this.canvas = canvas; this.collisionManager = new CollisionManager(); - this.tank = new PlayerTank(0, 0, this.collisionManager); + this.tank = new Tank(Constants.PLAYER_TANK_ID_IMAGE.getValue(), 0, 0, this.collisionManager); + this.aiTanks = new AITankManager(this.collisionManager) this.tileset = null; this.map = null; } @@ -39,10 +41,10 @@ export class GameField { this.tileset.addTile(544, 64, 32, 32, BlockTypeEnum.Forest); //Tank - this.tileset.addTile(0, 2, 28, 30, 101); - this.tileset.addTile(64, 0, 28, 28, 102); - this.tileset.addTile(128, 0, 28, 28, 103); - this.tileset.addTile(192, 0, 28, 28, 104); + this.tileset.addTile(0, 2, 28, 30, Constants.PLAYER_TANK_ID_IMAGE.getValue() + DirectionEnum.Up.getValue()); + this.tileset.addTile(64, 0, 28, 28, Constants.PLAYER_TANK_ID_IMAGE.getValue() + DirectionEnum.Left.getValue()); + this.tileset.addTile(128, 0, 28, 28, Constants.PLAYER_TANK_ID_IMAGE.getValue() + DirectionEnum.Down.getValue()); + this.tileset.addTile(192, 0, 28, 28, Constants.PLAYER_TANK_ID_IMAGE.getValue() + DirectionEnum.Right.getValue()); await this.tileset.loadAll(); } @@ -53,7 +55,7 @@ export class GameField { } if (!this.map) { - this.map = await new MapParser().getMap(3, this.tileset); + this.map = await new MapParser().getMap(4, this.tileset); this.canvas.height = this.height; this.canvas.width = this.width; this.collisionManager.attachMap(this.map); @@ -68,7 +70,7 @@ export class GameField { this.ctx.fillRect(x * Constants.TILE_SIZE.getValue(), y * Constants.TILE_SIZE.getValue(), Constants.TILE_SIZE.getValue(), Constants.TILE_SIZE.getValue()); let element = this.map.getElement(x, y); - if (element instanceof Forest){ + if (element.getType() == BlockTypeEnum.Forest){ lateDraw.push({element, x, y}); } else { diff --git a/src/Graphics/Map/map.js b/src/Graphics/Map/map.js index 256311a..a639d27 100644 --- a/src/Graphics/Map/map.js +++ b/src/Graphics/Map/map.js @@ -3,13 +3,17 @@ export class Map { this.width = width; this.height = height; this._map = []; - this._enemies = []; + this._enemiesCount = null; } pushElement(element) { this._map.push(element); } + setEnemyCount(count) { + this._enemiesCount = count; + } + 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 2ad0617..60abc0a 100644 --- a/src/Graphics/Map/mapParser.js +++ b/src/Graphics/Map/mapParser.js @@ -71,6 +71,7 @@ export class MapParser { } } //TODO bots from map + map.setEnemyCount(mapObject.bots); return map; } diff --git a/src/Tanks/aiTankManager.js b/src/Tanks/aiTankManager.js new file mode 100644 index 0000000..1c5380b --- /dev/null +++ b/src/Tanks/aiTankManager.js @@ -0,0 +1,27 @@ +export class AITankManager { + constructor(collisionManager) { + this._count = null; + this._collision = collisionManager; + this._canSpawn = false; + this._activeTanks = null; + } + + setCount(count) { + this._count = count; + this._canSpawn = true; + } + + checkIfCanSpawn() { + return this._canSpawn && this._count != 0; + } + + isAllTanksDied() { + return this._count == 0; + } + + getCount() { + return this._count; + } + + +} \ No newline at end of file diff --git a/src/Tanks/playerTank.js b/src/Tanks/tank.js similarity index 93% rename from src/Tanks/playerTank.js rename to src/Tanks/tank.js index a6feedd..9484863 100644 --- a/src/Tanks/playerTank.js +++ b/src/Tanks/tank.js @@ -2,9 +2,10 @@ import { Constants } from "../constants"; import { DirectionEnum } from "./directionEnum"; import { ShotManager } from "./shotElement"; -export class PlayerTank { +export class Tank { - constructor(x, y, collisionManager) { + constructor(id, x, y, collisionManager) { + this.id = id; this.x = x; this.y = y; this.collisionManager = collisionManager; @@ -75,7 +76,7 @@ export class PlayerTank { } draw(tileset, ctx) { - ctx.drawImage(tileset.getImageOf(100 + this.direction.getValue()), this.x, this.y, + ctx.drawImage(tileset.getImageOf(this.id + this.direction.getValue()), this.x, this.y, Constants.TANK_SIZE.getValue(), Constants.TANK_SIZE.getValue()); this.shotManager.draw(ctx); } diff --git a/src/constants.js b/src/constants.js index 1d13182..044e898 100644 --- a/src/constants.js +++ b/src/constants.js @@ -2,9 +2,11 @@ export class Constants { static TILE_SIZE = new Constants(32); static TANK_SIZE = new Constants(24); static TANK_COLL_OFFSET = new Constants(0.125); - static SHOT_SPEED = new Constants(3.0); + static SHOT_SPEED = new Constants(2.0); static SHOT_SIZE = new Constants(5); + static PLAYER_TANK_ID_IMAGE = new Constants(100); + constructor(value){ this._value = value; }