2020-01-29 00:21:53 +01:00
|
|
|
import Field from "./Field.js";
|
|
|
|
import Tileset from "./Tileset.js";
|
2020-01-30 23:40:42 +01:00
|
|
|
import TilorswiftEvent from "./events/TilorswiftEvent.js";
|
2020-02-14 00:12:24 +01:00
|
|
|
import GraphicSet from "../../js/GraphicSet.js";
|
2020-01-29 00:21:53 +01:00
|
|
|
|
|
|
|
export default class Terrain
|
|
|
|
{
|
2020-01-29 00:33:53 +01:00
|
|
|
constructor(tileset, tilesX, tilesY, backgroundColor = 'black')
|
2020-01-29 00:21:53 +01:00
|
|
|
{
|
|
|
|
this.tileset = tileset;
|
|
|
|
this.fields = [];
|
|
|
|
this.tilesX = tilesX;
|
|
|
|
this.tilesY = tilesY;
|
2020-02-04 23:03:33 +01:00
|
|
|
this.entranceTileX = undefined;
|
|
|
|
this.entranceTileY = undefined;
|
2020-02-11 21:20:11 +01:00
|
|
|
this.targetTileX = undefined;
|
|
|
|
this.targetTileY = undefined;
|
2020-01-29 00:33:53 +01:00
|
|
|
this.backgroundColor = backgroundColor;
|
2020-02-14 00:12:24 +01:00
|
|
|
this.backgroundImage = undefined;
|
2020-01-29 00:21:53 +01:00
|
|
|
this.htmlElement = document.createElement('table');
|
2020-01-30 20:51:29 +01:00
|
|
|
this.brushTileIndex = 0;
|
2020-01-29 00:21:53 +01:00
|
|
|
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
init()
|
|
|
|
{
|
2020-01-29 00:33:53 +01:00
|
|
|
this.htmlElement.id = 'level';
|
|
|
|
this.htmlElement.style.width = this.tileset.getTileWidth() * this.tilesX + 'px';
|
|
|
|
this.htmlElement.style.height = this.tileset.getTileHeight() * this.tilesY + 'px';
|
|
|
|
|
2020-01-29 00:21:53 +01:00
|
|
|
for (let r = 0; r < this.tilesY; r++) {
|
2020-02-09 00:29:46 +01:00
|
|
|
this._insertRow();
|
2020-01-29 00:21:53 +01:00
|
|
|
}
|
2020-01-30 23:40:42 +01:00
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.BUTTON_TILE_CLICKED,
|
|
|
|
(event) => {
|
|
|
|
this.brushTileIndex = event.button.index;
|
|
|
|
}
|
|
|
|
)
|
2020-01-29 00:21:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getElement()
|
|
|
|
{
|
|
|
|
return this.htmlElement;
|
|
|
|
}
|
|
|
|
|
2020-01-30 23:40:42 +01:00
|
|
|
getTileset()
|
|
|
|
{
|
|
|
|
return this.tileset;
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:06:59 +01:00
|
|
|
getWidth()
|
|
|
|
{
|
|
|
|
return this.tilesX * this.tilest.getTileWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
getHeight()
|
|
|
|
{
|
|
|
|
return this.tilesY * this.tilest.getTileHeight();
|
|
|
|
}
|
|
|
|
|
2020-02-09 00:29:46 +01:00
|
|
|
addRows(index, quantity = 1)
|
2020-02-05 22:29:09 +01:00
|
|
|
{
|
|
|
|
for (let q = 0; q < quantity; q++) {
|
2020-02-09 00:29:46 +01:00
|
|
|
this._insertRow(index);
|
2020-02-05 22:29:09 +01:00
|
|
|
this.tilesY++;
|
2020-02-14 00:12:24 +01:00
|
|
|
this.entranceTileY = this.entranceTileY === undefined ? undefined : this.entranceTileY + 1;
|
|
|
|
this.targetTileY = this.targetTileY === undefined ? undefined : this.targetTileY + 1;
|
2020-02-05 22:29:09 +01:00
|
|
|
}
|
2020-02-09 00:29:46 +01:00
|
|
|
|
|
|
|
this.htmlElement.style.height = this.tileset.getTileHeight() * this.tilesY + 'px';
|
2020-02-05 22:29:09 +01:00
|
|
|
}
|
|
|
|
|
2020-02-09 00:29:46 +01:00
|
|
|
_insertRow(index = undefined)
|
2020-02-05 22:29:09 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-14 00:12:24 +01:00
|
|
|
console.log(index);
|
|
|
|
|
2020-02-05 22:29:09 +01:00
|
|
|
if (index === undefined || index >= this.tilesY - 1) {
|
|
|
|
this.fields.push(row);
|
|
|
|
this.htmlElement.appendChild(tr);
|
2020-02-14 00:12:24 +01:00
|
|
|
} else if (index === 0) {
|
|
|
|
this.fields = [row].concat(this.fields);
|
|
|
|
console.log(this.fields[0]);
|
|
|
|
this.htmlElement.insertBefore(tr, this.htmlElement.childNodes[index]);
|
2020-02-05 22:29:09 +01:00
|
|
|
} else {
|
2020-02-14 00:12:24 +01:00
|
|
|
this.fields = this.fields.slice(0, index).concat([row]).concat(this.fields.slice(index));
|
|
|
|
console.log(this.fields[1]);
|
2020-02-05 22:29:09 +01:00
|
|
|
this.htmlElement.insertBefore(tr, this.htmlElement.childNodes[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 00:29:46 +01:00
|
|
|
addColumns(index, quantity = 1)
|
|
|
|
{
|
|
|
|
for (let c = 0; c < quantity; c++) {
|
|
|
|
this._insertColumn(index);
|
|
|
|
this.tilesX++;
|
2020-02-14 00:12:24 +01:00
|
|
|
this.entranceTileX = this.entranceTileX === undefined ? undefined : this.entranceTileX + 1;
|
|
|
|
this.targetTileX = this.targetTileX === undefined ? undefined : this.targetTileX + 1;
|
2020-02-09 00:29:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.htmlElement.style.width = this.tileset.getTileWidth() * this.tilesX + 'px';
|
|
|
|
}
|
|
|
|
|
|
|
|
_insertColumn(index = undefined)
|
|
|
|
{
|
|
|
|
if (index === undefined || index > this.tilesX - 1) {
|
|
|
|
index = this.tilesX;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let y = 0; y < this.tilesY; y++) {
|
|
|
|
let field = new Field(this.tileset);
|
|
|
|
this.fields[y] = this.fields[y].slice(0, index).concat(field).concat(this.fields[y].slice(index));
|
|
|
|
|
|
|
|
let htmlRow = this.htmlElement.childNodes[y];
|
|
|
|
htmlRow.insertBefore(field.getElement(), htmlRow.childNodes[index]);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 00:21:53 +01:00
|
|
|
setFieldIndex(x, y, index)
|
|
|
|
{
|
|
|
|
this.fields[y][x].setIndex(index);
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:03:33 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:20:11 +01:00
|
|
|
setTargetPoint(tileX, tileY)
|
|
|
|
{
|
|
|
|
if (this.fields[tileY][tileX].index === -1) {
|
|
|
|
if (this.targetTileX !== undefined && this.targetTileY !== undefined) {
|
|
|
|
this.fields[this.targetTileY][this.targetTileX].setTargetPoint(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.targetTileX = tileX;
|
|
|
|
this.targetTileY = tileY;
|
|
|
|
|
|
|
|
this.fields[tileY][tileX].setTargetPoint(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:03:33 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:20:11 +01:00
|
|
|
hasTargetPoint()
|
|
|
|
{
|
|
|
|
return this.targetTileX !== undefined && this.targetTileY !== undefined;
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:06:59 +01:00
|
|
|
static createFromJson(terrainData)
|
2020-01-29 00:21:53 +01:00
|
|
|
{
|
2020-02-14 00:12:24 +01:00
|
|
|
let graphicSet = GraphicSet[terrainData.tileset];
|
2020-01-29 00:21:53 +01:00
|
|
|
|
2020-02-14 00:12:24 +01:00
|
|
|
let tileset = new Tileset(terrainData.tileset);
|
|
|
|
let terrain = new Terrain(tileset, terrainData.columns, terrainData.rows, graphicSet.backgroundColor);
|
2020-01-29 00:21:53 +01:00
|
|
|
|
2020-02-09 22:06:59 +01:00
|
|
|
for (let y = 0; y < terrainData.rows; y++) {
|
|
|
|
for (let x = 0; x < terrainData.columns; x++) {
|
2020-01-29 00:21:53 +01:00
|
|
|
terrain.setFieldIndex(x, y, terrainData.matrix[y][x]);
|
2020-02-09 22:06:59 +01:00
|
|
|
|
|
|
|
if (x === terrainData.startX && y === terrainData.startY) {
|
|
|
|
terrain.setEntrancePoint(x, y);
|
2020-02-11 21:20:11 +01:00
|
|
|
} else if (x === terrainData.targetX && y === terrainData.targetY) {
|
|
|
|
terrain.setTargetPoint(x, y);
|
2020-02-09 22:06:59 +01:00
|
|
|
}
|
2020-01-29 00:21:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return terrain;
|
|
|
|
}
|
|
|
|
}
|