X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;ds=sidebyside;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2FGamePhysics.java;h=70afcbfe5d3f1be77cdd772d61584b142022f7bd;hb=462f97f8da742fe35f516fec00ca9a581d688e7a;hp=38253948725eae1e5fbf6c874509d1d7fe13f771;hpb=7cb80fd35da9bb41a3f2b96dd898d4bbb1e9b718;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 3825394..70afcbf 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.game.objects.Drop; 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; @@ -19,7 +20,6 @@ import java.util.Iterator; @GameScope public class GamePhysics { - 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; @@ -94,31 +94,72 @@ public class GamePhysics { (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); } } @@ -214,8 +255,8 @@ public class GamePhysics { } } else { player.getVelocity().y += PL_JUMP_VELOCITY * delta; - if (player.getVelocity().y < -PL_SPEED) { - player.getVelocity().y = -PL_SPEED; + if (player.getVelocity().y < -player.getSpeed()) { + player.getVelocity().y = -player.getSpeed(); } } } else { @@ -233,7 +274,7 @@ public class GamePhysics { mobXColl(player); if (mMainConfig.isTouch() && !player.isFlyMode() && player.canJump() && player.getVelocity().x != 0 && checkJump(player)) { - player.getVelocity().y = PL_JUMP_VELOCITY; + player.jump(); player.setCanJump(false); } } @@ -246,8 +287,8 @@ public class GamePhysics { mob.getVelocity().y += PL_JUMP_VELOCITY * delta; - if (mob.getVelocity().y < -PL_SPEED) { - mob.getVelocity().y = -PL_SPEED; + 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; @@ -264,7 +305,7 @@ public class GamePhysics { mobXColl(mob); if (mob.canJump() && mob.getVelocity().x != 0 && checkJump(mob)) { - mob.getVelocity().y += PL_JUMP_VELOCITY; + mob.jump(); mob.setCanJump(false); } } @@ -275,10 +316,7 @@ public class GamePhysics { for (Iterator 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(); } }