DEADSOFTWARE

Add player animation and fly mode
authorfred-boy <fred-boy@protonmail.com>
Wed, 11 Apr 2018 18:09:50 +0000 (01:09 +0700)
committerfred-boy <fred-boy@protonmail.com>
Wed, 11 Apr 2018 18:09:50 +0000 (01:09 +0700)
android/assets/mobs/char.png [new file with mode: 0644]
core/src/ru/deadsoftware/cavecraft/Assets.java
core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java
core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java
core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java
core/src/ru/deadsoftware/cavecraft/game/objects/Player.java

diff --git a/android/assets/mobs/char.png b/android/assets/mobs/char.png
new file mode 100644 (file)
index 0000000..382b6c7
Binary files /dev/null and b/android/assets/mobs/char.png differ
index 96dbbdc328e46d8c69ddcdd70493b25fd7440389..b24356fb6686593613a39f17e2d532b1a0cdd3f7 100644 (file)
@@ -2,6 +2,7 @@ package ru.deadsoftware.cavecraft;
 
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.g2d.Animation;
 import com.badlogic.gdx.graphics.g2d.Sprite;
 import com.badlogic.gdx.graphics.g2d.TextureRegion;
 
@@ -10,7 +11,7 @@ public class Assets {
     public static final int BLOCK_TEXTURES = 3;
 
     public static Texture charTexture;
-    public static TextureRegion[] playerSkin = new TextureRegion[2];
+    public static Sprite[][] playerSkin = new Sprite[2][4];
 
     public static Sprite shade;
 
@@ -28,11 +29,34 @@ public class Assets {
     public static TextureRegion touchSpace;
 
     public static void load() {
-        charTexture = new Texture(Gdx.files.internal("char.png"));
-        playerSkin[0] = new TextureRegion(charTexture, 0,0,8,30);
-        playerSkin[0].flip(false,true);
-        playerSkin[1] = new TextureRegion(charTexture, 8,0,8,30);
-        playerSkin[1].flip(false,true);
+        charTexture = new Texture(Gdx.files.internal("mobs/char.png"));
+        //LOOK TO LEFT
+        //head
+        playerSkin[0][0] = new Sprite(new TextureRegion(charTexture, 0,0,12,12));
+        playerSkin[0][0].flip(false,true);
+        //body
+        playerSkin[0][1] = new Sprite(new TextureRegion(charTexture, 0,13,12,12));
+        playerSkin[0][1].flip(false,true);
+        //hand
+        playerSkin[0][2] = new Sprite(new TextureRegion(charTexture, 25,5,20,20));
+        playerSkin[0][2].flip(false,true);
+        //leg
+        playerSkin[0][3] = new Sprite(new TextureRegion(charTexture, 25,27,20,20));
+        playerSkin[0][3].flip(false,true);
+        //LOOK TO RIGHT
+        //head
+        playerSkin[1][0] = new Sprite(new TextureRegion(charTexture, 13,0,12,12));
+        playerSkin[1][0].flip(false,true);
+        //body
+        playerSkin[1][1] = new Sprite(new TextureRegion(charTexture, 13,13,12,12));
+        playerSkin[1][1].flip(false,true);
+        //hand
+        playerSkin[1][2] = new Sprite(new TextureRegion(charTexture, 37,5,20,20));
+        playerSkin[1][2].flip(false,true);
+        //leg
+        playerSkin[1][3] = new Sprite(new TextureRegion(charTexture, 37,27,20,20));
+        playerSkin[1][3].flip(false,true);
+
 
         shade = new Sprite(new Texture(Gdx.files.internal("shade.png")));
 
index 313675d414b6788dfa8b1ca935677f87dd0cbd87..aec59d21387608383401b3182e42c75e79588fe9 100644 (file)
@@ -49,14 +49,31 @@ public class GameInputHandler {
             gameProc.cursorY = (int)(gameProc.player.position.y/16);
             if (gameProc.ctrlMode > 1) gameProc.ctrlMode = 0;
         }
-         if (keyCode == Input.Keys.SPACE &&
-                 gameProc.player.canJump) gameProc.player.moveY.add(0, -8);
+        if (keyCode == Input.Keys.SPACE) {
+             if (gameProc.player.canJump) {
+                 gameProc.player.moveY.add(0, -8);
+             } else if (!gameProc.player.flyMode) {
+                 gameProc.player.flyMode = true;
+                 gameProc.player.moveY.setZero();
+             } else {
+                 gameProc.player.moveY.y = -GamePhysics.PL_SPEED;
+             }
+        }
+        if (keyCode == Input.Keys.CONTROL_LEFT) {
+            gameProc.player.moveY.y = GamePhysics.PL_SPEED;
+        }
     }
 
     public void keyUp(int keyCode) {
         if (keyCode == Input.Keys.A || keyCode == Input.Keys.D) {
             gameProc.player.moveX.x = 0;
         }
+        if (keyCode == Input.Keys.SPACE) {
+            if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
+        }
+        if (keyCode == Input.Keys.CONTROL_LEFT) {
+            if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
+        }
     }
 
     public void mouseMoved(int screenX, int screenY) {
index f49eb178b9f9501ebfb783cc0382debde6ac190e..f8843f2c6851ba086a4c3a8e3fbb6aeccb61e529 100644 (file)
@@ -21,12 +21,12 @@ public class GamePhysics {
 
     private boolean checkColl(Rectangle rect) {
         int[] bl = new int [6];
-        bl[0] = gameProc.world.getForeMap(((int)(rect.x+2)/16), ((int)rect.y/16));
-        bl[1] = gameProc.world.getForeMap(((int)(rect.x+rect.width-2)/16), ((int)rect.y/16));
-        bl[2] = gameProc.world.getForeMap(((int)(rect.x+2)/16), ((int)(rect.y+rect.height/2)/16));
-        bl[3] = gameProc.world.getForeMap(((int)(rect.x+rect.width-2)/16), ((int)(rect.y+rect.height/2)/16));
-        bl[4] = gameProc.world.getForeMap(((int)(rect.x+2)/16), ((int)(rect.y+rect.height-1)/16));
-        bl[5] = gameProc.world.getForeMap(((int)(rect.x+rect.width-2)/16), ((int)(rect.y+(rect.height-1))/16));
+        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;
         }
@@ -36,6 +36,7 @@ public class GamePhysics {
     private void playerPhy(Player pl) {
         pl.position.add(pl.moveY);
         if (checkColl(pl.getRect())) {
+            pl.flyMode = false;
             pl.canJump = true;
             int d = -1;
             if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
@@ -45,7 +46,7 @@ public class GamePhysics {
         } else {
             pl.canJump = false;
         }
-        pl.moveY.add(gravity);
+        if (!pl.flyMode) pl.moveY.add(gravity);
         pl.position.add(pl.moveX);
         if (pl.position.x<0 ||
                 pl.position.x+pl.width>gameProc.world.getWidth()*16)
index 6a41ea26f1a25e56005adb997dea168b9f1714ec..59557a2b9418ceebdcc5fc1f229d05ba512aceb0 100644 (file)
@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.OrthographicCamera;
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
 import com.badlogic.gdx.graphics.Color;
+import com.badlogic.gdx.math.Vector2;
 import com.badlogic.gdx.math.Vector3;
 import ru.deadsoftware.cavecraft.Assets;
 import ru.deadsoftware.cavecraft.CaveGame;
@@ -66,18 +67,50 @@ public class GameRenderer {
     }
 
     private void drawMob(Mob mob) {
-        spriteBatch.draw(Assets.playerSkin[mob.dir],
-                mob.position.x - camera.position.x, mob.position.y - camera.position.y);
+        mob.draw(spriteBatch,
+                mob.position.x-camera.position.x, mob.position.y-camera.position.y);
     }
 
     private void drawPlayer(Player pl) {
-        spriteBatch.draw(Assets.playerSkin[pl.dir],
-                pl.position.x - camera.position.x, pl.position.y - camera.position.y);
+        if (!pl.moveX.equals(Vector2.Zero)) {
+            Assets.playerSkin[0][2].rotate(Mob.ANIM_SPEED);
+            Assets.playerSkin[1][2].rotate(-Mob.ANIM_SPEED);
+            Assets.playerSkin[0][3].rotate(-Mob.ANIM_SPEED);
+            Assets.playerSkin[1][3].rotate(Mob.ANIM_SPEED);
+        } else {
+            Assets.playerSkin[0][2].setRotation(0);
+            Assets.playerSkin[1][2].setRotation(0);
+            Assets.playerSkin[0][3].setRotation(0);
+            Assets.playerSkin[1][3].setRotation(0);
+        }
+        if (Assets.playerSkin[0][2].getRotation()>=60 || Assets.playerSkin[0][2].getRotation()<=-60)
+            Mob.ANIM_SPEED = -Mob.ANIM_SPEED;
+        Assets.playerSkin[1][2].setPosition(
+                pl.position.x - camera.position.x - 6,
+                pl.position.y - camera.position.y);
+        Assets.playerSkin[1][2].draw(spriteBatch);
+        Assets.playerSkin[1][3].setPosition(
+                pl.position.x - camera.position.x - 6,
+                pl.position.y - camera.position.y + 10);
+        Assets.playerSkin[1][3].draw(spriteBatch);
+        Assets.playerSkin[0][3].setPosition(
+                pl.position.x - camera.position.x - 6,
+                pl.position.y - camera.position.y + 10);
+        Assets.playerSkin[0][3].draw(spriteBatch);
+
+        spriteBatch.draw(Assets.playerSkin[pl.dir][0],
+                pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y - 2);
+        spriteBatch.draw(Assets.playerSkin[pl.dir][1],
+                pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y + 8);
+
+        Assets.playerSkin[0][2].setPosition(
+                pl.position.x - camera.position.x - 6,
+                pl.position.y - camera.position.y);
+        Assets.playerSkin[0][2].draw(spriteBatch);
     }
 
     private void drawGUI() {
-        spriteBatch.draw(Assets.invBar, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2,
-                0);//camera.viewportHeight - Assets.invBar.getRegionHeight());
+        spriteBatch.draw(Assets.invBar, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2, 0);
         for (int i=0; i<8; i++) {
             if (gameProc.player.inventory[i]>0) {
                 spriteBatch.draw(Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture(),
@@ -110,9 +143,7 @@ public class GameRenderer {
 
         spriteBatch.begin();
         drawWorld();
-        for (Mob mob : gameProc.mobs) {
-            drawMob(mob);
-        }
+        for (Mob mob : gameProc.mobs) drawMob(mob);
         drawPlayer(gameProc.player);
         drawGUI();
         spriteBatch.end();
index 318ea153be76b578418df085492bab1039a99659..97b1c18dc0b16f86bed4e7ba3998202691f3be48 100644 (file)
@@ -1,8 +1,10 @@
 package ru.deadsoftware.cavecraft.game.mobs;
 
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.math.RandomXS128;
 import com.badlogic.gdx.math.Rectangle;
 import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavecraft.Assets;
 import ru.deadsoftware.cavecraft.game.GameProc;
 
 public class Human extends Mob{
@@ -32,6 +34,23 @@ public class Human extends Mob{
         moveX.add(-2+4*dir, 0);
     }
 
+    @Override
+    public void draw(SpriteBatch spriteBatch, float x, float y) {
+        spriteBatch.draw(Assets.playerSkin[dir][0], x-2, y-2);
+        if (Assets.playerSkin[0][2].getRotation()>=60 || Assets.playerSkin[0][2].getRotation()<=-60)
+            Mob.ANIM_SPEED = -Mob.ANIM_SPEED;
+        Assets.playerSkin[1][2].setPosition(x-6,y);
+        Assets.playerSkin[1][2].draw(spriteBatch);
+        Assets.playerSkin[1][3].setPosition(x-6, y+10);
+        Assets.playerSkin[1][3].draw(spriteBatch);
+        Assets.playerSkin[0][3].setPosition(x-6, y+10);
+        Assets.playerSkin[0][3].draw(spriteBatch);
+        spriteBatch.draw(Assets.playerSkin[dir][1], x-2, y + 8);
+
+        Assets.playerSkin[0][2].setPosition(x-6, y);
+        Assets.playerSkin[0][2].draw(spriteBatch);
+    }
+
     public Rectangle getRect() {
         return new Rectangle(position.x, position.y, width, height);
     }
index 054f0897c3b5b37991d01b1509024492f30965c8..a4dca28e465841cfb177ccfa9f9fd2a63982e732 100644 (file)
@@ -1,16 +1,20 @@
 package ru.deadsoftware.cavecraft.game.mobs;
 
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.math.Rectangle;
 import com.badlogic.gdx.math.Vector2;
 
 public abstract class Mob {
 
+    public static int ANIM_SPEED = 6;
+
     public Vector2 position;
     public Vector2 moveX, moveY;
     public int width, height, dir;
     public boolean canJump;
 
     public abstract void ai();
+    public abstract void draw(SpriteBatch spriteBatch, float x, float y);
     public abstract Rectangle getRect();
 
 }
index 2953d940989febecabc83467aff77e0de7c98623..56aa9c1e4d098aef7d371f1be9083a9602b58484 100644 (file)
@@ -10,12 +10,13 @@ public class Player {
     public int width, height, dir;
     public boolean canJump;
     public int[] inventory;
+    public boolean flyMode = false;
 
     public Player() {
         position = new Vector2(0, 0);
         moveX = new Vector2(0, 0);
         moveY = new Vector2(0, 0);
-        width = 8;
+        width = 4;
         height = 30;
         inventory = new int[9];
         inventory[0] = 1;
@@ -24,7 +25,7 @@ public class Player {
     }
 
     public Rectangle getRect() {
-        return new Rectangle(position.x, position.y, width, height);
+        return new Rectangle(position.x+2, position.y, width, height);
     }
 
 }