refactor: ecs diff

This commit is contained in:
cha0s 2024-06-22 12:30:25 -05:00
parent 3b6aee099f
commit 0c06dd7b83
7 changed files with 64 additions and 60 deletions

View File

@ -3,13 +3,9 @@ import {System} from '@/ecs/index.js';
export default class ApplyControlMovement extends System {
tick() {
const {diff} = this.ecs;
for (const id in diff) {
if (diff[id].Controlled) {
const {Controlled, Momentum, Speed} = this.ecs.get(id);
Momentum.x = Speed.speed * (Controlled.moveRight - Controlled.moveLeft);
Momentum.y = Speed.speed * (Controlled.moveDown - Controlled.moveUp);
}
for (const {Controlled, Momentum, Speed} of this.ecs.changed(['Controlled'])) {
Momentum.x = Speed.speed * (Controlled.moveRight - Controlled.moveLeft);
Momentum.y = Speed.speed * (Controlled.moveDown - Controlled.moveUp);
}
}

View File

@ -3,16 +3,12 @@ import {System} from '@/ecs/index.js';
export default class CalculateAabbs extends System {
tick() {
const {diff} = this.ecs;
for (const id in diff) {
if (diff[id].Position) {
const {Position: {x, y}, VisibleAabb} = this.ecs.get(id);
if (VisibleAabb) {
VisibleAabb.x0 = x - 32;
VisibleAabb.x1 = x + 32;
VisibleAabb.y0 = y - 32;
VisibleAabb.y1 = y + 32;
}
for (const {Position: {x, y}, VisibleAabb} of this.ecs.changed(['Position'])) {
if (VisibleAabb) {
VisibleAabb.x0 = x - 32;
VisibleAabb.x1 = x + 32;
VisibleAabb.y0 = y - 32;
VisibleAabb.y1 = y + 32;
}
}
}

View File

@ -3,23 +3,19 @@ import {System} from '@/ecs/index.js';
export default class ClampPositions extends System {
tick() {
const {diff} = this.ecs;
const {AreaSize} = this.ecs.get(1);
for (const id in diff) {
if (diff[id].Position) {
const {Position} = this.ecs.get(id);
if (Position.x < 0) {
Position.x = 0;
}
if (Position.y < 0) {
Position.y = 0;
}
if (Position.x >= AreaSize.x) {
Position.x = AreaSize.x - 0.0001;
}
if (Position.y >= AreaSize.y) {
Position.y = AreaSize.y - 0.0001;
}
for (const {Position} of this.ecs.changed(['Position'])) {
if (Position.x < 0) {
Position.x = 0;
}
if (Position.y < 0) {
Position.y = 0;
}
if (Position.x >= AreaSize.x) {
Position.x = AreaSize.x - 0.0001;
}
if (Position.y >= AreaSize.y) {
Position.y = AreaSize.y - 0.0001;
}
}
}

View File

@ -3,23 +3,19 @@ import {System} from '@/ecs/index.js';
export default class ControlDirection extends System {
tick() {
const {diff} = this.ecs;
for (const id in diff) {
const {Controlled} = diff[id];
if (Controlled) {
const {Controlled: {moveUp, moveRight, moveDown, moveLeft}, Direction} = this.ecs.get(id);
if (moveUp > 0) {
Direction.direction = 0;
}
if (moveDown > 0) {
Direction.direction = 2;
}
if (moveLeft > 0) {
Direction.direction = 3;
}
if (moveRight > 0) {
Direction.direction = 1;
}
for (const {Controlled, Direction} of this.ecs.changed(['Controlled'])) {
const {moveUp, moveRight, moveDown, moveLeft} = Controlled;
if (moveUp > 0) {
Direction.direction = 0;
}
if (moveDown > 0) {
Direction.direction = 2;
}
if (moveLeft > 0) {
Direction.direction = 3;
}
if (moveRight > 0) {
Direction.direction = 1;
}
}
}

View File

@ -11,11 +11,8 @@ export default class FollowCamera extends System {
}
tick() {
const {diff} = this.ecs;
for (const id in diff) {
if (diff[id].Position) {
this.updateCamera(this.ecs.get(id));
}
for (const entity of this.ecs.changed(['Position'])) {
this.updateCamera(entity);
}
}

View File

@ -81,11 +81,8 @@ export default class UpdateSpatialHash extends System {
}
tick() {
const {diff} = this.ecs;
for (const id in diff) {
if (diff[id].VisibleAabb) {
this.updateHash(this.ecs.get(id));
}
for (const entity of this.ecs.changed(['VisibleAabb'])) {
this.updateHash(entity);
}
}

View File

@ -67,6 +67,32 @@ export default class Ecs {
this.createManySpecific(creating);
}
changed(criteria) {
const it = Object.entries(this.diff).values();
return {
[Symbol.iterator]() {
return this;
},
next: () => {
let result = it.next();
let satisfied = false;
while (!result.done) {
for (const componentName of criteria) {
if (!(componentName in result.value[1])) {
result = it.next();
continue;
}
}
break;
}
if (result.done) {
return {done: true, value: undefined};
}
return {done: false, value: this.get(result.value[0])};
},
};
}
create(components = {}) {
const [entityId] = this.createMany([components]);
return entityId;