DEADSOFTWARE

MainComponent in kotlin
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / world / GameWorld.java
index c038f1541d21822f90c122a75e30961dfee1fa40..b53e0a628e23f4e466bc5ea08492aecbe1c243eb 100644 (file)
@@ -1,9 +1,12 @@
 package ru.deadsoftware.cavedroid.game.world;
 
+import com.badlogic.gdx.math.MathUtils;
 import kotlin.Pair;
+import org.jetbrains.annotations.Nullable;
 import ru.deadsoftware.cavedroid.game.GameItemsHolder;
 import ru.deadsoftware.cavedroid.game.GameScope;
 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
+import ru.deadsoftware.cavedroid.game.mobs.Pig;
 import ru.deadsoftware.cavedroid.game.model.block.Block;
 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem;
 import ru.deadsoftware.cavedroid.game.model.item.Item;
@@ -14,7 +17,6 @@ import ru.deadsoftware.cavedroid.game.objects.container.Furnace;
 import ru.deadsoftware.cavedroid.game.objects.container.ContainerController;
 import ru.deadsoftware.cavedroid.misc.utils.MeasureUnitsUtilsKt;
 
-import javax.annotation.CheckForNull;
 import javax.inject.Inject;
 
 @GameScope
@@ -40,8 +42,8 @@ public class GameWorld {
                      MobsController mobsController,
                      GameItemsHolder gameItemsHolder,
                      ContainerController containerController,
-                     @CheckForNull Block[][] foreMap,
-                     @CheckForNull Block[][] backMap) {
+                     @Nullable Block[][] foreMap,
+                     @Nullable Block[][] backMap) {
         mDropController = dropController;
         mMobsController = mobsController;
         mGameItemsHolder = gameItemsHolder;
@@ -55,6 +57,7 @@ public class GameWorld {
             Pair<Block[][], Block[][]> maps = new GameWorldGenerator(mWorldConfig, mGameItemsHolder).generate();
             mForeMap = maps.getFirst();
             mBackMap = maps.getSecond();
+            spawnInitialMobs();
             mMobsController.getPlayer().respawn(this, mGameItemsHolder);
         } else {
             mForeMap = foreMap;
@@ -183,8 +186,12 @@ public class GameWorld {
         setMap(x, y, BACKGROUND_Z, block);
     }
 
+    public boolean canPlaceToForeground(int x, int y, Block value) {
+        return !hasForeAt(x, y) || value == mGameItemsHolder.getFallbackBlock() || !getForeMap(x, y).hasCollision();
+    }
+
     public boolean placeToForeground(int x, int y, Block value) {
-        if (!hasForeAt(x, y) || value == mGameItemsHolder.getFallbackBlock() || !getForeMap(x, y).hasCollision()) {
+        if (canPlaceToForeground(x, y, value)) {
             setForeMap(x, y, value);
             return true;
         } else if (value instanceof Block.Slab && isSameSlab(value, getForeMap(x, y))) {
@@ -219,10 +226,25 @@ public class GameWorld {
         return toolLevel >= block.getParams().getToolLevel();
     }
 
+    private void spawnInitialMobs() {
+        for (int x = 0; x < getWidth(); x++) {
+            int y = 0;
+            while (y < getWorldConfig().getSeaLevel()) {
+                if (getForeMap(x, y) == mGameItemsHolder.getBlock("grass")) {
+                    if (MathUtils.randomBoolean(.125f)) {
+                        mMobsController.addMob(new Pig(MeasureUnitsUtilsKt.getPx(x), MeasureUnitsUtilsKt.getPx(y)));
+                    }
+                    break;
+                }
+                y++;
+            }
+        }
+    }
+
     public void destroyForeMap(int x, int y) {
         Block block = getForeMap(x, y);
         if (block.isContainer()) {
-            mContainerController.destroyContainer(x, y, FOREGROUND_Z);
+            mContainerController.destroyContainer(x, y, FOREGROUND_Z, true);
         }
         if (block.hasDrop() && shouldDrop(block)) {
             for (int i = 0; i < block.getParams().getDropInfo().getCount(); i++) {
@@ -239,6 +261,9 @@ public class GameWorld {
 
     public void destroyBackMap(int x, int y) {
         Block block = getBackMap(x, y);
+        if (block.isContainer()) {
+            mContainerController.destroyContainer(x, y, BACKGROUND_Z, true);
+        }
         if (block.hasDrop() && shouldDrop(block)) {
             for (int i = 0; i < block.getParams().getDropInfo().getCount(); i++) {
                 mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop()));
@@ -248,24 +273,24 @@ public class GameWorld {
         placeToBackground(x, y, mGameItemsHolder.getFallbackBlock());
     }
 
-    @CheckForNull
+    @Nullable
     private Container getContainerAt(int x, int y, int z) {
         return mContainerController.getContainer(transformX(x), y, z);
     }
 
-    @CheckForNull
+    @Nullable
     public Container getForegroundContainer(int x, int y) {
         return getContainerAt(x, y, FOREGROUND_Z);
     }
 
-    @CheckForNull
+    @Nullable
     public Container getBackgroundContainer(int x, int y) {
         return getContainerAt(x, y, BACKGROUND_Z);
     }
 
-    @CheckForNull
+    @Nullable
     public Furnace getForegroundFurnace(int x, int y) {
-        @CheckForNull
+        @Nullable
         final Container container = getForegroundContainer(x, y);
 
         if (container instanceof Furnace) {
@@ -275,9 +300,9 @@ public class GameWorld {
         return null;
     }
 
-    @CheckForNull
+    @Nullable
     public Furnace getBackgroundFurnace(int x, int y) {
-        @CheckForNull
+        @Nullable
         final Container container = getBackgroundContainer(x, y);
 
         if (container instanceof Furnace) {