DEADSOFTWARE

Refactor world generator
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GamePhysics.java
index cb5b64c844e1ca85886ea58c6269756d669f4f2c..e3ceb531cb3a37c4ac9aec5adf5b6c83152dc838 100644 (file)
@@ -10,6 +10,7 @@ import ru.deadsoftware.cavedroid.game.mobs.MobsController;
 import ru.deadsoftware.cavedroid.game.mobs.Player;
 import ru.deadsoftware.cavedroid.game.objects.Drop;
 import ru.deadsoftware.cavedroid.game.objects.DropController;
+import ru.deadsoftware.cavedroid.game.world.GameWorld;
 
 import javax.inject.Inject;
 import java.util.Iterator;
@@ -18,9 +19,11 @@ import java.util.Iterator;
 @GameScope
 public class GamePhysics {
 
-    public static final int PL_SPEED = 120;
+    public static final float PL_SPEED = 69.072f;
+    public static final float PL_JUMP_VELOCITY = -133.332f;
+    public static final float PL_TERMINAL_VELOCITY = 1254.4f;
 
-    private final Vector2 gravity = new Vector2(0, .09f);
+    private final Vector2 gravity = new Vector2(0, 444.44f);
 
     private final GameWorld mGameWorld;
     private final MainConfig mMainConfig;
@@ -93,18 +96,19 @@ public class GamePhysics {
 
     private void dropPhy(Drop drop, float delta) {
         int dropToPlayer = drop.closeToPlayer(mGameWorld, mMobsController.getPlayer());
+
         if (dropToPlayer > 0) {
-            drop.moveToPlayer(mGameWorld, mMobsController.getPlayer(), dropToPlayer);
+            drop.moveToPlayer(mGameWorld, mMobsController.getPlayer(), dropToPlayer, delta);
         } else {
-            if (drop.getVelocity().x >= .5f) {
-                drop.getVelocity().x -= .5f;
-            } else if (drop.getVelocity().x <= -.5f) {
-                drop.getVelocity().x += .5f;
+            if (drop.getVelocity().x >= 300f) {
+                drop.getVelocity().x = 300f;
+            } else if (drop.getVelocity().x <= -300f) {
+                drop.getVelocity().x = -300f;
             } else {
                 drop.getVelocity().x = 0;
             }
-            if (drop.getVelocity().y < 9) {
-                drop.getVelocity().y += gravity.y / 4;
+            if (drop.getVelocity().y < PL_TERMINAL_VELOCITY) {
+                drop.getVelocity().y += gravity.y * delta;
             }
         }
         drop.move(delta);
@@ -113,7 +117,7 @@ public class GamePhysics {
         if (checkColl(drop)) {
             drop.getVelocity().set(0, -1);
             do {
-                drop.move(delta);
+                drop.move(1);
             } while (checkColl(drop));
             drop.getVelocity().setZero();
         }
@@ -174,6 +178,14 @@ public class GamePhysics {
 
             mob.getVelocity().y = 0;
 
+
+
+            //todo fall damage
+            // h = (v^2) / 2g
+            // dmg = max(0, (h - 48) / 32) - half of blocks fallen starting from 3 blocks height
+            //                int dmg = ((int)Math.max(0f, (((mob.getVelocity().y * mob.getVelocity().y) / (2 * gravity.y)) - 48f) / 16f));
+            //                if (dmg > 0) System.out.println("Damage: " + dmg);
+
         } else {
             mob.y += 1;
             mob.setCanJump(checkColl(mob));
@@ -195,21 +207,21 @@ public class GamePhysics {
                 player.swim = true;
             }
             if (!player.swim) {
-                if (!player.isFlyMode() && player.getVelocity().y < 270f) {
-                    player.getVelocity().x += gravity.y / 4;
+                if (!player.isFlyMode() && player.getVelocity().y < 32f) {
+                    player.getVelocity().y += gravity.y * delta;
                 }
-                if (!player.isFlyMode() && player.getVelocity().y > 270f) {
-                    player.getVelocity().add(0, -60f);
+                if (!player.isFlyMode() && player.getVelocity().y > 32f) {
+                    player.getVelocity().y -= player.getVelocity().y * 32f * delta;
                 }
             } else {
-                player.getVelocity().add(0, -30f);
-                if (player.getVelocity().y < -180) {
-                    player.getVelocity().y = -180;
+                player.getVelocity().y += PL_JUMP_VELOCITY * delta;
+                if (player.getVelocity().y < -PL_SPEED) {
+                    player.getVelocity().y = -PL_SPEED;
                 }
             }
         } else {
-            if (!player.isFlyMode() && player.getVelocity().y < 1080) {
-                player.getVelocity().add(gravity);
+            if (!player.isFlyMode() && player.getVelocity().y < PL_TERMINAL_VELOCITY) {
+                player.getVelocity().y += gravity.y * delta;
             }
         }
 
@@ -222,24 +234,24 @@ public class GamePhysics {
         mobXColl(player);
 
         if (mMainConfig.isTouch() && !player.isFlyMode() && player.canJump() && player.getVelocity().x != 0 && checkJump(player)) {
-            player.getVelocity().add(0, -480);
+            player.getVelocity().y = PL_JUMP_VELOCITY;
             player.setCanJump(false);
         }
     }
 
     private void mobPhy(Mob mob, float delta) {
         if (mob.getType() == Mob.Type.MOB && GameItems.isFluid(getBlock(mob))) {
-            if (mob.getVelocity().y > 540) {
-                mob.getVelocity().add(0, -5.4f);
+            if (mob.getVelocity().y > 32f) {
+                mob.getVelocity().y -= mob.getVelocity().y * 32f * delta;
             }
 
-            mob.getVelocity().add(0, -30f);
+            mob.getVelocity().y += PL_JUMP_VELOCITY * delta;
 
-            if (mob.getVelocity().y < -180) {
-                mob.getVelocity().y = -180;
+            if (mob.getVelocity().y < -PL_SPEED) {
+                mob.getVelocity().y = -PL_SPEED;
             }
-        } else if (!mob.isFlyMode() && mob.getVelocity().y < 1080) {
-            mob.getVelocity().add(gravity);
+        } else if (!mob.isFlyMode() && mob.getVelocity().y < PL_TERMINAL_VELOCITY) {
+            mob.getVelocity().y += gravity.y * delta;
         }
 
         mob.y += mob.getVelocity().y * delta;
@@ -253,7 +265,7 @@ public class GamePhysics {
         mobXColl(mob);
 
         if (mob.canJump() && mob.getVelocity().x != 0 && checkJump(mob)) {
-            mob.getVelocity().add(0, -480);
+            mob.getVelocity().y = PL_JUMP_VELOCITY;
             mob.setCanJump(false);
         }
     }