diff --git a/src/Blocks/types/block.js b/src/Blocks/types/block.js index 385a63b..2fca9e2 100644 --- a/src/Blocks/types/block.js +++ b/src/Blocks/types/block.js @@ -14,6 +14,10 @@ export class Block { return; } + getAllBoxes() { + return this._boxes; + } + } export class Box { @@ -27,4 +31,20 @@ export class Box { draw(ctx, x, y, tile){ ctx.drawImage(tile, this._xBox, this._yBox, this._wBox, this._hBox, x + this._xBox, y + this._yBox, this._wBox, this._hBox); } + + getXMin(){ + return this._xBox; + } + + getXMax() { + return this._xBox + this._wBox; + } + + getYMin() { + return this._yBox; + } + + getYMax() { + return this._yBox + this._hBox; + } } \ No newline at end of file diff --git a/src/Blocks/types/forest.js b/src/Blocks/types/forest.js index a289e10..9df966f 100644 --- a/src/Blocks/types/forest.js +++ b/src/Blocks/types/forest.js @@ -4,4 +4,8 @@ export class Forest extends Block { constructor(boxes, tile){ super(boxes, tile); } + + getAllBoxes(){ + return []; + } } \ No newline at end of file diff --git a/src/GameLogic/collisionManager.js b/src/GameLogic/collisionManager.js index 0827337..022d0f9 100644 --- a/src/GameLogic/collisionManager.js +++ b/src/GameLogic/collisionManager.js @@ -1,13 +1,17 @@ import { Constants } from "../constants"; +import { Map } from "../Graphics/Map/map"; import { CollisionTypeEnum } from "./collisionType"; export class CollisionManager { + /** + * @type {Map} + */ _map; constructor() { } - attachMap(map){ + attachMap(map) { this._map = map; } @@ -41,8 +45,46 @@ export class CollisionManager { } _checkBlockCollision(xMin, xMax, yMin, yMax) { + const blocksToCheck = this._getAllElementsOfMapOn(xMin, xMax, yMin, yMax); + const tilesize = Constants.TILE_SIZE.getValue(); + + for (let i = 0; i < blocksToCheck.length; i++) { + let boxes = blocksToCheck[i]["block"].getAllBoxes(); + let x = blocksToCheck[i]["xPos"]; + let y = blocksToCheck[i]["yPos"]; + + for (let j = 0; j < boxes.length; j++) { + const box = boxes[j]; + if (this.intersect(xMin, xMax, yMin, yMax, x * tilesize + box.getXMin(), x * tilesize + box.getXMax(), + y * tilesize + box.getYMin(), y * tilesize + box.getYMax())) return CollisionTypeEnum.Block; + } + } + return null; } + _getAllElementsOfMapOn(xMin, xMax, yMin, yMax) { + const tilesize = Constants.TILE_SIZE.getValue(); + let elements = []; + + for (let x = 0; x < this._map.width; x++) { + for (let y = 0; y < this._map.height; y++) { + if (this.intersect(xMin, xMax, yMin, yMax, x * tilesize, (x + 1) * tilesize, y * tilesize, (y + 1) * tilesize)) + elements.push({ + "block": this._map.getElement(x, y), + "xPos": x, + "yPos": y + }); + } + } + + return elements; + } + + intersect(xMin, xMax, yMin, yMax, anXMin, anXMax, anYMin, anYMax) { + return (xMin <= anXMax && xMax >= anXMin) && + (yMin <= anYMax && yMax >= anYMin); + } + } \ No newline at end of file diff --git a/src/Graphics/Map/map.js b/src/Graphics/Map/map.js index 90d42e8..256311a 100644 --- a/src/Graphics/Map/map.js +++ b/src/Graphics/Map/map.js @@ -6,7 +6,6 @@ export class Map { this._enemies = []; } - pushElement(element) { this._map.push(element); } @@ -14,4 +13,5 @@ export class Map { getElement(x, y){ return this._map[x + y*this.width]; } + } \ No newline at end of file diff --git a/src/KeyAdapters/tankKeyAdapter.js b/src/KeyAdapters/tankKeyAdapter.js index 68a75da..7cb015a 100644 --- a/src/KeyAdapters/tankKeyAdapter.js +++ b/src/KeyAdapters/tankKeyAdapter.js @@ -24,6 +24,6 @@ export class TankKeyAdapter extends GameKeyAdapterInterface { break; } - console.log(event.key); + //console.log(event.key); } } \ No newline at end of file diff --git a/src/Tanks/playerTank.js b/src/Tanks/playerTank.js index 8cbb780..b692c69 100644 --- a/src/Tanks/playerTank.js +++ b/src/Tanks/playerTank.js @@ -12,27 +12,34 @@ export class PlayerTank { } move(direction) { - const tilesize = Constants.TILE_SIZE.getValue(); + const tanksize = Constants.TANK_SIZE.getValue(); + const collisionOffset = Constants.TANK_COLL_OFFSET.getValue() * tanksize; + this.direction = direction; + const collisionMinX = this.x + collisionOffset; + const collisionMaxX = this.x + tanksize - collisionOffset; + const collisionMinY = this.y + collisionOffset; + const collisionMaxY = this.y + tanksize - collisionOffset; + switch (direction) { case DirectionEnum.Up: - if (!this.collisionManager.checkCollisionForTank(this.x, this.x + tilesize, this.y - this.speed, this.y + tilesize - this.speed)) + if (!this.collisionManager.checkCollisionForTank(collisionMinX, collisionMaxX, collisionMinY - this.speed, collisionMaxY - this.speed)) this.y -= this.speed; break; case DirectionEnum.Left: - if (!this.collisionManager.checkCollisionForTank(this.x - this.speed, this.x + tilesize - this.speed, this.y, this.y + tilesize)) + if (!this.collisionManager.checkCollisionForTank(collisionMinX - this.speed, collisionMaxX - this.speed, collisionMinY, collisionMaxY)) this.x -= this.speed; break; case DirectionEnum.Right: - if (!this.collisionManager.checkCollisionForTank(this.x + this.speed, this.x + tilesize + this.speed, this.y, this.y + tilesize)) + if (!this.collisionManager.checkCollisionForTank(collisionMinX + this.speed, collisionMaxX + this.speed, collisionMinY, collisionMaxY)) this.x += this.speed; break; case DirectionEnum.Down: - if (!this.collisionManager.checkCollisionForTank(this.x, this.x + tilesize, this.y + this.speed, this.y + tilesize + this.speed)) + if (!this.collisionManager.checkCollisionForTank(collisionMinX, collisionMaxX, collisionMinY + this.speed, collisionMaxY + this.speed)) this.y += this.speed; break; } diff --git a/src/constants.js b/src/constants.js index 5228c3f..db3e29d 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,6 +1,7 @@ export class Constants { static TILE_SIZE = new Constants(32); - static TANK_SIZE = new Constants(28); + static TANK_SIZE = new Constants(24); + static TANK_COLL_OFFSET = new Constants(0.125); constructor(value){ this._value = value;