diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java
index 86784046014616b1c0d7b1cbbbb4189a9ad343a8..cb5b37e35a5d5a0c157c225cfac938e4154dbdfc 100644 (file)
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
-import ru.deadsoftware.cavedroid.CaveGame;
+import ru.deadsoftware.cavedroid.MainConfig;
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.MobsController;
+import ru.deadsoftware.cavedroid.game.mobs.player.Player;
+import ru.deadsoftware.cavedroid.game.model.block.Block;
+import ru.deadsoftware.cavedroid.game.objects.drop.Drop;
+import ru.deadsoftware.cavedroid.game.objects.drop.DropController;
+import ru.deadsoftware.cavedroid.game.world.GameWorld;
+import javax.annotation.CheckForNull;
+import javax.inject.Inject;
import java.util.Iterator;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
-class GamePhysics {
+@GameScope
+public class GamePhysics {
- static final int PL_SPEED = 2;
+ public static final float PL_JUMP_VELOCITY = -133.332f;
+ public static final float PL_TERMINAL_VELOCITY = 1254.4f;
- private Vector2 gravity = new Vector2(0, .9f);;
+ private final Vector2 gravity = new Vector2(0, 444.44f);
- 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);
+ private final GameWorld mGameWorld;
+ private final MainConfig mMainConfig;
+ private final MobsController mMobsController;
+ private final DropController mDropController;
+ private final GameItemsHolder mGameItemsHolder;
- bl = GP.world.getForeMap(blX / 16, blY / 16);
- if (checkColl(new Rectangle(blX, rect.y - 18, rect.width, rect.height))) bl = 0;
+ @Inject
+ public GamePhysics(GameWorld gameWorld,
+ MainConfig mainConfig,
+ MobsController mobsController,
+ DropController dropController,
+ GameItemsHolder gameItemsHolder) {
+ mGameWorld = gameWorld;
+ mMainConfig = mainConfig;
+ mMobsController = mobsController;
+ mDropController = dropController;
+ mGameItemsHolder = gameItemsHolder;
+ }
+
+ /**
+ * Checks if mob should jump
+ *
+ * @return true if mob should jump
+ */
+ private boolean checkJump(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);
+ Block block = mGameWorld.getForeMap(blX / 16, blY / 16);
+
+ if (checkColl(new Rectangle(blX, mob.getY() - 18, mob.getWidth(), mob.getHeight())) != null) {
+ return false;
+ }
- return (bl > 0 && GameItems.getBlock(bl).toJump() &&
- (rect.y + rect.height) - GameItems.getBlock(bl).getRect(blX / 16, blY / 16).y > 8);
+ return (block.toJump() &&
+ (mob.getY() + mob.getHeight()) - block.getRectangle(blX / 16, blY / 16).y > 8);
}
- private boolean checkColl(Rectangle rect) {
- int bl;
+ /**
+ * @return colliding rect or null if no collision
+ */
+ @CheckForNull
+ private Rectangle checkColl(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 > mGameWorld.getHeight()) {
+ maxY = mGameWorld.getHeight();
+ }
+
+ Block 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))) {
- return true;
+ if (!mGameWorld.hasForeAt(x, y)) {
+ continue;
+ }
+ block = mGameWorld.getForeMap(x, y);
+ if (block.hasCollision()) {
+ final Rectangle blockRect = block.getRectangle(x, y);
+ if (Intersector.overlaps(rect, blockRect)) {
+ return blockRect;
}
}
}
}
- return false;
+
+ return null;
}
- 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 Block getBlock(Rectangle rect) {
+ return mGameWorld.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();
+ private Rectangle getShiftedPlayerRect(float shift) {
+ final Player player = mMobsController.getPlayer();
+ return new Rectangle(player.x + shift, player.y, player.width, player.height);
+ }
+
+ /**
+ * @return Rectangle representing magneting target for this drop
+ */
+ @CheckForNull
+ private Rectangle getShiftedMagnetingPlayerRect(Drop drop) {
+ final Player player = mMobsController.getPlayer();
+
+ if (!player.inventory.canPickItem(drop)) {
+ return null;
+ }
+
+ if (drop.canMagnetTo(player)) {
+ return getShiftedPlayerRect(0);
+ }
+
+ final Rectangle shiftedLeft = getShiftedPlayerRect(-mGameWorld.getWidthPx());
+ if (drop.canMagnetTo(shiftedLeft)) {
+ return shiftedLeft;
+ }
+
+ final Rectangle shiftedRight = getShiftedPlayerRect(mGameWorld.getWidthPx());
+ if (drop.canMagnetTo(shiftedRight)) {
+ return shiftedRight;
+ }
+
+ return null;
+ }
+
+ private void pickUpDropIfPossible(Rectangle shiftedPlayerTarget, Drop drop) {
+ final Player player = mMobsController.getPlayer();
+
+ if (Intersector.overlaps(shiftedPlayerTarget, drop)) {
+ player.inventory.pickDrop(drop);
+ }
+ }
+
+ private void dropPhy(Drop drop, float delta) {
+ final Rectangle playerMagnetTarget = getShiftedMagnetingPlayerRect(drop);
+ final Vector2 dropVelocity = drop.getVelocity();
+
+
+ if (playerMagnetTarget != null) {
+ final Vector2 magnetVector = new Vector2(playerMagnetTarget.x - drop.x,
+ playerMagnetTarget.y - drop.y);
+ magnetVector.nor().scl(Drop.MAGNET_VELOCITY * delta);
+ dropVelocity.add(magnetVector);
} 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;
+ dropVelocity.y += gravity.y * delta;
+ }
+
+ dropVelocity.x = MathUtils.clamp(dropVelocity.x, -Drop.MAGNET_VELOCITY, Drop.MAGNET_VELOCITY);
+ dropVelocity.y = MathUtils.clamp(dropVelocity.y, -Drop.MAGNET_VELOCITY, Drop.MAGNET_VELOCITY);
+
+ drop.x += dropVelocity.x * delta;
+ drop.y += dropVelocity.y * delta;
+
+ if (checkColl(drop) != null) {
+ dropVelocity.setZero();
+ do {
+ drop.y--;
+ } while (checkColl(drop) != null);
+ }
+
+ if (playerMagnetTarget != null) {
+ pickUpDropIfPossible(playerMagnetTarget, drop);
}
}
private void mobXColl(Mob mob) {
- if (checkColl(mob.getRect())) {
- if (mob.canJump && !mob.flyMode) {
- mob.pos.y -= 8;
+ if (mob.getVelocity().x == 0f) {
+ return;
+ }
+
+ @CheckForNull Rectangle collidingRect = checkColl(mob);
+
+ if (collidingRect != null) {
+ if (mob.canJump() && !mob.isFlyMode() && collidingRect.y >= mob.y + mob.height - 8) {
+ mob.y = collidingRect.y - mob.height;
+ return;
}
- if (checkColl(mob.getRect())) {
- if (mob.canJump && !mob.flyMode) mob.pos.y += 8;
+
+ collidingRect = checkColl(mob);
+
+ if (collidingRect != null) {
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.getVelocity().x < 0) {
+ d = 1;
+ } else if (mob.getVelocity().x > 0) {
+ d = -1;
+ }
+
+ if (d < 0) {
+ mob.x = collidingRect.x - mob.width;
+ } else {
+ mob.x = collidingRect.x + collidingRect.width;
+ }
+
+// mob.x = MathUtils.round(mob.getX());
+// while (checkColl(mob) != null) {
+// mob.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();
+
+ mob.checkWorldBounds(mGameWorld);
}
private void mobYColl(Mob mob) {
- if (checkColl(mob.getRect())) {
+ @CheckForNull final Rectangle collidingRect = checkColl(mob);
+ if (collidingRect != null) {
int d = -1;
- if (mob.mov.y < 0) d = 1;
+
+ if (mob.getVelocity().y < 0) {
+ d = 1;
+ }
+
if (d == -1) {
- mob.canJump = true;
- mob.flyMode = false;
+ mob.setCanJump(true);
+ mob.setFlyMode(false);
+
+ int dmg = ((int)Math.max(0f, (((mob.getVelocity().y * mob.getVelocity().y) / (2 * gravity.y)) - 48f) / 16f));
+ if (dmg > 0) {
+ mob.damage(dmg);
+ }
}
- 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();
+
+ if (d < 0) {
+ mob.y = collidingRect.y - mob.height;
+ } else {
+ mob.y = collidingRect.y + collidingRect.height;
}
+
+
+// mob.y = MathUtils.round(mob.getY());
+//
+// while (checkColl(mob)) {
+// mob.y += d;
+// }
+
+ mob.getVelocity().y = 0;
+
} else {
- mob.canJump = false;
+ mob.y += 1;
+ mob.setCanJump(checkColl(mob) != null);
+ mob.y -= 1;
}
- if (mob.pos.y > GP.world.getHeightPx()) {
+
+ if (mob.getY() > mGameWorld.getHeightPx()) {
mob.kill();
}
}
- private void playerPhy(Player pl) {
- pl.pos.y += pl.mov.y;
- mobYColl(pl);
- if (pl.isDead()) return;
+ private void playerPhy(Player player, float delta) {
+ if (player.isDead()) {
+ return;
+ }
- if (GameItems.isFluid(getBlock(pl.getRect()))) {
- 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);
+ if (getBlock(player).isFluid()) {
+ if (mMainConfig.isTouch() && player.getVelocity().x != 0 && !player.swim && !player.isFlyMode()) {
+ player.swim = true;
+ }
+ if (!player.swim) {
+ if (!player.isFlyMode() && player.getVelocity().y < 32f) {
+ player.getVelocity().y += gravity.y * delta;
+ }
+ if (!player.isFlyMode() && player.getVelocity().y > 32f) {
+ player.getVelocity().y -= player.getVelocity().y * 32f * delta;
+ }
} else {
- pl.mov.add(0, -.5f);
- if (pl.mov.y < -3) pl.mov.y = -3;
+ player.getVelocity().y += PL_JUMP_VELOCITY * delta;
+ if (player.getVelocity().y < -player.getSpeed()) {
+ player.getVelocity().y = -player.getSpeed();
+ }
}
} else {
- if (!pl.flyMode && pl.mov.y < 18) pl.mov.add(gravity);
+ if (!player.isFlyMode() && player.getVelocity().y < PL_TERMINAL_VELOCITY) {
+ player.getVelocity().y += gravity.y * delta;
+ }
}
- pl.pos.x += pl.mov.x * (pl.flyMode ? 1.5f : 1) * (GameItems.isFluid(getBlock(pl.getRect())) && !pl.flyMode ? .8f : 1);
- mobXColl(pl);
+ player.y += player.getVelocity().y * delta;
+ mobYColl(player);
+
+ player.x += player.getVelocity().x * (player.isFlyMode() ? 1.5f : 1) *
+ (getBlock(player).isFluid() && !player.isFlyMode() ? .8f : 1) * delta;
+
+ mobXColl(player);
- if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDir()) && !pl.flyMode && pl.canJump && pl.mov.x != 0) {
- pl.mov.add(0, -8);
- pl.canJump = false;
+ if (mMainConfig.isTouch() && !player.isFlyMode() && player.canJump() && player.getVelocity().x != 0 && checkJump(player)) {
+ player.jump();
+ player.setCanJump(false);
}
}
- private void mobPhy(Mob mob) {
- mob.pos.y += mob.mov.y;
+ private void mobPhy(Mob mob, float delta) {
+ if (mob.getType() == Mob.Type.MOB && getBlock(mob).isFluid()) {
+ if (mob.getVelocity().y > 32f) {
+ mob.getVelocity().y -= mob.getVelocity().y * 32f * delta;
+ }
+
+ mob.getVelocity().y += PL_JUMP_VELOCITY * delta;
+
+ if (mob.getVelocity().y < -mob.getSpeed()) {
+ mob.getVelocity().y = -mob.getSpeed();
+ }
+ } else if (!mob.isFlyMode() && mob.getVelocity().y < PL_TERMINAL_VELOCITY) {
+ mob.getVelocity().y += gravity.y * delta;
+ }
+
+ mob.y += mob.getVelocity().y * delta;
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.isDead()) {
+ return;
+ }
- mob.pos.x += mob.mov.x;
+ mob.x += mob.getVelocity().x * delta;
mobXColl(mob);
- if (checkJump(mob.getRect(), mob.getDir()) && mob.canJump && mob.mov.x != 0) {
- mob.mov.add(0, -8);
- mob.canJump = false;
+ if (mob.canJump() && mob.getVelocity().x != 0 && checkJump(mob)) {
+ mob.jump();
+ mob.setCanJump(false);
}
}
void update(float delta) {
- //TODO use delta time
- for (Iterator<Drop> it = GP.drops.iterator(); it.hasNext(); ) {
+ Player player = mMobsController.getPlayer();
+
+ for (Iterator<Drop> it = mDropController.getIterator(); 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();
+ dropPhy(drop, delta);
+ if (drop.getPickedUp()) {
+ it.remove();
+ }
}
- for (Iterator<Mob> it = GP.mobs.iterator(); it.hasNext(); ) {
+ for (Iterator<Mob> it = mMobsController.getMobs().iterator(); it.hasNext(); ) {
Mob mob = it.next();
- mob.ai();
- mobPhy(mob);
- if (mob.isDead()) it.remove();
+ mob.ai(mGameWorld, mGameItemsHolder, mMobsController, delta);
+ mobPhy(mob, delta);
+ if (mob.isDead()) {
+ it.remove();
+ }
}
- playerPhy(GP.player);
- 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);
+ playerPhy(player, delta);
+ player.ai(mGameWorld, mGameItemsHolder, mMobsController, delta);
+ if (player.isDead()) {
+ player.respawn(mGameWorld, mGameItemsHolder);
+ }
}
}