-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for shared world items
- Loading branch information
Showing
4 changed files
with
97 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 15 additions & 11 deletions
26
packages/components/src/core/Types/src/base-world-item.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
import { Base } from "./base"; | ||
import { World } from "./world"; | ||
import { Event } from "./event"; | ||
import { Components } from "../../Components"; | ||
|
||
/** | ||
* One of the elements that make a world. It can be either a scene, a camera | ||
* or a renderer. | ||
*/ | ||
export abstract class BaseWorldItem extends Base { | ||
private _world: World | null = null; | ||
readonly worlds = new Map<string, World>(); | ||
|
||
readonly onWorldChanged = new Event<World | null>(); | ||
readonly onWorldChanged = new Event<{ | ||
world: World; | ||
action: "added" | "removed"; | ||
}>(); | ||
|
||
get world() { | ||
if (!this._world) { | ||
throw new Error("World not initialized!"); | ||
} | ||
return this._world; | ||
} | ||
currentWorld: World | null = null; | ||
|
||
protected constructor(components: Components) { | ||
super(components); | ||
|
||
set world(world: World | null) { | ||
this._world = world; | ||
this.onWorldChanged.trigger(world); | ||
this.onWorldChanged.add(({ world, action }) => { | ||
if (action === "removed") { | ||
this.worlds.delete(world.uuid); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters