diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-04 18:30:11 -0600 |
---|---|---|
committer | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-04 18:30:11 -0600 |
commit | dee568c51dbf2393aa7bd75f4241602af8022a2c (patch) | |
tree | 2e559ede69540b680a00ccf20bf96ff998230ed8 /src/systems | |
parent | 14ddb31441e35dce7425385948a9ee63b262cece (diff) | |
download | bbiy-dee568c51dbf2393aa7bd75f4241602af8022a2c.tar.gz bbiy-dee568c51dbf2393aa7bd75f4241602af8022a2c.zip |
Fix flickering issue by having singleton sprites; add loading priority; load levels from source
Diffstat (limited to 'src/systems')
-rw-r--r-- | src/systems/gridSystem.js | 16 | ||||
-rw-r--r-- | src/systems/render.js | 12 |
2 files changed, 18 insertions, 10 deletions
diff --git a/src/systems/gridSystem.js b/src/systems/gridSystem.js index b8d49e6..ef5a6e1 100644 --- a/src/systems/gridSystem.js +++ b/src/systems/gridSystem.js @@ -1,11 +1,11 @@ -game.system.GridSystem = ({ xDim, yDim, canvasWidth, canvasHeight }) => { +game.system.GridSystem = ({ xDim, yDim }) => { const entitiesGrid = Array(yDim).fill(null).map(() => Array(xDim).fill(null).map(() => new Map())); - const gridWidth = canvasWidth / xDim; - const gridHeight = canvasHeight / yDim; + let gridWidth = game.canvas.width / xDim; + let gridHeight = game.canvas.height / yDim; const gameCoordsToGrid = ({ x, y }) => { - return { x: Math.floor((x+gridWidth/2) / canvasWidth * xDim), y: Math.floor((y+gridHeight/2) / canvasHeight * yDim) }; + return { x: Math.floor((x+gridWidth/2) / game.canvas.width * xDim), y: Math.floor((y+gridHeight/2) / game.canvas.height * yDim) }; }; const gridCoordsToGame = ({ x, y }) => { @@ -39,8 +39,8 @@ game.system.GridSystem = ({ xDim, yDim, canvasWidth, canvasHeight }) => { rebuildGrid(gridEntities); gridEntities.map((entity) => { if (entity.hasComponent("appearance")) { - entity.components.appearance.width = canvasWidth / xDim; - entity.components.appearance.height = canvasHeight / yDim; + entity.components.appearance.width = gridWidth; + entity.components.appearance.height = gridHeight; } if (entity.hasComponent("position")) { const newGridCoords = gameCoordsToGrid(entity.components.position); @@ -89,7 +89,9 @@ game.system.GridSystem = ({ xDim, yDim, canvasWidth, canvasHeight }) => { entity.components.momentum.dy = 0; } } - } + } else { + entity.addComponent(game.components.Position({...gridCoordsToGame(entity.components.gridPosition)})); + }; }); }; diff --git a/src/systems/render.js b/src/systems/render.js index 0a4c72d..4ec67c7 100644 --- a/src/systems/render.js +++ b/src/systems/render.js @@ -2,12 +2,18 @@ game.system.Render = (graphics) => { const update = (elapsedTime, entities) => { graphics.clear(); - for (let id in entities) { - const entity = entities[id]; + const entitiesArray = Object.keys(entities).map(key => entities[key]); + const sortedEntities = entitiesArray.sort((a, b) => { + const aprior = a.hasComponent("loadPriority") ? a.components.loadPriority.priority : 0; + const bprior = b.hasComponent("loadPriority") ? b.components.loadPriority.priority : 0; + return bprior - aprior; + }); + + sortedEntities.forEach((entity) => { if (entity.sprite && entity.hasComponent("position") && entity.hasComponent("appearance")) { entity.sprite.draw(elapsedTime, {...entity.components.position, ...entity.components.appearance}); } - } + }); } return { update }; };
\ No newline at end of file |