SHARE
TWEET

Untitled

a guest Jul 24th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = class ProceduralGenerator {
  2.  
  3.     constructor () {
  4.  
  5.         this.allCells = []
  6.         this.freshCells = []
  7.  
  8.         return this
  9.     }
  10.  
  11.     setTile (setTileFunction) { this.sendSetTile = setTileFunction }
  12.     tileSpawnRules (rule) { this.canSpawnCell = rule }
  13.     randBetween (min, max) { return parseInt(Math.random() * (max - min) + min) }
  14.  
  15.     getCellCount () {
  16.         // console.log(this.allCells)
  17.         return this.allCells.length
  18.     }
  19.  
  20.     addCell (x, y, z) {
  21.         this.sendSetTile(x, y, z)
  22.         this.allCells.push({x, y, z})
  23.         this.freshCells.push({x, y, z})
  24.     }
  25.  
  26.     createRootCell (x, y, z) {
  27.         this.addCell(x, y, z)
  28.     }
  29.  
  30.     multiplicateCell (x, y, z, distance, options) {
  31.         if (options == undefined) {
  32.             options = [
  33.                 {x: x - distance, y: y - distance},
  34.                 {x: x + distance, y: y + distance},
  35.                 {x: x - distance, y: y + distance},
  36.                 {x: x + distance, y: y - distance}
  37.             ]
  38.         }
  39.  
  40.         var randomIndex = this.randBetween(0, options.length)
  41.         var pos = options[randomIndex]
  42.         var posX = Math.floor(pos.x)
  43.         var posY = Math.floor(pos.y)
  44.  
  45.         if (this.canSpawnCell(posX, posY, z)){
  46.             this.addCell(posX, posY, z)
  47.         } else if(options.length > 1) {
  48.             options.splice(randomIndex, 1)
  49.             this.multiplicateCell(x, y, z, distance, options)
  50.         }
  51.     }
  52.  
  53.     multiplicateCells (distance, maxCellCount) {
  54.  
  55.         for(var i = 0; i < this.freshCells.length; i++){
  56.             let cell = this.freshCells[i]
  57.  
  58.             if (this.getCellCount() >= maxCellCount) break;
  59.             this.multiplicateCell(cell.x, cell.y, cell.z, distance)
  60.             if (this.getCellCount() >= maxCellCount) break;
  61.             this.multiplicateCell(cell.x, cell.y, cell.z, distance)
  62.             this.freshCells.splice(i, 1)
  63.          }
  64.  
  65.         if (this.getCellCount() < maxCellCount) {
  66.             this.multiplicateCells(distance, maxCellCount)
  67.         }
  68.     }
  69.  
  70.     inflateCell (x, y, z, inflateSize) {
  71.         var halfInflate = inflateSize/2
  72.         // print(halfInflate*(-1))
  73.         for (var addY = halfInflate*(-1); addY <= halfInflate; addY++) {
  74.             for (var addX = halfInflate*(-1); addX <= halfInflate; addX++) {
  75.  
  76.                 // console.log(addX, addY)
  77.                 // print(addX, addY)
  78.                 var posX = Math.floor(x+addX)
  79.                 var posY = Math.floor(y+addY)
  80.  
  81.                 if (this.canSpawnCell(posX, posY, z)) {
  82.                     this.sendSetTile(posX, posY, z)
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     inflateCells (inflateSize) {
  89.         for (var i = 0; i < this.allCells.length; i++) {
  90.        
  91.             let cell = this.allCells[i]
  92.             this.inflateCell(cell.x, cell.y, cell.z, inflateSize)
  93.             // if (i == 50)  return;
  94.         }
  95.     }
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top