import { ECellType, SimCommandCreateTower, SimMain } from "../Simulation/index.js"; import { Vector2 } from "../Util/index.js"; import { VisMain } from "../Vis/index.js"; export class ViewGame { _div; _viewManager; _canvas = null; _simMain = null; _visMain = null; _gdRoot; _buttonReload = null; _buttonStart = null; constructor(div, gdRoot, viewManager) { this._gdRoot = gdRoot; this._div = div; this._viewManager = viewManager; this.canvasResized = this.canvasResized.bind(this); this.canvasMouseMoved = this.canvasMouseMoved.bind(this); this.canvasMouseDown = this.canvasMouseDown.bind(this); this.buttonReloadClicked = this.buttonReloadClicked.bind(this); this.buttonStartClicked = this.buttonStartClicked.bind(this); } get div() { return this._div; } activate() { this._canvas = document.getElementById("canvas-game"); this._buttonReload = document.getElementById("button-reload"); this._buttonStart = document.getElementById("button-start"); this._simMain = new SimMain(this._gdRoot, 0); this._visMain = new VisMain(this._simMain, this._canvas); window.addEventListener("resize", this.canvasResized, false); this._canvas?.addEventListener("mousemove", this.canvasMouseMoved); this._canvas?.addEventListener("mousedown", this.canvasMouseDown); this._buttonReload?.addEventListener("click", this.buttonReloadClicked); this._buttonStart?.addEventListener("click", this.buttonStartClicked); } deactivate() { window.removeEventListener("resize", this.canvasResized, false); this._canvas?.removeEventListener("mousemove", this.canvasMouseMoved); this._canvas?.removeEventListener("mousedown", this.canvasMouseDown); this._buttonReload?.removeEventListener("click", this.buttonReloadClicked); this._buttonStart?.removeEventListener("click", this.buttonStartClicked); this._canvas = null; this._buttonReload = null; this._buttonStart = null; this._simMain = null; this._visMain = null; } canvasResized() { this._visMain?.onResized(); } buttonReloadClicked() { this._simMain?.reload(); } buttonStartClicked() { if (this._simMain == null) { return; } this._simMain.currentLevel.paused = false; } canvasMouseMoved(e) { const simLevel = this._simMain?.currentLevel; if (simLevel == null || this._visMain == null) { return; } const rect = this._canvas.getBoundingClientRect(); const hex = this._visMain.visLevel.getHexFromScreenCoords(new Vector2(e.clientX - rect.left, e.clientY - rect.top)); const index = simLevel.getCellIndex(hex); if (index != -1 && simLevel.simCells[index].type != ECellType.Blocked) { simLevel.highlightedIndex = index; } else { simLevel.highlightedIndex = -1; } } canvasMouseDown(e) { const simLevel = this._simMain?.currentLevel; if (simLevel == null || this._visMain == null) { return; } const rect = this._canvas.getBoundingClientRect(); const hex = this._visMain.visLevel.getHexFromScreenCoords(new Vector2(e.clientX - rect.left, e.clientY - rect.top)); this._simMain?.addCommand(new SimCommandCreateTower(this._gdRoot, simLevel, hex, 0)); } } //# sourceMappingURL=ViewGame.js.map