From 99a56427db13dd0ecd025e433a438b77245cb739 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Sat, 24 Nov 2018 23:22:25 +0700 Subject: [PATCH] Refactor --- .../cavecraft/game/GameInput.java | 41 +++--- .../cavecraft/game/GamePhysics.java | 116 ++++++++-------- .../deadsoftware/cavecraft/game/GameProc.java | 34 ++--- .../cavecraft/game/GameRenderer.java | 125 ++++-------------- .../deadsoftware/cavecraft/game/mobs/Mob.java | 12 +- .../deadsoftware/cavecraft/game/mobs/Pig.java | 4 +- .../cavecraft/game/objects/Player.java | 109 ++++++++++----- .../cavecraft/misc/InputHandlerGame.java | 3 - 8 files changed, 195 insertions(+), 249 deletions(-) diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java index 2b9e1dd..8bfea89 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java @@ -20,18 +20,25 @@ public class GameInput { return GameItems.isFluid(gp.world.getForeMap(gp.player.getMapX(), gp.player.getMapY())); } + private boolean insideCreativeInv(int screenX, int screenY) { + return (screenX > gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 && + screenX < gp.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 && + screenY > gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 && + screenY < gp.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2); + } + private void wasdPressed(int keycode) { if (gp.ctrlMode == 0 || !CaveGame.TOUCH) { switch (keycode) { case Input.Keys.A: gp.player.mov.x = -GamePhysics.PL_SPEED; gp.player.setDir(0); - if (CaveGame.TOUCH && checkSwim()) gp.swim = true; + if (CaveGame.TOUCH && checkSwim()) gp.player.swim = true; break; case Input.Keys.D: gp.player.mov.x = GamePhysics.PL_SPEED; gp.player.setDir(1); - if (CaveGame.TOUCH && checkSwim()) gp.swim = true; + if (CaveGame.TOUCH && checkSwim()) gp.player.swim = true; break; } } else { @@ -69,7 +76,7 @@ public class GameInput { case Input.Keys.SPACE: if (checkSwim()) { - gp.swim = true; + gp.player.swim = true; } else if (gp.player.canJump) { gp.player.mov.add(0, -7); } else if (!gp.player.flyMode && gp.player.gameMode == 1) { @@ -116,20 +123,17 @@ public class GameInput { case Input.Keys.A: case Input.Keys.D: gp.player.mov.x = 0; - if (CaveGame.TOUCH && gp.swim) gp.swim = false; + if (CaveGame.TOUCH && gp.player.swim) gp.player.swim = false; break; case Input.Keys.SPACE: case Input.Keys.CONTROL_LEFT: if (gp.player.flyMode) gp.player.mov.y = 0; - if (gp.swim) gp.swim = false; + if (gp.player.swim) gp.player.swim = false; break; } } - public void mouseMoved(int screenX, int screenY) { - } - public void touchDown(int screenX, int screenY, int button) { gp.touchDownTime = TimeUtils.millis(); gp.isTouchDown = true; @@ -144,11 +148,7 @@ public class GameInput { gp.isKeyDown = false; } if (gp.isTouchDown) { - if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && - screenX > gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 && - screenX < gp.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 && - screenY > gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 && - screenY < gp.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2) { + if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && insideCreativeInv(screenX, screenY)) { int ix = (int) (screenX - (gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 + 8)) / 18; int iy = (int) (screenY - (gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18; int item = gp.creativeScroll * 8 + (ix + iy * 8); @@ -164,10 +164,10 @@ public class GameInput { } else if (screenY < Assets.invBar.getRegionHeight() && screenX > gp.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 && screenX < gp.renderer.getWidth() / 2 + Assets.invBar.getRegionWidth() / 2) { - gp.slot = (int) ((screenX - (gp.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2)) / 20); + gp.player.invSlot = (int) ((screenX - (gp.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2)) / 20); } else if (button == Input.Buttons.RIGHT) { gp.useItem(gp.curX, gp.curY, - gp.player.inv[gp.slot], false); + gp.player.inv[gp.player.invSlot], false); } else if (button == Input.Buttons.LEFT) { gp.blockDmg = 0; } @@ -177,10 +177,7 @@ public class GameInput { public void touchDragged(int screenX, int screenY) { if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && Math.abs(screenY - gp.touchDownY) > 16) { - if (screenX > gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 && - screenX < gp.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 && - screenY > gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 && - screenY < gp.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2) { + if (insideCreativeInv(screenX, screenY)) { gp.creativeScroll -= (screenY - gp.touchDownY) / 16; gp.touchDownX = screenX; gp.touchDownY = screenY; @@ -194,9 +191,9 @@ public class GameInput { public void scrolled(int amount) { switch (CaveGame.STATE) { case GAME_PLAY: - gp.slot += amount; - if (gp.slot < 0) gp.slot = 8; - if (gp.slot > 8) gp.slot = 0; + gp.player.invSlot += amount; + if (gp.player.invSlot < 0) gp.player.invSlot = 8; + if (gp.player.invSlot > 8) gp.player.invSlot = 0; break; case GAME_CREATIVE_INV: gp.creativeScroll += amount; diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index 8f3e8fe..101ff79 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -86,25 +86,55 @@ class GamePhysics { } } - private void playerPhy(Player pl) { - pl.pos.y += pl.mov.y; - if (checkColl(pl.getRect())) { + private void mobXColl(Mob mob) { + if (checkColl(mob.getRect())) { + if (mob.canJump && !mob.flyMode) { + mob.pos.y -= 8; + } + if (checkColl(mob.getRect())) { + if (mob.canJump && !mob.flyMode) mob.pos.y += 8; + int d = 0; + if (mob.mov.x < 0) d = 1; + else if (mob.mov.x > 0) d = -1; + mob.pos.x = MathUtils.round(mob.pos.x); + while (checkColl(mob.getRect())) mob.pos.x += d; + if (mob.canJump) mob.changeDir(); + } + } + if (mob.pos.x + mob.getWidth() / 2 < 0) mob.pos.x += gp.world.getWidthPx(); + if (mob.pos.x + mob.getWidth() / 2 > gp.world.getWidthPx()) mob.pos.x -= gp.world.getWidthPx(); + } + + private void mobYColl(Mob mob) { + if (checkColl(mob.getRect())) { int d = -1; - if (pl.mov.y < 0) d = 1; + if (mob.mov.y < 0) d = 1; if (d == -1) { - pl.flyMode = false; - pl.canJump = true; + mob.canJump = true; + mob.flyMode = false; + } + mob.pos.y = MathUtils.round(mob.pos.y); + while (checkColl(mob.getRect())) mob.pos.y += d; + mob.mov.y = 0; + if (mob.getType() > 0) { + gp.world.setForeMap(mob.getMapX(), mob.getMapY(), mob.getType()); + mob.kill(); } - pl.pos.y = MathUtils.round(pl.pos.y); - while (checkColl(pl.getRect())) pl.pos.y += d; - pl.mov.y = 0; } else { - pl.canJump = false; + mob.canJump = false; + } + if (mob.pos.y > gp.world.getHeightPx()) { + mob.kill(); } + } + + private void playerPhy(Player pl) { + pl.pos.y += pl.mov.y; + mobYColl(pl); if (GameItems.isFluid(getBlock(pl.getRect()))) { - if (CaveGame.TOUCH && pl.mov.x != 0 && !gp.swim && !pl.flyMode) gp.swim = true; - if (!gp.swim) { + if (CaveGame.TOUCH && pl.mov.x != 0 && !pl.swim && !pl.flyMode) pl.swim = true; + if (!pl.swim) { if (!pl.flyMode && pl.mov.y < 4.5f) pl.mov.add(gravity.x / 4, gravity.y / 4); if (!pl.flyMode && pl.mov.y > 4.5f) pl.mov.add(0, -1f); } else { @@ -116,22 +146,8 @@ class GamePhysics { } pl.pos.x += pl.mov.x; - if (checkColl(pl.getRect())) { - if (pl.canJump && !pl.flyMode) pl.pos.y -= 8; - if (checkColl(pl.getRect())) { - if (pl.canJump && !pl.flyMode) pl.pos.y += 8; - int d = 0; - if (pl.mov.x < 0) d = 1; - else if (pl.mov.x > 0) d = -1; - pl.pos.x = MathUtils.round(pl.pos.x); - while (checkColl(pl.getRect())) pl.pos.x += d; - } - } - if (pl.pos.x + pl.getWidth() / 2 < 0) pl.pos.x += gp.world.getWidthPx(); - if (pl.pos.x + pl.getWidth() / 2 > gp.world.getWidthPx()) pl.pos.x -= gp.world.getWidthPx(); - if (pl.pos.y > gp.world.getHeightPx()) { - pl.respawn(gp.world); - } + mobXColl(pl); + if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDir()) && !pl.flyMode && pl.canJump && pl.mov.x != 0) { pl.mov.add(0, -8); pl.canJump = false; @@ -140,47 +156,17 @@ class GamePhysics { private void mobPhy(Mob mob) { mob.pos.y += mob.mov.y; - if (checkColl(mob.getRect())) { - int d = -1; - if (mob.mov.y < 0) d = 1; - if (d == -1) mob.canJump = true; - mob.pos.y = MathUtils.round(mob.pos.y); - while (checkColl(mob.getRect())) mob.pos.y += d; - mob.mov.y = 0; - if (mob.getType() > 0) { - gp.world.setForeMap(mob.getMapX(), mob.getMapY(), mob.getType()); - mob.kill(); - } - } else { - mob.canJump = false; - } + mobYColl(mob); if (mob.getType() == 0 && GameItems.isFluid(getBlock(mob.getRect()))) { if (mob.mov.y > 9) mob.mov.add(0, -.9f); mob.mov.add(0, -.5f); if (mob.mov.y < -3) mob.mov.y = -3; - } else if (mob.mov.y < 18) mob.mov.add(gravity); + } else if (!mob.flyMode && mob.mov.y < 18) mob.mov.add(gravity); mob.pos.x += mob.mov.x; - if (checkColl(mob.getRect())) { - if (mob.canJump) { - mob.pos.y -= 8; - } - if (checkColl(mob.getRect())) { - if (mob.canJump) mob.pos.y += 8; - int d = 0; - if (mob.mov.x < 0) d = 1; - else if (mob.mov.x > 0) d = -1; - mob.pos.x = MathUtils.round(mob.pos.x); - while (checkColl(mob.getRect())) mob.pos.x += d; - if (mob.canJump) mob.changeDir(); - } - } - if (mob.pos.x + mob.getWidth() / 2 < 0) mob.pos.x += gp.world.getWidthPx(); - if (mob.pos.x + mob.getWidth() / 2 > gp.world.getWidthPx()) mob.pos.x -= gp.world.getWidthPx(); - if (mob.pos.y > gp.world.getHeightPx()) { - mob.pos.y = 0; - } + mobXColl(mob); + if (checkJump(mob.getRect(), mob.getDir()) && mob.canJump && mob.mov.x != 0) { mob.mov.add(0, -8); mob.canJump = false; @@ -188,6 +174,7 @@ class GamePhysics { } void update(float delta) { + //TODO use delta time for (Iterator it = gp.drops.iterator(); it.hasNext(); ) { Drop drop = it.next(); dropPhy(drop); @@ -203,10 +190,11 @@ class GamePhysics { } playerPhy(gp.player); + if (gp.player.isDead()) gp.player.respawn(gp.world); gp.renderer.setCamPos( - gp.player.pos.x + (float) gp.player.getWidth() / 2 - gp.renderer.getWidth() / 2, - gp.player.pos.y + (float) gp.player.getHeight() / 2 - gp.renderer.getHeight() / 2); + gp.player.pos.x + gp.player.getWidth() / 2 - gp.renderer.getWidth() / 2, + gp.player.pos.y + gp.player.getHeight() / 2 - gp.renderer.getHeight() / 2); } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index dd2c387..8910d49 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -19,31 +19,27 @@ import java.util.ArrayList; public class GameProc implements Serializable { - public static double RUN_TIME = 0; + static boolean DO_UPD = false; + static int UPD_X = -1, UPD_Y = -1; - public static boolean DO_UPD = false; - public static int UPD_X = -1, UPD_Y = -1; + public transient GameWorld world; + public transient GameRenderer renderer; + public transient GamePhysics physics; public Player player; - public ArrayList mobs; public ArrayList drops; - public transient GameWorld world; - public transient GameRenderer renderer; - public transient GamePhysics physics; - - public int curX, curY; - public int slot; - public int ctrlMode; - public int creativeScroll, maxCreativeScroll; - public int blockDmg = 0; - public boolean isTouchDown, isKeyDown, swim; - public int touchDownX, touchDownY, keyDownCode; - public int touchDownBtn; + public boolean isTouchDown, isKeyDown; + public int ctrlMode, touchDownX, touchDownY, touchDownBtn, keyDownCode; public long touchDownTime; + int curX, curY; + int creativeScroll, maxCreativeScroll; + int blockDmg = 0; + + public GameProc(int gameMode) { world = new GameWorld(); world.generate(1024, 256); @@ -380,7 +376,7 @@ public class GameProc implements Serializable { } } - public void useItem(int x, int y, int id, boolean bg) { + void useItem(int x, int y, int id, boolean bg) { if (id > 0 && GameItems.getItem(id).getType() == 0) { if (!bg) world.placeToForeground(x, y, GameItems.getItem(id).getBlock()); else world.placeToBackground(x, y, GameItems.getItem(id).getBlock()); @@ -388,8 +384,6 @@ public class GameProc implements Serializable { } public void update(float delta) { - RUN_TIME += delta; - if (DO_UPD) { for (int y = UPD_Y; y < UPD_Y + 16; y++) for (int x = UPD_X; x < UPD_X + 16; x++) { @@ -436,7 +430,7 @@ public class GameProc implements Serializable { if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) { if (touchDownBtn == Input.Buttons.RIGHT) { - useItem(curX, curY, player.inv[slot], true); + useItem(curX, curY, player.inv[player.invSlot], true); isTouchDown = false; } else if (touchDownY < Assets.invBar.getRegionHeight() && touchDownX > renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 && diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 7780d51..72686d1 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -1,15 +1,11 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.badlogic.gdx.math.MathUtils; import ru.deadsoftware.cavecraft.CaveGame; import ru.deadsoftware.cavecraft.GameScreen; import ru.deadsoftware.cavecraft.game.mobs.Mob; import ru.deadsoftware.cavecraft.game.objects.Drop; -import ru.deadsoftware.cavecraft.game.objects.Player; import ru.deadsoftware.cavecraft.misc.Assets; import ru.deadsoftware.cavecraft.misc.Renderer; @@ -41,37 +37,29 @@ public class GameRenderer extends Renderer { } } - private void drawWorldBackground() { - int minX = (int) (getCamX() / 16) - 1; - int minY = (int) (getCamY() / 16) - 1; - int maxX = (int) ((getCamX() + getWidth()) / 16) + 1; - int maxY = (int) ((getCamY() + getHeight()) / 16) + 1; - if (minY < 0) minY = 0; - if (maxY > gp.world.getHeight()) maxY = gp.world.getHeight(); - for (int y = minY; y < maxY; y++) { - for (int x = minX; x < maxX; x++) { - if ((gp.world.getForeMap(x, y) == 0 || GameItems.getBlock(gp.world.getForeMap(x, y)).isTransparent()) - && gp.world.getBackMap(x, y) > 0) { - spriter.draw( - Assets.blockTex[GameItems.getBlock(gp.world.getBackMap(x, y)).getTex()], - drawX(x), drawY(y)); - if (gp.world.getForeMap(x, y) == 0 && x == gp.curX && y == gp.curY) - drawWreck(gp.world.getBackMap(gp.curX, gp.curY)); - Assets.shade.setPosition(drawX(x), drawY(y)); - Assets.shade.draw(spriter); - } - if (gp.world.getForeMap(x, y) > 0 && GameItems.getBlock(gp.world.getForeMap(x, y)).isBackground()) { - spriter.draw( - Assets.blockTex[GameItems.getBlock(gp.world.getForeMap(x, y)).getTex()], - drawX(x), drawY(y)); - if (x == gp.curX && y == gp.curY) - drawWreck(gp.world.getForeMap(gp.curX, gp.curY)); - } + private void drawBlock(int x, int y, boolean drawBG) { + if (drawBG) { + if ((gp.world.getForeMap(x, y) == 0 || GameItems.getBlock(gp.world.getForeMap(x, y)).isTransparent()) + && gp.world.getBackMap(x, y) > 0) { + spriter.draw( + Assets.blockTex[GameItems.getBlock(gp.world.getBackMap(x, y)).getTex()], + drawX(x), drawY(y)); + if (gp.world.getForeMap(x, y) == 0 && x == gp.curX && y == gp.curY) + drawWreck(gp.world.getBackMap(gp.curX, gp.curY)); + Assets.shade.setPosition(drawX(x), drawY(y)); + Assets.shade.draw(spriter); } } + if (gp.world.getForeMap(x, y) > 0 && GameItems.getBlock(gp.world.getForeMap(x, y)).isBackground() == drawBG) { + spriter.draw( + Assets.blockTex[GameItems.getBlock(gp.world.getForeMap(x, y)).getTex()], + drawX(x), drawY(y)); + if (x == gp.curX && y == gp.curY) + drawWreck(gp.world.getForeMap(gp.curX, gp.curY)); + } } - private void drawWorldForeground() { + private void drawWorld(boolean bg) { int minX = (int) (getCamX() / 16) - 1; int minY = (int) (getCamY() / 16) - 1; int maxX = (int) ((getCamX() + getWidth()) / 16) + 1; @@ -80,13 +68,7 @@ public class GameRenderer extends Renderer { if (maxY > gp.world.getHeight()) maxY = gp.world.getHeight(); for (int y = minY; y < maxY; y++) { for (int x = minX; x < maxX; x++) { - if (gp.world.getForeMap(x, y) > 0 && !GameItems.getBlock(gp.world.getForeMap(x, y)).isBackground()) { - spriter.draw( - Assets.blockTex[GameItems.getBlock(gp.world.getForeMap(x, y)).getTex()], - drawX(x), drawY(y)); - if (x == gp.curX && y == gp.curY) - drawWreck(gp.world.getForeMap(gp.curX, gp.curY)); - } + drawBlock(x, y, bg); } } } @@ -123,65 +105,6 @@ public class GameRenderer extends Renderer { } } - private void drawPlayer(Player pl) { - - float drawX = pl.pos.x - getCamX() - 2; - float drawY = pl.pos.y - getCamY(); - - if (pl.mov.x != 0 || Assets.plSprite[0][2].getRotation() != 0) { - Assets.plSprite[0][2].rotate(Player.ANIM_SPEED); - Assets.plSprite[1][2].rotate(-Player.ANIM_SPEED); - Assets.plSprite[0][3].rotate(-Player.ANIM_SPEED); - Assets.plSprite[1][3].rotate(Player.ANIM_SPEED); - } else { - Assets.plSprite[0][2].setRotation(0); - Assets.plSprite[1][2].setRotation(0); - Assets.plSprite[0][3].setRotation(0); - Assets.plSprite[1][3].setRotation(0); - } - if (Assets.plSprite[0][2].getRotation() >= 60 || Assets.plSprite[0][2].getRotation() <= -60) - Player.ANIM_SPEED = -Player.ANIM_SPEED; - - //back hand - Assets.plSprite[1][2].setPosition(drawX - 6, drawY); - Assets.plSprite[1][2].draw(spriter); - //back leg - Assets.plSprite[1][3].setPosition(drawX - 6, drawY + 10); - Assets.plSprite[1][3].draw(spriter); - //front leg - Assets.plSprite[0][3].setPosition(drawX - 6, drawY + 10); - Assets.plSprite[0][3].draw(spriter); - //head - spriter.draw(Assets.plSprite[pl.getDir()][0], drawX - 2, drawY - 2); - //body - spriter.draw(Assets.plSprite[pl.getDir()][1], drawX - 2, drawY + 8); - //item in hand - if (pl.inv[gp.slot] > 0) { - float handRotation = MathUtils.degRad * Assets.plSprite[0][2].getRotation(); - switch (GameItems.getItem(pl.inv[gp.slot]).getType()) { - case 0: - Assets.blockTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].setPosition( - drawX - 8 * MathUtils.sin(handRotation), - drawY + 6 + 8 * MathUtils.cos(handRotation)); - Assets.blockTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].draw(spriter); - break; - default: - Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].flip((pl.getDir() == 0), false); - Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].setRotation( - -45 + pl.getDir() * 90 + Assets.plSprite[0][2].getRotation()); - Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].setPosition( - drawX - 10 + (12 * pl.getDir()) - 8 * MathUtils.sin(handRotation), - drawY + 2 + 8 * MathUtils.cos(handRotation)); - Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].draw(spriter); - Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].flip((pl.getDir() == 0), false); - break; - } - } - //front hand - Assets.plSprite[0][2].setPosition(drawX - 6, drawY); - Assets.plSprite[0][2].draw(spriter); - } - @SuppressWarnings("IntegerDivisionInFloatingPointContext") private void drawCreative() { float x = getWidth() / 2 - (float) Assets.creativeInv.getRegionWidth() / 2; @@ -245,7 +168,7 @@ public class GameRenderer extends Renderer { } } spriter.draw(Assets.invBarCur, - getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 - 1 + 20 * gp.slot, + getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 - 1 + 20 * gp.player.invSlot, -1); } @@ -264,11 +187,11 @@ public class GameRenderer extends Renderer { } private void drawGamePlay() { - drawWorldBackground(); - drawPlayer(gp.player); + drawWorld(true); + gp.player.draw(spriter, gp.player.pos.x - getCamX() - 2, gp.player.pos.y - getCamY()); for (Mob mob : gp.mobs) drawMob(mob); for (Drop drop : gp.drops) drawDrop(drop); - drawWorldForeground(); + drawWorld(false); drawGUI(); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java index 241f018..f776ff5 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java @@ -8,25 +8,33 @@ import java.io.Serializable; public abstract class Mob implements Serializable { - protected int anim, animSpeed = 6; + public boolean flyMode; private float width, height; private int dir; public Vector2 pos; public Vector2 mov; + private boolean dead; public boolean canJump; + protected int anim, animDelta = 6; - protected Mob(float x, float y, float width, float height, int dir) { + protected Mob(float x, float y, float width, float height, int dir, boolean player) { pos = new Vector2(x, y); + mov = new Vector2(0, 0); this.width = width; this.height = height; canJump = false; + flyMode = false; dead = false; this.dir = dir; } + protected Mob(float x, float y, float width, float height, int dir) { + this(x, y, width, height, dir, false); + } + public int getMapX() { return (int) (pos.x + (getWidth() / 2)) / 16; } diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java index aa3f797..1dd9f9a 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java @@ -25,10 +25,10 @@ public class Pig extends Mob { if (mov.x != 0f) mov.x = 0; else mov.x = -1 + 2 * getDir(); } - if (mov.x != 0f) anim += animSpeed; + if (mov.x != 0f) anim += animDelta; else anim = 0; if (anim >= 60 || anim <= -60) { - animSpeed = -animSpeed; + animDelta = -animDelta; } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java index 5672807..16e3a52 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java @@ -1,38 +1,34 @@ package ru.deadsoftware.cavecraft.game.objects; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavecraft.game.GameItems; import ru.deadsoftware.cavecraft.game.GameWorld; +import ru.deadsoftware.cavecraft.game.mobs.Mob; +import ru.deadsoftware.cavecraft.misc.Assets; import java.io.Serializable; -public class Player implements Serializable { +public class Player extends Mob implements Serializable { - public static int ANIM_SPEED = 6; - - public Vector2 pos; - public Vector2 mov; - private int width, height, dir, hp; - public boolean canJump; public int[] inv; - public boolean flyMode = false; + public int invSlot; public int gameMode; + public boolean swim; public Player(GameWorld world, int gameMode) { + super(0, 0, 4, 30, 1, true); this.gameMode = gameMode; - mov = new Vector2(0, 0); - width = 4; - height = 30; inv = new int[9]; - hp = 20; pos = getSpawnPoint(world).cpy(); + swim = false; } public void respawn(GameWorld world) { pos.set(getSpawnPoint(world)); mov.setZero(); - hp = 20; } private Vector2 getSpawnPoint(GameWorld world) { @@ -48,38 +44,81 @@ public class Player implements Serializable { return new Vector2(x * 16 + 8 - (float) getWidth() / 2, (float) y * 16 - getHeight()); } - public int getMapX() { - return (int) (pos.x + (getWidth() / 2)) / 16; - } - - public int getMapY() { - return (int) (pos.y + (getHeight() / 2)) / 16; - } - - public int getWidth() { - return width; - } - - public int getHeight() { - return height; + public void setDir(int dir) { + if (dir != getDir()) changeDir(); } - public int getHp() { - return hp; + @Override + public void ai() { } - public void setHp(int hp) { - this.hp = hp; + @Override + public void changeDir() { + switchDir(); } - public int getDir() { - return dir; + @Override + public void draw(SpriteBatch spriteBatch, float x, float y) { + if (mov.x != 0 || Assets.plSprite[0][2].getRotation() != 0) { + Assets.plSprite[0][2].rotate(animDelta); + Assets.plSprite[1][2].rotate(-animDelta); + Assets.plSprite[0][3].rotate(-animDelta); + Assets.plSprite[1][3].rotate(animDelta); + } else { + Assets.plSprite[0][2].setRotation(0); + Assets.plSprite[1][2].setRotation(0); + Assets.plSprite[0][3].setRotation(0); + Assets.plSprite[1][3].setRotation(0); + } + if (Assets.plSprite[0][2].getRotation() >= 60 || Assets.plSprite[0][2].getRotation() <= -60) + animDelta = -animDelta; + + //back hand + Assets.plSprite[1][2].setPosition(x - 6, y); + Assets.plSprite[1][2].draw(spriteBatch); + //back leg + Assets.plSprite[1][3].setPosition(x - 6, y + 10); + Assets.plSprite[1][3].draw(spriteBatch); + //front leg + Assets.plSprite[0][3].setPosition(x - 6, y + 10); + Assets.plSprite[0][3].draw(spriteBatch); + //head + spriteBatch.draw(Assets.plSprite[getDir()][0], x - 2, y - 2); + //body + spriteBatch.draw(Assets.plSprite[getDir()][1], x - 2, y + 8); + //item in hand + if (inv[invSlot] > 0) { + float handRotation = MathUtils.degRad * Assets.plSprite[0][2].getRotation(); + switch (GameItems.getItem(inv[invSlot]).getType()) { + case 0: + Assets.blockTex[GameItems.getItem(inv[invSlot]).getTex()].setPosition( + x - 8 * MathUtils.sin(handRotation), + y + 6 + 8 * MathUtils.cos(handRotation)); + Assets.blockTex[GameItems.getItem(inv[invSlot]).getTex()].draw(spriteBatch); + break; + default: + Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].flip((getDir() == 0), false); + Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].setRotation( + -45 + getDir() * 90 + Assets.plSprite[0][2].getRotation()); + Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].setPosition( + x - 10 + (12 * getDir()) - 8 * MathUtils.sin(handRotation), + y + 2 + 8 * MathUtils.cos(handRotation)); + Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].draw(spriteBatch); + Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].flip((getDir() == 0), false); + break; + } + } + //front hand + Assets.plSprite[0][2].setPosition(x - 6, y); + Assets.plSprite[0][2].draw(spriteBatch); } - public void setDir(int dir) { - this.dir = dir; + @Override + public int getType() { + return 0; } + @Override public Rectangle getRect() { return new Rectangle(pos.x, pos.y, getWidth(), getHeight()); } diff --git a/core/src/ru/deadsoftware/cavecraft/misc/InputHandlerGame.java b/core/src/ru/deadsoftware/cavecraft/misc/InputHandlerGame.java index e3778de..0d0cb56 100644 --- a/core/src/ru/deadsoftware/cavecraft/misc/InputHandlerGame.java +++ b/core/src/ru/deadsoftware/cavecraft/misc/InputHandlerGame.java @@ -119,9 +119,6 @@ public class InputHandlerGame implements InputProcessor { @Override public boolean mouseMoved(int screenX, int screenY) { - screenX *= gp.renderer.getWidth() / getWidth(); - screenY *= gp.renderer.getHeight() / getHeight(); - gameInput.mouseMoved(screenX, screenY); return false; } -- 2.29.2