diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-02 16:04:13 -0600 |
---|---|---|
committer | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-02 16:04:13 -0600 |
commit | 5a55b5fa0c678ff03842d2adec8543e754546718 (patch) | |
tree | 4f615829acc10045d81a9d553c757a14792bf81a /src | |
parent | 4d34b0b4b890e957030fbf6e3c8e1d4a02d2a627 (diff) | |
download | bbiy-5a55b5fa0c678ff03842d2adec8543e754546718.tar.gz bbiy-5a55b5fa0c678ff03842d2adec8543e754546718.zip |
Grid system & simple physics
Diffstat (limited to 'src')
-rw-r--r-- | src/bootstrap.js | 52 | ||||
-rw-r--r-- | src/components/gridPosition.js | 1 | ||||
-rw-r--r-- | src/components/momentum.js | 1 | ||||
-rw-r--r-- | src/entities/entity.js | 15 | ||||
-rw-r--r-- | src/game.js | 37 | ||||
-rw-r--r-- | src/render/graphics.js | 3 | ||||
-rw-r--r-- | src/systems/gridSystem.js | 73 | ||||
-rw-r--r-- | src/systems/physics.js | 13 | ||||
-rw-r--r-- | src/systems/render.js | 17 | ||||
-rw-r--r-- | src/systems/system.js | 1 | ||||
-rw-r--r-- | src/utils/objectEquivalence.js | 14 | ||||
-rw-r--r-- | src/utils/unitizeVector.js | 11 |
12 files changed, 185 insertions, 53 deletions
diff --git a/src/bootstrap.js b/src/bootstrap.js index 657067c..fc06962 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -1,30 +1,32 @@ game.bootstrap = (() => { const scripts = [ - { src: ['src/render/graphics.js'], id: 'render' }, - { src: ['src/components/component.js', 'src/components/position.js', 'src/components/appearence.js'], id: 'components' }, - { src: ['src/entities/entity.js'], id: 'entity' }, - { src: ['src/entities/bigblue.js'], id: 'entities' }, - { src: ['src/entities/flag.js'], id: 'entities' }, - { src: ['src/entities/floor.js'], id: 'entities' }, - { src: ['src/entities/grass.js'], id: 'entities' }, - { src: ['src/entities/hedge.js'], id: 'entities' }, - { src: ['src/entities/liquid.js'], id: 'entities' }, - { src: ['src/entities/rock.js'], id: 'entities' }, - { src: ['src/entities/wall.js'], id: 'entities' }, - { src: ['src/entities/wordBigBlue.js'], id: 'entities' }, - { src: ['src/entities/wordFlag.js'], id: 'entities' }, - { src: ['src/entities/wordIs.js'], id: 'entities' }, - { src: ['src/entities/wordKill.js'], id: 'entities' }, - { src: ['src/entities/wordLava.js'], id: 'entities' }, - { src: ['src/entities/wordPush.js'], id: 'entities' }, - { src: ['src/entities/wordRock.js'], id: 'entities' }, - { src: ['src/entities/wordSink.js'], id: 'entities' }, - { src: ['src/entities/wordStop.js'], id: 'entities' }, - { src: ['src/entities/wordWall.js'], id: 'entities' }, - { src: ['src/entities/wordWater.js'], id: 'entities' }, - { src: ['src/entities/wordWin.js'], id: 'entities' }, - { src: ['src/entities/wordYou.js'], id: 'entities' }, - { src: ['src/systems/render.js'], id: 'systems' }, + { src: ['src/utils/objectEquivalence.js', 'src/utils/unitizeVector.js'], id: 'utils'}, + { src: ['src/render/graphics.js'], id: 'graphics' }, + { src: ['src/components/component.js'], id: 'component' }, + { + src: [ + 'src/components/position.js', 'src/components/momentum.js', 'src/components/gridPosition.js', + 'src/components/appearence.js' + ], + id: 'components' + }, + { src: ['src/entities/entity.js'], id: 'entity' }, + { + src: [ + 'src/entities/bigblue.js', 'src/entities/flag.js', 'src/entities/floor.js', 'src/entities/grass.js', 'src/entities/hedge.js', + 'src/entities/liquid.js', 'src/entities/rock.js', 'src/entities/wall.js', 'src/entities/wordBigBlue.js', + 'src/entities/wordFlag.js', 'src/entities/wordIs.js', 'src/entities/wordKill.js', 'src/entities/wordLava.js', + 'src/entities/wordPush.js', 'src/entities/wordRock.js', 'src/entities/wordSink.js', 'src/entities/wordStop.js', + 'src/entities/wordWall.js', 'src/entities/wordWater.js', 'src/entities/wordWin.js', 'src/entities/wordYou.js' + ], + id: 'entities' + }, + { src: ['src/systems/system.js'], id: 'system' }, + { + src: [ + 'src/systems/render.js', 'src/systems/gridSystem.js', 'src/systems/physics.js' + ], + id: 'systems' }, { src: ['src/game.js'], id: 'game' }, ]; const assets = {}; diff --git a/src/components/gridPosition.js b/src/components/gridPosition.js new file mode 100644 index 0000000..ede4b23 --- /dev/null +++ b/src/components/gridPosition.js @@ -0,0 +1 @@ +game.components.GridPosition = ({x, y}) => game.Component('gridPosition', {x, y});
\ No newline at end of file diff --git a/src/components/momentum.js b/src/components/momentum.js new file mode 100644 index 0000000..b974aa7 --- /dev/null +++ b/src/components/momentum.js @@ -0,0 +1 @@ +game.components.Momentum = ({dx, dy}) => game.Component('momentum', {dx, dy});
\ No newline at end of file diff --git a/src/entities/entity.js b/src/entities/entity.js index 33d1031..1eb5223 100644 --- a/src/entities/entity.js +++ b/src/entities/entity.js @@ -5,13 +5,14 @@ game.Entity = (id=game.nextId++) => { const addComponent = (component) => { components[component.name] = component; - } - const removeComponent = (component) => { - delete components[component.name]; - } - const hasComponent = (component) => { - components[component.name] !== undefined; - } + }; + const hasComponent = (componentName) => components[componentName] !== undefined; + const removeComponent = (componentName) => { + if (hasComponent(componentName)) { + delete components[componentName]; + } + }; + return { id, diff --git a/src/game.js b/src/game.js index cd82cc0..9cf60c6 100644 --- a/src/game.js +++ b/src/game.js @@ -1,20 +1,41 @@ -game.loop = (elapsedTime) => { - game.systems.Render.update(elapsedTime); +let lastTimeStamp; +game.loop = (timeStamp) => { + let elapsedTime = timeStamp - lastTimeStamp; + lastTimeStamp = timeStamp; + + game.systemOrder.map((i) => { + game.systems[i].update(elapsedTime, game.entities); + }); requestAnimationFrame(game.loop); } game.initialize = () => { + game.systemOrder = ["render", "physics", "gridSystem"]; + game.systems = { + render: game.system.Render(game.graphics), + physics: game.system.Physics(), + gridSystem: game.system.GridSystem({ + xDim: 15, + yDim: 15, + canvasWidth: game.canvas.width, + canvasHeight: game.canvas.height, + }), + }; + game.entities = {}; - game.bigBlue = game.createBigBlue(); - game.entities[game.bigBlue.id] = game.bigBlue; - game.bigBlue2 = game.createBigBlue(); - game.bigBlue2.components.position = game.components.Position({x: 200, y: 100}); - game.entities[game.bigBlue2.id] = game.bigBlue2; + + Array(400).fill(null).forEach((_, i) => { + const bigBlue = game.createBigBlue(); + bigBlue.addComponent(game.components.GridPosition({x: Math.floor(Math.random() * 15), y: Math.floor(Math.random() * 13)})); + game.entities[bigBlue.id] = bigBlue; + }); game.rock = game.createRock(); - game.rock.components.position = game.components.Position({x: 200, y: 200}); + game.rock.addComponent(game.components.Position({x: 200, y: 200})); + game.rock.addComponent(game.components.GridPosition({x: 0, y: 0})); game.entities[game.rock.id] = game.rock; + lastTimeStamp = performance.now() requestAnimationFrame(game.loop); } diff --git a/src/render/graphics.js b/src/render/graphics.js index de98633..6a199f0 100644 --- a/src/render/graphics.js +++ b/src/render/graphics.js @@ -3,10 +3,9 @@ game.graphics = ( (context) => { context.imageSmoothingEnabled = false; const clear = () => { - context.clearRect(0, 0, game.width, game.height); + context.clearRect(0, 0, game.canvas.width, game.canvas.height); }; - // Maybe this should be a component? const Sprite = ({image, spriteX, spriteY, spriteWidth, spriteHeight, timePerFrame, cols, rows, numFrames, drawFunction}) => { timePerFrame = timePerFrame ?? 100; numFrames = numFrames ?? 1; diff --git a/src/systems/gridSystem.js b/src/systems/gridSystem.js new file mode 100644 index 0000000..05ce17e --- /dev/null +++ b/src/systems/gridSystem.js @@ -0,0 +1,73 @@ +game.system.GridSystem = ({ xDim, yDim, canvasWidth, canvasHeight }) => { + const entitiesGrid = Array(yDim).fill(null).map(() => Array(xDim).fill(null).map(() => new Map())); + + const gridWidth = canvasWidth / xDim; + const gridHeight = canvasHeight / yDim; + + const gameCoordsToGrid = ({ x, y }) => { + return { x: Math.floor((x+gridWidth/2) / canvasWidth * xDim), y: Math.floor((y+gridHeight/2) / canvasHeight * yDim) }; + }; + + const gridCoordsToGame = ({ x, y }) => { + return { x: x * gridWidth, y: y * gridHeight }; + }; + + const rebuildGrid = (entities) => { + let changedIds = new Set(); + entities.map(entity => { + const { x, y } = entity.components.gridPosition; + if (!entitiesGrid[y][x].has(entity.id)) { + changedIds.add(entity.id); + } + }); + entitiesGrid.forEach((row) => row.forEach((entitiesInCell) => { + for (let id of entitiesInCell.keys()) { + if (changedIds.has(id)) { + entitiesInCell.delete(id); + } + } + })); + changedIds.forEach(id => { + const entity = entities[id]; + const { x, y } = entity.components.gridPosition; + entitiesGrid[y][x].set(entity.id, entity); + }); + } + + const update = (_elapsedTime, entities) => { + const gridEntities = Object.keys(entities).filter((x) => entities[x].hasComponent("gridPosition")).map((x) => entities[x]); + rebuildGrid(gridEntities); + gridEntities.map((entity) => { + if (entity.hasComponent("appearance")) { + entity.components.appearance.width = canvasWidth / xDim; + entity.components.appearance.height = canvasHeight / yDim; + } + if (entity.hasComponent("position")) { + const newGridCoords = gameCoordsToGrid(entity.components.position); + const oldGridCoords = entity.components.gridPosition; + if (!equivalence(newGridCoords, oldGridCoords)) { + const momentumVector = unitize({ + dx: newGridCoords.x - oldGridCoords.x, + dy: newGridCoords.y - oldGridCoords.y, + }); + + // TODO: Loop in momentum direction until we find an entity that does not have "push" component + entity.components.gridPosition.x = entity.components.gridPosition.x + momentumVector.dx; + entity.components.gridPosition.y = entity.components.gridPosition.y + momentumVector.dy; + + entity.components.position = { + ...entity.components.position, + ...gridCoordsToGame(entity.components.gridPosition) + }; + + if (entity.hasComponent("momentum")) { + entity.components.momentum.dx = 0; + entity.components.momentum.dy = 0; + } + } + } + }); + }; + + return { entitiesGrid, gameCoordsToGrid, gridCoordsToGame, update, gridWidth, gridHeight }; +};
\ No newline at end of file diff --git a/src/systems/physics.js b/src/systems/physics.js new file mode 100644 index 0000000..c9b6cf7 --- /dev/null +++ b/src/systems/physics.js @@ -0,0 +1,13 @@ +game.system.Physics = () => { + const update = (elapsedTime) => { + for (let id in game.entities) { + const entity = game.entities[id]; + if (entity.hasComponent("momentum")) { + const {dx, dy} = entity.components.momentum; + entity.components.position.x += dx * elapsedTime; + entity.components.position.y += dy * elapsedTime; + } + } + } + return { update }; +}
\ No newline at end of file diff --git a/src/systems/render.js b/src/systems/render.js index c8eff4b..0a4c72d 100644 --- a/src/systems/render.js +++ b/src/systems/render.js @@ -1,18 +1,13 @@ -game.systems = {}; -game.systems.Render = ((graphics) => { - const renderEntities = (elapsedTime, entities) => { +game.system.Render = (graphics) => { + const update = (elapsedTime, entities) => { + graphics.clear(); + for (let id in entities) { const entity = entities[id]; - if (entity.sprite && entity.components.position && entity.components.appearance) { -// document.getElementById("game-canvas").getContext("2d").drawImage(game.assets.bigblue, 100, 100, 100, 100); + if (entity.sprite && entity.hasComponent("position") && entity.hasComponent("appearance")) { entity.sprite.draw(elapsedTime, {...entity.components.position, ...entity.components.appearance}); } } } - - const update = (elapsedTime) => { - graphics.clear(); - renderEntities(elapsedTime, game.entities); - } return { update }; -})(game.graphics);
\ No newline at end of file +};
\ No newline at end of file diff --git a/src/systems/system.js b/src/systems/system.js new file mode 100644 index 0000000..f331401 --- /dev/null +++ b/src/systems/system.js @@ -0,0 +1 @@ +game.system = {};
\ No newline at end of file diff --git a/src/utils/objectEquivalence.js b/src/utils/objectEquivalence.js new file mode 100644 index 0000000..e33dd76 --- /dev/null +++ b/src/utils/objectEquivalence.js @@ -0,0 +1,14 @@ +const equivalence = (obj1, obj2) => { + if (obj1 === null) { + return obj1 === null; + } + return Object.keys(obj1).every(key => { + if (obj2[key] === undefined) { + return false; + } + if (typeof obj1[key] === 'object') { + return equivalence(obj1[key], obj2[key]); + } + return obj1[key] === obj2[key]; + }); +}; diff --git a/src/utils/unitizeVector.js b/src/utils/unitizeVector.js new file mode 100644 index 0000000..6297ca8 --- /dev/null +++ b/src/utils/unitizeVector.js @@ -0,0 +1,11 @@ +const unitize = (vector) => { + // Not *technically* a unit vector, but has x,y components of |magnitude| = 1 + Object.keys(vector).forEach((key) => { + if (typeof vector[key] === 'object') { + vector[key] = vector[key].unitize(); + } else if (typeof vector[key] === 'number') { + vector[key] = (vector[key] === 0 ? 0 : vector[key] / Math.abs(vector[key])); + } + }); + return vector; +}
\ No newline at end of file |