Little refactor and AITankManager

This commit is contained in:
Nikita Iryupin 2021-06-02 10:18:15 +02:00
parent ce4cbdc194
commit 09bbb7fcd5
18 changed files with 84 additions and 45 deletions

View File

@ -15,10 +15,7 @@
"X Bf X Bf X B8 Bc B4 X Bf X Bf X ", "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 " "X X X X X Ba Ef B5 X X X X X "
], ],
"bots": [ "bots": 20,
"18*basic",
"2*fast"
],
"size": { "size": {
"width": 13, "width": 13,
"height": 13 "height": 13

View File

@ -15,11 +15,7 @@
"X BF X X X B8 BC B4 X BF X BF X ", "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 " "X BF X BF X BA Ef B5 X BF BF BF X "
], ],
"bots": [ "bots": 20,
"2*armor",
"4*fast",
"14*basic"
],
"size": { "size": {
"width": 13, "width": 13,
"height": 13 "height": 13

View File

@ -15,11 +15,7 @@
"BF BF X T5 X B8 BC B4 X Ff Ff Ff X ", "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 " "TF BF BF X X BA Ef B5 X BF X X X "
], ],
"bots": [ "bots": 20,
"14*basic",
"4*fast",
"2*armor"
],
"size": { "size": {
"width": 13, "width": 13,
"height": 13 "height": 13

View File

@ -15,12 +15,7 @@
"Ff X B3 B3 X B8 Bc B4 B3 B3 X Ff Ff ", "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 " "Tf Ff X X X Ba Ef B5 X X Ff Ff Tf "
], ],
"bots": [ "bots": 20,
"10*power",
"5*fast",
"2*basic",
"3*armor"
],
"size": { "size": {
"width": 13, "width": 13,
"height": 13 "height": 13

View File

@ -11,6 +11,11 @@ export class Block {
constructor(boxes, tile) { constructor(boxes, tile) {
this._boxes = boxes; this._boxes = boxes;
this._tile = tile; this._tile = tile;
this._type = null;
}
getType(){
return this._type;
} }
draw(ctx, x, y){ draw(ctx, x, y){

View File

@ -1,10 +1,12 @@
import { DirectionEnum } from "../../Tanks/directionEnum"; import { DirectionEnum } from "../../Tanks/directionEnum";
import { BlockTypeEnum } from "../blockTypeEnum";
import { Block, Box } from "./block" import { Block, Box } from "./block"
export class Brick extends Block { export class Brick extends Block {
constructor(boxes, tile) { constructor(boxes, tile) {
super(boxes, tile); super(boxes, tile);
this._type = BlockTypeEnum.Brick;
} }
/** /**

View File

@ -1,11 +1,18 @@
import { BlockTypeEnum } from "../blockTypeEnum";
import {Block} from "./block" import {Block} from "./block"
export class Eagle extends Block { export class Eagle extends Block {
constructor(boxes, tile, destroyedTile) { constructor(boxes, tile, destroyedTile) {
super(boxes, tile); super(boxes, tile);
this._type = BlockTypeEnum.Eagle;
this._destroyedTile = destroyedTile; this._destroyedTile = destroyedTile;
this._isDestroyed = false; this._isDestroyed = false;
} }
resolveAttack(box, direction) {
this._isDestroyed = true;
this._tile = this._destroyedTile;
}
} }

View File

@ -1,8 +1,10 @@
import { BlockTypeEnum } from "../blockTypeEnum";
import { Block } from "./block" import { Block } from "./block"
export class Forest extends Block { export class Forest extends Block {
constructor(boxes, tile){ constructor(boxes, tile){
super(boxes, tile); super(boxes, tile);
this._type = BlockTypeEnum.Forest;
} }
getAllBoxes(){ getAllBoxes(){

View File

@ -1,8 +1,10 @@
import { BlockTypeEnum } from "../blockTypeEnum";
import {Block} from "./block" import {Block} from "./block"
export class Iron extends Block { export class Iron extends Block {
constructor(boxes, tile) { constructor(boxes, tile) {
super(boxes, tile); super(boxes, tile);
this._type = BlockTypeEnum.Iron;
} }

View File

@ -1,8 +1,10 @@
import {Block} from "./block" import { BlockTypeEnum } from "../blockTypeEnum";
import { Block } from "./block"
export class River extends Block { export class River extends Block {
constructor(boxes, tile) { constructor(boxes, tile) {
super(boxes, tile); super(boxes, tile);
this._type = BlockTypeEnum.River;
} }

View File

@ -1,3 +1,4 @@
import { BlockTypeEnum } from "../Blocks/blockTypeEnum";
import { Constants } from "../constants"; import { Constants } from "../constants";
import { Map } from "../Graphics/Map/map"; import { Map } from "../Graphics/Map/map";
@ -20,7 +21,7 @@ export class CollisionManager {
return (this._checkTankCollision(xMin, xMax, yMin, yMax) != null) || return (this._checkTankCollision(xMin, xMax, yMin, yMax) != null) ||
(this._checkBoundaries(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) if (!this._map)
return false; return false;
let blockColl = this._checkBlockCollision(xMin, xMax, yMin, yMax); let blockColl = this._checkBlockCollision(xMin, xMax, yMin, yMax, true);
if (blockColl) { if (blockColl) {
for (let blockInfo of blockColl) { for (let blockInfo of Array.from(blockColl)) {
blockInfo.block.resolveAttack(blockInfo.boxId, direction); blockInfo.block.resolveAttack(blockInfo.boxId, direction);
} }
return true; return true;
@ -54,12 +55,15 @@ export class CollisionManager {
return null; return null;
} }
_checkBlockCollision(xMin, xMax, yMin, yMax) { _checkBlockCollision(xMin, xMax, yMin, yMax, isShot) {
const blocksToCheck = this._getAllElementsOfMapOn(xMin, xMax, yMin, yMax); const blocksToCheck = this._getAllElementsOfMapOn(xMin, xMax, yMin, yMax);
const tilesize = Constants.TILE_SIZE.getValue(); const tilesize = Constants.TILE_SIZE.getValue();
let blockColl = new Set(); let blockColl = new Set();
for (let i = 0; i < blocksToCheck.length; i++) { for (let i = 0; i < blocksToCheck.length; i++) {
if (isShot && blocksToCheck[i].block.getType() == BlockTypeEnum.River)
continue;
let boxes = blocksToCheck[i].block.getAllBoxes(); let boxes = blocksToCheck[i].block.getAllBoxes();
let x = blocksToCheck[i].xPos; let x = blocksToCheck[i].xPos;
let y = blocksToCheck[i].yPos; let y = blocksToCheck[i].yPos;

View File

@ -1,6 +0,0 @@
export class CollisionTypeEnum {
static Block = new CollisionTypeEnum();
static Tank = new CollisionTypeEnum();
static Shot = new CollisionTypeEnum();
static Edge = new CollisionTypeEnum();
}

View File

@ -1,10 +1,11 @@
import { Tileset } from "../Graphics/Tileset/tileset" import { Tileset } from "../Graphics/Tileset/tileset"
import { PlayerTank } from "../Tanks/playerTank"; import { Tank } from "../Tanks/tank";
import { Constants } from "../constants" import { Constants } from "../constants"
import { BlockTypeEnum } from "../Blocks/blockTypeEnum" import { BlockTypeEnum } from "../Blocks/blockTypeEnum"
import { MapParser } from "../Graphics/Map/mapParser"; import { MapParser } from "../Graphics/Map/mapParser";
import { CollisionManager } from "./collisionManager"; import { CollisionManager } from "./collisionManager";
import { Forest } from "./../Blocks/types/forest"; import { AITankManager } from "./../Tanks/aiTankManager";
import { DirectionEnum } from "../Tanks/directionEnum";
export class GameField { export class GameField {
constructor(width = 512, height = 512, ctx, canvas) { constructor(width = 512, height = 512, ctx, canvas) {
@ -13,7 +14,8 @@ export class GameField {
this.ctx = ctx; this.ctx = ctx;
this.canvas = canvas; this.canvas = canvas;
this.collisionManager = new CollisionManager(); 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.tileset = null;
this.map = null; this.map = null;
} }
@ -39,10 +41,10 @@ export class GameField {
this.tileset.addTile(544, 64, 32, 32, BlockTypeEnum.Forest); this.tileset.addTile(544, 64, 32, 32, BlockTypeEnum.Forest);
//Tank //Tank
this.tileset.addTile(0, 2, 28, 30, 101); this.tileset.addTile(0, 2, 28, 30, Constants.PLAYER_TANK_ID_IMAGE.getValue() + DirectionEnum.Up.getValue());
this.tileset.addTile(64, 0, 28, 28, 102); this.tileset.addTile(64, 0, 28, 28, Constants.PLAYER_TANK_ID_IMAGE.getValue() + DirectionEnum.Left.getValue());
this.tileset.addTile(128, 0, 28, 28, 103); this.tileset.addTile(128, 0, 28, 28, Constants.PLAYER_TANK_ID_IMAGE.getValue() + DirectionEnum.Down.getValue());
this.tileset.addTile(192, 0, 28, 28, 104); this.tileset.addTile(192, 0, 28, 28, Constants.PLAYER_TANK_ID_IMAGE.getValue() + DirectionEnum.Right.getValue());
await this.tileset.loadAll(); await this.tileset.loadAll();
} }
@ -53,7 +55,7 @@ export class GameField {
} }
if (!this.map) { 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.height = this.height;
this.canvas.width = this.width; this.canvas.width = this.width;
this.collisionManager.attachMap(this.map); 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()); 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); let element = this.map.getElement(x, y);
if (element instanceof Forest){ if (element.getType() == BlockTypeEnum.Forest){
lateDraw.push({element, x, y}); lateDraw.push({element, x, y});
} else } else
{ {

View File

@ -3,13 +3,17 @@ export class Map {
this.width = width; this.width = width;
this.height = height; this.height = height;
this._map = []; this._map = [];
this._enemies = []; this._enemiesCount = null;
} }
pushElement(element) { pushElement(element) {
this._map.push(element); this._map.push(element);
} }
setEnemyCount(count) {
this._enemiesCount = count;
}
getElement(x, y){ getElement(x, y){
return this._map[x + y*this.width]; return this._map[x + y*this.width];
} }

View File

@ -71,6 +71,7 @@ export class MapParser {
} }
} }
//TODO bots from map //TODO bots from map
map.setEnemyCount(mapObject.bots);
return map; return map;
} }

View File

@ -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;
}
}

View File

@ -2,9 +2,10 @@ import { Constants } from "../constants";
import { DirectionEnum } from "./directionEnum"; import { DirectionEnum } from "./directionEnum";
import { ShotManager } from "./shotElement"; 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.x = x;
this.y = y; this.y = y;
this.collisionManager = collisionManager; this.collisionManager = collisionManager;
@ -75,7 +76,7 @@ export class PlayerTank {
} }
draw(tileset, ctx) { 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()); Constants.TANK_SIZE.getValue(), Constants.TANK_SIZE.getValue());
this.shotManager.draw(ctx); this.shotManager.draw(ctx);
} }

View File

@ -2,9 +2,11 @@ export class Constants {
static TILE_SIZE = new Constants(32); static TILE_SIZE = new Constants(32);
static TANK_SIZE = new Constants(24); static TANK_SIZE = new Constants(24);
static TANK_COLL_OFFSET = new Constants(0.125); 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 SHOT_SIZE = new Constants(5);
static PLAYER_TANK_ID_IMAGE = new Constants(100);
constructor(value){ constructor(value){
this._value = value; this._value = value;
} }