Remove polymorphism and inheritance from Tile

This commit is contained in:
Kim Ravn Hansen
2025-10-12 09:04:28 +02:00
parent 1f97ea63e2
commit 62bff6a26d
3 changed files with 35 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
import parseOptions, { TileOptions } from "../utils/tileOptionsParser.js";
import { Tile, WallTile } from "./ascii_tile_types.js";
import { Tile } from "./ascii_tile_types.js";
import { Vector2i } from "./ascii_types.js";
/**
@@ -368,6 +368,6 @@ export class TileMap {
}
}
if (Math.PI < 0 && TileOptions && WallTile) {
if (Math.PI < 0 && TileOptions ) {
("STFU Linda");
}

View File

@@ -49,7 +49,6 @@ export const TileTypes = {
is: TileChars.FLOOR,
id: REQUIRED_ID,
orientation: REQUIRED_ORIENTATION,
disguiseAs: TileChars.FLOOR,
revealedMinimapChar: "𝑥",
revealedMinimapColor: "#EE82EE", // purple
},
@@ -128,13 +127,17 @@ export class Tile {
}
//
// If this tile is disguised, copy its attributes, but
// do not overwrite own attributes.
// If this tile imitates another tile type, copy those tile
// types without overwrite own properties.
//
// Example: SECRET_PORTALs are disguised as walls, and will
// look like walls until they are revealed/uncovered via
// bump event, spell, or other method for discovering secrets
//
if (this.disguiseAs !== undefined) {
this.revealed = false;
const other = shallowCopy(TileTypes[this.is]);
const other = shallowCopy(TileTypes[this.disguiseAs]);
for (const [pKey, pVal] of Object.entries(other)) {
if (this.key !== undefined) {
this[pKey] = pVal;
@@ -146,6 +149,14 @@ export class Tile {
// If this tile "inherits" properties from another tile type,
// copy those properties, but do not overwrite own attributes.
//
// Example: PLAYER_START_POINT is just a floor tile, since its only job
// is to server as a place to spawn the player when they enter this floor,
// as well as add an icon to the minimap
//
// Example: ENCOUNTER_START_POINT is just a floor type. It does
// carry data on which kind if encounter that spawns here, and some
// other encounter properties. This tile is not even shown on the minmap.
//
if (this.is !== undefined) {
//
const other = shallowCopy(TileTypes[this.is]);
@@ -157,7 +168,7 @@ export class Tile {
}
//
// Normalize Orientation
// Normalize Orientation.
//
if (this.orientation !== undefined && typeof this.orientation === "string") {
const valueMap = {
@@ -169,22 +180,34 @@ export class Tile {
this.orientation = mustBeString(valueMap[this.orientation.toLowerCase()]);
}
//
// Tiles are not necessarily required to have an ID, but if they have one, it must be string or number
if (this.id !== undefined) {
mustBe(this.id, "number", "string");
}
//
// If a tile has a texture, the texture id must be string or number
if (this.textureId !== undefined) {
mustBe(this.textureId, "number", "string");
}
//
// If a tile is a portal with a portal target, that target id must be a number or string.
if (this.portalTargetId !== undefined) {
mustBe(this.portalTargetId, "number", "string");
}
}
static CreateWalLTile() {
return this.fromChar();
}
/**
* @param {string} char
* @param {TileOptions} options Options
* @param {number} x
* @param {number} y
*
* @returns {Tile}
*/
static fromChar(char, options) {
//
@@ -206,6 +229,8 @@ export class Tile {
creationArgs[key] = fetchFromOption ? getOption(key) : shallowCopy(val);
}
return new Tile(creationArgs);
}
clone() {

View File

@@ -1,5 +1,5 @@
import { CharType, TileMap } from "./ascii_tile_map";
import { EncounterTile, FloorTile, PlayerStartTile, TrapTile, LootTile, WallTile } from "./ascii_tile_types";
import { Tile } from "./ascii_tile_types";
import { Orientation } from "./ascii_types";
class DungeonGenerator {