Some formatting and redoing enum to class
This commit is contained in:
parent
e969cdd606
commit
1cda01992f
@ -1,5 +1,5 @@
|
|||||||
export class Tile {
|
export class Tile {
|
||||||
constructor(x, y, width, height, id){
|
constructor(x, y, width, height, id) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
@ -10,7 +10,7 @@ export class Tile {
|
|||||||
this.img = null;
|
this.img = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
addImage(img){
|
addImage(img) {
|
||||||
this.img = img;
|
this.img = img;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
import {Tile} from "./tile"
|
import { Tile } from "./tile"
|
||||||
|
|
||||||
export class Tileset {
|
export class Tileset {
|
||||||
tiles = [];
|
tiles = [];
|
||||||
@ -8,7 +8,7 @@ export class Tileset {
|
|||||||
this.image.src = `./${imageName}`;
|
this.image.src = `./${imageName}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
addTile(x, y, width, height, id){
|
addTile(x, y, width, height, id) {
|
||||||
this.tiles.push(new Tile(x, y, width, height, id));
|
this.tiles.push(new Tile(x, y, width, height, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,9 +34,9 @@ export class Tileset {
|
|||||||
return canvas;
|
return canvas;
|
||||||
}
|
}
|
||||||
|
|
||||||
getImageOf(id){
|
getImageOf(id) {
|
||||||
for (let i = 0; i < this.tiles.length; i++){
|
for (let i = 0; i < this.tiles.length; i++) {
|
||||||
if (this.tiles[i].id == id){
|
if (this.tiles[i].id == id) {
|
||||||
return this.tiles[i].img;
|
return this.tiles[i].img;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
export class GameKeyAdapterInterface {
|
export class GameKeyAdapterInterface {
|
||||||
handleEvent(ev){}
|
handleEvent(ev) { }
|
||||||
}
|
}
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import {Game} from "./game";
|
import { Game } from "./game";
|
||||||
|
|
||||||
export function init(){
|
export function init() {
|
||||||
const canvas = document.getElementById("canvas");
|
const canvas = document.getElementById("canvas");
|
||||||
const ctx = canvas.getContext("2d");
|
const ctx = canvas.getContext("2d");
|
||||||
if (ctx) {
|
if (ctx) {
|
||||||
|
|||||||
@ -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;
|
|
||||||
}
|
}
|
||||||
@ -2,7 +2,7 @@ import { GameField } from "./gameField";
|
|||||||
import { TankKeyAdapter } from "./KeyAdapters/tankKeyAdapter";
|
import { TankKeyAdapter } from "./KeyAdapters/tankKeyAdapter";
|
||||||
|
|
||||||
export class Game {
|
export class Game {
|
||||||
constructor(width = 500, height = 500, ctx){
|
constructor(width = 500, height = 500, ctx) {
|
||||||
this.gameField = new GameField(width, height, ctx, canvas);
|
this.gameField = new GameField(width, height, ctx, canvas);
|
||||||
this.gameKeyAdapter = new TankKeyAdapter(this.gameField.tank);
|
this.gameKeyAdapter = new TankKeyAdapter(this.gameField.tank);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import {Tileset} from "./Graphics/Tileset/tileset"
|
import { Tileset } from "./Graphics/Tileset/tileset"
|
||||||
import {Map} from "./Graphics/map";
|
import { Map } from "./Graphics/map";
|
||||||
import { PlayerTank } from "./playerTank";
|
import { PlayerTank } from "./playerTank";
|
||||||
|
|
||||||
export class GameField {
|
export class GameField {
|
||||||
@ -7,17 +7,17 @@ export class GameField {
|
|||||||
GAME_WIDTH = 13;
|
GAME_WIDTH = 13;
|
||||||
GAME_HEIGHT = 13;
|
GAME_HEIGHT = 13;
|
||||||
|
|
||||||
constructor(width = 512, height = 512, ctx, canvas){
|
constructor(width = 512, height = 512, ctx, canvas) {
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.canvas = canvas
|
this.canvas = canvas
|
||||||
this.tank = new PlayerTank(0,0,0);
|
this.tank = new PlayerTank(0, 0, 0);
|
||||||
this.tileset = null;
|
this.tileset = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//add all tiles here!
|
//add all tiles here!
|
||||||
async setupTileset(){
|
async setupTileset() {
|
||||||
this.tileset = new Tileset("nesBattle.png");
|
this.tileset = new Tileset("nesBattle.png");
|
||||||
|
|
||||||
//Bricks
|
//Bricks
|
||||||
@ -44,7 +44,7 @@ export class GameField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async drawField() {
|
async drawField() {
|
||||||
if (!this.tileset){
|
if (!this.tileset) {
|
||||||
await this.setupTileset();
|
await this.setupTileset();
|
||||||
this.canvas.height = this.height;
|
this.canvas.height = this.height;
|
||||||
this.canvas.width = this.width;
|
this.canvas.width = this.width;
|
||||||
@ -52,16 +52,16 @@ export class GameField {
|
|||||||
|
|
||||||
let map = new Map().tiles;
|
let map = new Map().tiles;
|
||||||
|
|
||||||
for (let x = 0; x < this.GAME_WIDTH; x += 1){
|
for (let x = 0; x < this.GAME_WIDTH; x += 1) {
|
||||||
for (let y = 0; y <= this.GAME_HEIGHT; y += 1) {
|
for (let y = 0; y <= this.GAME_HEIGHT; y += 1) {
|
||||||
let id = map[x + y*this.GAME_WIDTH];
|
let id = map[x + y * this.GAME_WIDTH];
|
||||||
|
|
||||||
if (id != 0) {
|
if (id != 0) {
|
||||||
this.ctx.drawImage(this.tileset.getImageOf(id), x*this.TILE_SIZE, y*this.TILE_SIZE, this.TILE_SIZE, this.TILE_SIZE);
|
this.ctx.drawImage(this.tileset.getImageOf(id), x * this.TILE_SIZE, y * this.TILE_SIZE, this.TILE_SIZE, this.TILE_SIZE);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
this.ctx.fillStyle = "black";
|
this.ctx.fillStyle = "black";
|
||||||
this.ctx.fillRect(x*this.TILE_SIZE, y*this.TILE_SIZE, this.TILE_SIZE, this.TILE_SIZE);
|
this.ctx.fillRect(x * this.TILE_SIZE, y * this.TILE_SIZE, this.TILE_SIZE, this.TILE_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user