diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java
index 7d13744d9260f7cfb86774641ff70e57c0d67930..cd900d873dc1398e6b41ea492be77a87feaca25a 100644 (file)
import ru.deadsoftware.cavedroid.game.objects.DropController;
import ru.deadsoftware.cavedroid.game.world.GameWorld;
+import javax.annotation.CheckForNull;
import javax.inject.Inject;
import java.util.Iterator;
public static final float PL_SPEED = 69.072f;
public static final float PL_JUMP_VELOCITY = -133.332f;
+ public static final float PL_TERMINAL_VELOCITY = 1254.4f;
private final Vector2 gravity = new Vector2(0, 444.44f);
(int) (rect.y + rect.height / 8 * 7) / 16);
}
+ 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 (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.pickUpDrop(drop);
+ }
+ }
+
private void dropPhy(Drop drop, float delta) {
- int dropToPlayer = drop.closeToPlayer(mGameWorld, mMobsController.getPlayer());
- if (dropToPlayer > 0) {
- drop.moveToPlayer(mGameWorld, mMobsController.getPlayer(), dropToPlayer);
+ 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.getVelocity().x >= .5f) {
- drop.getVelocity().x -= .5f;
- } else if (drop.getVelocity().x <= -.5f) {
- drop.getVelocity().x += .5f;
- } else {
- drop.getVelocity().x = 0;
- }
- if (drop.getVelocity().y < 9) {
- drop.getVelocity().y += gravity.y / 4;
- }
+ dropVelocity.y += gravity.y * delta;
}
- drop.move(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)) {
- drop.getVelocity().set(0, -1);
+ dropVelocity.setZero();
do {
- drop.move(delta);
+ drop.y--;
} while (checkColl(drop));
- drop.getVelocity().setZero();
+ }
+
+ if (playerMagnetTarget != null) {
+ pickUpDropIfPossible(playerMagnetTarget, drop);
}
}
mob.getVelocity().y = 0;
+
+
+ //todo fall damage
+ // h = (v^2) / 2g
+ // dmg = max(0, (h - 48) / 32) - half of blocks fallen starting from 3 blocks height
+ // int dmg = ((int)Math.max(0f, (((mob.getVelocity().y * mob.getVelocity().y) / (2 * gravity.y)) - 48f) / 16f));
+ // if (dmg > 0) System.out.println("Damage: " + dmg);
+
} else {
mob.y += 1;
mob.setCanJump(checkColl(mob));
player.swim = true;
}
if (!player.swim) {
- if (!player.isFlyMode() && player.getVelocity().y < 270f) {
- player.getVelocity().x += gravity.y / 4;
+ if (!player.isFlyMode() && player.getVelocity().y < 32f) {
+ player.getVelocity().y += gravity.y * delta;
}
- if (!player.isFlyMode() && player.getVelocity().y > 270f) {
- player.getVelocity().add(0, -60f);
+ if (!player.isFlyMode() && player.getVelocity().y > 32f) {
+ player.getVelocity().y -= player.getVelocity().y * 32f * delta;
}
} else {
- player.getVelocity().add(0, -30f);
- if (player.getVelocity().y < -180) {
- player.getVelocity().y = -180;
+ player.getVelocity().y += PL_JUMP_VELOCITY * delta;
+ if (player.getVelocity().y < -PL_SPEED) {
+ player.getVelocity().y = -PL_SPEED;
}
}
} else {
- if (!player.isFlyMode() && player.getVelocity().y < 1080) {
+ if (!player.isFlyMode() && player.getVelocity().y < PL_TERMINAL_VELOCITY) {
player.getVelocity().y += gravity.y * delta;
}
}
private void mobPhy(Mob mob, float delta) {
if (mob.getType() == Mob.Type.MOB && GameItems.isFluid(getBlock(mob))) {
- if (mob.getVelocity().y > 540) {
- mob.getVelocity().add(0, -5.4f);
+ if (mob.getVelocity().y > 32f) {
+ mob.getVelocity().y -= mob.getVelocity().y * 32f * delta;
}
- mob.getVelocity().add(0, -30f);
+ mob.getVelocity().y += PL_JUMP_VELOCITY * delta;
- if (mob.getVelocity().y < -180) {
- mob.getVelocity().y = -180;
+ if (mob.getVelocity().y < -PL_SPEED) {
+ mob.getVelocity().y = -PL_SPEED;
}
- } else if (!mob.isFlyMode() && mob.getVelocity().y < 1080) {
- mob.getVelocity().add(gravity);
+ } else if (!mob.isFlyMode() && mob.getVelocity().y < PL_TERMINAL_VELOCITY) {
+ mob.getVelocity().y += gravity.y * delta;
}
mob.y += mob.getVelocity().y * delta;
mobXColl(mob);
if (mob.canJump() && mob.getVelocity().x != 0 && checkJump(mob)) {
- mob.getVelocity().add(0, -480);
+ mob.getVelocity().y = PL_JUMP_VELOCITY;
mob.setCanJump(false);
}
}
for (Iterator<Drop> it = mDropController.getIterator(); it.hasNext(); ) {
Drop drop = it.next();
dropPhy(drop, delta);
- if (Intersector.overlaps(drop, player)) {
- drop.pickUpDrop(player);
- }
- if (drop.isPickedUp()) {
+ if (drop.getPickedUp()) {
it.remove();
}
}