From b9841a5aa1ccc1c4d30b23854b14d8de346951fd Mon Sep 17 00:00:00 2001 From: fredboy Date: Thu, 9 May 2024 18:45:59 +0700 Subject: [PATCH] Bug fixes --- .../cavedroid/game/GamePhysics.java | 71 ++++++++++++------- .../cavedroid/game/GameRenderer.java | 34 +++++++-- ...tCraftingInventoryItemMouseInputHandler.kt | 10 --- ...tSurvivalInventoryItemMouseInputHandler.kt | 10 --- .../handler/touch/JoystickInputHandler.kt | 15 +++- .../cavedroid/game/mobs/player/Inventory.kt | 14 ++-- .../cavedroid/game/world/GameWorld.java | 42 +++++++---- 7 files changed, 124 insertions(+), 72 deletions(-) diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index a2354f0..eee83c7 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -56,7 +56,7 @@ public class GamePhysics { 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()))) { + if (checkColl(new Rectangle(blX, mob.getY() - 18, mob.getWidth(), mob.getHeight())) != null) { return false; } @@ -64,7 +64,11 @@ public class GamePhysics { (mob.getY() + mob.getHeight()) - block.getRectangle(blX / 16, blY / 16).y > 8); } - private boolean checkColl(Rectangle rect) { + /** + * @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; @@ -86,14 +90,15 @@ public class GamePhysics { } block = mGameWorld.getForeMap(x, y); if (block.hasCollision()) { - if (Intersector.overlaps(rect, block.getRectangle(x, y))) { - return true; + final Rectangle blockRect = block.getRectangle(x, y); + if (Intersector.overlaps(rect, blockRect)) { + return blockRect; } } } } - return false; + return null; } private Block getBlock(Rectangle rect) { @@ -162,11 +167,11 @@ public class GamePhysics { drop.x += dropVelocity.x * delta; drop.y += dropVelocity.y * delta; - if (checkColl(drop)) { + if (checkColl(drop) != null) { dropVelocity.setZero(); do { drop.y--; - } while (checkColl(drop)); + } while (checkColl(drop) != null); } if (playerMagnetTarget != null) { @@ -175,16 +180,21 @@ public class GamePhysics { } private void mobXColl(Mob mob) { - if (checkColl(mob)) { - if (mob.canJump() && !mob.isFlyMode()) { - mob.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)) { - if (mob.canJump() && !mob.isFlyMode()) { - mob.y += 8; - } + collidingRect = checkColl(mob); + if (collidingRect != null) { int d = 0; if (mob.getVelocity().x < 0) { @@ -193,12 +203,17 @@ public class GamePhysics { d = -1; } - mob.x = MathUtils.round(mob.getX()); - - while (checkColl(mob)) { - mob.x += d; + 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(); } @@ -209,7 +224,8 @@ public class GamePhysics { } private void mobYColl(Mob mob) { - if (checkColl(mob)) { + @CheckForNull final Rectangle collidingRect = checkColl(mob); + if (collidingRect != null) { int d = -1; if (mob.getVelocity().y < 0) { @@ -226,17 +242,24 @@ public class GamePhysics { } } - mob.y = MathUtils.round(mob.getY()); - - while (checkColl(mob)) { - mob.y += d; + 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.y += 1; - mob.setCanJump(checkColl(mob)); + mob.setCanJump(checkColl(mob) != null); mob.y -= 1; } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index 302b705..36f6c75 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.ObjectMap; +import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavedroid.MainConfig; import ru.deadsoftware.cavedroid.game.input.IGameInputHandler; import ru.deadsoftware.cavedroid.game.input.Joystick; @@ -57,6 +58,11 @@ public class GameRenderer extends Renderer { private final TouchButton mouseLeftTouchButton, mouseRightTouchButton; + private final Vector2 mCamCenterToPlayer = new Vector2(); + + private float mTouchDownX, mTouchDownY; + private long mCameraDelayMs = 0L; + @Inject GameRenderer(MainConfig mainConfig, MobsController mobsController, @@ -92,14 +98,15 @@ public class GameRenderer extends Renderer { Gdx.gl.glClearColor(0f, .6f, .6f, 1f); } - private float mTouchDownX, mTouchDownY; - private void updateDynamicCameraPosition(float delta) { Player player = mMobsController.getPlayer(); float plTargetX = player.getX() + player.getWidth() / 2; float plTargetY = player.getY() + player.getHeight() / 2; + float camCenterX = getCamX() + getWidth() / 2; + float camCenterY = getCamY() + getHeight() / 2; + float camTargetX, camTargetY; if (player.controlMode == Player.ControlMode.WALK) { @@ -110,11 +117,20 @@ public class GameRenderer extends Renderer { camTargetY = MeasureUnitsUtilsKt.getPx(player.cursorY) + MeasureUnitsUtilsKt.getPx(1) / 2; } - float camCenterX = getCamX() + getWidth() / 2; - float camCenterY = getCamY() + getHeight() / 2; - Vector2 moveVector = new Vector2(camTargetX - camCenterX, camTargetY - camCenterY); + if (player.controlMode == Player.ControlMode.WALK && player.getVelocity().isZero()) { + mCameraDelayMs = TimeUtils.millis(); + mCamCenterToPlayer.x = plTargetX - camCenterX; + mCamCenterToPlayer.y = plTargetY - camCenterY; + } + + if (TimeUtils.timeSinceMillis(mCameraDelayMs) < 500L && !player.getVelocity().isZero()) { + updateStaticCameraPosition(plTargetX - mCamCenterToPlayer.x, + camCenterY + moveVector.y * delta * 2); + return; + } + float camX = getCamX(); float camY = getCamY(); float worldWidth = MeasureUnitsUtilsKt.getPx(mGameWorld.getWidth()) - getWidth() / 2; @@ -152,11 +168,15 @@ public class GameRenderer extends Renderer { setCamPos(camX, camY); } + private void updateStaticCameraPosition(float targetX, float targetY) { + setCamPos(targetX - getWidth() / 2, targetY - getHeight() / 2); + } + private void updateStaticCameraPosition() { Player player = mMobsController.getPlayer(); - setCamPos(player.getX() + player.getWidth() / 2 - getWidth() / 2, - player.getY() + player.getHeight() / 2 - getHeight() / 2); + updateStaticCameraPosition(player.getX() + player.getWidth() / 2, + player.getY() + player.getHeight() / 2); } private void updateCameraPosition(float delta) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt index b3fb223..1b4838a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt @@ -141,16 +141,6 @@ class SelectCraftingInventoryItemMouseInputHandler @Inject constructor( window.craftResult = gameItemsHolder.craftItem(window.craftingItems .map { it?.item ?: gameItemsHolder.fallbackItem }) } - } else if (action.actionKey.touchUp) { - window.selectedItem?.let { selectedItem -> - dropController.addDrop( - /* x = */ mobsController.player.x + (32f * mobsController.player.direction.basis), - /* y = */ mobsController.player.y, - /* item = */ selectedItem.item, - /* count = */ selectedItem.amount, - ) - window.selectedItem = null - } } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt index d5858ac..91ec311 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt @@ -141,16 +141,6 @@ class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor( window.craftResult = gameItemsHolder.craftItem(window.craftingItems .map { it?.item ?: gameItemsHolder.fallbackItem }) } - } else if (action.actionKey.touchUp) { - window.selectedItem?.let { selectedItem -> - dropController.addDrop( - /* x = */ mobsController.player.x + (32f * mobsController.player.direction.basis), - /* y = */ mobsController.player.y, - /* item = */ selectedItem.item, - /* count = */ selectedItem.amount, - ) - window.selectedItem = null - } } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/touch/JoystickInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/touch/JoystickInputHandler.kt index d4fb451..1c020a5 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/touch/JoystickInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/touch/JoystickInputHandler.kt @@ -31,7 +31,7 @@ class JoystickInputHandler @Inject constructor( set(value) { if (!value) { resetVelocity() - if (TimeUtils.timeSinceMillis(activateTimeMs) < 100L && + if (TimeUtils.timeSinceMillis(activateTimeMs) < 200L && mobsController.player.controlMode != Player.ControlMode.CURSOR) { mobsController.player.jump() } @@ -76,10 +76,13 @@ class JoystickInputHandler @Inject constructor( private fun handleCursor() { val joystick = mainConfig.joystick ?: return - if (TimeUtils.timeSinceMillis(cursorTimeoutMs) < 200L) { + if (TimeUtils.timeSinceMillis(cursorTimeoutMs) < 150L) { return } + val pastCursorX = mobsController.player.cursorX + val pastCursorY = mobsController.player.cursorY + if (Math.abs(joystick.activeX - joystick.centerX) >= Joystick.RADIUS / 2) { mobsController.player.cursorX += if (joystick.activeX > joystick.centerX) 1 else -1 cursorTimeoutMs = TimeUtils.millis() @@ -91,6 +94,10 @@ class JoystickInputHandler @Inject constructor( } mobsController.player.checkCursorBounds(gameWorld) + + if (mobsController.player.cursorX != pastCursorX || mobsController.player.cursorY != pastCursorY) { + mobsController.player.blockDamage = 0f + } } private fun handleDragged() { @@ -106,6 +113,10 @@ class JoystickInputHandler @Inject constructor( val joystick = mainConfig.joystick ?: return val joyVector = joystick.getVelocityVector() + if (mobsController.player.isFlyMode) { + joyVector.scl(2f); + } + mobsController.player.velocity.x = joyVector.x mobsController.player.setDir( diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt index 36827c3..d99a90e 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt @@ -47,23 +47,27 @@ class Inventory( tooltipManager.showHotbarTooltip(activeItem.item.params.name) } - val activeItem get() = items[activeSlot] + val activeItem get() = _items[activeSlot] fun initItems(gameItemsHolder: GameItemsHolder, tooltipManager: TooltipManager) { this.tooltipManager = tooltipManager fallbackItem = gameItemsHolder.fallbackItem.toInventoryItem() - items.forEach { item -> + _items.forEach { item -> item.init(gameItemsHolder) } } private fun getItemPickSlot(item: Item): Int { - for (i in items.indices) { - val inventoryItem = items[i] + for (i in _items.indices) { + val inventoryItem = _items[i] if (item == inventoryItem.item && inventoryItem.canBeAdded()) { return i } + } + + for (i in _items.indices) { + val inventoryItem = _items[i] if (inventoryItem.item.isNone()) { return i @@ -79,7 +83,7 @@ class Inventory( fun pickDrop(drop: Drop) { val slot = getItemPickSlot(drop.item).takeIf { it >= 0 } ?: return - val inventoryItem = items[slot] + val inventoryItem = _items[slot] if (inventoryItem.item == drop.item) { inventoryItem.add(drop.amount) diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java index 8c31da7..686f1c9 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -26,7 +26,7 @@ public class GameWorld { private final Block[][] mForeMap; private final Block[][] mBackMap; - private final WorldGeneratorConfig mWorldConfig = WorldGeneratorConfig.Companion.getDefault(); + private final WorldGeneratorConfig mWorldConfig = WorldGeneratorConfig.Companion.getDefault(); @Inject public GameWorld(DropController dropController, @@ -97,23 +97,37 @@ public class GameWorld { private Block getMap(int x, int y, int layer) { Block map = mGameItemsHolder.getFallbackBlock(); - try { - x = transformX(x); - map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y]; - } catch (ArrayIndexOutOfBoundsException ignored) { + + if (y < 0 || y >= getHeight()) { + return map; + } + + x = transformX(x); + + if (x < 0 || x >= getWidth()) { + return map; } + + map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y]; + return map; } private void setMap(int x, int y, int layer, Block value) { - try { - x = transformX(x); - if (layer == 0) { - mForeMap[x][y] = value; - } else { - mBackMap[x][y] = value; - } - } catch (ArrayIndexOutOfBoundsException ignored) { + if (y < 0 || y >= getHeight()) { + return; + } + + x = transformX(x); + + if (x < 0 || x >= getWidth()) { + return; + } + + if (layer == 0) { + mForeMap[x][y] = value; + } else { + mBackMap[x][y] = value; } } @@ -183,7 +197,7 @@ public class GameWorld { private boolean shouldDrop(Block block) { final Item item = mMobsController.getPlayer().inventory.getActiveItem().getItem(); - int toolLevel = item.isTool() ? ((Item.Tool)item).getLevel() : 0; + int toolLevel = item.isTool() ? ((Item.Tool) item).getLevel() : 0; if (item.isTool() && block.getParams().getToolType() != item.getClass()) { toolLevel = 0; } -- 2.29.2