vue-shovel/src/level/index.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-04-22 23:00:10 +02:00
import SeedRng from 'seedrandom'
2019-06-11 17:14:43 +02:00
import SimplexNoise from 'open-simplex-noise'
2018-04-22 23:00:10 +02:00
import {type as T, level as L} from './def'
import BlockGen from './first-iteration'
import BlockExt from './second-iteration'
2019-06-08 23:42:08 +02:00
import PlayerChanges from './third-iteration'
2018-04-22 23:00:10 +02:00
export default class Level {
constructor (width, height, seed = 'super random seed') {
const random = SeedRng(seed)
2019-06-11 17:14:43 +02:00
const noiseGen = new SimplexNoise(parseInt(seed, 32))
2018-04-22 23:00:10 +02:00
this._w = width
this._h = height
this._grid = new Array(this._h)
this.blockGen = new BlockGen(noiseGen)
this.blockExt = new BlockExt(noiseGen)
2019-06-08 23:42:08 +02:00
this.playerChanges = new PlayerChanges()
}
change (level, column, newBlock) {
if (newBlock.hp <= 0) {
newBlock = level > L.rock ? { ...T.cave } : { ...T.air }
}
this.playerChanges.apply(level, column, newBlock)
2018-04-22 23:00:10 +02:00
}
grid (x, y) {
this.generate(x, y, this._w, this._h)
2018-04-22 23:00:10 +02:00
return this._grid
}
2019-06-08 23:42:08 +02:00
generate (column, y, width, height) {
for (let i = 0; i < height; i++) {
2018-04-23 00:35:47 +02:00
const level = y + i
2019-06-08 23:42:08 +02:00
const row = Array(width)
2018-04-22 23:00:10 +02:00
const previousRow = this._grid[i - 1] || Array()
2018-04-23 00:35:47 +02:00
this.blockGen.level(level, column, row, previousRow)
this.blockExt.level(level, column, row, previousRow)
2019-06-08 23:42:08 +02:00
this.playerChanges.level(level, column, row)
2018-04-22 23:00:10 +02:00
this._grid[i] = row
}
}
}