From f362e93210d49a6ba8a0667886a2ebfd6b4011aa Mon Sep 17 00:00:00 2001 From: BenCheung0422 <74168521+BenCheung0422@users.noreply.github.com> Date: Sat, 12 Oct 2024 01:30:18 +0800 Subject: [PATCH] Fix stamina cost of boat --- .../java/minicraft/entity/mob/Player.java | 2 +- .../java/minicraft/entity/vehicle/Boat.java | 41 ++++++++----------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/client/java/minicraft/entity/mob/Player.java b/src/client/java/minicraft/entity/mob/Player.java index de7c68d19..172b5ba2c 100644 --- a/src/client/java/minicraft/entity/mob/Player.java +++ b/src/client/java/minicraft/entity/mob/Player.java @@ -384,7 +384,7 @@ public void tick() { if (isSwimming() && !potioneffects.containsKey(PotionType.Swim) && ride == null) staminaRecharge = 0; // Don't recharge stamina while swimming. - if (ride != null && ((Boat)ride).isMoving()) + if (ride instanceof Boat && ((Boat) ride).isMoving()) staminaRecharge = 0; // Don't recharge stamina while rowing the boat. // Recharge a bolt for each multiple of maxStaminaRecharge. diff --git a/src/client/java/minicraft/entity/vehicle/Boat.java b/src/client/java/minicraft/entity/vehicle/Boat.java index f79172390..fcf99b6ea 100644 --- a/src/client/java/minicraft/entity/vehicle/Boat.java +++ b/src/client/java/minicraft/entity/vehicle/Boat.java @@ -25,14 +25,13 @@ public class Boat extends Entity implements PlayerRideable { }; private static final int MOVE_SPEED = 1; + private static final int STAMINA_COST_TIME = 45; // a unit of stamina is consumed per this amount of time of move private @Nullable Entity passenger; - private @NotNull Direction dir; private int walkDist = 0; - - private boolean moving = false; + private int unitMoveCounter = 0; public Boat(@NotNull Direction dir) { super(6, 6); @@ -132,6 +131,12 @@ public boolean canSwim() { @Override public boolean rideTick(Player passenger, Vector2 vec) { if (this.passenger != passenger) return false; + + if (unitMoveCounter >= STAMINA_COST_TIME) { + passenger.payStamina(1); + unitMoveCounter -= STAMINA_COST_TIME; + } + boolean inLava = isInLava(), inWater = isInWater(); if (inLava) { if (Updater.tickCount % 2 != 0) return true; // A bit slower when in lava. @@ -139,29 +144,16 @@ public boolean rideTick(Player passenger, Vector2 vec) { int xd = (int) (vec.x * MOVE_SPEED); int yd = (int) (vec.y * MOVE_SPEED); dir = Direction.getDirection(xd, yd); - if (move(xd, yd)) { + if (passenger.stamina > 0 && move(xd, yd)) { if (!(inWater || inLava) || (inLava && Updater.tickCount % 4 == 0) || (inWater && Updater.tickCount % 2 != 0)) walkDist++; // Slower the animation syncPassengerState(passenger); - if (Updater.tickCount % 60 == 0) { - passenger.payStamina(1); - } - moving = true; - } else moving = false; - return true; - } - - @Override - public boolean move(int xd, int yd) { - if (passenger != null) { - if (((Player) passenger).stamina > 0 && ((Player) passenger).staminaRechargeDelay == 0) { - return super.move(xd, yd); - } else if (((Player) passenger).staminaRechargeDelay == 0) { - ((Player) passenger).staminaRechargeDelay = 5; - return false; - } + unitMoveCounter++; + return true; + } else { + if (unitMoveCounter > 0) unitMoveCounter--; + return false; } - return false; } public @NotNull Direction getDir() { @@ -169,13 +161,14 @@ public boolean move(int xd, int yd) { } public boolean isMoving() { - return moving; + return unitMoveCounter > 0; } @Override public boolean startRiding(Player player) { if (passenger == null) { passenger = player; + unitMoveCounter = 0; syncPassengerState(passenger); return true; } else @@ -186,6 +179,8 @@ public boolean startRiding(Player player) { public void stopRiding(Player player) { if (passenger == player) { passenger = null; + moving = false; + unitMoveCounter = 0; // reset counters } } }