vue-shovel/src/level/index.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-04-22 23:00:10 +02:00
import SeedRng from 'seedrandom'
import FastSimplexNoise from 'fast-simplex-noise'
import {type as T, level as L} from './def'
import BlockGen from './first-iteration'
import BlockExt from './second-iteration'
2018-04-23 00:35:47 +02:00
import Player 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)
const noiseGen = new FastSimplexNoise({ random })
this._w = width
this._h = height
this._grid = new Array(this._h)
this.blockGen = new BlockGen(noiseGen)
this.blockExt = new BlockExt(noiseGen)
2018-04-23 00:35:47 +02:00
this.player = new Player(this._grid)
2018-04-22 23:00:10 +02:00
}
2018-04-23 00:35:47 +02:00
grid (x, y, px, py) {
this.generate(x, y, this._w, this._h, px, py)
2018-04-22 23:00:10 +02:00
return this._grid
}
2018-04-23 00:35:47 +02:00
generate (x, y, w, h, px, py) {
for (let i = 0; i < h; i++) {
const level = y + i
const column = x
const row = Array(w)
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)
2018-04-22 23:00:10 +02:00
this._grid[i] = row
}
2018-04-23 00:35:47 +02:00
this.player.setPosition(px, py)
2018-04-22 23:00:10 +02:00
}
}