From 0cd06369f56bff98929128fa2d2724e47fc4c8bd Mon Sep 17 00:00:00 2001 From: Lajos Meszaros Date: Wed, 15 May 2024 23:46:34 +0200 Subject: [PATCH] feat: add Fogs, Paths and Zones class to make Selection deterministic --- src/ArxMap.ts | 31 ++++++++++++++++++++----------- src/Fogs.ts | 17 +++++++++++++++++ src/Paths.ts | 17 +++++++++++++++++ src/Selection.ts | 46 +++++++++++++++++++--------------------------- src/Zones.ts | 17 +++++++++++++++++ src/index.ts | 3 +++ 6 files changed, 93 insertions(+), 38 deletions(-) create mode 100644 src/Fogs.ts create mode 100644 src/Paths.ts create mode 100644 src/Zones.ts diff --git a/src/ArxMap.ts b/src/ArxMap.ts index 19b67bad..21aad342 100644 --- a/src/ArxMap.ts +++ b/src/ArxMap.ts @@ -19,6 +19,7 @@ import { Audio } from '@src/Audio.js' import { Entities } from '@src/Entities.js' import { Entity } from '@src/Entity.js' import { Fog } from '@src/Fog.js' +import { Fogs } from '@src/Fogs.js' import { HUD } from '@src/HUD.js' import { LevelLoader } from '@src/LevelLoader.js' import { Light } from '@src/Light.js' @@ -26,6 +27,7 @@ import { Lights } from '@src/Lights.js' import { Manifest } from '@src/Manifest.js' import { generateMetadata } from '@src/MetaData.js' import { Path } from '@src/Path.js' +import { Paths } from '@src/Paths.js' import { Player } from '@src/Player.js' import { Polygon } from '@src/Polygon.js' import { MeshImportProps, Polygons } from '@src/Polygons.js' @@ -38,6 +40,7 @@ import { Translations } from '@src/Translations.js' import { UI } from '@src/UI.js' import { Vector3 } from '@src/Vector3.js' import { Zone } from '@src/Zone.js' +import { Zones } from '@src/Zones.js' import { compile } from '@src/compile.js' import { MapFinalizedError, MapNotFinalizedError } from '@src/errors.js' import { times } from '@src/faux-ramda.js' @@ -60,12 +63,12 @@ type ToBeSortedLater = { export class ArxMap { polygons = new Polygons() lights = new Lights() - fogs: Fog[] = [] + fogs = new Fogs() entities = new Entities() - zones: Zone[] = [] - paths: Path[] = [] - player: Player = new Player() - portals: Portal[] = [] + zones = new Zones() + paths = new Paths() + player = new Player() + portals: Portal[] = [] // TODO: create Portals class config: ArxMapConfig = { isFinalized: false, offset: new Vector3(0, 0, 0), @@ -117,9 +120,15 @@ export class ArxMap { this.entities.push(Entity.fromArxInteractiveObject(entity)) }) - this.fogs = dlf.fogs.map(Fog.fromArxFog) - this.zones = dlf.zones.map(Zone.fromArxZone) - this.paths = dlf.paths.map(Path.fromArxPath) + dlf.fogs.forEach((fog) => { + this.fogs.push(Fog.fromArxFog(fog)) + }) + dlf.zones.forEach((zone) => { + this.zones.push(Zone.fromArxZone(zone)) + }) + dlf.paths.forEach((path) => { + this.paths.push(Path.fromArxPath(path)) + }) fts.polygons.forEach((polygon) => { this.polygons.push(Polygon.fromArxPolygon(polygon, llf.colors, fts.textureContainers, areNormalsCalculated)) @@ -154,9 +163,9 @@ export class ArxMap { scene: { levelIdx: settings.levelIdx, }, - fogs: this.fogs.map((fog) => fog.toArxFog()), - paths: this.paths.map((path) => path.toArxPath()), - zones: this.zones.map((zone) => zone.toArxZone()), + ...this.fogs.toArxData(), + ...this.paths.toArxData(), + ...this.zones.toArxData(), ...this.entities.toArxData(), } diff --git a/src/Fogs.ts b/src/Fogs.ts new file mode 100644 index 00000000..a4acb2f6 --- /dev/null +++ b/src/Fogs.ts @@ -0,0 +1,17 @@ +import { Fog } from './Fog.js' + +export class Fogs extends Array { + toArxData() { + const arxFogs = this.map((fog) => { + return fog.toArxFog() + }) + + return { + fogs: arxFogs, + } + } + + empty() { + this.length = 0 + } +} diff --git a/src/Paths.ts b/src/Paths.ts new file mode 100644 index 00000000..268f3db6 --- /dev/null +++ b/src/Paths.ts @@ -0,0 +1,17 @@ +import { Path } from './Path.js' + +export class Paths extends Array { + toArxData() { + const arxPaths = this.map((path) => { + return path.toArxPath() + }) + + return { + paths: arxPaths, + } + } + + empty() { + this.length = 0 + } +} diff --git a/src/Selection.ts b/src/Selection.ts index f8799837..93e764e0 100644 --- a/src/Selection.ts +++ b/src/Selection.ts @@ -1,12 +1,12 @@ import { Box3 } from 'three' import { Entities } from '@src/Entities.js' -import { Fog } from '@src/Fog.js' +import { Fogs } from '@src/Fogs.js' import { Lights } from '@src/Lights.js' -import { Path } from '@src/Path.js' +import { Paths } from '@src/Paths.js' import { Polygons } from '@src/Polygons.js' import { Texture } from '@src/Texture.js' import { Vector3 } from '@src/Vector3.js' -import { Zone } from '@src/Zone.js' +import { Zones } from '@src/Zones.js' import { groupSequences } from '@src/faux-ramda.js' export abstract class Selection> { @@ -254,7 +254,7 @@ export class EntitiesSelection extends Selection { } } -export class FogsSelection extends Selection { +export class FogsSelection extends Selection { copy() { const applyToAll = !this.hasSelection() @@ -268,7 +268,7 @@ export class FogsSelection extends Selection { this.clearSelection() } - return new FogsSelection(copiedItems) as this + return new FogsSelection(new Fogs(...copiedItems)) as this } move(offset: Vector3) { @@ -278,7 +278,7 @@ export class FogsSelection extends Selection { } } -export class PathsSelection extends Selection { +export class PathsSelection extends Selection { copy() { const applyToAll = !this.hasSelection() @@ -292,7 +292,7 @@ export class PathsSelection extends Selection { this.clearSelection() } - return new PathsSelection(copiedItems) as this + return new PathsSelection(new Paths(...copiedItems)) as this } move(offset: Vector3) { @@ -302,7 +302,7 @@ export class PathsSelection extends Selection { } } -export class ZonesSelection extends Selection { +export class ZonesSelection extends Selection { copy() { const applyToAll = !this.hasSelection() @@ -316,7 +316,7 @@ export class ZonesSelection extends Selection { this.clearSelection() } - return new ZonesSelection(copiedItems) as this + return new ZonesSelection(new Zones(...copiedItems)) as this } move(offset: Vector3) { @@ -328,7 +328,7 @@ export class ZonesSelection extends Selection { // ---------------------------------------- -type ArrayLikeArxTypes = Polygons | Lights | Entities | Fog[] | Path[] | Zone[] +type ArrayLikeArxTypes = Polygons | Lights | Entities | Fogs | Paths | Zones const instances = new WeakMap>() @@ -337,9 +337,9 @@ type OverloadsOf$ = { (items: Polygons): PolygonSelection (items: Entities): EntitiesSelection (items: Lights): LightsSelection - (items: Fog[]): FogsSelection - (items: Path[]): PathsSelection - (items: Zone[]): ZonesSelection + (items: Fogs): FogsSelection + (items: Paths): PathsSelection + (items: Zones): ZonesSelection } /** @@ -354,6 +354,7 @@ export const $: OverloadsOf$ = , T extends Selection>(it } let instance = instances.get(items) + if (instance === undefined) { if (items instanceof Polygons) { instance = new PolygonSelection(items) @@ -361,21 +362,12 @@ export const $: OverloadsOf$ = , T extends Selection>(it instance = new EntitiesSelection(items) } else if (items instanceof Lights) { instance = new LightsSelection(items) + } else if (items instanceof Fogs) { + instance = new FogsSelection(items) + } else if (items instanceof Paths) { + instance = new PathsSelection(items) } else { - if (items.length > 0) { - const item = items[0] - if (item instanceof Fog) { - instance = new FogsSelection(items as Fog[]) - } else if (item instanceof Zone) { - instance = new PathsSelection(items as Path[]) - } else { - instance = new ZonesSelection(items as Zone[]) - } - } else { - throw new Error( - `Selection: can't determine type of array that was given to $(), try passing in a non-empty array`, - ) - } + instance = new ZonesSelection(items) } instances.set(items, instance) diff --git a/src/Zones.ts b/src/Zones.ts new file mode 100644 index 00000000..47836c6e --- /dev/null +++ b/src/Zones.ts @@ -0,0 +1,17 @@ +import { Zone } from './Zone.js' + +export class Zones extends Array { + toArxData() { + const arxZones = this.map((zone) => { + return zone.toArxZone() + }) + + return { + zones: arxZones, + } + } + + empty() { + this.length = 0 + } +} diff --git a/src/index.ts b/src/index.ts index d21e2ce7..a91ef7ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,12 +7,14 @@ export { Entities } from '@src/Entities.js' export { Entity } from '@src/Entity.js' export { EntityModel } from '@src/EntityModel.js' export { Fog } from '@src/Fog.js' +export { Fogs } from '@src/Fogs.js' export { HUD, HudElements } from '@src/HUD.js' export { LevelLoader } from '@src/LevelLoader.js' export { Light } from '@src/Light.js' export { Lights } from '@src/Lights.js' export { Material } from '@src/Material.js' export { Path } from '@src/Path.js' +export { Paths } from '@src/Paths.js' export { Player } from '@src/Player.js' export { Polygon } from '@src/Polygon.js' export { Polygons, QUADIFY, DONT_QUADIFY, SHADING_FLAT, SHADING_SMOOTH } from '@src/Polygons.js' @@ -37,6 +39,7 @@ export { Vectors } from '@src/Vectors.js' export { Vector3 } from '@src/Vector3.js' export { Vertex } from '@src/Vertex.js' export { Zone } from '@src/Zone.js' +export { Zones } from '@src/Zones.js' export { MapFinalizedError, MapNotFinalizedError } from '@src/errors.js'