summaryrefslogtreecommitdiff
path: root/src/systems
diff options
context:
space:
mode:
Diffstat (limited to 'src/systems')
-rw-r--r--src/systems/collision.js29
-rw-r--r--src/systems/keyboardInput.js6
-rw-r--r--src/systems/render.js4
-rw-r--r--src/systems/undo.js32
4 files changed, 53 insertions, 18 deletions
diff --git a/src/systems/collision.js b/src/systems/collision.js
index 152dcd6..08a1d91 100644
--- a/src/systems/collision.js
+++ b/src/systems/collision.js
@@ -1,5 +1,6 @@
game.system.Collision = (entitiesGrid) => {
const update = (elapsedTime, entities, changedIds) => {
+ const thisChangedIds = new Set();
for (let entity of Object.keys(entities).map((id) => entities[id])) {
if (entity.hasComponent("controllable") && entity.hasComponent("gridPosition") && entity.hasComponent("momentum")) {
const momentum = unitize(entity.components.momentum);
@@ -38,25 +39,25 @@ game.system.Collision = (entitiesGrid) => {
entity.removeComponent("momentum");
} else {
entitiesToPush.map((e) => {
- const particles = game.createBorderParticles({
- colors: ["#16f7c9", "#0d6e5a", "#2fa18a", "#48cfb4", "#58877d", "#178054", "#2cdb92"],
- maxSpeed: 0.20,
- minRadius: 1,
- maxRadius: 3,
- minLife: 100,
- maxLife: 300,
- minAmount: 20,
- maxAmount: 50,
- });
- particles.addComponent(game.components.Position(e.components.position));
- particles.addComponent(game.components.Appearance({width: game.canvas.width / game.config.xDim, height: game.canvas.height / game.config.yDim}));
- game.entities[particles.id] = particles;
+// const particles = game.createBorderParticles({
+// colors: ["#16f7c9", "#0d6e5a", "#2fa18a", "#48cfb4", "#58877d", "#178054", "#2cdb92"],
+// maxSpeed: 0.20,
+// minRadius: 1,
+// maxRadius: 3,
+// minLife: 100,
+// maxLife: 300,
+// minAmount: 20,
+// maxAmount: 50,
+// });
+// particles.addComponent(game.components.Position(e.components.position));
+// particles.addComponent(game.components.Appearance({width: game.canvas.width / game.config.xDim, height: game.canvas.height / game.config.yDim}));
+// game.entities[particles.id] = particles;
e.addComponent(game.components.Momentum({...momentum}))
});
}
}
}
- return new Set();
+ return thisChangedIds;
};
return { update };
};
diff --git a/src/systems/keyboardInput.js b/src/systems/keyboardInput.js
index 60a62a1..5ce6737 100644
--- a/src/systems/keyboardInput.js
+++ b/src/systems/keyboardInput.js
@@ -1,4 +1,4 @@
-game.system.KeyboardInput = () => {
+game.system.KeyboardInput = (undoSystem) => {
"use strict";
const keys = {};
const keyPress = (event) => {
@@ -20,9 +20,11 @@ game.system.KeyboardInput = () => {
} else if (controls.includes('down') && keys['ArrowDown']) {
entity.addComponent(game.components.Momentum({ dx: 0, dy: 1 }));
}
-
}
}
+ if (keys['z']) {
+ undoSystem.undo(entities);
+ }
Object.keys(keys).map((key) => delete keys[key]);
return new Set();
diff --git a/src/systems/render.js b/src/systems/render.js
index adf3e86..d3bacb8 100644
--- a/src/systems/render.js
+++ b/src/systems/render.js
@@ -10,8 +10,8 @@ game.system.Render = (graphics) => {
});
sortedEntities.forEach((entity) => {
- if (entity.sprite && entity.hasComponent("position") && entity.hasComponent("appearance")) {
- entity.sprite.draw(elapsedTime, {...entity.components.position, ...entity.components.appearance});
+ if (entity.hasComponent("position") && entity.hasComponent("appearance") && entity.hasComponent("sprite")) {
+ game.sprites[entity.components.sprite.spriteName].draw(elapsedTime, {...entity.components.position, ...entity.components.appearance});
}
});
diff --git a/src/systems/undo.js b/src/systems/undo.js
new file mode 100644
index 0000000..fa36c93
--- /dev/null
+++ b/src/systems/undo.js
@@ -0,0 +1,32 @@
+game.system.Undo = (entitiesGrid) => {
+ const states = [];
+
+ const update = (elapsedTime, entities, changedIds) => {
+ if (changedIds.size) {
+ lastUndid = false;
+ const state = {};
+ for (let id in entities) {
+ if (entities[id].hasComponent("gridPosition")) {
+ state[id] = JSON.parse(JSON.stringify(entities[id].components));
+ }
+ }
+ states.push(state);
+ }
+ return new Set();
+ }
+
+ const undo = (entities) => {
+ states.map((state) => console.log(state[65].gridPosition));
+ let state = states.slice(0, -1).pop();
+ for (let id in state) {
+ for (let componentName in state[id]) {
+ entities[id].addComponent({name: componentName, ...state[id][componentName]});
+ }
+ }
+ if (states.length > 1) {
+ states.pop();
+ }
+ }
+
+ return { update, undo };
+} \ No newline at end of file