Skip to content

Commit

Permalink
#9 implement entity add/remove events
Browse files Browse the repository at this point in the history
  • Loading branch information
minecrawler committed Jul 5, 2023
1 parent 79f18c0 commit 655fdcf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/events/internal-events.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import type {ISystem} from "../system/system.spec.ts";
import type {TObjectProto} from "../_.spec.ts";
import type {IIStateProto, IState} from "../state/state.spec.ts";
import type {IState} from "../state/state.spec.ts";
import type {IEntity} from "../entity/entity.spec.ts";


export class SimECSEvent {}

class SimECSEntityEvent extends SimECSEvent {
constructor(
public readonly entity: Readonly<IEntity>,
) { super() }
}

export class SimECSEntityAddEvent extends SimECSEntityEvent {}
export class SimECSEntityRemoveEvent extends SimECSEntityEvent {}

class SimECSPDAEvent extends SimECSEvent {
constructor(
public readonly state: Readonly<IState> | undefined,
Expand Down
7 changes: 7 additions & 0 deletions src/world/runtime/runtime-world_entities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {type RuntimeWorld} from "./runtime-world.ts";
import type {IEntity} from "../../entity/entity.spec.ts";
import {addEntitySym, removeEntitySym} from "../../query/_.ts";
import {SimECSEntityAddEvent, SimECSEntityRemoveEvent} from "../../events/internal-events.ts";

export function addEntity(this: RuntimeWorld, entity: Readonly<IEntity>): void {
this.data.entities.add(entity);
Expand All @@ -11,6 +12,9 @@ export function addEntity(this: RuntimeWorld, entity: Readonly<IEntity>): void {
query[addEntitySym](entity);
}
}

// todo: await in 0.7.0
this.eventBus.publish(new SimECSEntityAddEvent(entity));
}

export function hasEntity(this: RuntimeWorld, entity: Readonly<IEntity>): boolean {
Expand All @@ -26,4 +30,7 @@ export function removeEntity(this: RuntimeWorld, entity: Readonly<IEntity>): voi
query[removeEntitySym](entity);
}
}

// todo: await in 0.7.0
this.eventBus.publish(new SimECSEntityRemoveEvent(entity));
}

0 comments on commit 655fdcf

Please sign in to comment.