Some formatting and redoing enum to class
This commit is contained in:
parent
e969cdd606
commit
1cda01992f
@ -1,5 +1,5 @@
|
||||
import { GameKeyAdapterInterface } from "./gameKeyAdapterInterface";
|
||||
import Direction from "./../directionEnum";
|
||||
import {DirectionEnum} from "./../directionEnum";
|
||||
|
||||
export class TankKeyAdapter extends GameKeyAdapterInterface{
|
||||
|
||||
@ -11,16 +11,16 @@ export class TankKeyAdapter extends GameKeyAdapterInterface{
|
||||
handleEvent(event){
|
||||
switch(event.key){
|
||||
case "ArrowLeft":
|
||||
this.tank.move(Direction().left);
|
||||
this.tank.move(DirectionEnum.Left);
|
||||
break;
|
||||
case "ArrowRight":
|
||||
this.tank.move(Direction().right);
|
||||
this.tank.move(DirectionEnum.Right);
|
||||
break;
|
||||
case "ArrowUp":
|
||||
this.tank.move(Direction().up);
|
||||
this.tank.move(DirectionEnum.Up);
|
||||
break;
|
||||
case "ArrowDown":
|
||||
this.tank.move(Direction().down);
|
||||
this.tank.move(DirectionEnum.Down);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,15 @@
|
||||
const DirectionEnum = {"up":1, "left":2, "down": 3, "right": 4};
|
||||
Object.freeze(DirectionEnum);
|
||||
export class 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;
|
||||
}
|
||||
@ -1,42 +1,39 @@
|
||||
import Direction from "./directionEnum";
|
||||
import {DirectionEnum} from "./directionEnum";
|
||||
|
||||
export class PlayerTank {
|
||||
|
||||
constructor(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.speed = 2.0;
|
||||
this.direction = Direction().up;
|
||||
this.speed = 5.0;
|
||||
this.direction = DirectionEnum.Up;
|
||||
}
|
||||
|
||||
move(direction) {
|
||||
switch (direction) {
|
||||
case Direction().up:
|
||||
case DirectionEnum.Up:
|
||||
this.y -= this.speed;
|
||||
this.direction = Direction().up;
|
||||
break;
|
||||
|
||||
case Direction().left:
|
||||
case DirectionEnum.Left:
|
||||
this.x -= this.speed;
|
||||
this.direction = Direction().left;
|
||||
break;
|
||||
|
||||
case Direction().right:
|
||||
case DirectionEnum.Right:
|
||||
this.x += this.speed;
|
||||
this.direction = Direction().right;
|
||||
break;
|
||||
|
||||
case Direction().down:
|
||||
case DirectionEnum.Down:
|
||||
this.y += this.speed;
|
||||
this.direction = Direction().down;
|
||||
break;
|
||||
}
|
||||
this.direction = direction;
|
||||
|
||||
console.log(this.x + " " + this.y);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user