Skip to content

Commit

Permalink
feat: add Fogs, Paths and Zones class to make Selection deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed May 15, 2024
1 parent fee5443 commit 0cd0636
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 38 deletions.
31 changes: 20 additions & 11 deletions src/ArxMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ 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'
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'
Expand All @@ -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'
Expand All @@ -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),
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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(),
}

Expand Down
17 changes: 17 additions & 0 deletions src/Fogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Fog } from './Fog.js'

export class Fogs extends Array<Fog> {
toArxData() {
const arxFogs = this.map((fog) => {
return fog.toArxFog()
})

return {
fogs: arxFogs,
}
}

empty() {
this.length = 0
}
}
17 changes: 17 additions & 0 deletions src/Paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Path } from './Path.js'

export class Paths extends Array<Path> {
toArxData() {
const arxPaths = this.map((path) => {
return path.toArxPath()
})

return {
paths: arxPaths,
}
}

empty() {
this.length = 0
}
}
46 changes: 19 additions & 27 deletions src/Selection.ts
Original file line number Diff line number Diff line change
@@ -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<T extends Array<any>> {
Expand Down Expand Up @@ -254,7 +254,7 @@ export class EntitiesSelection extends Selection<Entities> {
}
}

export class FogsSelection extends Selection<Fog[]> {
export class FogsSelection extends Selection<Fogs> {
copy() {
const applyToAll = !this.hasSelection()

Expand All @@ -268,7 +268,7 @@ export class FogsSelection extends Selection<Fog[]> {
this.clearSelection()
}

return new FogsSelection(copiedItems) as this
return new FogsSelection(new Fogs(...copiedItems)) as this
}

move(offset: Vector3) {
Expand All @@ -278,7 +278,7 @@ export class FogsSelection extends Selection<Fog[]> {
}
}

export class PathsSelection extends Selection<Path[]> {
export class PathsSelection extends Selection<Paths> {
copy() {
const applyToAll = !this.hasSelection()

Expand All @@ -292,7 +292,7 @@ export class PathsSelection extends Selection<Path[]> {
this.clearSelection()
}

return new PathsSelection(copiedItems) as this
return new PathsSelection(new Paths(...copiedItems)) as this
}

move(offset: Vector3) {
Expand All @@ -302,7 +302,7 @@ export class PathsSelection extends Selection<Path[]> {
}
}

export class ZonesSelection extends Selection<Zone[]> {
export class ZonesSelection extends Selection<Zones> {
copy() {
const applyToAll = !this.hasSelection()

Expand All @@ -316,7 +316,7 @@ export class ZonesSelection extends Selection<Zone[]> {
this.clearSelection()
}

return new ZonesSelection(copiedItems) as this
return new ZonesSelection(new Zones(...copiedItems)) as this
}

move(offset: Vector3) {
Expand All @@ -328,7 +328,7 @@ export class ZonesSelection extends Selection<Zone[]> {

// ----------------------------------------

type ArrayLikeArxTypes = Polygons | Lights | Entities | Fog[] | Path[] | Zone[]
type ArrayLikeArxTypes = Polygons | Lights | Entities | Fogs | Paths | Zones

const instances = new WeakMap<ArrayLikeArxTypes, Selection<ArrayLikeArxTypes>>()

Expand All @@ -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
}

/**
Expand All @@ -354,28 +354,20 @@ export const $: OverloadsOf$ = <U extends Array<any>, T extends Selection<U>>(it
}

let instance = instances.get(items)

if (instance === undefined) {
if (items instanceof Polygons) {
instance = new PolygonSelection(items)
} else if (items instanceof Entities) {
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)
Expand Down
17 changes: 17 additions & 0 deletions src/Zones.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Zone } from './Zone.js'

export class Zones extends Array<Zone> {
toArxData() {
const arxZones = this.map((zone) => {
return zone.toArxZone()
})

return {
zones: arxZones,
}
}

empty() {
this.length = 0
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'

Expand Down

0 comments on commit 0cd0636

Please sign in to comment.