From: fred-boy Date: Wed, 11 Apr 2018 18:09:50 +0000 (+0700) Subject: Add player animation and fly mode X-Git-Tag: alpha0.1~15 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=50082e8304a80c41dd8b1c1d80142e7c91a2d924;p=cavedroid.git Add player animation and fly mode --- diff --git a/android/assets/mobs/char.png b/android/assets/mobs/char.png new file mode 100644 index 0000000..382b6c7 Binary files /dev/null and b/android/assets/mobs/char.png differ diff --git a/core/src/ru/deadsoftware/cavecraft/Assets.java b/core/src/ru/deadsoftware/cavecraft/Assets.java index 96dbbdc..b24356f 100644 --- a/core/src/ru/deadsoftware/cavecraft/Assets.java +++ b/core/src/ru/deadsoftware/cavecraft/Assets.java @@ -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"))); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java index 313675d..aec59d2 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java @@ -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) { diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index f49eb17..f8843f2 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -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) diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 6a41ea2..59557a2 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -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(); diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java index 318ea15..97b1c18 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java @@ -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); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java index 054f089..a4dca28 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java @@ -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(); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java index 2953d94..56aa9c1 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java @@ -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); } }