Skip to content

Commit

Permalink
fix entity mutation command
Browse files Browse the repository at this point in the history
  • Loading branch information
minecrawler committed Apr 14, 2024
1 parent ba8a8fa commit ff82c2d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/events/internal-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class SimECSCloneEntityEvent extends SimECSEntityEvent {
super(clone);
}
}
export class SimECSMutateEntityEvent extends SimECSEntityEvent {}
export class SimECSEntityAddComponentEvent extends SimECSEntityEvent {
constructor(
public readonly entity: Readonly<IEntity>,
Expand Down
9 changes: 5 additions & 4 deletions src/world/runtime/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {IPreptimeWorld} from "../../preptime/preptime-world.spec.ts";
import type {IQuery} from "../../../query/query.spec.ts";
import {addEntitySym} from "../../../query/_.ts";
import {Entity} from "../../../entity/entity.ts";
import {SimECSMutateEntityEvent} from "../../../events/internal-events.ts";

export * from "./commands.spec.ts";

Expand Down Expand Up @@ -103,10 +104,10 @@ export class Commands implements ICommands {
throw new Error(`The entity "${entity.id}" cannot be mutated!`);
}

mutator(entity);
this.commands.push(() => {
this.world.removeEntity(entity);
this.world.addEntity(entity);
this.commands.push(async () => {
mutator(entity);
this.world.refreshEntityQueryRegistration(entity);
await this.world.eventBus.publish(new SimECSMutateEntityEvent(entity));
});
}

Expand Down
8 changes: 8 additions & 0 deletions src/world/runtime/runtime-world.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {type InstanceMap} from "../../util/instance-map.ts";
import type {TObjectProto, TTypeProto} from "../../_.spec.ts";
import type {TGroupHandle} from "../world.spec.ts";
import type {ISystem} from "../../system/system.spec.ts";
import type {RuntimeWorld} from "./runtime-world.ts";

export * from './commands/commands.spec.ts';

Expand Down Expand Up @@ -86,6 +87,13 @@ export interface IRuntimeWorld extends IImmutableWorld {
*/
hmrReplaceSystem(newSystem: ISystem): void

/**
* Refresh an entity's registration with queries.
* This is useful e.g. after an entity was mutated
* @param entity
*/
refreshEntityQueryRegistration(entity: Readonly<IEntity>): void

/**
* Replace a resource from this world with a new value
* @param obj
Expand Down
13 changes: 6 additions & 7 deletions src/world/runtime/runtime-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
getResources,
hasResource,
} from "../common/world_resources.ts";
import {addEntity, hasEntity, removeEntity} from "./runtime-world_entities.ts";
import {addEntity, hasEntity, refreshEntityQueryRegistration, removeEntity} from "./runtime-world_entities.ts";
import {addResource, removeResource, replaceResource} from "./runtime-world_resources.ts";
import {SimECSPushDownAutomaton} from "../../pda/sim-ecs-pda.ts";
import type {IState} from "../../state/state.spec.ts";
Expand Down Expand Up @@ -333,12 +333,10 @@ export class RuntimeWorld implements IRuntimeWorld, IMutableWorld {

// @ts-ignore
[Symbol.dispose]() {
{ // unset the context for all systems
let system;

for (system of this.systems) {
system.unsetRuntimeContext(this);
}
// unset the context for all systems
let system;
for (system of this.systems) {
system.unsetRuntimeContext(this);
}
}

Expand All @@ -353,6 +351,7 @@ export class RuntimeWorld implements IRuntimeWorld, IMutableWorld {
public createEntity = createEntity;
public getEntities = getEntities;
public hasEntity = hasEntity;
public refreshEntityQueryRegistration = refreshEntityQueryRegistration;
public removeEntity = removeEntity;


Expand Down
12 changes: 10 additions & 2 deletions src/world/runtime/runtime-world_entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function addEntity(this: RuntimeWorld, entity: Readonly<IEntity>): void {
this.entityEventHandlers.set(entity, eventMap);
}

{
{ // add entity to queries
let query;
for (query of this.queries) {
query[addEntitySym](entity);
Expand All @@ -58,14 +58,22 @@ export function hasEntity(this: RuntimeWorld, entity: Readonly<IEntity>): boolea
return this.data.entities.has(entity);
}

export function refreshEntityQueryRegistration(this: RuntimeWorld, entity: Readonly<IEntity>): void {
let query;
for (query of this.queries) {
query[removeEntitySym](entity);
query[addEntitySym](entity);
}
}

export function removeEntity(this: RuntimeWorld, entity: Readonly<IEntity>): void {
if (!this.data.entities.has(entity)) {
return;
}

this.data.entities.delete(entity);

{
{ // Remove entity from all queries
let query;
for (query of this.queries) {
query[removeEntitySym](entity);
Expand Down

0 comments on commit ff82c2d

Please sign in to comment.