From aef3c05bc7f3feba7832078aa1cb4bd96c52c1dd Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Sat, 2 Apr 2022 18:37:36 -0600 Subject: [PATCH] Simple controlling system --- src/bootstrap.js | 4 ++-- src/components/controllable.js | 1 + src/game.js | 4 +++- src/systems/keyboardInput.js | 30 ++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/components/controllable.js create mode 100644 src/systems/keyboardInput.js diff --git a/src/bootstrap.js b/src/bootstrap.js index fc06962..93cb8f5 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -6,7 +6,7 @@ game.bootstrap = (() => { { src: [ 'src/components/position.js', 'src/components/momentum.js', 'src/components/gridPosition.js', - 'src/components/appearence.js' + 'src/components/appearence.js', 'src/components/controllable.js' ], id: 'components' }, @@ -24,7 +24,7 @@ game.bootstrap = (() => { { src: ['src/systems/system.js'], id: 'system' }, { src: [ - 'src/systems/render.js', 'src/systems/gridSystem.js', 'src/systems/physics.js' + 'src/systems/render.js', 'src/systems/gridSystem.js', 'src/systems/physics.js', 'src/systems/keyboardInput.js' ], id: 'systems' }, { src: ['src/game.js'], id: 'game' }, diff --git a/src/components/controllable.js b/src/components/controllable.js new file mode 100644 index 0000000..84b05ee --- /dev/null +++ b/src/components/controllable.js @@ -0,0 +1 @@ +game.components.Controllable = ({ controls }) => game.Component('controllable', { controls }); \ No newline at end of file diff --git a/src/game.js b/src/game.js index 9cf60c6..04aa4fc 100644 --- a/src/game.js +++ b/src/game.js @@ -11,7 +11,7 @@ game.loop = (timeStamp) => { } game.initialize = () => { - game.systemOrder = ["render", "physics", "gridSystem"]; + game.systemOrder = ["render", "physics", "gridSystem", "keyboardInput"]; game.systems = { render: game.system.Render(game.graphics), physics: game.system.Physics(), @@ -21,6 +21,7 @@ game.initialize = () => { canvasWidth: game.canvas.width, canvasHeight: game.canvas.height, }), + keyboardInput: game.system.KeyboardInput(), }; game.entities = {}; @@ -34,6 +35,7 @@ game.initialize = () => { game.rock = game.createRock(); game.rock.addComponent(game.components.Position({x: 200, y: 200})); game.rock.addComponent(game.components.GridPosition({x: 0, y: 0})); + game.rock.addComponent(game.components.Controllable({controls: ['left', 'right', 'up', 'down']})); game.entities[game.rock.id] = game.rock; lastTimeStamp = performance.now() 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