mr-crocs-adventures/tilorswift/js/Terrain.js

280 lines
7.9 KiB
JavaScript
Raw Normal View History

2020-01-29 00:21:53 +01:00
import Field from "./Field.js";
import Tileset from "./Tileset.js";
import TilorswiftEvent from "./events/TilorswiftEvent.js";
import GraphicSet from "../../js/GraphicSet.js";
2020-01-29 00:21:53 +01:00
export default class Terrain
{
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;
this.backgroundColor = backgroundColor;
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()
{
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
}
window.addEventListener(
TilorswiftEvent.BUTTON_TILE_CLICKED,
(event) => {
this.brushTileIndex = event.button.index;
}
)
2020-01-29 00:21:53 +01:00
}
getElement()
{
return this.htmlElement;
}
getTileset()
{
return this.tileset;
}
2020-02-09 22:06:59 +01:00
getWidth()
{
2024-08-01 15:32:53 +02:00
return this.tilesX * this.tileset.getTileWidth();
2020-02-09 22:06:59 +01:00
}
getHeight()
{
2024-08-01 15:32:53 +02:00
return this.tilesY * this.tileset.getTileHeight();
2020-02-09 22:06:59 +01:00
}
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++;
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
2024-08-01 15:32:53 +02:00
for (let y = index; y < this.tilesY; y++) {
for (let x = 0; x < this.tilesX; x++) {
// this.fields[y][x].index += quantity;
console.log(x, y);
this.fields[y][x].y += quantity;
}
}
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
{
2023-09-24 01:48:01 +02:00
const row = [];
const tr = document.createElement('tr');
2020-02-05 22:29:09 +01:00
for (let col = 0; col < this.tilesX; col++) {
2023-09-24 01:48:01 +02:00
const field = new Field(this.tileset, col, this.fields.length);
2020-02-05 22:29:09 +01:00
row.push(field);
2023-09-24 01:48:01 +02:00
tr.appendChild(field.getElement());
2020-02-05 22:29:09 +01:00
}
if (index === undefined || index >= this.tilesY - 1) {
this.fields.push(row);
this.htmlElement.appendChild(tr);
} else if (index === 0) {
this.fields = [row].concat(this.fields);
this.htmlElement.insertBefore(tr, this.htmlElement.childNodes[index]);
2020-02-05 22:29:09 +01:00
} else {
this.fields = this.fields.slice(0, index).concat([row]).concat(this.fields.slice(index));
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)
{
2024-08-01 15:32:53 +02:00
console.log(this.fields);
2020-02-09 00:29:46 +01:00
for (let c = 0; c < quantity; c++) {
this._insertColumn(index);
this.tilesX++;
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
}
2024-08-01 15:32:53 +02:00
for (let y = 0; y < this.tilesY; y++) {
for (let x = index; x < this.tilesX; x++) {
this.fields[y][x].x += quantity;
}
}
2020-02-09 00:29:46 +01:00
this.htmlElement.style.width = this.tileset.getTileWidth() * this.tilesX + 'px';
2024-08-01 15:32:53 +02:00
console.log(this.fields);
2020-02-09 00:29:46 +01:00
}
_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;
}
static createFromJson(levelData)
2020-01-29 00:21:53 +01:00
{
2023-09-24 01:48:01 +02:00
const graphicSet = GraphicSet[levelData.tileset];
2020-01-29 00:21:53 +01:00
2023-09-24 01:48:01 +02:00
const tileset = new Tileset(levelData.tileset);
const terrain = new Terrain(tileset, levelData.columns, levelData.rows, graphicSet.backgroundColor);
2023-09-18 22:33:55 +02:00
terrain.backgroundImage = graphicSet.backgroundImage ?? undefined;
2020-01-29 00:21:53 +01:00
for (let y = 0; y < levelData.rows; y++) {
for (let x = 0; x < levelData.columns; x++) {
terrain.setFieldIndex(x, y, levelData.matrix[y][x]);
2020-02-09 22:06:59 +01:00
if (x === levelData.startX && y === levelData.startY) {
2020-02-09 22:06:59 +01:00
terrain.setEntrancePoint(x, y);
} else if (x === levelData.targetX && y === levelData.targetY) {
2020-02-11 21:20:11 +01:00
terrain.setTargetPoint(x, y);
2020-02-09 22:06:59 +01:00
}
2020-01-29 00:21:53 +01:00
}
}
return terrain;
}
2023-09-24 01:48:01 +02:00
getFieldNeighbours(field)
{
const neighbours = [];
for (let x = Math.max(0, field.x - 1); x < Math.min(this.tilesX, field.x + 2); x++) {
for (let y = Math.max(0, field.y - 1); y < Math.min(this.tilesY, field.y + 2); y++) {
if (field.x === x && field.y === y) {
continue;
}
neighbours.push(this.fields[y][x]);
}
}
return neighbours;
}
hasFieldNeighbour(field, offsetX, offsetY)
{
const x = field.x + offsetX;
const y = field.y + offsetY;
if (x < 0 || x > this.tilesX - 1) {
return true;
}
if (y < 0 || y > this.tilesY - 1) {
return true;
}
return this.fields[y][x].index > -1;
}
getFieldNeighbourCode(field)
{
let code = '';
if (!this.hasFieldNeighbour(field, -1, 0)) {
code += 'l';
}
if (!this.hasFieldNeighbour(field, 0, -1)) {
code += 't';
}
if (!this.hasFieldNeighbour(field, 1, 0)) {
code += 'r';
}
if (!this.hasFieldNeighbour(field, 0, 1)) {
code += 'b';
}
return code;
}
2023-09-18 22:33:55 +02:00
}