DEADSOFTWARE

Refactor world generator
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / world / GameWorld.java
index 18afcd4b3eec1c725a4675c5b8b429c317256da4..b5ac7eef895e2c280578da80c1d133cd8e036705 100644 (file)
@@ -8,11 +8,14 @@ import ru.deadsoftware.cavedroid.game.GameScope;
 import ru.deadsoftware.cavedroid.game.mobs.FallingGravel;
 import ru.deadsoftware.cavedroid.game.mobs.FallingSand;
 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
+import ru.deadsoftware.cavedroid.game.model.world.generator.WorldGeneratorConfig;
 import ru.deadsoftware.cavedroid.game.objects.Block;
 import ru.deadsoftware.cavedroid.game.objects.DropController;
 
 import javax.annotation.CheckForNull;
 import javax.inject.Inject;
+import java.sql.Time;
+import java.util.Timer;
 
 @GameScope
 public class GameWorld implements Disposable {
@@ -23,6 +26,8 @@ public class GameWorld implements Disposable {
 
     private final DropController mDropController;
     private final MobsController mMobsController;
+
+    private final Timer mGameFluidsTimer;
     private final GameFluidsThread mGameFluidsThread;
 
     private final int mWidth;
@@ -47,7 +52,7 @@ public class GameWorld implements Disposable {
         if (isNewGame) {
             mWidth = DEFAULT_WIDTH;
             mHeight = DEFAULT_HEIGHT;
-            Pair<int[][], int[][]> maps = GameWorldGenerator.INSTANCE.generate(mWidth, mHeight, TimeUtils.millis());
+            Pair<int[][], int[][]> maps = new GameWorldGenerator(WorldGeneratorConfig.Companion.getDefaultWithSeed(TimeUtils.millis())).generate();
             mForeMap = maps.getFirst();
             mBackMap = maps.getSecond();
             mMobsController.getPlayer().respawn(this);
@@ -59,6 +64,9 @@ public class GameWorld implements Disposable {
         }
 
         mGameFluidsThread = new GameFluidsThread(this, mMobsController, Thread.currentThread());
+
+        mGameFluidsTimer = new Timer();
+        mGameFluidsTimer.scheduleAtFixedRate(mGameFluidsThread, 0, GameFluidsThread.FLUID_UPDATE_INTERVAL_MS);
     }
 
     public int getWidth() {
@@ -210,7 +218,7 @@ public class GameWorld implements Disposable {
                 setForeMap(x, y, 0);
                 mMobsController.addMob(FallingSand.class, x * 16, y * 16);
                 updateBlock(x, y - 1);
-            }   
+            }
         }
 
         if (getForeMap(x, y) == 11) {
@@ -245,18 +253,10 @@ public class GameWorld implements Disposable {
             }
             mShouldUpdate = false;
         }
-
-        if (!mGameFluidsThread.isAlive()) {
-            mGameFluidsThread.start();
-        }
-    }
-
-    public void startFluidsThread() {
-        mGameFluidsThread.start();
     }
 
     @Override
     public void dispose() {
-        mGameFluidsThread.interrupt();
+        mGameFluidsTimer.cancel();
     }
 }
\ No newline at end of file