diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-19 16:02:53 -0600 |
---|---|---|
committer | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-19 16:02:53 -0600 |
commit | 58ceff79496f60969eecb28a767b1ea28292becb (patch) | |
tree | 3a6acad4d9333aef2e18bb5d073e054926b20340 /src/systems | |
parent | 9b597426ac45775f63b1fe4365c6fa8f3c3179af (diff) | |
download | bbiy-58ceff79496f60969eecb28a767b1ea28292becb.tar.gz bbiy-58ceff79496f60969eecb28a767b1ea28292becb.zip |
Finish rule system; add burn and sink; particle effects on kill, sink
Diffstat (limited to 'src/systems')
-rw-r--r-- | src/systems/collision.js | 107 | ||||
-rw-r--r-- | src/systems/keyboardInput.js | 2 | ||||
-rw-r--r-- | src/systems/logic.js | 35 | ||||
-rw-r--r-- | src/systems/render.js | 2 |
4 files changed, 100 insertions, 46 deletions
diff --git a/src/systems/collision.js b/src/systems/collision.js index d769636..013ae1e 100644 --- a/src/systems/collision.js +++ b/src/systems/collision.js @@ -2,50 +2,83 @@ 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); - - let found; - const proposed = {x: entity.components.gridPosition.x + momentum.dx, y: entity.components.gridPosition.y + momentum.dy}; - const entitiesToPush = []; - let wall = false; - do { - const proposedClampedInBounds = clamp(proposed, game.config.xDim-1, game.config.yDim-1); - if (!equivalence(proposed, proposedClampedInBounds)) { - break; + if (entity.hasComponent("position") && entity.hasComponent("gridPosition") && (entity.hasComponent("burnable") || entity.hasComponent("sinkable")) && entity.hasComponent("alive")) { + for (let collided of entitiesGrid[entity.components.gridPosition.y][entity.components.gridPosition.x].values()) { + if (!equivalence(entity, collided) && collided.hasComponent("alive")){ + if (entity.hasComponent("burnable") && collided.hasComponent("burn")) { + const burnedParticleSpawner = game.createBorderParticles({colors: ["#f58d42", "#d6600b", "#c7a312", "#f2b844"]}); + burnedParticleSpawner.addComponent(game.components.Position(collided.components.position)); + burnedParticleSpawner.addComponent(game.components.Appearance({width: game.canvas.width / game.config.xDim, height: game.canvas.height / game.config.yDim})); + game.entities[burnedParticleSpawner.id] = burnedParticleSpawner; + entity.removeComponent("alive"); + break; + } else if (entity.hasComponent("sinkable") && collided.hasComponent("sink")) { + const sunkParticleSpawner = game.createBorderParticles({colors: ["#16f7c9", "#0d6e5a", "#2fa18a", "#48cfb4", "#58877d", "#178054", "#2cdb92"]}); + sunkParticleSpawner.addComponent(game.components.Position(collided.components.position)); + sunkParticleSpawner.addComponent(game.components.Appearance({width: game.canvas.width / game.config.xDim, height: game.canvas.height / game.config.yDim})); + game.entities[sunkParticleSpawner.id] = sunkParticleSpawner; + entity.removeComponent("alive"); + collided.removeComponent("alive"); + break; + } } + } + } + if (entity.hasComponent("controllable") && entity.hasComponent("gridPosition")) { + for (let collided of entitiesGrid[entity.components.gridPosition.y][entity.components.gridPosition.x].values()) { + if (collided.hasComponent("win")) { + game.systems.menu.bringUpMenu(); + game.systems.menu.setState("levelSelect"); + } + } - found = false; - - const entitiesInCell = entitiesGrid[proposed.y][proposed.x]; + if (entity.hasComponent("momentum")) { + const momentum = unitize(entity.components.momentum); - for (let next of entitiesInCell.values()) { - if (next.hasComponent("stop")) { - wall = next; - found = false; + let found; + const proposed = {x: entity.components.gridPosition.x + momentum.dx, y: entity.components.gridPosition.y + momentum.dy}; + const entitiesToPush = []; + let wall = false; + do { + const proposedClampedInBounds = clamp(proposed, game.config.xDim-1, game.config.yDim-1); + if (!equivalence(proposed, proposedClampedInBounds)) { break; } - if (next.hasComponent("pushable")) { - entitiesToPush.push(next); - found = true; + + found = false; + + const entitiesInCell = entitiesGrid[proposed.y][proposed.x]; + + for (let next of entitiesInCell.values()) { + if (next.hasComponent("alive")) { + if (next.hasComponent("stop")) { + wall = next; + found = false; + break; + } + if (next.hasComponent("pushable")) { + entitiesToPush.push(next); + found = true; + } + } } - } - proposed.x += momentum.dx; - proposed.y += momentum.dy; - } while(found); - - if (wall) { - entity.removeComponent("momentum"); - } else { - entitiesToPush.map((e) => { - const pushedParticleSpawner = game.createBorderParticles(); - pushedParticleSpawner.addComponent(game.components.Position(e.components.position)); - pushedParticleSpawner.addComponent(game.components.Appearance({width: game.canvas.width / game.config.xDim, height: game.canvas.height / game.config.yDim})); - game.entities[pushedParticleSpawner.id] = pushedParticleSpawner; - - e.addComponent(game.components.Momentum({...momentum})); - }); + proposed.x += momentum.dx; + proposed.y += momentum.dy; + } while(found); + + if (wall) { + entity.removeComponent("momentum"); + } else { + entitiesToPush.map((e) => { + const pushedParticleSpawner = game.createBorderParticles({maxSpeed: 0.1, minAmount: 10, maxAmount: 15}); + pushedParticleSpawner.addComponent(game.components.Position(e.components.position)); + pushedParticleSpawner.addComponent(game.components.Appearance({width: game.canvas.width / game.config.xDim, height: game.canvas.height / game.config.yDim})); + game.entities[pushedParticleSpawner.id] = pushedParticleSpawner; + + e.addComponent(game.components.Momentum({...momentum})); + }); + } } } } diff --git a/src/systems/keyboardInput.js b/src/systems/keyboardInput.js index 06a73c9..1b399ca 100644 --- a/src/systems/keyboardInput.js +++ b/src/systems/keyboardInput.js @@ -12,7 +12,7 @@ game.system.KeyboardInput = () => { const update = (elapsedTime, entities, changedIds) => { for (let id in entities) { const entity = entities[id]; - if (entity.hasComponent('controllable')) { + if (entity.hasComponent('controllable') && entity.hasComponent('alive')) { const controls = entity.components.controllable.controls; if (!changedIds.has(entity.id)) { if (controls.includes('left') && keys[game.controls.left]) { diff --git a/src/systems/logic.js b/src/systems/logic.js index 8e3bbc0..207fbc8 100644 --- a/src/systems/logic.js +++ b/src/systems/logic.js @@ -1,6 +1,7 @@ game.system.Logic = (entitiesGrid) => { "use strict"; let currentVerbRules = []; + let previousControllableIds = new Set(); const isWord = (entity) => entity.hasComponent("gridPosition") && (entity.hasComponent("verb") || entity.hasComponent("noun")); const getFirstWordEntity = (gridPosition) => { @@ -19,12 +20,16 @@ game.system.Logic = (entitiesGrid) => { "stop": game.components.Stop(), "push": game.components.Pushable(), "you": game.components.Controllable({controls: ['left', 'right', 'up', 'down']}), - + "burn": game.components.Burn(), + "sink": game.components.Sink(), + "win": game.components.Win(), }; const nounsToEntityCreators = { "rock": game.createRock, "wall": game.createWall, + "bigblue": game.createBigBlue, + "flag": game.createFlag, }; const doOnRule = (rule, entities, direction) => { @@ -38,26 +43,42 @@ game.system.Logic = (entitiesGrid) => { currentVerbRules.push(rule); } for (let id in entities) { - if (entities[id].hasComponent("name") && entities[id].components.name.selector == entityName) { + const entity = entities[id]; + if (entity.hasComponent("alive") && entity.hasComponent("name") && entity.components.name.selector == entityName) { changedEntityIds.push(id); const component = verbActionsToComponent[verb]; if (component) { if (direction == "apply") { - entities[id].addComponent(component); + if (verb == "you") { + if (!previousControllableIds.has(id)) { + const newYouParticleSpawner = game.createBorderParticles({colors: ["#ffc0cb", "#ffb6c1", "#ffc1cc", "#ffbcd9", "#ff1493"], minAmount: 80, maxAmount: 150, maxSpeed: 0.5}); + newYouParticleSpawner.addComponent(game.components.Position(entity.components.position)); + newYouParticleSpawner.addComponent(game.components.Appearance({width: game.canvas.width / game.config.xDim, height: game.canvas.height / game.config.yDim})); + game.entities[newYouParticleSpawner.id] = newYouParticleSpawner; + } + } + entity.addComponent(component); } else if (direction == "deapply") { - entities[id].removeComponent(component.name); + if (entity.hasComponent("controllable")) { + previousControllableIds.add(id); + } + entity.removeComponent(component.name); } } } } + if (direction == "apply" && changedEntityIds.some((id) => previousControllableIds.has(id))) { + previousControllableIds = new Set(); + } } if (application.hasComponent("noun")) { const applicationEntityName = application.components.noun.select; for (let id in entities) { - if (entities[id].hasComponent("name") && entities[id].components.name.selector == entityName) { + const entity = entities[id]; + if (entity.hasComponent("name") && entity.components.name.selector == entityName) { const e = nounsToEntityCreators[applicationEntityName](); - entities[id].components.name = e.components.name; - entities[id].components.sprite = e.components.sprite; + entity.components.name = e.components.name; + entity.components.sprite = e.components.sprite; } } } diff --git a/src/systems/render.js b/src/systems/render.js index 8a3e633..e0db7e3 100644 --- a/src/systems/render.js +++ b/src/systems/render.js @@ -14,7 +14,7 @@ game.system.Render = (graphics) => { const drawSpec = {...entity.components.position, ...entity.components.appearance}; if (entity.hasComponent("sprite")) { game.sprites[entity.components.sprite.spriteName].draw(elapsedTime, drawSpec); - } else if (entity.hasComponent("particles")) { + } else if (entity.hasComponent("particles") && entity.particleSprite) { entity.particleSprite.draw(elapsedTime, drawSpec); } } |