92 lines
2.9 KiB
JavaScript
92 lines
2.9 KiB
JavaScript
import { Constants } from "../constants";
|
|
import { DirectionEnum } from "./directionEnum";
|
|
import { ShotManager } from "./shotElement";
|
|
|
|
export class Tank {
|
|
|
|
constructor(id, x, y, collisionManager) {
|
|
this.id = id;
|
|
this.x = x;
|
|
this.y = y;
|
|
this.collisionManager = collisionManager;
|
|
this.speed = 3.0;
|
|
this.direction = DirectionEnum.Up;
|
|
|
|
this.isDestroyed = false;
|
|
|
|
this.shotManager = new ShotManager(collisionManager, this);
|
|
}
|
|
|
|
move(direction) {
|
|
const box = this.getCollisionBox();
|
|
|
|
this.direction = direction;
|
|
|
|
switch (direction) {
|
|
case DirectionEnum.Up:
|
|
if (!this.collisionManager.checkCollisionForTank(box.minX, box.maxX, box.minY - this.speed, box.maxY - this.speed, this))
|
|
this.y -= this.speed;
|
|
break;
|
|
|
|
case DirectionEnum.Left:
|
|
if (!this.collisionManager.checkCollisionForTank(box.minX - this.speed, box.maxX - this.speed, box.minY, box.maxY, this))
|
|
this.x -= this.speed;
|
|
break;
|
|
|
|
case DirectionEnum.Right:
|
|
if (!this.collisionManager.checkCollisionForTank(box.minX + this.speed, box.maxX + this.speed, box.minY, box.maxY, this))
|
|
this.x += this.speed;
|
|
break;
|
|
|
|
case DirectionEnum.Down:
|
|
if (!this.collisionManager.checkCollisionForTank(box.minX, box.maxX, box.minY + this.speed, box.maxY + this.speed, this))
|
|
this.y += this.speed;
|
|
break;
|
|
}
|
|
}
|
|
|
|
getCollisionBox() {
|
|
const tanksize = Constants.TANK_SIZE.getValue();
|
|
const collisionOffset = Constants.TANK_COLL_OFFSET.getValue() * tanksize;
|
|
|
|
return {
|
|
minX: this.x + collisionOffset,
|
|
maxX: this.x + tanksize - collisionOffset,
|
|
minY: this.y + collisionOffset,
|
|
maxY: this.y + tanksize - collisionOffset
|
|
}
|
|
}
|
|
|
|
shot() {
|
|
let x, y = 0
|
|
const tanksize = Constants.TANK_SIZE.getValue();
|
|
|
|
switch (this.direction) {
|
|
case DirectionEnum.Up:
|
|
x = this.x + tanksize / 2;
|
|
y = this.y;
|
|
break;
|
|
case DirectionEnum.Left:
|
|
x = this.x;
|
|
y = this.y + tanksize / 2;
|
|
break;
|
|
case DirectionEnum.Down:
|
|
x = this.x + tanksize / 2;
|
|
y = this.y + tanksize;
|
|
break;
|
|
case DirectionEnum.Right:
|
|
x = this.x + tanksize;
|
|
y = this.y + tanksize / 2;
|
|
break;
|
|
}
|
|
|
|
this.shotManager.tryShot(this.direction, x, y);
|
|
}
|
|
|
|
draw(tileset, ctx) {
|
|
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);
|
|
}
|
|
|
|
} |