Skip to content

Commit

Permalink
refactor(core): merge world and level
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaisthorpe committed Sep 26, 2023
1 parent fcd2d72 commit 15e078b
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 171 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-moose-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tedengine/ted': minor
---

Merge world and level together
4 changes: 2 additions & 2 deletions packages/ted/src/actor-components/scene-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ export default class TSceneComponent extends TActorComponent {

public applyCentralForce(force: vec3) {
if (!this.collider) return;
this.actor.level?.world?.applyCentralForce(this, force);
this.actor?.world?.applyCentralForce(this, force);
}

public applyCentralImpulse(impulse: vec3) {
if (!this.collider) return;
this.actor.level?.world?.applyCentralImpulse(this, impulse);
this.actor?.world?.applyCentralImpulse(this, impulse);
}
}
4 changes: 2 additions & 2 deletions packages/ted/src/core/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type TActorComponent from '../actor-components/actor-component';
import TSceneComponent from '../actor-components/scene-component';
import type TEngine from '../engine/engine';
import type { TSerializedRenderTask } from '../renderer/frame-params';
import type TLevel from './level';
import type TWorld from './world';

export interface TActorWithOnUpdate extends TActor {
onUpdate(engine: TEngine, delta: number): Promise<void>;
Expand All @@ -14,7 +14,7 @@ const hasOnUpdate = (state: TActor): state is TActorWithOnUpdate =>

export default class TActor {
public uuid: string = uuidv4();
public level?: TLevel;
public world?: TWorld;

/**
* List of components that belong to this actor
Expand Down
24 changes: 12 additions & 12 deletions packages/ted/src/core/game-state.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ICamera } from '../cameras/camera';
import type TEngine from '../engine/engine';
import type TActor from './actor';
import TLevel from './level';
import TWorld from './world';

export interface TGameStateWithOnUpdate extends TGameState {
onUpdate(engine: TEngine, delta: number): Promise<void>;
Expand Down Expand Up @@ -40,18 +40,18 @@ const hasOnLeave = (state: TGameState): state is TGameStateWithOnLeave =>

export default class TGameState {
public created = false;
public level?: TLevel;
public world?: TWorld;

public activeCamera?: ICamera;

/**
* Adds actor to the level in this game state.
* Adds actor to the world in this game state.
* This is here for convience.
*
* @param actor
*/
public addActor(actor: TActor): void {
this.level?.addActor(actor);
this.world?.addActor(actor);
}

/**
Expand All @@ -64,15 +64,15 @@ export default class TGameState {
* @hidden
*/
public async update(engine: TEngine, delta: number): Promise<void> {
await this.level?.update(engine, delta);
await this.world?.update(engine, delta);

if (hasOnUpdate(this)) {
await this.onUpdate(engine, delta);
}
}

public getRenderTasks() {
return this.level?.getRenderTasks() || [];
return this.world?.getRenderTasks() || [];
}

/**
Expand All @@ -82,9 +82,9 @@ export default class TGameState {
* @hidden
*/
public async create(engine: TEngine) {
this.level = new TLevel(engine);
this.world = new TWorld(engine);

await this.level.load();
await this.world.create();

if (hasOnCreate(this)) {
await this.onCreate(engine);
Expand All @@ -98,7 +98,7 @@ export default class TGameState {
* @hidden
*/
public async enter(engine: TEngine, ...args: any[]) {
this.level?.start();
this.world?.start();

if (hasOnEnter(this)) {
await this.onEnter(engine, ...args);
Expand All @@ -112,7 +112,7 @@ export default class TGameState {
* @hidden
*/
public async leave(engine: TEngine) {
this.level?.pause();
this.world?.pause();

if (hasOnLeave(this)) {
await this.onLeave(engine);
Expand All @@ -132,9 +132,9 @@ export default class TGameState {
}

/**
* Currently only calls destroy on the level which stops the physics worker
* Currently only calls destroy on the world which stops the physics worker
*/
public async destroy() {
this.level?.destroy();
this.world?.destroy();
}
}
110 changes: 0 additions & 110 deletions packages/ted/src/core/level.ts

This file was deleted.

Loading

0 comments on commit 15e078b

Please sign in to comment.