DEADSOFTWARE

Even better collision
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GamePhysics.java
index 1ed455b75ce04cd450e7c6c5b407dfe31b205b8b..8b79fe0116273791655bac30dc245e82c962371e 100644 (file)
@@ -1,8 +1,8 @@
 package ru.deadsoftware.cavecraft.game;
 
-import com.badlogic.gdx.Gdx;
-import com.badlogic.gdx.math.Rectangle;
-import com.badlogic.gdx.math.Vector2;
+import com.badlogic.gdx.math.*;
+import ru.deadsoftware.cavecraft.Items;
+import ru.deadsoftware.cavecraft.game.mobs.Mob;
 import ru.deadsoftware.cavecraft.game.objects.Player;
 
 public class GamePhysics {
@@ -15,63 +15,127 @@ public class GamePhysics {
 
     public GamePhysics(GameProc gameProc) {
         this.gameProc = gameProc;
-        gravity = new Vector2(0,2);
+        gravity = new Vector2(0,1);
+    }
+
+    private boolean checkJump(Rectangle rect, int dir) {
+        int bl = 0;
+        switch (dir) {
+            case 0:
+                if ((int)((rect.x+(rect.width/2))/16) - 1>=0)
+                    bl = gameProc.world.getForeMap(
+                        (int)((rect.x+(rect.width/2))/16) - 1,
+                        (int)(rect.y/16)+1);
+                break;
+            case 1:
+                if ((int)((rect.x+(rect.width/2))/16) + 1<gameProc.world.getWidth())
+                    bl = gameProc.world.getForeMap(
+                        (int)((rect.x+(rect.width/2))/16) + 1,
+                        (int)(rect.y/16)+1);
+                break;
+            default:
+                bl=0;
+        }
+        return (bl>0 && Items.BLOCKS.getValueAt(bl).collision);
     }
 
     private boolean checkColl(Rectangle rect) {
-        int[] bl = new int [6];
-        bl[0] = gameProc.world.getForeMap(((int)rect.x/16), ((int)rect.y/16));
-        bl[1] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)rect.y/16));
-        bl[2] = gameProc.world.getForeMap(((int)rect.x/16), ((int)(rect.y+rect.height/2)/16));
-        bl[3] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+rect.height/2)/16));
-        bl[4] = gameProc.world.getForeMap(((int)rect.x/16), ((int)(rect.y+rect.height-1)/16));
-        bl[5] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+(rect.height-1))/16));
-        for (int b: bl) if (b>0) {
-            return true;
+        int bl;
+        int minX = (int) ((rect.x+rect.width/2)/16)-4;
+        int minY = (int) ((rect.y+rect.height/2)/16)-4;
+        int maxX = (int) ((rect.x+rect.width/2)/16)+4;
+        int maxY = (int) ((rect.y+rect.height/2)/16)+4;
+        if (minX<0) minX=0;
+        if (minY<0) minY=0;
+        if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth();
+        if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
+        for (int y=minY; y<maxY; y++) {
+            for (int x=minX; x<maxX; x++) {
+                bl = gameProc.world.getForeMap(x,y);
+                if (bl>0 && Items.BLOCKS.getValueAt(bl).collision){
+                    if (Intersector.overlaps(rect, Items.BLOCKS.getValueAt(bl).getRect(x,y))){
+                        return true;
+                    }
+                }
+            }
         }
         return false;
     }
 
-    private void movePlayer(Player pl) {
-        pl.position.add(pl.moveX);
-        if (checkColl(pl.getRect())) {
-            int d = 0;
-            if (pl.moveX.x<0) d=1; else if (pl.moveX.x>0) d=-1;
-            while (checkColl(pl.getRect())) pl.position.x+=d;
-            //pl.moveX.setZero();
-        }
+    private void playerPhy(Player pl) {
         pl.position.add(pl.moveY);
         if (checkColl(pl.getRect())) {
-            int d = 0;
+            pl.flyMode = false;
+            pl.canJump = true;
+            int d = -1;
             if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
+            pl.position.y = MathUtils.round(pl.position.y);
             while (checkColl(pl.getRect())) pl.position.y+=d;
             pl.moveY.setZero();
+        } else {
+            pl.canJump = false;
         }
-        pl.moveY.add(gravity);
-        switch (pl.dir) {
-            case 0:
-                gameProc.renderer.camTargetPos.x = pl.position.x-gameProc.renderer.camera.viewportWidth+100;
-                break;
-            case 1:
-                gameProc.renderer.camTargetPos.x = pl.position.x-100;
-                break;
+        if (!pl.flyMode) pl.moveY.add(gravity);
+        pl.position.add(pl.moveX);
+        if (pl.position.x<0 ||
+                pl.position.x+pl.texWidth>=gameProc.world.getWidth()*16)
+            pl.position.sub(pl.moveX);
+        if (checkColl(pl.getRect())) {
+            if (pl.canJump && !pl.flyMode) pl.position.y-=8;
+            if (checkColl(pl.getRect())) {
+                if (pl.canJump && !pl.flyMode) pl.position.y+=8;
+                int d = 0;
+                if (pl.moveX.x < 0) d = 1;
+                else if (pl.moveX.x > 0) d = -1;
+                pl.position.x = MathUtils.round(pl.position.x);
+                while (checkColl(pl.getRect())) pl.position.x += d;
+            }
         }
+
+        /*if (checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
+            pl.moveY.add(0, -8);
+            pl.canJump = false;
+        }*/
     }
 
-    public void update(float delta) {
-        movePlayer(gameProc.player);
-        if (gameProc.renderer.camera.position.x - gameProc.renderer.camTargetPos.x <= 8 &&
-                gameProc.renderer.camera.position.x - gameProc.renderer.camTargetPos.x >= -8) {
-            gameProc.renderer.camera.position.x = gameProc.renderer.camTargetPos.x;
+    private void mobPhy(Mob mob) {
+        mob.position.add(mob.moveY);
+        if (checkColl(mob.getRect())) {
+            mob.canJump = true;
+            int d = -1;
+            if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
+            mob.position.y = MathUtils.round(mob.position.y);
+            while (checkColl(mob.getRect())) mob.position.y+=d;
+            mob.moveY.setZero();
+        } else {
+            mob.canJump = false;
         }
-        if (gameProc.renderer.camera.position.x > gameProc.renderer.camTargetPos.x) {
-            gameProc.renderer.camera.position.sub(16,0,0);
+        mob.moveY.add(gravity);
+        mob.position.add(mob.moveX);
+        if (mob.position.x<32 ||
+                mob.position.x+mob.width>gameProc.world.getWidth()*16-32)
+            mob.position.sub(mob.moveX);
+        if (checkColl(mob.getRect())) {
+            int d = 0;
+            if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
+            mob.position.x = MathUtils.round(mob.position.x);
+            while (checkColl(mob.getRect())) mob.position.x+=d;
         }
-        if (gameProc.renderer.camera.position.x < gameProc.renderer.camTargetPos.x) {
-            gameProc.renderer.camera.position.add(16,0,0);
+}
+
+    public void update(float delta) {
+
+        for (Mob mob : gameProc.mobs) {
+            mob.ai();
+            mobPhy(mob);
         }
-        gameProc.renderer.camera.position.y = gameProc.player.position.y+gameProc.player.height/2
-                -gameProc.renderer.camera.viewportHeight/2;
+        playerPhy(gameProc.player);
+
+        gameProc.renderer.camera.position.set(
+                gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
+                gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
+                0
+        );
     }
 
 }