diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java
index 2d2e4a444563ebdf6997948021164484722e2766..ed31da7433799f44ed0544f8f8f5d5aedd9aea2d 100644 (file)
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
+import org.jetbrains.annotations.NotNull;
import ru.deadsoftware.cavedroid.CaveGame;
import ru.deadsoftware.cavedroid.game.mobs.Mob;
-import ru.deadsoftware.cavedroid.game.objects.Drop;
import ru.deadsoftware.cavedroid.game.mobs.Player;
+import ru.deadsoftware.cavedroid.game.objects.Drop;
import java.util.Iterator;
private final Vector2 gravity = new Vector2(0, .9f);
- private boolean checkJump(Rectangle rect, int dir) {
- int bl;
- int blX = (int) (rect.x + rect.width * dir - 8 + 16 * dir);
- int blY = (int) (rect.y + rect.height - 8);
+ /**
+ * Checks if mob should jump
+ *
+ * @return true if mob should jump
+ */
+ private boolean checkJump(@NotNull Mob mob) {
+ int dir = mob.looksLeft() ? 0 : 1;
+ int blX = (int) (mob.getX() + mob.getWidth() * dir - 8 + 16 * dir);
+ int blY = (int) (mob.getY() + mob.getHeight() - 8);
+ int block = GP.world.getForeMap(blX / 16, blY / 16);
- bl = GP.world.getForeMap(blX / 16, blY / 16);
- if (checkColl(new Rectangle(blX, rect.y - 18, rect.width, rect.height))) bl = 0;
+ if (checkColl(new Rectangle(blX, mob.getY() - 18, mob.getWidth(), mob.getHeight()))) {
+ block = 0;
+ }
- return (bl > 0 && GameItems.getBlock(bl).toJump() &&
- (rect.y + rect.height) - GameItems.getBlock(bl).getRect(blX / 16, blY / 16).y > 8);
+ return (block > 0 && GameItems.getBlock(block).toJump() &&
+ (mob.getY() + mob.getHeight()) - GameItems.getBlock(block).getRect(blX / 16, blY / 16).y > 8);
}
- private boolean checkColl(Rectangle rect) {
- int bl;
+ private boolean checkColl(@NotNull Rectangle rect) {
int minX = (int) ((rect.x + rect.width / 2) / 16) - 4;
int minY = (int) ((rect.y + rect.height / 2) / 16) - 4;
int maxX = (int) ((rect.x + rect.width / 2) / 16) + 4;
int maxY = (int) ((rect.y + rect.height / 2) / 16) + 4;
- if (minY < 0) minY = 0;
- if (maxY > GP.world.getHeight()) maxY = GP.world.getHeight();
+
+ if (minY < 0) {
+ minY = 0;
+ }
+
+ if (maxY > GP.world.getHeight()) {
+ maxY = GP.world.getHeight();
+ }
+
+ int block;
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).hasCollision()) {
- if (Intersector.overlaps(rect, GameItems.getBlock(bl).getRect(x, y))) {
+ block = GP.world.getForeMap(x, y);
+ if (block > 0 && GameItems.getBlock(block).hasCollision()) {
+ if (Intersector.overlaps(rect, GameItems.getBlock(block).getRect(x, y))) {
return true;
}
}
}
}
+
return false;
}
- private int getBlock(Rectangle rect) {
- return GP.world.getForeMap((int) (rect.x + rect.width / 2) / 16, (int) (rect.y + rect.height / 8 * 7) / 16);
+ private int getBlock(@NotNull Rectangle rect) {
+ return GP.world.getForeMap((int) (rect.x + rect.width / 2) / 16,
+ (int) (rect.y + rect.height / 8 * 7) / 16);
}
private void dropPhy(Drop drop) {
if (drop.closeToPlayer() > 0) {
drop.moveToPlayer();
} else {
- if (drop.move.x >= .5f) drop.move.x -= .5f;
- else if (drop.move.x <= -.5f) drop.move.x += .5f;
- else drop.move.x = 0;
- if (drop.move.y < 9) drop.move.y += gravity.y / 4;
- }
- drop.pos.add(drop.move);
- 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--;
- drop.move.y = 0;
+ if (drop.getMove().x >= .5f) {
+ drop.getMove().x -= .5f;
+ } else if (drop.getMove().x <= -.5f) {
+ drop.getMove().x += .5f;
+ } else {
+ drop.getMove().x = 0;
+ }
+ if (drop.getMove().y < 9) {
+ drop.getMove().y += gravity.y / 4;
+ }
+ }
+ drop.move();
+
+
+ if (checkColl(drop)) {
+ drop.getMove().set(0, -1);
+ do {
+ drop.move();
+ } while (checkColl(drop));
+ drop.getMove().setZero();
}
}
private void mobXColl(Mob mob) {
- if (checkColl(mob.getRect())) {
+ if (checkColl(mob)) {
if (mob.canJump() && !mob.isFlyMode()) {
- mob.getPos().y -= 8;
+ mob.y -= 8;
}
- if (checkColl(mob.getRect())) {
- if (mob.canJump() && !mob.isFlyMode()) mob.getPos().y += 8;
+
+ if (checkColl(mob)) {
+ if (mob.canJump() && !mob.isFlyMode()) {
+ mob.y += 8;
+ }
+
int d = 0;
- 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.getMove().x < 0) {
+ d = 1;
+ } else if (mob.getMove().x > 0) {
+ d = -1;
+ }
+
+ mob.x = MathUtils.round(mob.getX());
+
+ while (checkColl(mob)) {
+ mob.x += d;
+ }
+
+ if (mob.canJump()) {
+ mob.changeDir();
+ }
}
}
- 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();
+
+ mob.checkWorldBounds();
}
private void mobYColl(Mob mob) {
- if (checkColl(mob.getRect())) {
+ if (checkColl(mob)) {
int d = -1;
- if (mob.getMov().y < 0) d = 1;
+
+ if (mob.getMove().y < 0) {
+ d = 1;
+ }
+
if (d == -1) {
mob.setCanJump(true);
mob.setFlyMode(false);
}
- 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();
+
+ mob.y = MathUtils.round(mob.getY());
+
+ while (checkColl(mob)) {
+ mob.y += d;
}
+
+ mob.getMove().y = 0;
+
} else {
mob.setCanJump(false);
}
+
if (mob.getY() > GP.world.getHeightPx()) {
mob.kill();
}
}
- private void playerPhy(Player pl) {
- pl.getPos().y += pl.getMov().y;
- mobYColl(pl);
- if (pl.isDead()) return;
+ private void playerPhy(Player player) {
+ player.y += player.getMove().y;
+ mobYColl(player);
+
+ if (player.isDead()) {
+ return;
+ }
- if (GameItems.isFluid(getBlock(pl.getRect()))) {
- if (CaveGame.TOUCH && pl.getMov().x != 0 && !pl.swim && !pl.isFlyMode()) pl.swim = true;
- if (!pl.swim) {
- 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);
+ if (GameItems.isFluid(getBlock(player))) {
+ if (CaveGame.TOUCH && player.getMove().x != 0 && !player.swim && !player.isFlyMode()) {
+ player.swim = true;
+ }
+ if (!player.swim) {
+ if (!player.isFlyMode() && player.getMove().y < 4.5f) {
+ player.getMove().add(gravity.x / 4, gravity.y / 4);
+ }
+ if (!player.isFlyMode() && player.getMove().y > 4.5f) {
+ player.getMove().add(0, -1f);
+ }
} else {
- pl.getMov().add(0, -.5f);
- if (pl.getMov().y < -3) pl.getMov().y = -3;
+ player.getMove().add(0, -.5f);
+ if (player.getMove().y < -3) {
+ player.getMove().y = -3;
+ }
}
} else {
- if (!pl.isFlyMode() && pl.getMov().y < 18) pl.getMov().add(gravity);
+ if (!player.isFlyMode() && player.getMove().y < 18) {
+ player.getMove().add(gravity);
+ }
}
- pl.getPos().x += pl.getMov().x * (pl.isFlyMode() ? 1.5f : 1) * (GameItems.isFluid(getBlock(pl.getRect())) && !pl.isFlyMode() ? .8f : 1);
- mobXColl(pl);
+ player.x += player.getMove().x * (player.isFlyMode() ? 1.5f : 1) * (GameItems.isFluid(getBlock(player)) && !player.isFlyMode() ? .8f : 1);
+ mobXColl(player);
- if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDirection()) && !pl.isFlyMode() && pl.canJump() && pl.getMov().x != 0) {
- pl.getMov().add(0, -8);
- pl.setCanJump(false);
+ if (CaveGame.TOUCH && !player.isFlyMode() && player.canJump() && player.getMove().x != 0 && checkJump(player)) {
+ player.getMove().add(0, -8);
+ player.setCanJump(false);
}
}
private void mobPhy(Mob mob) {
- mob.getPos().y += mob.getMov().y;
+ if (mob.getType() == Mob.Type.MOB && GameItems.isFluid(getBlock(mob))) {
+ if (mob.getMove().y > 9) {
+ mob.getMove().add(0, -.9f);
+ }
+
+ mob.getMove().add(0, -.5f);
+
+ if (mob.getMove().y < -3) {
+ mob.getMove().y = -3;
+ }
+ } else if (!mob.isFlyMode() && mob.getMove().y < 18) {
+ mob.getMove().add(gravity);
+ }
+
+ mob.y += mob.getMove().y;
mobYColl(mob);
- if (mob.isDead()) return;
- if (mob.getType() == 0 && GameItems.isFluid(getBlock(mob.getRect()))) {
- 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);
+ if (mob.isDead()) {
+ return;
+ }
- mob.getPos().x += mob.getMov().x;
+ mob.x += mob.getMove().x;
mobXColl(mob);
- if (checkJump(mob.getRect(), mob.getDirection()) && mob.canJump() && mob.getMov().x != 0) {
- mob.getMov().add(0, -8);
+ if (mob.canJump() && mob.getMove().x != 0 && checkJump(mob)) {
+ mob.getMove().add(0, -8);
mob.setCanJump(false);
}
}
for (Iterator<Drop> it = GP.drops.iterator(); it.hasNext(); ) {
Drop drop = it.next();
dropPhy(drop);
- if (Intersector.overlaps(drop.getRect(), GP.player.getRect())) drop.pickUpDrop(GP.player);
- if (drop.pickedUp) it.remove();
+ if (Intersector.overlaps(drop, GP.player)) {
+ drop.pickUpDrop(GP.player);
+ }
+ if (drop.isPickedUp()) {
+ it.remove();
+ }
}
for (Iterator<Mob> it = GP.mobs.iterator(); it.hasNext(); ) {
Mob mob = it.next();
mob.ai();
mobPhy(mob);
- if (mob.isDead()) it.remove();
+ if (mob.isDead()) {
+ it.remove();
+ }
}
playerPhy(GP.player);
- if (GP.player.isDead()) GP.player.respawn();
+ if (GP.player.isDead()) {
+ GP.player.respawn();
+ }
- GP.renderer.setCamPos(
- GP.player.getPos().x + GP.player.getWidth() / 2 - GP.renderer.getWidth() / 2,
- GP.player.getPos().y + GP.player.getHeight() / 2 - GP.renderer.getHeight() / 2);
+ GP.renderer.setCamPos(GP.player.getX() + GP.player.getWidth() / 2 - GP.renderer.getWidth() / 2,
+ GP.player.getY() + GP.player.getHeight() / 2 - GP.renderer.getHeight() / 2);
}
}