|
|
|
|
@@ -1,16 +1,16 @@
|
|
|
|
|
import { AssetPreloaderService } from "../../../assetPreloaderService";
|
|
|
|
|
import { GdRoot } from "../data/GdRoot";
|
|
|
|
|
import { ECellType } from "../sim/ECellType";
|
|
|
|
|
import { SimCell } from "../sim/SimCell";
|
|
|
|
|
import { SimEnemy } from "../sim/SimEnemy";
|
|
|
|
|
import { SimLevel } from "../sim/SimLevel";
|
|
|
|
|
import { SimMain } from "../sim/SimMain";
|
|
|
|
|
import { SimProjectile } from "../sim/SimProjectile";
|
|
|
|
|
import { Hex } from "../util/Hex";
|
|
|
|
|
import { Vector2 } from "../util/Vector2";
|
|
|
|
|
import { VisEnemy } from "./VisEnemy";
|
|
|
|
|
import { VisMain } from "./VisMain";
|
|
|
|
|
import { VisProjectile } from "./VisProjectile";
|
|
|
|
|
import { AssetPreloaderService } from '../../../assetPreloaderService';
|
|
|
|
|
import { GdRoot } from '../data/GdRoot';
|
|
|
|
|
import { ECellType } from '../sim/ECellType';
|
|
|
|
|
import { SimCell } from '../sim/SimCell';
|
|
|
|
|
import { SimEnemy } from '../sim/SimEnemy';
|
|
|
|
|
import { SimLevel } from '../sim/SimLevel';
|
|
|
|
|
import { SimMain } from '../sim/SimMain';
|
|
|
|
|
import { SimProjectile } from '../sim/SimProjectile';
|
|
|
|
|
import { Hex } from '../util/Hex';
|
|
|
|
|
import { Vector2 } from '../util/Vector2';
|
|
|
|
|
import { VisEnemy } from './VisEnemy';
|
|
|
|
|
import { VisMain } from './VisMain';
|
|
|
|
|
import { VisProjectile } from './VisProjectile';
|
|
|
|
|
|
|
|
|
|
export class VisLevel {
|
|
|
|
|
private screenCellWidth: number = -1;
|
|
|
|
|
@@ -33,7 +33,7 @@ export class VisLevel {
|
|
|
|
|
this.assets = assets;
|
|
|
|
|
this.visMain = visMain;
|
|
|
|
|
this.simMain = simMain;
|
|
|
|
|
this.gdRoot = gdRoot
|
|
|
|
|
this.gdRoot = gdRoot;
|
|
|
|
|
this.enemyMap = new Map<SimEnemy, VisEnemy>();
|
|
|
|
|
this.projectileMap = new Map<SimProjectile, VisProjectile>();
|
|
|
|
|
this.reset();
|
|
|
|
|
@@ -59,14 +59,14 @@ export class VisLevel {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.drawBackground();
|
|
|
|
|
ctx.globalCompositeOperation = "source-over";
|
|
|
|
|
ctx.globalCompositeOperation = 'source-over';
|
|
|
|
|
|
|
|
|
|
// Highlight hovered cell first (under everything else)
|
|
|
|
|
if (this.hoveredHex) {
|
|
|
|
|
const hoveredIdx = simLevel.getCellIndex(this.hoveredHex);
|
|
|
|
|
const hoveredCell = simLevel.cells[hoveredIdx];
|
|
|
|
|
if (hoveredCell && hoveredCell.distance <= gdLevel.radius) {
|
|
|
|
|
this.drawCellImage(ctx, hoveredCell, "cell-highlighted.svg");
|
|
|
|
|
this.drawCellImage(ctx, hoveredCell, 'cell-highlighted.svg');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -85,11 +85,11 @@ export class VisLevel {
|
|
|
|
|
this.drawProjectile(projectile);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ctx.fillStyle = "white";
|
|
|
|
|
ctx.fillText("Currency: " + simLevel.currency, 5, 15);
|
|
|
|
|
ctx.fillText("Current wave: " + simLevel.currentWave, 5, 35);
|
|
|
|
|
ctx.fillText("Enemies left: " + simLevel.enemiesLeftToSpawn, 5, 55);
|
|
|
|
|
ctx.fillText("Current step: " + simLevel.currentStep, 5, 75);
|
|
|
|
|
ctx.fillStyle = 'white';
|
|
|
|
|
ctx.fillText('Currency: ' + simLevel.currency, 5, 15);
|
|
|
|
|
ctx.fillText('Current wave: ' + simLevel.currentWave, 5, 35);
|
|
|
|
|
ctx.fillText('Enemies left: ' + simLevel.enemiesLeftToSpawn, 5, 55);
|
|
|
|
|
ctx.fillText('Current step: ' + simLevel.currentStep, 5, 75);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public updateSize() {
|
|
|
|
|
@@ -99,11 +99,11 @@ export class VisLevel {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const gdLevel = this.gdRoot.levels[simLevel.index];
|
|
|
|
|
const minSize = Math.min(this.visMain.canvas.height, this.visMain.canvas.width * Math.sqrt(3) / 2);
|
|
|
|
|
const minSize = Math.min(this.visMain.canvas.height, (this.visMain.canvas.width * Math.sqrt(3)) / 2);
|
|
|
|
|
this.hexSize = Math.floor(minSize / (4 * (gdLevel.radius - 1) - 4));
|
|
|
|
|
this.screenCellHeight = Math.ceil(2 * this.hexSize);
|
|
|
|
|
this.screenCellHeight = Math.ceil(this.screenCellHeight * 0.5) * 2;
|
|
|
|
|
this.screenCellWidth = Math.ceil(Math.sqrt(3) / 2 * this.screenCellHeight);
|
|
|
|
|
this.screenCellWidth = Math.ceil((Math.sqrt(3) / 2) * this.screenCellHeight);
|
|
|
|
|
this.screenCellWidth = Math.ceil(this.screenCellWidth * 0.5) * 2;
|
|
|
|
|
this.screenXOffset = this.screenCellWidth * (gdLevel.radius + 0.5);
|
|
|
|
|
this.screenYOffset = this.screenCellHeight * (gdLevel.radius + 0.5);
|
|
|
|
|
@@ -120,6 +120,11 @@ export class VisLevel {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public updateEveryFrame(currentStep: number) {
|
|
|
|
|
if (currentStep < 0) {
|
|
|
|
|
this.lastStep = -1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const simLevel = this.simMain.currentLevel;
|
|
|
|
|
if (simLevel == null) {
|
|
|
|
|
return;
|
|
|
|
|
@@ -137,11 +142,9 @@ export class VisLevel {
|
|
|
|
|
const visEnemy = this.enemyMap.get(simEnemy);
|
|
|
|
|
if (!visEnemy) {
|
|
|
|
|
this.enemyMap.set(simEnemy, new VisEnemy(this.gdRoot, this.assets, simEnemy, this.screenCellWidth, this.screenCellHeight));
|
|
|
|
|
}
|
|
|
|
|
else if (Math.floor(currentStep) != Math.floor(this.lastStep)) {
|
|
|
|
|
} else if (Math.floor(currentStep) != Math.floor(this.lastStep)) {
|
|
|
|
|
visEnemy.advanceStep();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
} else {
|
|
|
|
|
visEnemy.update(t);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
@@ -159,8 +162,7 @@ export class VisLevel {
|
|
|
|
|
const visProjectile = this.projectileMap.get(simProjectile);
|
|
|
|
|
if (!visProjectile) {
|
|
|
|
|
this.projectileMap.set(simProjectile, new VisProjectile(simProjectile));
|
|
|
|
|
}
|
|
|
|
|
else if (Math.floor(currentStep) != Math.floor(this.lastStep)) {
|
|
|
|
|
} else if (Math.floor(currentStep) != Math.floor(this.lastStep)) {
|
|
|
|
|
visProjectile.advanceStep();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
@@ -196,34 +198,34 @@ export class VisLevel {
|
|
|
|
|
const gdLevel = this.gdRoot.levels[simLevel.index];
|
|
|
|
|
if (this.background == null) {
|
|
|
|
|
const backgroundCanvas = this.visMain.canvas.cloneNode() as HTMLCanvasElement;
|
|
|
|
|
const backgroundContext = backgroundCanvas.getContext("2d")!;
|
|
|
|
|
const backgroundContext = backgroundCanvas.getContext('2d')!;
|
|
|
|
|
this.background = backgroundCanvas;
|
|
|
|
|
simLevel.cells.forEach((cell: SimCell) => {
|
|
|
|
|
if (cell.distance > gdLevel.radius) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (cell.blockedType != -1 && cell.type == ECellType.Blocked) {
|
|
|
|
|
this.drawCellImage(backgroundContext, cell, "cell-blocked-" + (cell.blockedType | 0) + ".svg");
|
|
|
|
|
this.drawCellImage(backgroundContext, cell, 'cell-blocked-' + (cell.blockedType | 0) + '.svg');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
backgroundContext.globalCompositeOperation = "source-atop";
|
|
|
|
|
backgroundContext.globalCompositeOperation = 'source-atop';
|
|
|
|
|
backgroundContext.fillStyle = this.visMain.wallPattern;
|
|
|
|
|
backgroundContext.fillRect(0, 0, this.visMain.canvas.width, this.visMain.canvas.height);
|
|
|
|
|
|
|
|
|
|
const cellCanvas = this.visMain.canvas.cloneNode() as HTMLCanvasElement;
|
|
|
|
|
const cellContext = cellCanvas.getContext("2d")!;
|
|
|
|
|
const cellContext = cellCanvas.getContext('2d')!;
|
|
|
|
|
simLevel.cells.forEach((cell: SimCell) => {
|
|
|
|
|
if (cell.distance > gdLevel.radius) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (cell.type != ECellType.Entry) {
|
|
|
|
|
this.drawCellImage(cellContext, cell, "cell.svg");
|
|
|
|
|
this.drawCellImage(cellContext, cell, 'cell.svg');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
backgroundContext.globalCompositeOperation = "destination-over";
|
|
|
|
|
backgroundContext.globalCompositeOperation = 'destination-over';
|
|
|
|
|
backgroundContext.drawImage(cellCanvas, 0, 0);
|
|
|
|
|
backgroundContext.globalCompositeOperation = "source-over";
|
|
|
|
|
backgroundContext.globalCompositeOperation = 'source-over';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.drawImage(this.background, 0, 0);
|
|
|
|
|
@@ -242,11 +244,7 @@ export class VisLevel {
|
|
|
|
|
const pos = Vector2.lerp(positions[0], positions[1], t);
|
|
|
|
|
const width = this.screenCellWidth * simProjectile.size;
|
|
|
|
|
const height = this.screenCellHeight * simProjectile.size;
|
|
|
|
|
this.visMain.context.drawImage(this.assets.getImage("images/projectile.svg"),
|
|
|
|
|
this.screenXOffset + pos.x * this.hexSize - width / 2,
|
|
|
|
|
this.screenYOffset + pos.y * this.hexSize - height / 2,
|
|
|
|
|
width,
|
|
|
|
|
height);
|
|
|
|
|
this.visMain.context.drawImage(this.assets.getImage('images/projectile.svg'), this.screenXOffset + pos.x * this.hexSize - width / 2, this.screenYOffset + pos.y * this.hexSize - height / 2, width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private drawEnemy(simEnemy: SimEnemy) {
|
|
|
|
|
@@ -260,12 +258,7 @@ export class VisLevel {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const pos = Vector2.lerp(positions[0], positions[1], t);
|
|
|
|
|
this.visMain.context.drawImage(
|
|
|
|
|
visEnemy.image,
|
|
|
|
|
this.screenXOffset + pos.x * this.hexSize - this.screenCellWidth / 2,
|
|
|
|
|
this.screenYOffset + pos.y * this.hexSize - this.screenCellHeight / 2,
|
|
|
|
|
this.screenCellWidth,
|
|
|
|
|
this.screenCellHeight);
|
|
|
|
|
this.visMain.context.drawImage(visEnemy.image, this.screenXOffset + pos.x * this.hexSize - this.screenCellWidth / 2, this.screenYOffset + pos.y * this.hexSize - this.screenCellHeight / 2, this.screenCellWidth, this.screenCellHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private drawCell(cell: SimCell) {
|
|
|
|
|
@@ -274,9 +267,9 @@ export class VisLevel {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.visMain.context.fillStyle = "rgba(192, 192, 192, 0.25)";
|
|
|
|
|
this.visMain.context.fillStyle = 'rgba(192, 192, 192, 0.25)';
|
|
|
|
|
if (cell.type == ECellType.Entry) {
|
|
|
|
|
this.drawCellImage(this.visMain.context, cell, "cell-entry-" + (cell.blockedType | 0) + ".svg");
|
|
|
|
|
this.drawCellImage(this.visMain.context, cell, 'cell-entry-' + (cell.blockedType | 0) + '.svg');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const highlightedCell = simLevel.cells[simLevel.highlightedIndex];
|
|
|
|
|
@@ -293,20 +286,20 @@ export class VisLevel {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (draw) {
|
|
|
|
|
this.drawCellImage(this.visMain.context, highlightedCell, "cell-highlighted.svg");
|
|
|
|
|
this.visMain.context.fillStyle = "rgba(0, 0, 0, 1)";
|
|
|
|
|
this.drawCellImage(this.visMain.context, highlightedCell, 'cell-highlighted.svg');
|
|
|
|
|
this.visMain.context.fillStyle = 'rgba(0, 0, 0, 1)';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (cell.tower != null) {
|
|
|
|
|
this.drawCellImage(this.visMain.context, cell, "tower-" + (cell.tower.index | 0) + ".svg");
|
|
|
|
|
this.drawCellImage(this.visMain.context, cell, 'tower-' + (cell.tower.index | 0) + '.svg');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const coords = this.getScreenCoords(cell.hex);
|
|
|
|
|
this.visMain.context.fillText("(" + cell.hex.col + ", " + cell.hex.row + ")", coords.x + 10, coords.y + this.screenCellHeight / 2 + 5);
|
|
|
|
|
this.visMain.context.fillText('(' + cell.hex.col + ', ' + cell.hex.row + ')', coords.x + 10, coords.y + this.screenCellHeight / 2 + 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private drawCellImage(context: CanvasRenderingContext2D, cell: SimCell, name: string) {
|
|
|
|
|
const coords = this.getScreenCoords(cell.hex);
|
|
|
|
|
context.drawImage(this.assets.getImage("images/" + name), coords.x, coords.y, this.screenCellWidth, this.screenCellHeight);
|
|
|
|
|
context.drawImage(this.assets.getImage('images/' + name), coords.x, coords.y, this.screenCellWidth, this.screenCellHeight);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|