summaryrefslogtreecommitdiff
path: root/src/systems/keyboardInput.js
diff options
context:
space:
mode:
authorLogan Hunt <loganhunt@simponic.xyz>2022-04-02 18:37:36 -0600
committerLogan Hunt <loganhunt@simponic.xyz>2022-04-02 18:37:36 -0600
commitaef3c05bc7f3feba7832078aa1cb4bd96c52c1dd (patch)
tree72a5db540a03fe8207e26ee55b64163fe84a8215 /src/systems/keyboardInput.js
parent61c835723a685121316b196fb6f49ab10c92c240 (diff)
downloadbbiy-aef3c05bc7f3feba7832078aa1cb4bd96c52c1dd.tar.gz
bbiy-aef3c05bc7f3feba7832078aa1cb4bd96c52c1dd.zip
Simple controlling system
Diffstat (limited to 'src/systems/keyboardInput.js')
-rw-r--r--src/systems/keyboardInput.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/systems/keyboardInput.js b/src/systems/keyboardInput.js
new file mode 100644
index 0000000..ff2211c
--- /dev/null
+++ b/src/systems/keyboardInput.js
@@ -0,0 +1,30 @@
+game.system.KeyboardInput = () => {
+ "use strict";
+ const keys = {};
+ const keyPress = (event) => {
+ if (!event.repeat) {
+ keys[event.key] = true;
+ }
+ };
+ const update = (elapsedTime, entities) => {
+ for (let id in entities) {
+ const entity = entities[id];
+ if (entity.hasComponent('controllable')) {
+ const controls = entity.components.controllable.controls;
+ if (controls.includes('left') && keys['ArrowLeft']) {
+ entity.addComponent(game.components.Momentum({ dx: -1, dy: 0 }));
+ } else if (controls.includes('right') && keys['ArrowRight']) {
+ entity.addComponent(game.components.Momentum({ dx: 1, dy: 0 }));
+ } else if (controls.includes('up') && keys['ArrowUp']) {
+ entity.addComponent(game.components.Momentum({ dx: 0, dy: -1 }));
+ } else if (controls.includes('down') && keys['ArrowDown']) {
+ entity.addComponent(game.components.Momentum({ dx: 0, dy: 1 }));
+ }
+
+ Object.keys(keys).map((key) => delete keys[key]);
+ }
+ }
+ };
+ window.addEventListener("keydown", keyPress);
+ return { keys, update };
+} \ No newline at end of file