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 X X X X Ba Ef B5 X X X X X "
],
"bots": [
"18*basic",
"2*fast"
],
"bots": 20,
"size": {
"width": 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 BF X BA Ef B5 X BF BF BF X "
],
"bots": [
"2*armor",
"4*fast",
"14*basic"
],
"bots": 20,
"size": {
"width": 13,
"height": 13

View File

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

View File

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

View File

@ -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){

View File

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

View File

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

View File

@ -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(){

View File

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

View File

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

View File

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

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 { 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
{

View File

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

View File

@ -71,6 +71,7 @@ export class MapParser {
}
}
//TODO bots from map
map.setEnemyCount(mapObject.bots);
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 { 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);
}

View File

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