Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = class ProceduralGenerator {
- constructor () {
- this.allCells = []
- this.freshCells = []
- return this
- }
- setTile (setTileFunction) { this.sendSetTile = setTileFunction }
- tileSpawnRules (rule) { this.canSpawnCell = rule }
- randBetween (min, max) { return parseInt(Math.random() * (max - min) + min) }
- getCellCount () {
- // console.log(this.allCells)
- return this.allCells.length
- }
- addCell (x, y, z) {
- this.sendSetTile(x, y, z)
- this.allCells.push({x, y, z})
- this.freshCells.push({x, y, z})
- }
- createRootCell (x, y, z) {
- this.addCell(x, y, z)
- }
- multiplicateCell (x, y, z, distance, options) {
- if (options == undefined) {
- options = [
- {x: x - distance, y: y - distance},
- {x: x + distance, y: y + distance},
- {x: x - distance, y: y + distance},
- {x: x + distance, y: y - distance}
- ]
- }
- var randomIndex = this.randBetween(0, options.length)
- var pos = options[randomIndex]
- var posX = Math.floor(pos.x)
- var posY = Math.floor(pos.y)
- if (this.canSpawnCell(posX, posY, z)){
- this.addCell(posX, posY, z)
- } else if(options.length > 1) {
- options.splice(randomIndex, 1)
- this.multiplicateCell(x, y, z, distance, options)
- }
- }
- multiplicateCells (distance, maxCellCount) {
- for(var i = 0; i < this.freshCells.length; i++){
- let cell = this.freshCells[i]
- if (this.getCellCount() >= maxCellCount) break;
- this.multiplicateCell(cell.x, cell.y, cell.z, distance)
- if (this.getCellCount() >= maxCellCount) break;
- this.multiplicateCell(cell.x, cell.y, cell.z, distance)
- this.freshCells.splice(i, 1)
- }
- if (this.getCellCount() < maxCellCount) {
- this.multiplicateCells(distance, maxCellCount)
- }
- }
- inflateCell (x, y, z, inflateSize) {
- var halfInflate = inflateSize/2
- // print(halfInflate*(-1))
- for (var addY = halfInflate*(-1); addY <= halfInflate; addY++) {
- for (var addX = halfInflate*(-1); addX <= halfInflate; addX++) {
- // console.log(addX, addY)
- // print(addX, addY)
- var posX = Math.floor(x+addX)
- var posY = Math.floor(y+addY)
- if (this.canSpawnCell(posX, posY, z)) {
- this.sendSetTile(posX, posY, z)
- }
- }
- }
- }
- inflateCells (inflateSize) {
- for (var i = 0; i < this.allCells.length; i++) {
- let cell = this.allCells[i]
- this.inflateCell(cell.x, cell.y, cell.z, inflateSize)
- // if (i == 50) return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement