Faster levels after reloads fixed (how, idk)

This commit is contained in:
Nikita Iryupin 2021-06-05 15:21:59 +02:00
parent 4aa6289f50
commit 61a61db761

View File

@ -9,6 +9,7 @@ import { DirectionEnum } from "../Tanks/directionEnum";
import { SoundEnum, SoundManager } from "./soundManager";
import $ from 'jquery';
import { isThursday } from "date-fns";
export class GameField {
@ -33,6 +34,8 @@ export class GameField {
this.tank = new Tank(Constants.PLAYER_TANK_ID_IMAGE.getValue(), 0, 18 * 16, this.collisionManager);
this.aiTanks = new AITankManager(this.collisionManager);
this.collisionManager.setMainTank(this.tank);
this.lastRequest = null;
}
//add all tiles here!
@ -86,37 +89,53 @@ export class GameField {
this.aiTanks.setCount(this.map._enemiesCount);
}
async play(level) {
this.ctx.clearRect(0,0, this.width, this.height);
async play(level, firstTime) {
this.ctx.clearRect(0, 0, this.width, this.height);
this.tank.deathCount = 20;
this.tank.isDestroyed = false;
this.tank.x = Constants.TANK_DEFAULT_SPAWN_X.getValue();
this.tank.y = Constants.TANK_DEFAULT_SPAWN_Y.getValue();
this.aiTanks = new AITankManager(this.collisionManager);
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();
this.aiTanks._activeTanks = [];
this.collisionManager._aiTanks = [];
$(".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();
if (firstTime) {
$(".start-level-button").on("click", () => {
$(".loading").hide();
this.soundManager.playSound(SoundEnum.GameStart);
requestAnimationFrame(() => this.gameLoop());
})
}
else {
$(".loading").hide();
requestAnimationFrame(() => this.gameLoop());
}
}
lose() {
this.soundManager.playSound(SoundEnum.GameOver);
requestAnimationFrame(() => this.drawFieldRecursive())
setTimeout(() => {
cancelAnimationFrame(this.lastRequest);
this.lastRequest = undefined;
this.play(1, false);
}, 2000);
}
win() {
this.play(this.map.mapLevel + 1);
win() {
cancelAnimationFrame(this.lastRequest);
this.lastRequest = undefined;
setTimeout(() => { this.play(this.map.mapLevel + 1, false); }, 2000);
}
@ -138,16 +157,15 @@ export class GameField {
this.drawField();
requestAnimationFrame(() => this.gameLoop());
this.lastRequest = requestAnimationFrame(() => this.gameLoop());
}
drawFieldRecursive() {
this.drawField();
requestAnimationFrame(() => this.drawFieldRecursive());
this.lastRequest = requestAnimationFrame(() => this.drawFieldRecursive());
}
drawField() {
let lateDraw = [];
for (let x = 0; x < this.map.width; x += 1) {