From 84377178320105196ad70bfa798ba8f299f961b2 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Sat, 21 Dec 2019 19:14:18 +0700 Subject: [PATCH] Reimplement mobs --- android/build.gradle | 2 +- .../cavedroid/game/GameInput.java | 26 +++--- .../cavedroid/game/GamePhysics.java | 84 +++++++++---------- .../deadsoftware/cavedroid/game/GameProc.java | 2 +- .../cavedroid/game/GameRenderer.java | 34 ++++---- .../cavedroid/game/mobs/FallingGravel.java | 3 +- .../cavedroid/game/mobs/FallingSand.java | 3 +- .../deadsoftware/cavedroid/game/mobs/Mob.java | 55 ++++++++++-- .../deadsoftware/cavedroid/game/mobs/Pig.java | 42 ++++++---- .../game/{objects => mobs}/Player.java | 2 +- .../cavedroid/game/objects/Drop.java | 60 +++++++++---- 11 files changed, 192 insertions(+), 121 deletions(-) rename core/src/ru/deadsoftware/cavedroid/game/{objects => mobs}/Player.java (98%) diff --git a/android/build.gradle b/android/build.gradle index b8cfebe..a5ed087 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -24,7 +24,7 @@ android { } defaultConfig { applicationId "ru.deadsoftware.cavedroid" - minSdkVersion 14 + minSdkVersion 24 targetSdkVersion 29 versionCode 10 versionName "alpha0.4" diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 58d3778..2fc38b7 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -36,13 +36,13 @@ public class GameInput { private void goUpwards() { if (checkSwim()) { GP.player.swim = true; - } else if (GP.player.canJump) { - GP.player.mov.add(0, -7); - } else if (!GP.player.flyMode && GP.player.gameMode == 1) { - GP.player.flyMode = true; - GP.player.mov.y = 0; - } else if (GP.player.flyMode) { - GP.player.mov.y = -GamePhysics.PL_SPEED; + } else if (GP.player.canJump()) { + GP.player.getMov().add(0, -7); + } else if (!GP.player.isFlyMode() && GP.player.gameMode == 1) { + GP.player.setFlyMode(true); + GP.player.getMov().y = 0; + } else if (GP.player.isFlyMode()) { + GP.player.getMov().y = -GamePhysics.PL_SPEED; } } @@ -59,12 +59,12 @@ public class GameInput { if (GP.controlMode == ControlMode.WALK || !CaveGame.TOUCH) { switch (keycode) { case Input.Keys.A: - GP.player.mov.x = -GamePhysics.PL_SPEED; + GP.player.getMov().x = -GamePhysics.PL_SPEED; GP.player.setDir(Mob.LEFT); if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true; break; case Input.Keys.D: - GP.player.mov.x = GamePhysics.PL_SPEED; + GP.player.getMov().x = GamePhysics.PL_SPEED; GP.player.setDir(Mob.RIGHT); if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true; break; @@ -74,7 +74,7 @@ public class GameInput { break; case Input.Keys.S: case Input.Keys.CONTROL_LEFT: - GP.player.mov.y = GamePhysics.PL_SPEED; + GP.player.getMov().y = GamePhysics.PL_SPEED; break; } } else { @@ -108,7 +108,7 @@ public class GameInput { } if (GP.controlMode == ControlMode.CURSOR) { - if (curX * 16 + 8 < GP.player.pos.x + GP.player.getWidth() / 2) { + if (curX * 16 + 8 < GP.player.getX() + GP.player.getWidth() / 2) { GP.player.setDir(Mob.LEFT); } else { GP.player.setDir(Mob.RIGHT); @@ -275,7 +275,7 @@ public class GameInput { switch (keycode) { case Input.Keys.A: case Input.Keys.D: - GP.player.mov.x = 0; + GP.player.getMov().x = 0; if (CaveGame.TOUCH && GP.player.swim) GP.player.swim = false; break; @@ -283,7 +283,7 @@ public class GameInput { case Input.Keys.S: case Input.Keys.SPACE: case Input.Keys.CONTROL_LEFT: - if (GP.player.flyMode) GP.player.mov.y = 0; + if (GP.player.isFlyMode()) GP.player.getMov().y = 0; if (GP.player.swim) GP.player.swim = false; break; } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 7e12e53..2d2e4a4 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -7,7 +7,7 @@ import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavedroid.CaveGame; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.objects.Drop; -import ru.deadsoftware.cavedroid.game.objects.Player; +import ru.deadsoftware.cavedroid.game.mobs.Player; import java.util.Iterator; @@ -17,7 +17,7 @@ class GamePhysics { static final int PL_SPEED = 2; - private final Vector2 gravity = new Vector2(0, .9f); + private final Vector2 gravity = new Vector2(0, .9f); private boolean checkJump(Rectangle rect, int dir) { int bl; @@ -77,90 +77,90 @@ class GamePhysics { private void mobXColl(Mob mob) { if (checkColl(mob.getRect())) { - if (mob.canJump && !mob.flyMode) { - mob.pos.y -= 8; + if (mob.canJump() && !mob.isFlyMode()) { + mob.getPos().y -= 8; } if (checkColl(mob.getRect())) { - if (mob.canJump && !mob.flyMode) mob.pos.y += 8; + if (mob.canJump() && !mob.isFlyMode()) mob.getPos().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.getMov().x < 0) d = 1; + else if (mob.getMov().x > 0) d = -1; + mob.getPos().x = MathUtils.round(mob.getX()); + while (checkColl(mob.getRect())) mob.getPos().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.getX() + mob.getWidth() / 2 < 0) mob.getPos().x += GP.world.getWidthPx(); + if (mob.getX() + mob.getWidth() / 2 > GP.world.getWidthPx()) mob.getPos().x -= GP.world.getWidthPx(); } private void mobYColl(Mob mob) { if (checkColl(mob.getRect())) { int d = -1; - if (mob.mov.y < 0) d = 1; + if (mob.getMov().y < 0) d = 1; if (d == -1) { - mob.canJump = true; - mob.flyMode = false; + mob.setCanJump(true); + mob.setFlyMode(false); } - mob.pos.y = MathUtils.round(mob.pos.y); - while (checkColl(mob.getRect())) mob.pos.y += d; - mob.mov.y = 0; + mob.getPos().y = MathUtils.round(mob.getY()); + while (checkColl(mob.getRect())) mob.getPos().y += d; + mob.getMov().y = 0; if (mob.getType() > 0) { GP.world.setForeMap(mob.getMapX(), mob.getMiddleMapY(), mob.getType()); mob.kill(); } } else { - mob.canJump = false; + mob.setCanJump(false); } - if (mob.pos.y > GP.world.getHeightPx()) { + if (mob.getY() > GP.world.getHeightPx()) { mob.kill(); } } private void playerPhy(Player pl) { - pl.pos.y += pl.mov.y; + pl.getPos().y += pl.getMov().y; mobYColl(pl); if (pl.isDead()) return; if (GameItems.isFluid(getBlock(pl.getRect()))) { - if (CaveGame.TOUCH && pl.mov.x != 0 && !pl.swim && !pl.flyMode) pl.swim = true; + if (CaveGame.TOUCH && pl.getMov().x != 0 && !pl.swim && !pl.isFlyMode()) 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); + if (!pl.isFlyMode() && pl.getMov().y < 4.5f) pl.getMov().add(gravity.x / 4, gravity.y / 4); + if (!pl.isFlyMode() && pl.getMov().y > 4.5f) pl.getMov().add(0, -1f); } else { - pl.mov.add(0, -.5f); - if (pl.mov.y < -3) pl.mov.y = -3; + pl.getMov().add(0, -.5f); + if (pl.getMov().y < -3) pl.getMov().y = -3; } } else { - if (!pl.flyMode && pl.mov.y < 18) pl.mov.add(gravity); + if (!pl.isFlyMode() && pl.getMov().y < 18) pl.getMov().add(gravity); } - pl.pos.x += pl.mov.x * (pl.flyMode ? 1.5f : 1) * (GameItems.isFluid(getBlock(pl.getRect())) && !pl.flyMode ? .8f : 1); + pl.getPos().x += pl.getMov().x * (pl.isFlyMode() ? 1.5f : 1) * (GameItems.isFluid(getBlock(pl.getRect())) && !pl.isFlyMode() ? .8f : 1); mobXColl(pl); - if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDirection()) && !pl.flyMode && pl.canJump && pl.mov.x != 0) { - pl.mov.add(0, -8); - pl.canJump = false; + if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDirection()) && !pl.isFlyMode() && pl.canJump() && pl.getMov().x != 0) { + pl.getMov().add(0, -8); + pl.setCanJump(false); } } private void mobPhy(Mob mob) { - mob.pos.y += mob.mov.y; + mob.getPos().y += mob.getMov().y; mobYColl(mob); if (mob.isDead()) return; 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.flyMode && mob.mov.y < 18) mob.mov.add(gravity); + if (mob.getMov().y > 9) mob.getMov().add(0, -.9f); + mob.getMov().add(0, -.5f); + if (mob.getMov().y < -3) mob.getMov().y = -3; + } else if (!mob.isFlyMode() && mob.getMov().y < 18) mob.getMov().add(gravity); - mob.pos.x += mob.mov.x; + mob.getPos().x += mob.getMov().x; mobXColl(mob); - if (checkJump(mob.getRect(), mob.getDirection()) && mob.canJump && mob.mov.x != 0) { - mob.mov.add(0, -8); - mob.canJump = false; + if (checkJump(mob.getRect(), mob.getDirection()) && mob.canJump() && mob.getMov().x != 0) { + mob.getMov().add(0, -8); + mob.setCanJump(false); } } @@ -183,8 +183,8 @@ class GamePhysics { if (GP.player.isDead()) GP.player.respawn(); GP.renderer.setCamPos( - GP.player.pos.x + GP.player.getWidth() / 2 - GP.renderer.getWidth() / 2, - GP.player.pos.y + GP.player.getHeight() / 2 - GP.renderer.getHeight() / 2); + GP.player.getPos().x + GP.player.getWidth() / 2 - GP.renderer.getWidth() / 2, + GP.player.getPos().y + GP.player.getHeight() / 2 - GP.renderer.getHeight() / 2); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index 245f0dc..346d9c1 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -7,7 +7,7 @@ import ru.deadsoftware.cavedroid.game.mobs.FallingGravel; import ru.deadsoftware.cavedroid.game.mobs.FallingSand; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.objects.Drop; -import ru.deadsoftware.cavedroid.game.objects.Player; +import ru.deadsoftware.cavedroid.game.mobs.Player; import ru.deadsoftware.cavedroid.misc.ControlMode; import java.io.Serializable; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index 1f18589..e6ead52 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -9,13 +9,14 @@ import com.badlogic.gdx.math.Rectangle; import ru.deadsoftware.cavedroid.CaveGame; import ru.deadsoftware.cavedroid.GameScreen; import ru.deadsoftware.cavedroid.game.mobs.Mob; +import ru.deadsoftware.cavedroid.game.mobs.Pig; import ru.deadsoftware.cavedroid.game.objects.Drop; +import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.ControlMode; import ru.deadsoftware.cavedroid.misc.Renderer; import static ru.deadsoftware.cavedroid.GameScreen.GP; -import static ru.deadsoftware.cavedroid.misc.Assets.guiMap; -import static ru.deadsoftware.cavedroid.misc.Assets.textureRegions; +import static ru.deadsoftware.cavedroid.misc.Assets.*; public class GameRenderer extends Renderer { @@ -36,7 +37,8 @@ public class GameRenderer extends Renderer { if (GP.input.getBlockDamage() > 0) { int index = 10 * GP.input.getBlockDamage() / GameItems.getBlock(bl).getHp(); String key = "break_" + index; - spriter.draw(textureRegions.get(key), GP.input.getCurX() * 16 - getCamX(), GP.input.getCurY() * 16 - getCamY()); + spriter.draw(textureRegions.get(key), GP.input.getCurX() * 16 - getCamX(), + GP.input.getCurY() * 16 - getCamY()); } } @@ -88,17 +90,9 @@ public class GameRenderer extends Renderer { } private void drawMob(Mob mob) { - 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); + float mobDrawX = mob.getX() - getCamX(); + float mobDrawY = mob.getY() - getCamY(); + mob.draw(spriter, mobDrawX, mobDrawY); } private void drawDrop(Drop drop) { @@ -181,9 +175,9 @@ public class GameRenderer extends Renderer { private void drawGamePlay() { 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); + GP.player.draw(spriter, GP.player.getX() - getCamX() - 2, GP.player.getY() - getCamY()); + GP.mobs.forEach(this::drawMob); + GP.drops.forEach(this::drawDrop); drawWorld(false); drawGUI(); } @@ -231,7 +225,7 @@ public class GameRenderer extends Renderer { } } shaper.setColor(Color.OLIVE); - shaper.rect(GP.player.pos.x / 16, GP.player.pos.y / 16 - 128, 1, 2); + shaper.rect(GP.player.getMapX(), GP.player.getUpperMapY() - 128, 1, 2); shaper.end(); //================= } @@ -239,8 +233,8 @@ public class GameRenderer extends Renderer { if (GameScreen.SHOW_DEBUG) { spriter.begin(); 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("X: " + GP.player.getMapX(), 0, 10); + drawString("Y: " + GP.player.getUpperMapY() / 16, 0, 20); drawString("CurX: " + GP.input.getCurX(), 0, 30); drawString("CurY: " + GP.input.getCurY(), 0, 40); drawString("Mobs: " + GP.mobs.size(), 0, 50); diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java index 7b23d4d..850f8dd 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java @@ -2,6 +2,7 @@ package ru.deadsoftware.cavedroid.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; +import org.jetbrains.annotations.NotNull; import ru.deadsoftware.cavedroid.misc.Assets; /** @@ -29,7 +30,7 @@ public class FallingGravel extends Mob { } @Override - public void draw(SpriteBatch spriteBatch, float x, float y) { + public void draw(@NotNull SpriteBatch spriteBatch, float x, float y) { spriteBatch.draw(Assets.gravelSprite, x, y); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java index ac20ffb..d193dcf 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java @@ -2,6 +2,7 @@ package ru.deadsoftware.cavedroid.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; +import org.jetbrains.annotations.NotNull; import ru.deadsoftware.cavedroid.misc.Assets; /** @@ -29,7 +30,7 @@ public class FallingSand extends Mob { } @Override - public void draw(SpriteBatch spriteBatch, float x, float y) { + public void draw(@NotNull SpriteBatch spriteBatch, float x, float y) { spriteBatch.draw(Assets.sandSprite, x, y); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 32c63cc..716c3df 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -1,6 +1,7 @@ package ru.deadsoftware.cavedroid.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; @@ -18,15 +19,18 @@ public abstract class Mob implements Serializable { private final float height; private int dir; - public boolean flyMode; - public final Vector2 pos; - public Vector2 mov; + protected final Vector2 pos; + protected Vector2 mov; private boolean dead; + private boolean canJump, flyMode; protected int animDelta = 6; - public boolean canJump; - int anim; + protected int anim; + + protected static int randomDir() { + return MathUtils.random(1); + } /** * @@ -43,7 +47,6 @@ public abstract class Mob implements Serializable { this.width = width; this.height = height; canJump = false; - flyMode = false; dead = false; this.dir = dir; } @@ -115,6 +118,10 @@ public abstract class Mob implements Serializable { return dead; } + public int getAnim() { + return anim; + } + /** * Set's mob's dead variable to true and nothing else. It doesn't delete the mob. */ @@ -130,12 +137,44 @@ public abstract class Mob implements Serializable { return new Rectangle(pos.x, pos.y, getWidth(), getHeight()); } - public abstract void ai(); + public Vector2 getPos() { + return pos; + } - public abstract void changeDir(); + public Vector2 getMov() { + return mov; + } + + public float getX() { + return pos.x; + } + + public float getY() { + return pos.y; + } + + public boolean canJump() { + return canJump; + } + + public void setCanJump(boolean canJump) { + this.canJump = canJump; + } + + public boolean isFlyMode() { + return flyMode; + } + + public void setFlyMode(boolean flyMode) { + this.flyMode = flyMode; + } public abstract void draw(SpriteBatch spriteBatch, float x, float y); + public abstract void ai(); + + public abstract void changeDir(); + /** * * @return 0 - if regular mob.
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java index e36da71..ecc9397 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java @@ -5,10 +5,13 @@ import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavedroid.misc.Assets; +import static ru.deadsoftware.cavedroid.GameScreen.GP; +import static ru.deadsoftware.cavedroid.misc.Assets.pigSprite; + public class Pig extends Mob { public Pig(float x, float y) { - super(x, y, 25, 18, MathUtils.random(1)); + super(x, y, 25, 18, Mob.randomDir()); mov = new Vector2(looksLeft() ? -1 : 1, 0); } @@ -32,27 +35,32 @@ public class Pig extends Mob { } } + @Override + public int getType() { + return 0; + } + @Override public void draw(SpriteBatch spriteBatch, float x, float y) { - Assets.pigSprite[0][1].setRotation(anim); - Assets.pigSprite[1][1].setRotation(-anim); + if (x + getWidth() - GP.world.getWidthPx() >= 0 && x - GP.world.getWidthPx() <= getWidth()) { + x -= GP.world.getWidthPx(); + } else if (x + getWidth() + GP.world.getWidthPx() >= 0 && x + GP.world.getWidthPx() <= getWidth()) { + x += GP.world.getWidthPx(); + } + + pigSprite[0][1].setRotation(getAnim()); + pigSprite[1][1].setRotation(-getAnim()); //back legs - Assets.pigSprite[1][1].setPosition(x - 4 + (9 - getDirection() * 9), y + 6); - Assets.pigSprite[1][1].draw(spriteBatch); - Assets.pigSprite[1][1].setPosition(x + 17 - (9 * getDirection()), y + 6); - Assets.pigSprite[1][1].draw(spriteBatch); + pigSprite[1][1].setPosition(x - 4 + (9 - getDirection() * 9), y + 6); + pigSprite[1][1].draw(spriteBatch); + pigSprite[1][1].setPosition(x + 17 - (9 * getDirection()), y + 6); + pigSprite[1][1].draw(spriteBatch); //front legs - Assets.pigSprite[0][1].setPosition(x - 4 + (9 - getDirection() * 9), y + 6); - Assets.pigSprite[0][1].draw(spriteBatch); - Assets.pigSprite[0][1].setPosition(x + 17 - (9 * getDirection()), y + 6); - Assets.pigSprite[0][1].draw(spriteBatch); + pigSprite[0][1].setPosition(x - 4 + (9 - getDirection() * 9), y + 6); + pigSprite[0][1].draw(spriteBatch); + pigSprite[0][1].setPosition(x + 17 - (9 * getDirection()), y + 6); + pigSprite[0][1].draw(spriteBatch); //head & body spriteBatch.draw(Assets.pigSprite[getDirection()][0], x, y); } - - @Override - public int getType() { - return 0; - } - } diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java similarity index 98% rename from core/src/ru/deadsoftware/cavedroid/game/objects/Player.java rename to core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 11863b7..df4e0c9 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -1,4 +1,4 @@ -package ru.deadsoftware.cavedroid.game.objects; +package ru.deadsoftware.cavedroid.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Rectangle; diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java index cda629d..44822b1 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java @@ -3,6 +3,7 @@ package ru.deadsoftware.cavedroid.game.objects; import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavedroid.game.mobs.Player; import java.io.Serializable; @@ -22,20 +23,31 @@ public class Drop implements Serializable { } public int closeToPlayer() { - 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; + boolean[] c = new boolean[3]; + + c[0] = Intersector.overlaps(new Rectangle(GP.player.getX() - 16, + GP.player.getY() - 16, GP.player.getWidth() + 32, GP.player.getHeight() + 32), + getRect()); + c[1] = Intersector.overlaps(new Rectangle((GP.player.getX() + GP.world.getWidthPx()) - 16, + GP.player.getY() - 16, GP.player.getWidth() + 32, GP.player.getHeight() + 32), + getRect()); + c[2] = Intersector.overlaps(new Rectangle((GP.player.getX() - GP.world.getWidthPx()) - 16, + GP.player.getY() - 16, GP.player.getWidth() + 32, GP.player.getHeight() + 32), + getRect()); + + for (int i = 0; i < 3; i++) { + if (c[i]) { + return i + 1; + } + } return 0; } public void moveToPlayer() { int ctp = closeToPlayer(); if (ctp > 0) { - float px = GP.player.pos.x; - float py = GP.player.pos.y; + float px = GP.player.getX(); + float py = GP.player.getY(); switch (ctp) { case 2: px += GP.world.getWidthPx(); @@ -45,15 +57,31 @@ public class Drop implements Serializable { break; } float dx = 0, dy = 0; - if (px + GP.player.getWidth() < pos.x + 4) dx = -.5f; - else if (px > pos.x + 4) dx = .5f; - if (py + GP.player.getHeight() < pos.y + 4) dy = -.5f; - else if (py > pos.y + 4) dy = .5f; + if (px + GP.player.getWidth() < pos.x + 4) { + dx = -.5f; + } else if (px > pos.x + 4) { + dx = .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; - if (move.x < -2) move.x = -1; - if (move.y > 2) move.y = 1; - if (move.y < -2) move.y = -1; + + if (move.x > 2) { + move.x = 1; + } else if (move.x < -2) { + move.x = -1; + } + + if (move.y > 2) { + move.y = 1; + } else if (move.y < -2) { + move.y = -1; + } } } -- 2.29.2