From dad4703ee0555e6e6c9c7daf4f21a7a851be04e7 Mon Sep 17 00:00:00 2001 From: Nikita Iryupin Date: Sat, 5 Jun 2021 14:20:07 +0200 Subject: [PATCH] Showing score, map changer (WIP) --- public/stages/stage-1.json | 2 +- public/stages/stage-2.json | 2 +- public/stages/stage-3.json | 2 +- public/stages/stage-4.json | 2 +- src/GameLogic/gameField.js | 36 ++++++++++++++++++++++++++++++----- src/Graphics/Map/map.js | 3 ++- src/Graphics/Map/mapParser.js | 7 +++++-- 7 files changed, 42 insertions(+), 12 deletions(-) diff --git a/public/stages/stage-1.json b/public/stages/stage-1.json index 9666f58..ddac98d 100644 --- a/public/stages/stage-1.json +++ b/public/stages/stage-1.json @@ -1,5 +1,5 @@ { - "name": "1", + "name": 1, "map": [ "X X X X X X X X X X X X X ", "X Bf X Bf X Bf X Bf X Bf X Bf X ", diff --git a/public/stages/stage-2.json b/public/stages/stage-2.json index cfaa2cd..5a5523f 100644 --- a/public/stages/stage-2.json +++ b/public/stages/stage-2.json @@ -1,5 +1,5 @@ { - "name": "2", + "name": 2, "map": [ "X X X Tf X X X Tf X X X X X ", "X B3 X Tf X X X Bf X Bf X Bf X ", diff --git a/public/stages/stage-3.json b/public/stages/stage-3.json index adf50ee..ad3b4dd 100644 --- a/public/stages/stage-3.json +++ b/public/stages/stage-3.json @@ -1,5 +1,5 @@ { - "name": "3", + "name": 3, "map": [ "X X X X BF X X X BF X X X X ", "X Ff Ff Ff BF X X X X X TC TC TC ", diff --git a/public/stages/stage-4.json b/public/stages/stage-4.json index 068dfaf..a5c1486 100644 --- a/public/stages/stage-4.json +++ b/public/stages/stage-4.json @@ -1,5 +1,5 @@ { - "name": "4", + "name": 4, "map": [ "X Ff Ff X X X X X X X X Ff X ", "Ff Ff X X Bc Bf Bf Bc Bc X X X Ff ", diff --git a/src/GameLogic/gameField.js b/src/GameLogic/gameField.js index 3815285..641ae46 100644 --- a/src/GameLogic/gameField.js +++ b/src/GameLogic/gameField.js @@ -11,9 +11,17 @@ import { SoundEnum, SoundManager } from "./soundManager"; import $ from 'jquery'; export class GameField { + + /** + * + * @param {number} width + * @param {number} height + * @param {CanvasRenderingContext2D} ctx + * @param {HTMLCanvasElement} canvas + */ constructor(width = 512, height = 512, ctx, canvas) { this.height = height; - this.width = width; + this.width = width + 100; this.ctx = ctx; this.canvas = canvas; this.tileset = null; @@ -79,8 +87,15 @@ export class GameField { } async play(level) { - if (!this.tileset || !this.map) { + this.ctx.clearRect(0,0, this.width, this.height); + if (!this.tileset || !this.map || this.map.mapLevel != level) { + $(".loading").show(); + $(".text-loading").show(); + $(".start-level-button").hide(); await this.prepareGame(level); + if (this.map == null) { + this.prepareGame(1); + } $(".text-loading").hide(); $(".start-level-button").show(); @@ -99,9 +114,8 @@ export class GameField { } win() { - - - requestAnimationFrame(() => this.drawFieldRecursive()) + + this.play(this.map.mapLevel + 1); } @@ -158,6 +172,18 @@ export class GameField { el.element.draw(this.ctx, el.x * Constants.TILE_SIZE.getValue(), el.y * Constants.TILE_SIZE.getValue()); } + this.drawPanel(); + + } + + drawPanel() { + this.ctx.fillStyle = "black"; + this.ctx.fillRect(this.width - 90, 0, 90, 100); + + this.ctx.font = "20px VT323, sans-serif"; + this.ctx.fillStyle = "white"; + this.ctx.fillText("Level: " + this.map.mapLevel, this.width - 90, 20); + this.ctx.fillText("Tanks: " + this.aiTanks._count, this.width - 90, 50); } } \ No newline at end of file diff --git a/src/Graphics/Map/map.js b/src/Graphics/Map/map.js index e66bc86..04b71d8 100644 --- a/src/Graphics/Map/map.js +++ b/src/Graphics/Map/map.js @@ -1,10 +1,11 @@ export class Map { - constructor(width, height, enemiesCount) { + constructor(width, height, enemiesCount, level) { this.width = width; this.height = height; this._map = []; this._enemiesCount = enemiesCount; + this.mapLevel = level; this._eagle = null; } diff --git a/src/Graphics/Map/mapParser.js b/src/Graphics/Map/mapParser.js index cd68386..c3be605 100644 --- a/src/Graphics/Map/mapParser.js +++ b/src/Graphics/Map/mapParser.js @@ -9,9 +9,12 @@ export class MapParser { } async getMap(levelNumber, tileset) { - let mapObject = await (await fetch(`./stages/stage-${levelNumber}.json`)).json(); + let fetchResult = await fetch(`./stages/stage-${levelNumber}.json`); + if (!fetchResult.ok) + return null; + let mapObject = await fetchResult.json(); - var map = new Map(mapObject.size.width, mapObject.size.height, mapObject.bots); + var map = new Map(mapObject.size.width, mapObject.size.height, mapObject.bots, mapObject.name); for (let i = 0; i < mapObject.map.length; i++) { let row = mapObject.map[i].match(/.{1,3}/g);