Intelligent graphic sets implemented

This commit is contained in:
Mal
2023-09-24 01:48:01 +02:00
parent 6f548a1b47
commit e6c247c655
12 changed files with 319 additions and 38 deletions

View File

@@ -75,14 +75,13 @@ export default class Terrain
_insertRow(index = undefined)
{
let row = [];
let tr = document.createElement('tr');
const row = [];
const tr = document.createElement('tr');
for (let col = 0; col < this.tilesX; col++) {
let field = new Field(this.tileset);
let td = field.getElement();
const field = new Field(this.tileset, col, this.fields.length);
row.push(field);
tr.appendChild(td);
tr.appendChild(field.getElement());
}
if (index === undefined || index >= this.tilesY - 1) {
@@ -183,10 +182,10 @@ export default class Terrain
static createFromJson(levelData)
{
let graphicSet = GraphicSet[levelData.tileset];
const graphicSet = GraphicSet[levelData.tileset];
let tileset = new Tileset(levelData.tileset);
let terrain = new Terrain(tileset, levelData.columns, levelData.rows, graphicSet.backgroundColor);
const tileset = new Tileset(levelData.tileset);
const terrain = new Terrain(tileset, levelData.columns, levelData.rows, graphicSet.backgroundColor);
terrain.backgroundImage = graphicSet.backgroundImage ?? undefined;
for (let y = 0; y < levelData.rows; y++) {
@@ -203,4 +202,60 @@ export default class Terrain
return terrain;
}
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;
}
}