import Field from "./Field.js"; import Tileset from "./Tileset.js"; import TilorswiftEvent from "./events/TilorswiftEvent.js"; export default class Terrain { constructor(tileset, tilesX, tilesY, backgroundColor = 'black') { this.tileset = tileset; this.fields = []; this.tilesX = tilesX; this.tilesY = tilesY; this.entranceTileX = undefined; this.entranceTileY = undefined; this.backgroundColor = backgroundColor; this.htmlElement = document.createElement('table'); this.brushTileIndex = 0; this.init(); } init() { this.htmlElement.id = 'level'; this.htmlElement.style.backgroundColor = this.backgroundColor; this.htmlElement.style.width = this.tileset.getTileWidth() * this.tilesX + 'px'; this.htmlElement.style.height = this.tileset.getTileHeight() * this.tilesY + 'px'; for (let r = 0; r < this.tilesY; r++) { this.insertRow(); } window.addEventListener( TilorswiftEvent.BUTTON_TILE_CLICKED, (event) => { this.brushTileIndex = event.button.index; } ) } getElement() { return this.htmlElement; } getTileset() { return this.tileset; } addRows(quantity, index) { for (let q = 0; q < quantity; q++) { this.insertRow(index); this.tilesY++; } } insertRow(index = undefined) { let row = []; let tr = document.createElement('tr'); for (let col = 0; col < this.tilesX; col++) { let field = new Field(this.tileset); let td = field.getElement(); row.push(field); tr.appendChild(td); } if (index === undefined || index >= this.tilesY - 1) { this.fields.push(row); this.htmlElement.appendChild(tr); } else { this.fields = this.fields.slice(0, index).concat(row).concat(this.fields.slice(index)); this.htmlElement.insertBefore(tr, this.htmlElement.childNodes[index]); } } setFieldIndex(x, y, index) { this.fields[y][x].setIndex(index); } setEntrancePoint(tileX, tileY) { if (this.fields[tileY][tileX].index === -1) { if (this.entranceTileX !== undefined && this.entranceTileY !== undefined) { this.fields[this.entranceTileY][this.entranceTileX].setEntrancePoint(false); } this.entranceTileX = tileX; this.entranceTileY = tileY; this.fields[tileY][tileX].setEntrancePoint(true); } } getFieldCoordinates(field) { for (let y = 0; y < this.fields.length; y++) { for (let x = 0; x < this.fields[y].length; x++) { if (this.fields[y][x] === field) { return {x: x, y: y}; } } } return undefined; } hasEntrancePoint() { return this.entranceTileX !== undefined && this.entranceTileY !== undefined; } static createFromJson(json) { let terrainData = JSON.parse(json); let imageTileset = new Image(); imageTileset.src = terrainData.tileset; let tileset = new Tileset(imageTileset, terrainData.tiles, terrainData.scale); let terrain = new Terrain(tileset, terrainData.columns, terrainData.rows, terrainData.backgroundColor); for (let y = 0; y < terrainData.matrix.length; y++) { for (let x = 0; x < terrainData.matrix[y].length; x++) { terrain.setFieldIndex(x, y, terrainData.matrix[y][x]); } } return terrain; } }