Skip to content

Commit

Permalink
feat: make worlds type generic
Browse files Browse the repository at this point in the history
  • Loading branch information
agviegas committed Apr 20, 2024
1 parent f6b716c commit 817907d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/components/src/core/Worlds/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const container = document.getElementById("container")!;
const components = new OBC.Components();

const worlds = components.get(OBC.Worlds);
const world = new OBC.SimpleWorld(components);
const world = new OBC.SimpleWorld<
OBC.SimpleScene,
OBC.SimpleCamera,
OBC.SimpleRenderer
>(components);

world.scene = new OBC.SimpleScene(components);
world.renderer = new OBC.SimpleRenderer(components, container);
Expand Down
21 changes: 14 additions & 7 deletions packages/components/src/core/Worlds/src/simple-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import {
} from "../../Types";
import { Components } from "../../Components";

export class SimpleWorld extends Base implements World {
export class SimpleWorld<
T extends BaseScene = BaseScene,
U extends BaseCamera = BaseCamera,
S extends BaseRenderer = BaseRenderer,
>
extends Base
implements World
{
readonly onAfterUpdate = new Event();

readonly onBeforeUpdate = new Event();
Expand All @@ -20,11 +27,11 @@ export class SimpleWorld extends Base implements World {

readonly onDisposed = new Event();

private _scene?: BaseScene;
private _scene?: T;

private _camera?: BaseCamera;
private _camera?: U;

private _renderer: BaseRenderer | null = null;
private _renderer: S | null = null;

get scene() {
if (!this._scene) {
Expand All @@ -33,7 +40,7 @@ export class SimpleWorld extends Base implements World {
return this._scene;
}

set scene(scene: BaseScene) {
set scene(scene: T) {
this._scene = scene;
scene.worlds.set(this.uuid, this);
scene.currentWorld = this;
Expand All @@ -47,7 +54,7 @@ export class SimpleWorld extends Base implements World {
return this._camera;
}

set camera(camera: BaseCamera) {
set camera(camera: U) {
this._camera = camera;
camera.worlds.set(this.uuid, this);
camera.currentWorld = this;
Expand All @@ -58,7 +65,7 @@ export class SimpleWorld extends Base implements World {
return this._renderer;
}

set renderer(renderer: BaseRenderer | null) {
set renderer(renderer: S | null) {
this._renderer = renderer;
if (renderer) {
renderer.worlds.set(this.uuid, this);
Expand Down

0 comments on commit 817907d

Please sign in to comment.