Skip to content

Commit

Permalink
feat: add query system
Browse files Browse the repository at this point in the history
  • Loading branch information
ufocoder committed May 1, 2024
1 parent dc4061f commit f8e6d20
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 448 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"lint": "eslint .",
"lint": "eslint ./src",
"prepare": "husky"
},
"devDependencies": {
Expand Down
14 changes: 9 additions & 5 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ interface Color {
a: number;
}

interface Сharacter {
x: number;
y: number;
angle: number;
health: number;
}

type LevelMap = number[][];

interface Level {
map: LevelMap;
textures: Record<number, string>;
player: {
x: number;
y: number;
angle: number;
};
player: Сharacter;
enemies: Сharacter[];
exit: {
x: number;
y: number;
Expand Down
31 changes: 19 additions & 12 deletions src/levels/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,41 @@ const level: Level = {
}
},
map: [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,1,1,1,1,0,0,0,0],
[0,0,0,0,0,1,0,0,0,1,0,0,0,0],
[0,0,0,0,0,2,0,0,0,1,0,0,0,0],
[0,0,0,0,0,1,0,0,0,1,0,0,0,0],
[0,0,0,0,0,1,1,1,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
],
textures: {
1: 'wall-1a',
2: 'wall-2b',
3: 'wall-3c',
9: 'exit',
1: 'wall',
2: 'exit',
},
player: {
x: 2,
y: 2,
angle: 45,
health: 100,
},
enemies: [
{
x: 3,
y: 9,
angle: 45,
health: 100,
},
{
x: 9,
y: 3,
angle: 45,
health: 100,
},
],
exit: {
x: 11,
y: 11,
x: 4,
y: 6,
},
}

Expand Down
16 changes: 12 additions & 4 deletions src/lib/ecs/System.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import Entity from "./Entity";
import QuerySystem from "./lib/QuerySystem";

export default interface System {
components: Array<Function>;
update(dt: number, entities: Entity[]): void;
destroy(): void;
export default abstract class BaseSystem {
querySystem: QuerySystem;

requiredComponents: Array<Function> = [];

constructor(querySystem: QuerySystem) {
this.querySystem = querySystem;
}

abstract update(dt: number, entities: Entity[]): void;
abstract destroy(): void;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component } from "src/lib/ecs/Component";

export default class FieldOfVisionComponent implements Component {
fov: number;
export default class CameraComponent implements Component {
fov: number; // FieldOfVisionComponent

constructor(fov: number = 0) {
this.fov = fov;
Expand Down
11 changes: 11 additions & 0 deletions src/lib/ecs/components/HealthComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Component } from "src/lib/ecs/Component";

export default class HealthComponent implements Component {
public maximum: number;
public current: number;

constructor(maximum: number, current: number) {
this.maximum = maximum;
this.current = current;
}
}
25 changes: 25 additions & 0 deletions src/lib/ecs/lib/PositionMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Entity from "../Entity";

export default class PositionMap {
map: (Entity | undefined)[][];

constructor(rows: number, cols: number) {
this.map = Array.from({ length: rows }, () => Array.from({ length: cols }, () => undefined));
}

public set(x: number, y: number, entity: Entity) {
this.map[y][x] = entity;
}

public get(x: number, y: number) {
return this.has(x,y) ? this.map[y][x] : undefined;
}

public has(x: number, y: number) {
return Boolean(this.map[y] && this.map[y][x]);
}

public clear() {
this.map = [[]];
}
}
25 changes: 25 additions & 0 deletions src/lib/ecs/lib/QuerySystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Entity from "../Entity";

export default class QuerySystem {

private entitiesByQuery: Map<string, Entity[]> = new Map();

constructor(private entities: Entity[]) {}

protected generateKey(componentTypes: Function[]) {
return componentTypes.map(type => type.name).sort().join(',');
}

query(componentTypes: Function[]): Entity[] {
const key = this.generateKey(componentTypes);

if (!this.entitiesByQuery.has(key)) {
const matchingEntities = this.entities.filter((entity) =>
componentTypes.every((type) => entity.components.has(type))
);
this.entitiesByQuery.set(key, matchingEntities);
}

return this.entitiesByQuery.get(key)!;
}
}
Loading

0 comments on commit f8e6d20

Please sign in to comment.