Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions core/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export class Entity {
};
this.componentsMask |= componentMask[componentName];
if (!exists) {
this.sim.hooks.addComponent.notify({
this.sim.hooks.publish({
type: "addComponent",
entity: this,
component: component.name,
});
Expand All @@ -95,7 +96,11 @@ export class Entity {
removeComponent(name: keyof CoreComponents): Entity {
this.componentsMask &= ~componentMask[name];
delete this.components[name];
this.sim.hooks.removeComponent.notify({ entity: this, component: name });
this.sim.hooks.publish({
type: "removeComponent",
entity: this,
component: name,
});

return this;
}
Expand All @@ -105,15 +110,15 @@ export class Entity {

if (!hasTag) {
this.tags.add(tag);
this.sim.hooks.addTag.notify({ tag, entity: this });
this.sim.hooks.publish({ type: "addTag", tag, entity: this });
}

return this;
}

removeTag(tag: EntityTag): Entity {
this.tags.delete(tag);
this.sim.hooks.removeTag.notify({ tag, entity: this });
this.sim.hooks.publish({ type: "removeTag", tag, entity: this });

return this;
}
Expand Down
132 changes: 67 additions & 65 deletions core/sim/Sim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import type { CoreComponents } from "@core/components/component";
import type { EntityTag } from "@core/tags";
import { componentMask } from "@core/components/masks";
import LZString from "lz-string";
import { Observable } from "@core/utils/observer";
import { defaultIndexer } from "@core/systems/utils/default";
import { Vec2 } from "ogl";
import { isVec2 } from "@core/utils/misc";
import { entityIndexer } from "@core/entityIndexer/entityIndexer";
import { defaultLogger } from "@core/log";
import { PubSub } from "@core/utils/pubsub";
import { Entity, EntityComponents } from "../entity";
import { BaseSim } from "./BaseSim";
import type { System } from "../systems/system";
Expand All @@ -26,6 +26,40 @@ import { openDb } from "../db";

const logger = defaultLogger.sub("sim");

export type ComponentAddEvent = {
type: "addComponent";
entity: Entity;
component: keyof CoreComponents;
};
export type ComponentRemoveEvent = {
type: "removeComponent";
entity: Entity;
component: keyof CoreComponents;
};
export type TagAddEvent = { type: "addTag"; entity: Entity; tag: EntityTag };
export type TagRemoveEvent = {
type: "removeTag";
entity: Entity;
tag: EntityTag;
};
export type EntityRemoveEvent = {
type: "removeEntity";
entity: Entity;
reason: string;
};
export type DestroySimEvent = {
type: "destroy";
};
export type PhaseEvent = {
type: "phase";
delta: number;
phase: "start" | "init" | "update" | "render" | "cleanup" | "end";
};
export type SpeedChangeEvent = {
type: "speedChange";
newSpeed: number;
};

export interface SimConfig {
systems: System[];
}
Expand All @@ -36,26 +70,16 @@ export class Sim extends BaseSim {

@Expose()
entityIdCounter: number = 1;
hooks: {
addComponent: Observable<{
entity: Entity;
component: keyof CoreComponents;
}>;
removeComponent: Observable<{
entity: Entity;
component: keyof CoreComponents;
}>;
addTag: Observable<{ entity: Entity; tag: EntityTag }>;
removeTag: Observable<{ entity: Entity; tag: EntityTag }>;
removeEntity: Observable<{ entity: Entity; reason: string }>;
destroy: Observable<void>;

phase: Record<
"start" | "init" | "update" | "render" | "cleanup" | "end",
Observable<number>
>;
onSpeedChange: Observable<number>;
};
hooks: PubSub<
| ComponentAddEvent
| ComponentRemoveEvent
| TagAddEvent
| TagRemoveEvent
| EntityRemoveEvent
| DestroySimEvent
| PhaseEvent
| SpeedChangeEvent
>;

@Expose()
@Type(() => Entity)
Expand All @@ -70,49 +94,27 @@ export class Sim extends BaseSim {
super();

this.entities = new Map();
this.hooks = {
addComponent: new Observable("addComponent"),
removeComponent: new Observable("removeComponent"),
addTag: new Observable("addTag"),
removeTag: new Observable("removeTag"),
removeEntity: new Observable("removeEntity"),
destroy: new Observable("destroy"),
phase: {
start: new Observable("phase.start", false),
init: new Observable("phase.init", false),
update: new Observable("phase.update", false),
render: new Observable("phase.render", false),
cleanup: new Observable("phase.cleanup", false),
end: new Observable("phase.end", false),
},
onSpeedChange: new Observable("onSpeedChange", false),
};
this.hooks = new PubSub();

entityIndexer.clear();
this.index = defaultIndexer;
for (const index of Object.values(defaultIndexer)) {
index.apply();
}

this.hooks.addComponent.subscribe(
"EntityIndexer",
({ entity, component }) => {
entityIndexer.updateMask(entity);
if (component === "position") {
entityIndexer.updateSector(entity.requireComponents(["position"]));
}
this.hooks.subscribe("addComponent", ({ entity, component }) => {
entityIndexer.updateMask(entity);
if (component === "position") {
entityIndexer.updateSector(entity.requireComponents(["position"]));
}
);
this.hooks.removeComponent.subscribe(
"EntityIndexer",
({ entity, component }) => {
entityIndexer.updateMask(entity);
if (component === "position") {
entityIndexer.removeFromSectors(entity);
}
});
this.hooks.subscribe("removeComponent", ({ entity, component }) => {
entityIndexer.updateMask(entity);
if (component === "position") {
entityIndexer.removeFromSectors(entity);
}
);
this.hooks.removeEntity.subscribe("EntityIndexer", ({ entity, reason }) => {
});
this.hooks.subscribe("removeEntity", ({ entity, reason }) => {
logger.log(
`Removing entity ${entity.id} ${
entity.cp.name?.value ??
Expand All @@ -127,7 +129,7 @@ export class Sim extends BaseSim {
);
entityIndexer.remove(entity);
});
this.hooks.destroy.subscribe("EntityIndexer", () => {
this.hooks.subscribe("destroy", () => {
entityIndexer.clear();
});

Expand All @@ -141,7 +143,7 @@ export class Sim extends BaseSim {
};

unregisterEntity = (entity: Entity, reason: string) => {
this.hooks.removeEntity.notify({ entity, reason });
this.hooks.publish({ type: "removeEntity", entity, reason });
this.entities.delete(entity.id);
};

Expand All @@ -154,19 +156,19 @@ export class Sim extends BaseSim {
return;
}

this.hooks.phase.start.notify(delta);
this.hooks.phase.init.notify(delta);
this.hooks.phase.update.notify(delta);
this.hooks.phase.render.notify(delta);
this.hooks.phase.cleanup.notify(delta);
this.hooks.phase.end.notify(delta);
this.hooks.publish({ type: "phase", delta, phase: "start" });
this.hooks.publish({ type: "phase", delta, phase: "init" });
this.hooks.publish({ type: "phase", delta, phase: "update" });
this.hooks.publish({ type: "phase", delta, phase: "render" });
this.hooks.publish({ type: "phase", delta, phase: "cleanup" });
this.hooks.publish({ type: "phase", delta, phase: "end" });

this.updateTimer(delta);
};

override setSpeed(value: number) {
super.setSpeed(value);
this.hooks.onSpeedChange.notify(value);
this.hooks.publish({ type: "speedChange", newSpeed: value });
}

init = () => {
Expand Down Expand Up @@ -225,7 +227,7 @@ export class Sim extends BaseSim {

destroy = () => {
this.stop();
this.hooks.destroy.notify();
this.hooks.publish({ type: "destroy" });
if (!isHeadless) {
window.sim = undefined!;
window.selected = undefined!;
Expand Down
6 changes: 5 additions & 1 deletion core/systems/ai/facilityPlanning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ export class FacilityPlanningSystem extends System<"plan"> {
apply = (sim: Sim) => {
super.apply(sim);

sim.hooks.phase.update.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "update") {
this.exec();
}
});
};

createNewFactory = (faction: Faction): Facility => {
Expand Down
6 changes: 5 additions & 1 deletion core/systems/ai/orderPlanning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ export class OrderPlanningSystem extends System<"exec"> {
apply = (sim: Sim): void => {
super.apply(sim);

sim.hooks.phase.update.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "update") {
this.exec();
}
});
};

exec = (): void => {
Expand Down
6 changes: 5 additions & 1 deletion core/systems/ai/shipPlanning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ export class ShipPlanningSystem extends System<"plan"> {
apply = (sim: Sim) => {
super.apply(sim);

sim.hooks.phase.update.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "update") {
this.exec();
}
});
};

getFacilityShipRequests = (faction: Faction): ShipRequest[] =>
Expand Down
6 changes: 5 additions & 1 deletion core/systems/ai/shipReturning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export class ShipReturningSystem extends System<"exec"> {
apply = (sim: Sim) => {
super.apply(sim);

sim.hooks.phase.update.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "update") {
this.exec();
}
});
};

exec = (): void => {
Expand Down
6 changes: 5 additions & 1 deletion core/systems/ai/spotting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export class SpottingSystem extends System<"exec"> {
apply = (sim: Sim) => {
super.apply(sim);

sim.hooks.phase.update.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "update") {
this.exec();
}
});
};

static getEnemies(
Expand Down
6 changes: 5 additions & 1 deletion core/systems/ai/tauHarassing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export class TauHarassingSystem extends System<"exec"> {
apply = (sim: Sim) => {
super.apply(sim);

sim.hooks.phase.update.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "update") {
this.exec();
}
});
};

getFleet = (): Ship | null => {
Expand Down
6 changes: 5 additions & 1 deletion core/systems/attacking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export class AttackingSystem extends System {
apply = (sim: Sim) => {
super.apply(sim);

sim.hooks.phase.update.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "update") {
this.exec();
}
});
};

exec = (): void => {
Expand Down
6 changes: 5 additions & 1 deletion core/systems/budgetPlanning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export class BudgetPlanningSystem extends System<"exec"> {
apply = (sim: Sim): void => {
super.apply(sim);

sim.hooks.phase.update.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "update") {
this.exec();
}
});
};

exec = (): void => {
Expand Down
6 changes: 5 additions & 1 deletion core/systems/collectibleUnregistering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export class CollectibleUnregisteringSystem extends System {
apply = (sim: Sim) => {
super.apply(sim);

sim.hooks.phase.cleanup.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "cleanup") {
this.exec();
}
});
};

exec = (): void => {
Expand Down
6 changes: 5 additions & 1 deletion core/systems/cooldowns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export class CooldownUpdatingSystem extends System {
apply = (sim: Sim): void => {
super.apply(sim);

sim.hooks.phase.init.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase, delta }) => {
if (phase === "init") {
this.exec(delta);
}
});
};
exec = (delta: number): void => {
this.sim.entities.forEach((entity) => entity.cooldowns.update(delta));
Expand Down
6 changes: 5 additions & 1 deletion core/systems/crewGrowing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export class CrewGrowingSystem extends System<"exec"> {
const offset =
Math.floor(sim.getTime() / gameDay) + 1 - sim.getTime() / gameDay;
this.cooldowns.use("exec", offset);
sim.hooks.phase.update.subscribe(this.constructor.name, this.exec);
sim.hooks.subscribe("phase", ({ phase }) => {
if (phase === "update") {
this.exec();
}
});
};

exec = (): void => {
Expand Down
Loading
Loading