From: fred-boy Date: Sat, 17 Nov 2018 18:34:36 +0000 (+0700) Subject: Code improvements X-Git-Tag: alpha0.4~74 X-Git-Url: https://deadsoftware.ru/gitweb?p=cavedroid.git;a=commitdiff_plain;h=cef4b5a9985bcbdfea6dc652147ecde0721d7fdc Code improvements --- diff --git a/android/src/ru/deadsoftware/cavecraft/AndroidLauncher.java b/android/src/ru/deadsoftware/cavecraft/AndroidLauncher.java index aed5e8e..1a0a929 100644 --- a/android/src/ru/deadsoftware/cavecraft/AndroidLauncher.java +++ b/android/src/ru/deadsoftware/cavecraft/AndroidLauncher.java @@ -1,22 +1,28 @@ package ru.deadsoftware.cavecraft; +import android.content.pm.PackageManager; import android.os.Bundle; - import com.badlogic.gdx.backends.android.AndroidApplication; import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; -import ru.deadsoftware.cavecraft.CaveGame; public class AndroidLauncher extends AndroidApplication { - @Override - protected void onCreate (Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); - config.hideStatusBar = true; - config.useImmersiveMode = true; - initialize(new CaveGame(true), config); - } + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); + config.hideStatusBar = true; + config.useImmersiveMode = true; + String gameFolder = null; + try { + gameFolder = getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir; + } catch (PackageManager.NameNotFoundException e) { + e.printStackTrace(); + exit(); + } + initialize(new CaveGame(gameFolder, true), config); + } - @Override - public void onBackPressed() { - } + @Override + public void onBackPressed() { + } } diff --git a/core/src/ru/deadsoftware/cavecraft/CaveGame.java b/core/src/ru/deadsoftware/cavecraft/CaveGame.java index 37fd2e1..b70038b 100644 --- a/core/src/ru/deadsoftware/cavecraft/CaveGame.java +++ b/core/src/ru/deadsoftware/cavecraft/CaveGame.java @@ -13,27 +13,18 @@ public class CaveGame extends Game { public static boolean TOUCH; - public CaveGame() { - this(false); + public CaveGame(String gameFolder) { + this(gameFolder, false); } - public CaveGame(boolean touch) { + public CaveGame(String gameFolder, boolean touch) { + GAME_FOLDER = gameFolder; TOUCH = touch; STATE = AppState.MENU_MAIN; } @Override public void create() { - switch (Gdx.app.getType()) { - case Desktop: - GAME_FOLDER = System.getProperty("user.home") + "/.cavecraft"; - break; - case Android: - GAME_FOLDER = "/sdcard/cavecraft"; - break; - default: - Gdx.app.exit(); - } Gdx.app.log("CaveGame", GAME_FOLDER); Gdx.files.absolute(GAME_FOLDER).mkdirs(); setScreen(new GameScreen()); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java index 687c6a9..2b9e1dd 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java @@ -17,25 +17,24 @@ public class GameInput { } private boolean checkSwim() { - return (GameItems.isFluid(gp.world.getForeMap((int) (gp.player.pos.x + gp.player.width / 2) / 16, - (int) (gp.player.pos.y + gp.player.height / 4 * 3) / 16))); + return GameItems.isFluid(gp.world.getForeMap(gp.player.getMapX(), gp.player.getMapY())); } private void wasdPressed(int keycode) { if (gp.ctrlMode == 0 || !CaveGame.TOUCH) { switch (keycode) { case Input.Keys.A: - gp.player.move.x = -GamePhysics.PL_SPEED; - gp.player.dir = 0; + gp.player.mov.x = -GamePhysics.PL_SPEED; + gp.player.setDir(0); if (CaveGame.TOUCH && checkSwim()) gp.swim = true; break; case Input.Keys.D: - gp.player.move.x = GamePhysics.PL_SPEED; - gp.player.dir = 1; + gp.player.mov.x = GamePhysics.PL_SPEED; + gp.player.setDir(1); if (CaveGame.TOUCH && checkSwim()) gp.swim = true; break; } - } else if (CaveGame.TOUCH) { + } else { switch (keycode) { case Input.Keys.A: gp.curX--; @@ -72,17 +71,17 @@ public class GameInput { if (checkSwim()) { gp.swim = true; } else if (gp.player.canJump) { - gp.player.move.add(0, -7); + gp.player.mov.add(0, -7); } else if (!gp.player.flyMode && gp.player.gameMode == 1) { gp.player.flyMode = true; - gp.player.move.y = 0; + gp.player.mov.y = 0; } else if (gp.player.flyMode) { - gp.player.move.y = -GamePhysics.PL_SPEED; + gp.player.mov.y = -GamePhysics.PL_SPEED; } break; case Input.Keys.CONTROL_LEFT: - gp.player.move.y = GamePhysics.PL_SPEED; + gp.player.mov.y = GamePhysics.PL_SPEED; break; case Input.Keys.E: @@ -116,13 +115,13 @@ public class GameInput { switch (keycode) { case Input.Keys.A: case Input.Keys.D: - gp.player.move.x = 0; + gp.player.mov.x = 0; if (CaveGame.TOUCH && gp.swim) gp.swim = false; break; case Input.Keys.SPACE: case Input.Keys.CONTROL_LEFT: - if (gp.player.flyMode) gp.player.move.y = 0; + if (gp.player.flyMode) gp.player.mov.y = 0; if (gp.swim) gp.swim = false; break; } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index b5c5eab..8f3e8fe 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -11,15 +11,15 @@ import ru.deadsoftware.cavecraft.game.objects.Player; import java.util.Iterator; -public class GamePhysics { +class GamePhysics { - public static final int PL_SPEED = 2; + static final int PL_SPEED = 2; private GameProc gp; private Vector2 gravity; - public GamePhysics(GameProc gp) { + GamePhysics(GameProc gp) { this.gp = gp; gravity = new Vector2(0, .9f); } @@ -53,7 +53,7 @@ public class GamePhysics { for (int y = minY; y < maxY; y++) { for (int x = minX; x < maxX; x++) { bl = gp.world.getForeMap(x, y); - if (bl > 0 && GameItems.getBlock(bl).coll) { + if (bl > 0 && GameItems.getBlock(bl).hasCollision()) { if (Intersector.overlaps(rect, GameItems.getBlock(bl).getRect(x, y))) { return true; } @@ -77,8 +77,8 @@ public class GamePhysics { if (drop.move.y < 9) drop.move.y += gravity.y / 4; } drop.pos.add(drop.move); - if (drop.pos.x + 8 > gp.world.getWidth() * 16) drop.pos.x -= gp.world.getWidth() * 16; - else if (drop.pos.x < 0) drop.pos.x += gp.world.getWidth() * 16; + if (drop.pos.x + 8 > gp.world.getWidthPx()) drop.pos.x -= gp.world.getWidthPx(); + else if (drop.pos.x < 0) drop.pos.x += gp.world.getWidthPx(); drop.pos.y = MathUtils.round(drop.pos.y); while (checkColl(drop.getRect())) { drop.pos.y--; @@ -87,83 +87,81 @@ public class GamePhysics { } private void playerPhy(Player pl) { - pl.pos.y += pl.move.y; + pl.pos.y += pl.mov.y; if (checkColl(pl.getRect())) { int d = -1; - if (pl.move.y < 0) d = 1; + if (pl.mov.y < 0) d = 1; if (d == -1) { pl.flyMode = false; pl.canJump = true; } pl.pos.y = MathUtils.round(pl.pos.y); while (checkColl(pl.getRect())) pl.pos.y += d; - pl.move.y = 0; + pl.mov.y = 0; } else { pl.canJump = false; } if (GameItems.isFluid(getBlock(pl.getRect()))) { - if (CaveGame.TOUCH && pl.move.x != 0 && !gp.swim && !pl.flyMode) gp.swim = true; + if (CaveGame.TOUCH && pl.mov.x != 0 && !gp.swim && !pl.flyMode) gp.swim = true; if (!gp.swim) { - if (!pl.flyMode && pl.move.y < 4.5f) pl.move.add(gravity.x / 4, gravity.y / 4); - if (!pl.flyMode && pl.move.y > 4.5f) pl.move.add(0, -1f); + 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 { - pl.move.add(0, -.5f); - if (pl.move.y < -3) pl.move.y = -3; + pl.mov.add(0, -.5f); + if (pl.mov.y < -3) pl.mov.y = -3; } } else { - if (!pl.flyMode && pl.move.y < 18) pl.move.add(gravity); + if (!pl.flyMode && pl.mov.y < 18) pl.mov.add(gravity); } - pl.pos.x += pl.move.x; + 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.move.x < 0) d = 1; - else if (pl.move.x > 0) d = -1; + 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.texWidth / 2 < 0) pl.pos.x += gp.world.getWidth() * 16; - if (pl.pos.x + pl.texWidth / 2 > gp.world.getWidth() * 16) - pl.pos.x -= gp.world.getWidth() * 16; - if (pl.pos.y > gp.world.getHeight() * 16) { + 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); } - if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && pl.move.x != 0) { - pl.move.add(0, -8); + if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDir()) && !pl.flyMode && pl.canJump && pl.mov.x != 0) { + pl.mov.add(0, -8); pl.canJump = false; } } private void mobPhy(Mob mob) { - mob.pos.y += mob.move.y; + mob.pos.y += mob.mov.y; if (checkColl(mob.getRect())) { int d = -1; - if (mob.move.y < 0) 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.move.y = 0; + mob.mov.y = 0; if (mob.getType() > 0) { - gp.world.setForeMap((int) mob.pos.x / 16, (int) mob.pos.y / 16, mob.getType()); - mob.pos.y = -1; - mob.dead = true; + gp.world.setForeMap(mob.getMapX(), mob.getMapY(), mob.getType()); + mob.kill(); } } else { mob.canJump = false; } if (mob.getType() == 0 && GameItems.isFluid(getBlock(mob.getRect()))) { - if (mob.move.y > 9) mob.move.add(0, -.9f); - mob.move.add(0, -.5f); - if (mob.move.y < -3) mob.move.y = -3; - } else if (mob.move.y < 18) mob.move.add(gravity); + 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); - mob.pos.x += mob.move.x; + mob.pos.x += mob.mov.x; if (checkColl(mob.getRect())) { if (mob.canJump) { mob.pos.y -= 8; @@ -171,26 +169,25 @@ public class GamePhysics { if (checkColl(mob.getRect())) { if (mob.canJump) mob.pos.y += 8; int d = 0; - if (mob.move.x < 0) d = 1; - else if (mob.move.x > 0) d = -1; + 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.width / 2 < 0) mob.pos.x += gp.world.getWidth() * 16; - if (mob.pos.x + mob.width / 2 > gp.world.getWidth() * 16) - mob.pos.x -= gp.world.getWidth() * 16; - if (mob.pos.y > gp.world.getHeight() * 16) { + 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; } - if (checkJump(mob.getRect(), mob.dir) && mob.canJump && mob.move.x != 0) { - mob.move.add(0, -8); + if (checkJump(mob.getRect(), mob.getDir()) && mob.canJump && mob.mov.x != 0) { + mob.mov.add(0, -8); mob.canJump = false; } } - public void update(float delta) { + void update(float delta) { for (Iterator it = gp.drops.iterator(); it.hasNext(); ) { Drop drop = it.next(); dropPhy(drop); @@ -202,15 +199,14 @@ public class GamePhysics { Mob mob = it.next(); mob.ai(); mobPhy(mob); - if (mob.dead) - it.remove(); + if (mob.isDead()) it.remove(); } playerPhy(gp.player); gp.renderer.setCamPos( - gp.player.pos.x + gp.player.texWidth / 2 - gp.renderer.getWidth() / 2, - gp.player.pos.y + gp.player.height / 2 - gp.renderer.getHeight() / 2); + 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); } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index 5b497a4..dd2c387 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -78,16 +78,16 @@ public class GameProc implements Serializable { private boolean isAutoselectable(int x, int y) { return (world.getForeMap(x, y) > 0 && - GameItems.getBlock(world.getForeMap(x, y)).coll); + GameItems.getBlock(world.getForeMap(x, y)).hasCollision()); } private void moveCursor() { int pastX = curX, pastY = curY; if (ctrlMode == 0 && CaveGame.TOUCH) { - curX = (int) (player.pos.x + player.texWidth / 2) / 16; - if (player.dir == 0) curX--; + curX = player.getMapX(); + if (player.getDir() == 0) curX--; else curX++; - curY = (int) (player.pos.y + player.texWidth) / 16; + curY = (int) (player.pos.y + player.getWidth()) / 16; if (!isAutoselectable(curX, curY)) { curY++; } @@ -95,7 +95,7 @@ public class GameProc implements Serializable { curY++; } if (!isAutoselectable(curX, curY)) { - if (player.dir == 0) curX++; + if (player.getDir() == 0) curX++; else curX--; } } else if (!CaveGame.TOUCH) { @@ -114,10 +114,10 @@ public class GameProc implements Serializable { if (curY < 0) curY = 0; if (curY >= world.getHeight()) curY = world.getHeight() - 1; if (ctrlMode == 1) { - if (curX * 16 + 8 < player.pos.x + player.texWidth / 2) - player.dir = 0; - if (curX * 16 + 8 > player.pos.x + player.texWidth / 2) - player.dir = 1; + if (curX * 16 + 8 < player.pos.x + player.getWidth() / 2) + player.setDir(0); + if (curX * 16 + 8 > player.pos.x + player.getWidth() / 2) + player.setDir(1); } } @@ -137,15 +137,15 @@ public class GameProc implements Serializable { if (world.getForeMap(x, y) == 8 || world.getForeMap(x, y) == 60) { if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).coll && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { + (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { world.setForeMap(x, y + 1, 60); updateBlock(x, y + 2); } else if (GameItems.isLava(world.getForeMap(x, y + 1))) { if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4); else world.setForeMap(x, y + 1, 68); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { + } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).coll && !GameItems.isFluid(world.getForeMap(x + 1, y))) || + (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y))) || (GameItems.isWater(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 61)) { world.setForeMap(x + 1, y, 61); updateBlock(x + 1, y + 1); @@ -156,7 +156,7 @@ public class GameProc implements Serializable { world.setForeMap(x + 1, y, 8); if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).coll && !GameItems.isFluid(world.getForeMap(x - 1, y))) || + (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y))) || (GameItems.isWater(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 61)) { world.setForeMap(x - 1, y, 61); updateBlock(x - 1, y + 1); @@ -170,15 +170,15 @@ public class GameProc implements Serializable { } if (world.getForeMap(x, y) == 61) { if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).coll && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { + (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { world.setForeMap(x, y + 1, 60); updateBlock(x, y + 2); } else if (GameItems.isLava(world.getForeMap(x, y + 1))) { if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4); else world.setForeMap(x, y + 1, 68); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { + } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).coll && !GameItems.isFluid(world.getForeMap(x + 1, y))) || + (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y))) || (GameItems.isWater(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 62)) { world.setForeMap(x + 1, y, 62); updateBlock(x + 1, y + 1); @@ -188,7 +188,7 @@ public class GameProc implements Serializable { } if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).coll && !GameItems.isFluid(world.getForeMap(x - 1, y))) || + (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y))) || (GameItems.isWater(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 62)) { world.setForeMap(x - 1, y, 62); updateBlock(x - 1, y + 1); @@ -201,15 +201,15 @@ public class GameProc implements Serializable { } if (world.getForeMap(x, y) == 62) { if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).coll && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { + (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { world.setForeMap(x, y + 1, 60); updateBlock(x, y + 2); } else if (GameItems.isLava(world.getForeMap(x, y + 1))) { if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4); else world.setForeMap(x, y + 1, 68); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { + } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).coll && !GameItems.isFluid(world.getForeMap(x + 1, y)))) { + (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y)))) { world.setForeMap(x + 1, y, 63); updateBlock(x + 1, y + 1); } else if (GameItems.isLava(world.getForeMap(x + 1, y))) { @@ -218,7 +218,7 @@ public class GameProc implements Serializable { } if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).coll && !GameItems.isFluid(world.getForeMap(x - 1, y)))) { + (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y)))) { world.setForeMap(x - 1, y, 63); updateBlock(x - 1, y + 1); } else if (GameItems.isLava(world.getForeMap(x - 1, y))) { @@ -230,7 +230,7 @@ public class GameProc implements Serializable { } if (world.getForeMap(x, y) == 63) { if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).coll && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { + (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { world.setForeMap(x, y + 1, 60); updateBlock(x, y + 2); } else if (GameItems.isLava(world.getForeMap(x, y + 1))) { @@ -256,14 +256,14 @@ public class GameProc implements Serializable { if (world.getForeMap(x, y) == 9 || world.getForeMap(x, y) == 64) { if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).coll && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { + (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { world.setForeMap(x, y + 1, 64); updateBlock(x, y + 2); } else if (GameItems.isWater(world.getForeMap(x, y + 1))) { world.setForeMap(x, y + 1, 1); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { + } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).coll && !GameItems.isFluid(world.getForeMap(x + 1, y))) || + (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y))) || (GameItems.isLava(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 65)) { world.setForeMap(x + 1, y, 65); updateBlock(x + 1, y + 1); @@ -272,7 +272,7 @@ public class GameProc implements Serializable { } if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).coll && !GameItems.isFluid(world.getForeMap(x - 1, y))) || + (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y))) || (GameItems.isLava(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 65)) { world.setForeMap(x - 1, y, 65); updateBlock(x - 1, y + 1); @@ -284,14 +284,14 @@ public class GameProc implements Serializable { } if (world.getForeMap(x, y) == 65) { if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).coll && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { + (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { world.setForeMap(x, y + 1, 64); updateBlock(x, y + 2); } else if (GameItems.isWater(world.getForeMap(x, y + 1))) { world.setForeMap(x, y + 1, 1); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { + } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).coll && !GameItems.isFluid(world.getForeMap(x + 1, y))) || + (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y))) || (GameItems.isLava(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 66)) { world.setForeMap(x + 1, y, 66); updateBlock(x + 1, y + 1); @@ -300,7 +300,7 @@ public class GameProc implements Serializable { } if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).coll && !GameItems.isFluid(world.getForeMap(x - 1, y))) || + (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y))) || (GameItems.isLava(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 66)) { world.setForeMap(x - 1, y, 66); updateBlock(x - 1, y + 1); @@ -312,14 +312,14 @@ public class GameProc implements Serializable { } if (world.getForeMap(x, y) == 66) { if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).coll && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { + (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { world.setForeMap(x, y + 1, 64); updateBlock(x, y + 2); } else if (GameItems.isWater(world.getForeMap(x, y + 1))) { world.setForeMap(x, y + 1, 1); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { + } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).coll && !GameItems.isFluid(world.getForeMap(x + 1, y)))) { + (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y)))) { world.setForeMap(x + 1, y, 67); updateBlock(x + 1, y + 1); } else if (GameItems.isWater(world.getForeMap(x + 1, y))) { @@ -327,7 +327,7 @@ public class GameProc implements Serializable { } if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).coll && !GameItems.isFluid(world.getForeMap(x - 1, y)))) { + (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y)))) { world.setForeMap(x - 1, y, 67); updateBlock(x - 1, y + 1); } else if (GameItems.isWater(world.getForeMap(x - 1, y))) { @@ -338,7 +338,7 @@ public class GameProc implements Serializable { } if (world.getForeMap(x, y) == 67) { if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).coll && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { + (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { world.setForeMap(x, y + 1, 64); updateBlock(x, y + 2); } else if (GameItems.isWater(world.getForeMap(x, y + 1))) { @@ -350,7 +350,7 @@ public class GameProc implements Serializable { private void updateBlock(int x, int y) { if (world.getForeMap(x, y) == 10) { - if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { + if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { world.setForeMap(x, y, 0); mobs.add(new FallingSand(x * 16, y * 16)); updateBlock(x, y - 1); @@ -358,22 +358,22 @@ public class GameProc implements Serializable { } if (world.getForeMap(x, y) == 11) { - if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { + if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { world.setForeMap(x, y, 0); mobs.add(new FallingGravel(x * 16, y * 16)); updateBlock(x, y - 1); } } - if (world.getForeMap(x, y) > 0 && GameItems.getBlock(world.getForeMap(x, y)).rb) { - if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { + if (world.getForeMap(x, y) > 0 && GameItems.getBlock(world.getForeMap(x, y)).requiresBlock()) { + if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { world.destroyForeMap(x, y, this); updateBlock(x, y - 1); } } if (world.getForeMap(x, y) == 2) { - if (world.getForeMap(x, y - 1) > 0 && (GameItems.getBlock(world.getForeMap(x, y - 1)).coll || + if (world.getForeMap(x, y - 1) > 0 && (GameItems.getBlock(world.getForeMap(x, y - 1)).hasCollision() || GameItems.isFluid(world.getForeMap(x, y - 1)))) { world.setForeMap(x, y, 3); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 5560bf6..dc91d62 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -1,7 +1,9 @@ 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; @@ -15,7 +17,7 @@ public class GameRenderer extends Renderer { private GameProc gp; - public GameRenderer(GameProc gp, float width, float heigth) { + GameRenderer(GameProc gp, float width, float heigth) { super(width, heigth); Gdx.gl.glClearColor(0f, .6f, .6f, 1f); this.gp = gp; @@ -48,7 +50,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)).tp) + 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()], @@ -58,7 +60,7 @@ public class GameRenderer extends Renderer { 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)).bg) { + 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)); @@ -78,7 +80,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)).bg) { + 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)); @@ -90,19 +92,24 @@ public class GameRenderer extends Renderer { } private void drawMob(Mob mob) { - mob.draw(spriter, - mob.pos.x - getCamX() - gp.world.getWidth() * 16, mob.pos.y - getCamY()); - mob.draw(spriter, - mob.pos.x - getCamX(), mob.pos.y - getCamY()); - mob.draw(spriter, - mob.pos.x - getCamX() + gp.world.getWidth() * 16, mob.pos.y - getCamY()); + float mobDrawX = mob.pos.x - getCamX(); + float mobDrawY = mob.pos.y - getCamY(); + + if (mobDrawX + mob.getWidth() - gp.world.getWidthPx() >= 0 && mobDrawX - gp.world.getWidthPx() <= getWidth()) + mob.draw(spriter, mobDrawX - gp.world.getWidthPx(), mobDrawY); + + if (mobDrawX + mob.getWidth() >= 0 && mobDrawX <= getWidth()) + mob.draw(spriter, mobDrawX, mobDrawY); + + if (mobDrawX + mob.getWidth() + gp.world.getWidthPx() >= 0 && mobDrawX + gp.world.getWidthPx() <= getWidth()) + mob.draw(spriter, mobDrawX + gp.world.getWidthPx(), mobDrawY); } private void drawDrop(Drop drop) { switch (GameItems.getItem(drop.getId()).getType()) { case 0: Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition( - drop.pos.x - getCamX() - gp.world.getWidth() * 16, + drop.pos.x - getCamX() - gp.world.getWidthPx(), drop.pos.y - getCamY()); Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].draw(spriter); Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition( @@ -110,14 +117,18 @@ public class GameRenderer extends Renderer { drop.pos.y - getCamY()); Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].draw(spriter); Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition( - drop.pos.x - getCamX() + gp.world.getWidth() * 16, + drop.pos.x - getCamX() + gp.world.getWidthPx(), drop.pos.y - getCamY()); Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].draw(spriter); } } private void drawPlayer(Player pl) { - if (pl.move.x != 0 || Assets.plSprite[0][2].getRotation() != 0) { + + 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); @@ -132,62 +143,52 @@ public class GameRenderer extends Renderer { Player.ANIM_SPEED = -Player.ANIM_SPEED; //back hand - Assets.plSprite[1][2].setPosition( - pl.pos.x - getCamX() - 6, - pl.pos.y - getCamY()); + Assets.plSprite[1][2].setPosition(drawX - 6, drawY); Assets.plSprite[1][2].draw(spriter); //back leg - Assets.plSprite[1][3].setPosition( - pl.pos.x - getCamX() - 6, - pl.pos.y - getCamY() + 10); + Assets.plSprite[1][3].setPosition(drawX - 6, drawY + 10); Assets.plSprite[1][3].draw(spriter); //front leg - Assets.plSprite[0][3].setPosition( - pl.pos.x - getCamX() - 6, - pl.pos.y - getCamY() + 10); + Assets.plSprite[0][3].setPosition(drawX - 6, drawY + 10); Assets.plSprite[0][3].draw(spriter); //head - spriter.draw(Assets.plSprite[pl.dir][0], - pl.pos.x - getCamX() - 2, - pl.pos.y - getCamY() - 2); + spriter.draw(Assets.plSprite[pl.getDir()][0], drawX - 2, drawY - 2); //body - spriter.draw(Assets.plSprite[pl.dir][1], - pl.pos.x - getCamX() - 2, pl.pos.y - getCamY() + 8); + 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( - pl.pos.x - getCamX() - 8 * MathUtils.sin(handRotation), - pl.pos.y - getCamY() + 6 + 8 * MathUtils.cos(handRotation)); + 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.dir == 0), false); + 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.dir * 90 + Assets.plSprite[0][2].getRotation()); + -45 + pl.getDir() * 90 + Assets.plSprite[0][2].getRotation()); Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].setPosition( - pl.pos.x - getCamX() - 10 + (12 * pl.dir) - 8 * MathUtils.sin(handRotation), - pl.pos.y - getCamY() + 2 + 8 * MathUtils.cos(handRotation)); + 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.dir == 0), false); + Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].flip((pl.getDir() == 0), false); break; } } //front hand - Assets.plSprite[0][2].setPosition( - pl.pos.x - getCamX() - 6, - pl.pos.y - getCamY()); + Assets.plSprite[0][2].setPosition(drawX - 6, drawY); Assets.plSprite[0][2].draw(spriter); } + @SuppressWarnings("IntegerDivisionInFloatingPointContext") private void drawCreative() { - float x = getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2; - float y = getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2; + float x = getWidth() / 2 - (float) Assets.creativeInv.getRegionWidth() / 2; + float y = getHeight() / 2 - (float) Assets.creativeInv.getRegionHeight() / 2; spriter.draw(Assets.creativeInv, x, y); spriter.draw(Assets.creativeScr, x + 156, - y + 18 + (gp.creativeScroll * (72 / gp.maxCreativeScroll))); + y + 18 + (gp.creativeScroll * (72f / gp.maxCreativeScroll))); for (int i = gp.creativeScroll * 8; i < gp.creativeScroll * 8 + 40; i++) { if (i > 0 && i < GameItems.getItemsSize()) switch (GameItems.getItem(i).getType()) { @@ -292,12 +293,19 @@ public class GameRenderer extends Renderer { drawString("FPS: " + GameScreen.FPS, 0, 0); drawString("X: " + (int) (gp.player.pos.x / 16), 0, 10); drawString("Y: " + (int) (gp.player.pos.y / 16), 0, 20); - drawString("Mobs: " + gp.mobs.size(), 0, 30); - drawString("Drops: " + gp.drops.size(), 0, 40); - drawString("Block: " + GameItems.getBlockKey(gp.world.getForeMap(gp.curX, gp.curY)), 0, 50); - drawString("Game mode: " + gp.player.gameMode, 0, 60); + drawString("CurX: " + gp.curX, 0, 30); + drawString("CurY: " + gp.curY, 0, 40); + drawString("Mobs: " + gp.mobs.size(), 0, 50); + drawString("Drops: " + gp.drops.size(), 0, 60); + drawString("Block: " + GameItems.getBlockKey(gp.world.getForeMap(gp.curX, gp.curY)), 0, 70); + drawString("Game mode: " + gp.player.gameMode, 0, 80); } spriter.end(); + + shaper.begin(ShapeRenderer.ShapeType.Line); + shaper.setColor(Color.BLACK); + shaper.rect(gp.player.pos.x - getCamX(), gp.player.pos.y - getCamY(), gp.player.getWidth(), gp.player.getHeight()); + shaper.end(); } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java index a58a73b..25d3660 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java @@ -16,6 +16,14 @@ public class GameWorld { return HEIGHT; } + public float getWidthPx() { + return WIDTH * 16f; + } + + public float getHeightPx() { + return HEIGHT * 16f; + } + public int[][] getFullForeMap() { return foreMap; } @@ -94,7 +102,7 @@ public class GameWorld { } public void placeToForeground(int x, int y, int value) { - if (getForeMap(x, y) == 0 || value == 0 || !GameItems.getBlock(getForeMap(x, y)).coll) { + if (getForeMap(x, y) == 0 || value == 0 || !GameItems.getBlock(getForeMap(x, y)).hasCollision()) { setForeMap(x, y, value); } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) { placeSlab(x, y, value); @@ -105,8 +113,8 @@ public class GameWorld { } public void placeToBackground(int x, int y, int value) { - if (value == 0 || (getBackMap(x, y) == 0 && GameItems.getBlock(value).coll) && - (!GameItems.getBlock(value).tp || value == 18)) { + if (value == 0 || (getBackMap(x, y) == 0 && GameItems.getBlock(value).hasCollision()) && + (!GameItems.getBlock(value).isTransparent() || value == 18)) { setBackMap(x, y, value); } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/FallingGravel.java index cc3c389..23515f4 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/FallingGravel.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/FallingGravel.java @@ -1,21 +1,15 @@ 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; import ru.deadsoftware.cavecraft.game.GameItems; import ru.deadsoftware.cavecraft.misc.Assets; public class FallingGravel extends Mob { - public FallingGravel(int x, int y) { - dir = 0; - pos = new Vector2(x, y); - move = new Vector2(0, 1); - width = 16; - height = 16; - canJump = false; - dead = false; + public FallingGravel(float x, float y) { + super(x, y, 16, 16, 0); + mov = new Vector2(0, 1); } @Override @@ -31,11 +25,6 @@ public class FallingGravel extends Mob { spriteBatch.draw(Assets.blockTex[GameItems.getBlock(11).getTex()], x, y); } - @Override - public Rectangle getRect() { - return new Rectangle(pos.x, pos.y, width, height); - } - @Override public int getType() { return 11; diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/FallingSand.java index b910884..aa12a87 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/FallingSand.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/FallingSand.java @@ -1,21 +1,15 @@ 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; import ru.deadsoftware.cavecraft.game.GameItems; import ru.deadsoftware.cavecraft.misc.Assets; public class FallingSand extends Mob { - public FallingSand(int x, int y) { - dir = 0; - pos = new Vector2(x, y); - move = new Vector2(0, 1); - width = 16; - height = 16; - canJump = false; - dead = false; + public FallingSand(float x, float y) { + super(x, y, 16, 16, 0); + mov = new Vector2(0, 1); } @Override @@ -31,11 +25,6 @@ public class FallingSand extends Mob { spriteBatch.draw(Assets.blockTex[GameItems.getBlock(10).getTex()], x, y); } - @Override - public Rectangle getRect() { - return new Rectangle(pos.x, pos.y, width, height); - } - @Override public int getType() { return 10; diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java index 67df9f7..241f018 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java @@ -8,12 +8,60 @@ import java.io.Serializable; public abstract class Mob implements Serializable { - public int ANIM_SPEED = 6; + protected int anim, animSpeed = 6; + private float width, height; + private int dir; + public Vector2 pos; - public Vector2 move; - public int width, height, dir, anim; + public Vector2 mov; + private boolean dead; + public boolean canJump; - public boolean dead; + + protected Mob(float x, float y, float width, float height, int dir) { + pos = new Vector2(x, y); + this.width = width; + this.height = height; + canJump = false; + dead = false; + this.dir = dir; + } + + public int getMapX() { + return (int) (pos.x + (getWidth() / 2)) / 16; + } + + public int getMapY() { + return (int) (pos.y + (getHeight() / 2)) / 16; + } + + public float getWidth() { + return width; + } + + public float getHeight() { + return height; + } + + public int getDir() { + return dir; + } + + protected void switchDir() { + dir = -dir + 1; + } + + public boolean isDead() { + return dead; + } + + public void kill() { + dead = true; + } + + public Rectangle getRect() { + return new Rectangle(pos.x, pos.y, getWidth(), getHeight()); + } public abstract void ai(); @@ -21,7 +69,5 @@ public abstract class Mob implements Serializable { public abstract void draw(SpriteBatch spriteBatch, float x, float y); - public abstract Rectangle getRect(); - public abstract int getType(); //0 - mob, 10 - sand, 11 - gravel } diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java index 38eac46..aa3f797 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java @@ -2,39 +2,33 @@ package ru.deadsoftware.cavecraft.game.mobs; 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.misc.Assets; public class Pig extends Mob { - public Pig(int x, int y) { - dir = MathUtils.random(1); - pos = new Vector2(x, y); - move = new Vector2(-1 + dir * 2, 0); - width = 25; - height = 18; - canJump = false; - dead = false; + public Pig(float x, float y) { + super(x, y, 25, 18, MathUtils.random(1)); + mov = new Vector2(-1 + getDir() * 2, 0); } @Override public void changeDir() { - dir = -dir + 1; - move.x = -1 + 2 * dir; + switchDir(); + mov.x = -1 + 2 * getDir(); } @Override public void ai() { if (MathUtils.randomBoolean(.0025f)) changeDir(); else if (MathUtils.randomBoolean(.0025f)) { - if (move.x != 0f) move.x = 0; - else move.x = -1 + 2 * dir; + if (mov.x != 0f) mov.x = 0; + else mov.x = -1 + 2 * getDir(); } - if (move.x != 0f) anim += ANIM_SPEED; + if (mov.x != 0f) anim += animSpeed; else anim = 0; if (anim >= 60 || anim <= -60) { - ANIM_SPEED = -ANIM_SPEED; + animSpeed = -animSpeed; } } @@ -43,22 +37,17 @@ public class Pig extends Mob { Assets.pigSprite[0][1].setRotation(anim); Assets.pigSprite[1][1].setRotation(-anim); //back legs - Assets.pigSprite[1][1].setPosition(x - 4 + (9 - dir * 9), y + 6); + Assets.pigSprite[1][1].setPosition(x - 4 + (9 - getDir() * 9), y + 6); Assets.pigSprite[1][1].draw(spriteBatch); - Assets.pigSprite[1][1].setPosition(x + 17 - (9 * dir), y + 6); + Assets.pigSprite[1][1].setPosition(x + 17 - (9 * getDir()), y + 6); Assets.pigSprite[1][1].draw(spriteBatch); //front legs - Assets.pigSprite[0][1].setPosition(x - 4 + (9 - dir * 9), y + 6); + Assets.pigSprite[0][1].setPosition(x - 4 + (9 - getDir() * 9), y + 6); Assets.pigSprite[0][1].draw(spriteBatch); - Assets.pigSprite[0][1].setPosition(x + 17 - (9 * dir), y + 6); + Assets.pigSprite[0][1].setPosition(x + 17 - (9 * getDir()), y + 6); Assets.pigSprite[0][1].draw(spriteBatch); //head & body - spriteBatch.draw(Assets.pigSprite[dir][0], x, y); - } - - @Override - public Rectangle getRect() { - return new Rectangle(pos.x, pos.y, width, height); + spriteBatch.draw(Assets.pigSprite[getDir()][0], x, y); } @Override diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Block.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Block.java index 8d27235..5871485 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Block.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Block.java @@ -8,7 +8,8 @@ public class Block { private int tex; private int hp, drop; - public boolean coll, bg, tp, rb; + //coll - collision, bg - background, tp - transparent, rb - requires block under it + private boolean coll, bg, tp, rb; public Block(int tex, int hp, int drop) { this(0, 0, 16, 16, tex, hp, drop, true, false, false, false); @@ -40,6 +41,22 @@ public class Block { this.rb = rb; } + public boolean hasCollision() { + return coll; + } + + public boolean isBackground() { + return bg; + } + + public boolean isTransparent() { + return tp; + } + + public boolean requiresBlock() { + return rb; + } + public int getTex() { return tex; } diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java index 271be8c..d2139c7 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java @@ -8,6 +8,7 @@ import ru.deadsoftware.cavecraft.game.GameProc; import java.io.Serializable; public class Drop implements Serializable { + private int id; public boolean pickedUp = false; public Vector2 move, pos; @@ -19,9 +20,9 @@ public class Drop implements Serializable { } public int closeToPlayer(GameProc gp) { - boolean c1 = Intersector.overlaps(new Rectangle(gp.player.pos.x - 16, gp.player.pos.y - 16, gp.player.texWidth + 32, gp.player.height + 32), getRect()); - boolean c2 = Intersector.overlaps(new Rectangle((gp.player.pos.x + gp.world.getWidth() * 16) - 16, gp.player.pos.y - 16, gp.player.texWidth + 32, gp.player.height + 32), getRect()); - boolean c3 = Intersector.overlaps(new Rectangle((gp.player.pos.x - gp.world.getWidth() * 16) - 16, gp.player.pos.y - 16, gp.player.texWidth + 32, gp.player.height + 32), getRect()); + boolean c1 = Intersector.overlaps(new Rectangle(gp.player.pos.x - 16, gp.player.pos.y - 16, gp.player.getWidth() + 32, gp.player.getHeight() + 32), getRect()); + boolean c2 = Intersector.overlaps(new Rectangle((gp.player.pos.x + gp.world.getWidthPx()) - 16, gp.player.pos.y - 16, gp.player.getWidth() + 32, gp.player.getHeight() + 32), getRect()); + boolean c3 = Intersector.overlaps(new Rectangle((gp.player.pos.x - gp.world.getWidthPx()) - 16, gp.player.pos.y - 16, gp.player.getWidth() + 32, gp.player.getHeight() + 32), getRect()); if (c1) return 1; if (c2) return 2; if (c3) return 3; @@ -35,16 +36,16 @@ public class Drop implements Serializable { float py = gp.player.pos.y; switch (ctp) { case 2: - px += gp.world.getWidth() * 16; + px += gp.world.getWidthPx(); break; case 3: - px -= gp.world.getWidth() * 16; + px -= gp.world.getWidthPx(); break; } float dx = 0, dy = 0; - if (px + gp.player.texWidth < pos.x + 4) dx = -.5f; + if (px + gp.player.getWidth() < pos.x + 4) dx = -.5f; else if (px > pos.x + 4) dx = .5f; - if (py + gp.player.height < pos.y + 4) dy = -.5f; + if (py + gp.player.getHeight() < pos.y + 4) dy = -.5f; else if (py > pos.y + 4) dy = .5f; move.add(dx, dy); if (move.x > 2) move.x = 1; diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java index 11838d1..5672807 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java @@ -12,8 +12,8 @@ public class Player implements Serializable { public static int ANIM_SPEED = 6; public Vector2 pos; - public Vector2 move; - public int width, height, dir, texWidth, hp; + public Vector2 mov; + private int width, height, dir, hp; public boolean canJump; public int[] inv; public boolean flyMode = false; @@ -21,18 +21,17 @@ public class Player implements Serializable { public Player(GameWorld world, int gameMode) { this.gameMode = gameMode; - pos = getSpawnPoint(world).cpy(); - move = new Vector2(0, 0); + mov = new Vector2(0, 0); width = 4; height = 30; - texWidth = 8; inv = new int[9]; hp = 20; + pos = getSpawnPoint(world).cpy(); } public void respawn(GameWorld world) { pos.set(getSpawnPoint(world)); - move.setZero(); + mov.setZero(); hp = 20; } @@ -44,15 +43,45 @@ public class Player implements Serializable { world.setForeMap(x, y, 1); break; } - if (world.getForeMap(x, y) > 0 && GameItems.getBlock(world.getForeMap(x, y)).coll) break; + if (world.getForeMap(x, y) > 0 && GameItems.getBlock(world.getForeMap(x, y)).hasCollision()) break; } - x = x * 16 + texWidth / 2; - y = y * 16 - height; - return new Vector2(x, y); + 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 int getHp() { + return hp; + } + + public void setHp(int hp) { + this.hp = hp; + } + + public int getDir() { + return dir; + } + + public void setDir(int dir) { + this.dir = dir; } public Rectangle getRect() { - return new Rectangle(pos.x + 2, pos.y, width, height); + return new Rectangle(pos.x, pos.y, getWidth(), getHeight()); } } diff --git a/core/src/ru/deadsoftware/cavecraft/menu/MenuRenderer.java b/core/src/ru/deadsoftware/cavecraft/menu/MenuRenderer.java index 8aaab64..7a660e7 100644 --- a/core/src/ru/deadsoftware/cavecraft/menu/MenuRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/menu/MenuRenderer.java @@ -57,11 +57,12 @@ public class MenuRenderer extends Renderer { (button.getY() + button.getHeight() / 2) - Assets.getStringHeight(button.getLabel()) / 2); } - private void drawMenuMain() { - for (Button button : menuMainBtns) { + private void drawButtons(Array