Skip to content

Commit

Permalink
feat: make it more faster
Browse files Browse the repository at this point in the history
  • Loading branch information
ufocoder committed May 8, 2024
1 parent 74522aa commit 5864717
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/lib/ecs/lib/PositionMap.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import Entity from "../Entity";
export default class PositionMap<T> {
map: Map<number, T>;

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

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

this.map = new Map<number, T>();
}

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

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

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

public clear() {
this.map = [[]];
this.map.clear();
}
}

0 comments on commit 5864717

Please sign in to comment.