No more ecs

This commit is contained in:
2026-09-25 16:05:25 -03:00
parent 4539898c58
commit 213363d0be
56 changed files with 1103 additions and 1644 deletions
-18
View File
@@ -1,18 +0,0 @@
import { Component } from "$/common/ecs/component.ts";
export class Position extends Component {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z = 0) {
super();
this.x = x;
this.y = y;
this.z = z;
}
clone() {
return new Position(this.x, this.y, this.z);
}
}
-14
View File
@@ -1,14 +0,0 @@
import { Component } from "$/common/ecs/component.ts";
export class Velocity extends Component {
vx: number;
vy: number;
vz: number;
constructor(vx: number, vy: number, vz = 0) {
super();
this.vx = vx;
this.vy = vy;
this.vz = vz;
}
}
-9
View File
@@ -1,9 +0,0 @@
export abstract class Component {
__component = true;
}
export abstract class SerializableComponent extends Component {
abstract serialize(): unknown;
// right, this is static you cant do this,,
// abstract deserialize<T>(data: unknown): T;
}
-28
View File
@@ -1,28 +0,0 @@
import type { Component } from "./component.ts";
// deno-lint-ignore no-explicit-any
type ComponentConstructor<T extends Component> = new (...args: any[]) => T;
export class Entity {
id: string;
// deno-lint-ignore no-explicit-any
components = new Map<ComponentConstructor<any>, Component>();
active = true;
constructor(id: string = crypto.randomUUID()) {
this.id = id;
}
add<T extends Component>(component: T): T {
this.components.set(component.constructor as ComponentConstructor<T>, component);
return component;
}
get<T extends Component>(type: ComponentConstructor<T>): T | undefined {
return this.components.get(type) as T;
}
get_all(): Iterable<Component> {
return this.components.values();
}
}
-4
View File
@@ -1,4 +0,0 @@
export * from "./component.ts";
export * from "./entity.ts";
export * from "./system.ts";
export * from "./world.ts";
-5
View File
@@ -1,5 +0,0 @@
import type { World } from "./world.ts";
export abstract class System {
abstract update(world: World, delta: number): void;
}
-80
View File
@@ -1,80 +0,0 @@
import type { Entity } from "./entity.ts";
import type { System } from "./system.ts";
export class World {
#entities = new Set<Entity>();
#entities_for_deletion = new Set<Entity>();
#systems = new Map<string, Set<System>>();
#tags = new Map<string, Entity[]>();
#states = new Set<string>();
#state: string = "";
#new_state: string | undefined;
constructor(initial_state: string) {
this.#state = initial_state;
this.add_state("*");
}
add_state(new_state: string) {
this.#states.add(new_state);
this.#systems.set(new_state, new Set());
}
add_entity(entity: Entity) {
this.#entities.add(entity);
}
add_system(system: System, state: string) {
console.assert(this.#states.has(state));
this.#systems.get(state)!.add(system);
}
add_tag(tag: string, entities: Entity[]) {
this.#tags.set(tag, entities);
}
get_tag(tag: string) {
return this.#tags.get(tag);
}
update(delta: number) {
for (const system of this.#systems.get("*") ?? []) {
system.update(this, delta);
}
for (const system of this.#systems.get(this.#state) ?? []) {
system.update(this, delta);
}
// should be faster than Set.prototype.difference lol
for (const entity of this.#entities_for_deletion) {
this.#entities.delete(entity);
}
if (this.#new_state) {
this.#state = this.#new_state;
this.#new_state = undefined;
}
}
get_entities() {
return this.#entities;
}
delete_entity(entity: Entity) {
this.#entities_for_deletion.add(entity);
}
clear_entities() {
for (const entity of this.#entities) {
this.#entities_for_deletion.add(entity);
}
}
get state() {
return this.#state;
}
set state(new_state: string) {
this.#new_state = new_state;
}
}
-18
View File
@@ -1,18 +0,0 @@
import { System, World } from "$/common/ecs/mod.ts";
import { Position } from "$/common/components/position.ts";
import { Velocity } from "$/common/components/velocity.ts";
export class MovementSystem extends System {
update(world: World, delta: number): void {
for (const entity of world.get_entities()) {
const position = entity.get(Position);
const velocity = entity.get(Velocity);
if (position && velocity) {
position.x += velocity.vx * delta;
position.y += velocity.vy * delta;
position.z += velocity.vz * delta;
}
}
}
}