store changes more efficiently

main
Norman Köhring 1 year ago
parent b2a4d2e547
commit 3a9d2044e1

@ -32,27 +32,27 @@ export default function createLevel(width: number, height: number, seed = 'extre
// stores the limit to where light still shines, // stores the limit to where light still shines,
// for each column currently visible on the screen // for each column currently visible on the screen
const _lightBarrier: number[] = [...new Array(width)].map(() => MAX_LIGHT) const _lightBarrier: number[] = [...new Array(width)].map(() => MAX_LIGHT)
const _changes: Change[] = [] const _changes: Change[][] = []
const blockGen = createBlockGenerator(rand) const blockGen = createBlockGenerator(rand)
const blockExt = createBlockExtender(rand) const blockExt = createBlockExtender(rand)
// Apply changes, coming from the player (tocktocktock-plopp!) // Apply changes, coming from the player (tocktocktock-plopp!)
function change(change: Change) { function change(change: Change) {
_changes.push(change) const x = change.x
console.log(_changes) if (!_changes[x]) _changes[x] = [change]
else _changes[x].push(change)
} }
function applyPlayerChanges(columnOffset: number, levelOffset: number) { function applyPlayerChanges(columnOffset: number, levelOffset: number) {
for (const change of _changes) { for (let col = columnOffset; col < columnOffset + width; col++) {
// ignore all changes outside of the current view const changes = _changes[col]
if (change.x < columnOffset || change.x > columnOffset + width) continue if (changes) {
if (change.y < levelOffset || change.y > levelOffset + height) continue const maxLevel = levelOffset + height
changes.forEach(c => {
if (change.type === 'exchange') { if (c.type !== 'exchange' || c.y < levelOffset || c.y > maxLevel) return
_grid[change.y - levelOffset][change.x - columnOffset] = blockTypes[change.newType] _grid[c.y - levelOffset][c.x - columnOffset] = blockTypes[c.newType]
} else if (change.type === 'damage') { })
console.warn('damaging blocks not yet supported')
} }
} }
} }
@ -68,10 +68,11 @@ export default function createLevel(width: number, height: number, seed = 'extre
let block = blockGen.generateBlock(level, col + columnOffset) let block = blockGen.generateBlock(level, col + columnOffset)
block = blockExt.extendBlock(level, col, columnOffset, block, previousBlock) block = blockExt.extendBlock(level, col, columnOffset, block, previousBlock)
const change = _changes.find(change => { const changes = _changes[columnOffset + col]
return change.x === columnOffset + col && change.y === level if (changes) {
}) const change = changes.find(c => c.y === level)
if (change && change.type === 'exchange') block = blockTypes[change.newType] if (change && change.type === 'exchange') block = blockTypes[change.newType]
}
previousBlock = block previousBlock = block

Loading…
Cancel
Save