DEADSOFTWARE

Add player and physics
authorfred-boy <fred-boy@protonmail.com>
Thu, 5 Apr 2018 16:43:42 +0000 (23:43 +0700)
committerfred-boy <fred-boy@protonmail.com>
Thu, 5 Apr 2018 16:43:42 +0000 (23:43 +0700)
core/src/ru/deadsoftware/cavecraft/GameScreen.java
core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java
core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavecraft/game/GameProc.java
core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
core/src/ru/deadsoftware/cavecraft/game/WorldGen.java
core/src/ru/deadsoftware/cavecraft/game/objects/Player.java [new file with mode: 0644]

index 299dfa15ebefa8e54453e710422f7ff89f221352..451f2a2e241520d25e193263083e783b00fb6a86 100644 (file)
@@ -40,7 +40,7 @@ public class GameScreen implements Screen {
 
     @Override
     public void resize(int width, int height) {
-
+        gameProc.resetRenderer();
     }
 
     @Override
@@ -67,11 +67,13 @@ public class GameScreen implements Screen {
 
         @Override
         public boolean keyDown(int keycode) {
+            gameInput.keyDown(keycode);
             return false;
         }
 
         @Override
         public boolean keyUp(int keycode) {
+            gameInput.keyUp(keycode);
             return false;
         }
 
@@ -82,24 +84,32 @@ public class GameScreen implements Screen {
 
         @Override
         public boolean touchDown(int screenX, int screenY, int pointer, int button) {
+            screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
+            screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
             gameInput.touchDown(screenX, screenY, button);
             return false;
         }
 
         @Override
         public boolean touchUp(int screenX, int screenY, int pointer, int button) {
+            screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
+            screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
             gameInput.touchUp(screenX, screenY, button);
             return false;
         }
 
         @Override
         public boolean touchDragged(int screenX, int screenY, int pointer) {
+            screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
+            screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
             gameInput.touchDragged(screenX, screenY);
             return false;
         }
 
         @Override
         public boolean mouseMoved(int screenX, int screenY) {
+            screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
+            screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
             gameInput.mouseMoved(screenX,screenY);
             return false;
         }
index 1273c05a7c6ebb97b0e3a17d6402b7a6ca7d788f..c3aa564c9b25c0ad45c104eaa6e0effe537db918 100644 (file)
@@ -11,9 +11,27 @@ public class GameInputHandler {
         this.gameProc = gameProc;
     }
 
+    public void  keyDown(int keyCode) {
+        if (keyCode == Input.Keys.LEFT) {
+            gameProc.player.moveX.add(-GamePhysics.PL_SPEED,0);
+            gameProc.player.dir = 0;
+        }
+        if (keyCode == Input.Keys.RIGHT) {
+            gameProc.player.moveX.add(GamePhysics.PL_SPEED,0);
+            gameProc.player.dir = 1;
+        }
+        if (keyCode == Input.Keys.UP) gameProc.player.moveY.add(0,-12);
+    }
+
+    public void keyUp(int keyCode) {
+        if (keyCode == Input.Keys.RIGHT || keyCode == Input.Keys.LEFT) {
+            gameProc.player.moveX.x = 0;
+        }
+    }
+
     public void mouseMoved(int screenX, int screenY) {
-        gameProc.cursorX = (int)((screenX+gameProc.renderer.camera.position.x)/32);
-        gameProc.cursorY = (int)((screenY+gameProc.renderer.camera.position.y)/32);
+        gameProc.cursorX = (int)((screenX+gameProc.renderer.camera.position.x)/16);
+        gameProc.cursorY = (int)((screenY+gameProc.renderer.camera.position.y)/16);
         if (gameProc.cursorX < 0)
             gameProc.cursorX = 0;
         if (gameProc.cursorX >= gameProc.world.getWidth())
@@ -26,26 +44,33 @@ public class GameInputHandler {
     }
 
     public void touchDown(int screenX, int screenY, int button) {
-        if (button == Input.Buttons.LEFT) {
-            if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) {
-                gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0);
-            } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) {
-                gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0);
-            }
-        } else {
-            gameProc.touchDownTime = TimeUtils.millis();
-            gameProc.isTouchDown = true;
-        }
+        gameProc.touchDownX = screenX;
+        gameProc.touchDownY = screenY;
+        gameProc.touchDownTime = TimeUtils.millis();
+        gameProc.isTouchDown = true;
     }
 
     public void touchUp(int screenX, int screenY, int button) {
-        if (gameProc.isTouchDown && button == Input.Buttons.RIGHT){
+        if (gameProc.isTouchDown) {
+            if (button == Input.Buttons.RIGHT){
                 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 1);
+            } else if (button == Input.Buttons.LEFT) {
+                if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) {
+                    gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0);
+                } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) {
+                    gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0);
+                }
+            }
         }
         gameProc.isTouchDown = false;
     }
 
     public void touchDragged(int screenX, int screenY) {
+        gameProc.renderer.camera.position.x += (gameProc.touchDownX-screenX);
+        gameProc.renderer.camera.position.y += (gameProc.touchDownY-screenY);
+        gameProc.touchDownX = screenX;
+        gameProc.touchDownY = screenY;
+        gameProc.isTouchDown = false;
     }
 
 }
diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
new file mode 100644 (file)
index 0000000..1ed455b
--- /dev/null
@@ -0,0 +1,77 @@
+package ru.deadsoftware.cavecraft.game;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.math.Rectangle;
+import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavecraft.game.objects.Player;
+
+public class GamePhysics {
+
+    public static final int PL_SPEED = 2;
+
+    private GameProc gameProc;
+
+    private Vector2 gravity;
+
+    public GamePhysics(GameProc gameProc) {
+        this.gameProc = gameProc;
+        gravity = new Vector2(0,2);
+    }
+
+    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;
+        }
+        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();
+        }
+        pl.position.add(pl.moveY);
+        if (checkColl(pl.getRect())) {
+            int d = 0;
+            if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
+            while (checkColl(pl.getRect())) pl.position.y+=d;
+            pl.moveY.setZero();
+        }
+        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;
+        }
+    }
+
+    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;
+        }
+        if (gameProc.renderer.camera.position.x > gameProc.renderer.camTargetPos.x) {
+            gameProc.renderer.camera.position.sub(16,0,0);
+        }
+        if (gameProc.renderer.camera.position.x < gameProc.renderer.camTargetPos.x) {
+            gameProc.renderer.camera.position.add(16,0,0);
+        }
+        gameProc.renderer.camera.position.y = gameProc.player.position.y+gameProc.player.height/2
+                -gameProc.renderer.camera.viewportHeight/2;
+    }
+
+}
index bd91e725ef85509829cd2fcf9eab7f30ae054b0c..61c64ffebf940ab81233bce72fb7da74977c4e2a 100644 (file)
@@ -1,13 +1,17 @@
 package ru.deadsoftware.cavecraft.game;
 
 import com.badlogic.gdx.utils.TimeUtils;
+import ru.deadsoftware.cavecraft.game.objects.Player;
 
 public class GameProc {
 
     public static double RUN_TIME = 0;
 
+    public Player player;
+
     public GameWorld world;
     public GameRenderer renderer;
+    public GamePhysics physics;
 
     public int cursorX, cursorY;
 
@@ -16,12 +20,21 @@ public class GameProc {
     public long touchDownTime;
 
     public GameProc() {
-        world = new GameWorld(512,16);
+        world = new GameWorld(512,32);
+        renderer = new GameRenderer(this);
+        physics = new GamePhysics(this);
+        player = new Player();
+    }
+
+    public void resetRenderer() {
         renderer = new GameRenderer(this);
     }
 
     public void update(float delta) {
         RUN_TIME += delta;
+
+        physics.update(delta);
+
         if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
             world.placeToBackground(cursorX,cursorY,1);
             isTouchDown = false;
index c6dd02f7053c11493d05474c99245aea3ab96e00..563f095203bd418c6822cb9039b907a70c48318d 100644 (file)
@@ -5,53 +5,95 @@ import com.badlogic.gdx.graphics.GL20;
 import com.badlogic.gdx.graphics.OrthographicCamera;
 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
 import com.badlogic.gdx.graphics.Color;
+import com.badlogic.gdx.math.Rectangle;
+import com.badlogic.gdx.math.Vector3;
 import ru.deadsoftware.cavecraft.GameScreen;
+import ru.deadsoftware.cavecraft.game.objects.Player;
 
 public class GameRenderer {
 
     private GameProc gameProc;
 
-    OrthographicCamera camera;
+    public Vector3 camTargetPos;
+    public OrthographicCamera camera;
     ShapeRenderer shapeRenderer;
 
     public GameRenderer(GameProc gameProc) {
-        Gdx.gl.glClearColor(0f,.8f,.8f,1f);
+        Gdx.gl.glClearColor(0f,.6f,.6f,1f);
         this.gameProc = gameProc;
         camera = new OrthographicCamera();
-        camera.setToOrtho(true, GameScreen.getWidth(), GameScreen.getHeight());
+        camera.setToOrtho(true, 320,
+                320*((float)GameScreen.getHeight()/GameScreen.getWidth()));
         camera.position.x=0;
         camera.position.y=0;
+        camTargetPos = camera.position.cpy();
         shapeRenderer = new ShapeRenderer();
         shapeRenderer.setProjectionMatrix(camera.combined);
+        shapeRenderer.setAutoShapeType(true);
     }
 
-    public void drawWorld() {
-        shapeRenderer.setAutoShapeType(true);
-        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
-        for (int y=0; y<gameProc.world.getHeight(); y++) {
-            for (int x=0; x<gameProc.world.getWidth(); x++) {
-                if (gameProc.world.getBackMap(x,y)>0) {
-                    shapeRenderer.setColor(Color.DARK_GRAY);
-                    shapeRenderer.rect(x*32-camera.position.x,
-                            y*32-camera.position.y,32,32);
-                }
+    private void setColor(int r, int g, int b) {
+        shapeRenderer.setColor(r/255f, g/255f, b/255f, 1f);
+    }
+
+    private void fillRect(float x, float y, float w, float h) {
+        shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
+        shapeRenderer.rect(x,y,w,h);
+    }
+
+    private void drawRect(float x, float y, float w, float h) {
+        shapeRenderer.set(ShapeRenderer.ShapeType.Line);
+        shapeRenderer.rect(x,y,w,h);
+    }
+
+    private void drawWorld() {
+        int minX = (int) (camera.position.x/16);
+        int minY = (int) (camera.position.y/16);
+        int maxX = (int) ((camera.position.x+camera.viewportWidth)/16)+1;
+        int maxY = (int) ((camera.position.y+camera.viewportHeight)/16)+1;
+        if (minX<0) minX=0;
+        if (minY<0) minY=0;
+        if (maxX>=gameProc.world.getWidth()) maxX = gameProc.world.getWidth()-1;
+        if (maxY>=gameProc.world.getHeight()) maxY = gameProc.world.getHeight()-1;
+        for (int y=minY; y<maxY; y++) {
+            for (int x=minX; x<maxX; x++) {
                 if (gameProc.world.getForeMap(x,y)>0) {
-                    shapeRenderer.setColor(Color.GRAY);
-                    shapeRenderer.rect(x*32-camera.position.x,
-                            y*32-camera.position.y,32,32);
+                    setColor(128,128,128);
+                    fillRect(x*16-camera.position.x,
+                            y*16-camera.position.y,16,16);
+                    setColor(0,0,0);
+                    drawRect(x*16-camera.position.x,
+                            y*16-camera.position.y,16,16);
+                } else {
+                    if (gameProc.world.getBackMap(x,y)>0) {
+                        setColor(64,64,64);
+                        fillRect(x*16-camera.position.x,
+                                y*16-camera.position.y,16,16);
+                        setColor(0,0,0);
+                        drawRect(x*16-camera.position.x,
+                                y*16-camera.position.y,16,16);
+                    }
                 }
+
             }
         }
         shapeRenderer.setColor(Color.ORANGE);
-        shapeRenderer.set(ShapeRenderer.ShapeType.Line);
-        shapeRenderer.rect(gameProc.cursorX*32-camera.position.x,
-                gameProc.cursorY*32-camera.position.y,32,32);
-        shapeRenderer.end();
+        drawRect(gameProc.cursorX*16-camera.position.x,
+                gameProc.cursorY*16-camera.position.y,16,16);
+    }
+
+    private void drawPlayer(Player pl) {
+        setColor(0,128,0);
+        fillRect(pl.position.x - camera.position.x,
+                pl.position.y - camera.position.y, pl.width, pl.height);
     }
 
     public void render() {
         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
+        shapeRenderer.begin();
         drawWorld();
+        drawPlayer(gameProc.player);
+        shapeRenderer.end();
     }
 
 }
index e3822d8d5f7d9c5096237c35adbcb755dfb21fca..faf98ed5e50e815b37863bd314f9922c6e83b8ef 100644 (file)
@@ -8,7 +8,7 @@ public class WorldGen {
         foreMap = new int[width][height];
         backMap = new int[width][height];
         for (int x=0; x<width; x++) {
-            for (int y=height-6; y<height; y++) {
+            for (int y=height-16; y<height; y++) {
                 foreMap[x][y]=1;
                 backMap[x][y]=1;
             }
diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java
new file mode 100644 (file)
index 0000000..c63e059
--- /dev/null
@@ -0,0 +1,24 @@
+package ru.deadsoftware.cavecraft.game.objects;
+
+import com.badlogic.gdx.math.Rectangle;
+import com.badlogic.gdx.math.Vector2;
+
+public class Player {
+
+    public Vector2 position;
+    public Vector2 moveX, moveY;
+    public int width, height, dir;
+
+    public Player() {
+        position = new Vector2(0, 0);
+        moveX = new Vector2(0, 0);
+        moveY = new Vector2(0, 0);
+        width = 8;
+        height = 30;
+    }
+
+    public Rectangle getRect() {
+        return new Rectangle(position.x, position.y, width, height);
+    }
+
+}