DEADSOFTWARE

Store block references intead of ids
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / world / GameWorldGenerator.kt
index 8776254f7dca8d6a1d3b3212aff95335bdb22733..b984c4dbacfd0e25002201701f8d332c0df7fcac 100644 (file)
@@ -1,6 +1,7 @@
 package ru.deadsoftware.cavedroid.game.world
 
-import ru.deadsoftware.cavedroid.game.GameItems
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
+import ru.deadsoftware.cavedroid.game.model.block.Block
 import ru.deadsoftware.cavedroid.game.model.world.Biome
 import ru.deadsoftware.cavedroid.game.model.world.generator.WorldGeneratorConfig
 import kotlin.math.abs
@@ -9,12 +10,13 @@ import kotlin.random.Random
 
 class GameWorldGenerator(
     private val config: WorldGeneratorConfig,
+    private val gameItemsHolder: GameItemsHolder,
 ) {
 
     private val random = Random(config.seed)
 
-    private val foreMap by lazy { Array(config.width) { IntArray(config.height) } }
-    private val backMap by lazy { Array(config.width) { IntArray(config.height) } }
+    private val foreMap by lazy { Array(config.width) { Array(config.height) { gameItemsHolder.fallbackBlock } } }
+    private val backMap by lazy { Array(config.width) { Array(config.height) { gameItemsHolder.fallbackBlock } } }
 
     private val heights by lazy { generateHeights() }
     private val biomesMap by lazy { generateBiomes() }
@@ -62,20 +64,20 @@ class GameWorldGenerator(
 
         val surfaceHeight = heights[x]
 
-        val grassId = GameItems.getBlockId("grass")
-        val bedrockId = GameItems.getBlockId("bedrock")
-        val dirtId = GameItems.getBlockId("dirt")
-        val stoneId = GameItems.getBlockId("stone")
+        val grass = gameItemsHolder.getBlock("grass")
+        val bedrock = gameItemsHolder.getBlock("bedrock")
+        val dirt = gameItemsHolder.getBlock("dirt")
+        val stone = gameItemsHolder.getBlock("stone")
 
-        foreMap[x][surfaceHeight] = grassId
-        foreMap[x][config.height - 1] = bedrockId
-        backMap[x][surfaceHeight] = grassId
-        backMap[x][config.height - 1] = bedrockId
+        foreMap[x][surfaceHeight] = grass
+        foreMap[x][config.height - 1] = bedrock
+        backMap[x][surfaceHeight] = grass
+        backMap[x][config.height - 1] = bedrock
 
         for (y in surfaceHeight + 1 ..< config.height - 1) {
             foreMap[x][y] = when {
-                y < surfaceHeight + random.nextInt(5, 8) -> dirtId
-                else -> stoneId
+                y < surfaceHeight + random.nextInt(5, 8) -> dirt
+                else -> stone
             }
             backMap[x][y] = foreMap[x][y]
         }
@@ -86,22 +88,22 @@ class GameWorldGenerator(
 
         val surfaceHeight = heights[x]
 
-        val sandId = GameItems.getBlockId("sand")
-        val bedrockId = GameItems.getBlockId("bedrock")
-        val sandstoneId = GameItems.getBlockId("sandstone")
-        val stoneId = GameItems.getBlockId("stone")
+        val sand = gameItemsHolder.getBlock("sand")
+        val bedrock = gameItemsHolder.getBlock("bedrock")
+        val sandstone = gameItemsHolder.getBlock("sandstone")
+        val stone = gameItemsHolder.getBlock("stone")
 
 
-        foreMap[x][surfaceHeight] = sandId
-        foreMap[x][config.height - 1] = bedrockId
-        backMap[x][surfaceHeight] = sandId
-        backMap[x][config.height - 1] = bedrockId
+        foreMap[x][surfaceHeight] = sand
+        foreMap[x][config.height - 1] = bedrock
+        backMap[x][surfaceHeight] = sand
+        backMap[x][config.height - 1] = bedrock
 
         for (y in surfaceHeight + 1 ..< config.height - 1) {
             foreMap[x][y] = when {
-                y < surfaceHeight + random.nextInt(5, 8) -> sandId
-                y < surfaceHeight + random.nextInt(0, 2) -> sandstoneId
-                else -> stoneId
+                y < surfaceHeight + random.nextInt(5, 8) -> sand
+                y < surfaceHeight + random.nextInt(0, 2) -> sandstone
+                else -> stone
             }
             backMap[x][y] = foreMap[x][y]
         }
@@ -112,37 +114,36 @@ class GameWorldGenerator(
     }
 
     private fun fillWater() {
-        val waterId = GameItems.getBlockId("water")
+        val water = gameItemsHolder.getBlock("water")
 
         for (x in 0 ..< config.width) {
             for (y in config.seaLevel ..< config.height) {
-                if (foreMap[x][y] != 0) {
+                if (foreMap[x][y] != gameItemsHolder.fallbackBlock) {
                     break
                 }
 
-                foreMap[x][y] = waterId
+                foreMap[x][y] = water
             }
         }
     }
 
     private fun generateCactus(x: Int) {
-        val cactusId = GameItems.getBlockId("cactus")
-        val cactusHeight = random.nextInt(5)
+        val cactus = gameItemsHolder.getBlock("cactus")
+        val cactusHeight = random.nextInt(3)
         val h = heights[x] - 1
 
         for (y in h downTo max(0, h - cactusHeight)) {
-            foreMap[x][y] = cactusId
+            foreMap[x][y] = cactus
         }
     }
 
     /**
      * Generate world
      */
-    fun generate(): Pair<Array<IntArray>, Array<IntArray>> {
+    fun generate(): Pair<Array<Array<Block>>, Array<Array<Block>>> {
         var biome = Biome.PLAINS
 
         for (x in 0 until config.width) {
-            val xHeight = heights[x]
             biome = biomesMap[x] ?: biome
 
             when (biome) {