Some formatting and redoing enum to class

This commit is contained in:
Nikita Iryupin 2021-04-21 23:39:25 +02:00
parent e969cdd606
commit 1cda01992f
10 changed files with 60 additions and 54 deletions

View File

@ -1,5 +1,5 @@
import { GameKeyAdapterInterface } from "./gameKeyAdapterInterface"; import { GameKeyAdapterInterface } from "./gameKeyAdapterInterface";
import Direction from "./../directionEnum"; import {DirectionEnum} from "./../directionEnum";
export class TankKeyAdapter extends GameKeyAdapterInterface{ export class TankKeyAdapter extends GameKeyAdapterInterface{
@ -11,16 +11,16 @@ export class TankKeyAdapter extends GameKeyAdapterInterface{
handleEvent(event){ handleEvent(event){
switch(event.key){ switch(event.key){
case "ArrowLeft": case "ArrowLeft":
this.tank.move(Direction().left); this.tank.move(DirectionEnum.Left);
break; break;
case "ArrowRight": case "ArrowRight":
this.tank.move(Direction().right); this.tank.move(DirectionEnum.Right);
break; break;
case "ArrowUp": case "ArrowUp":
this.tank.move(Direction().up); this.tank.move(DirectionEnum.Up);
break; break;
case "ArrowDown": case "ArrowDown":
this.tank.move(Direction().down); this.tank.move(DirectionEnum.Down);
break; break;
} }

View File

@ -1,6 +1,15 @@
const DirectionEnum = {"up":1, "left":2, "down": 3, "right": 4}; export class DirectionEnum {
Object.freeze(DirectionEnum); static Up = new DirectionEnum(1);
static Left = new DirectionEnum(2);
static Down = new DirectionEnum(3);
static Right = new DirectionEnum(4);
constructor(value){
this._value = value;
}
getValue(){
return this._value;
}
export default function Direction(){
return DirectionEnum;
} }

View File

@ -1,42 +1,39 @@
import Direction from "./directionEnum"; import {DirectionEnum} from "./directionEnum";
export class PlayerTank { export class PlayerTank {
constructor(x, y) { constructor(x, y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.speed = 2.0; this.speed = 5.0;
this.direction = Direction().up; this.direction = DirectionEnum.Up;
} }
move(direction) { move(direction) {
switch (direction) { switch (direction) {
case Direction().up: case DirectionEnum.Up:
this.y -= this.speed; this.y -= this.speed;
this.direction = Direction().up;
break; break;
case Direction().left: case DirectionEnum.Left:
this.x -= this.speed; this.x -= this.speed;
this.direction = Direction().left;
break; break;
case Direction().right: case DirectionEnum.Right:
this.x += this.speed; this.x += this.speed;
this.direction = Direction().right;
break; break;
case Direction().down: case DirectionEnum.Down:
this.y += this.speed; this.y += this.speed;
this.direction = Direction().down;
break; break;
} }
this.direction = direction;
console.log(this.x + " " + this.y); console.log(this.x + " " + this.y);
} }
draw(tileset, ctx, tilesize) { draw(tileset, ctx, tilesize) {
ctx.drawImage(tileset.getImageOf(100 + this.direction), this.x, this.y, tilesize, tilesize); ctx.drawImage(tileset.getImageOf(100 + this.direction.getValue()), this.x, this.y, tilesize, tilesize);
} }
} }