summaryrefslogtreecommitdiff
path: root/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-01-22 00:40:57 +0500
committerilotterytea <iltsu@alright.party>2025-01-22 00:41:39 +0500
commita7f77c115bc95eb8a667df1146cc26dc17367879 (patch)
tree77b08b0cb4f07f9b3d21c277d5621bd0ee5a488c /server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java
parente4997681a2d08adc9b9055b0b2a1dc52d54edd47 (diff)
feat: player jumps like a frog!!! (FROGartha reference) + player state
Diffstat (limited to 'server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java')
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java37
1 files changed, 37 insertions, 0 deletions
diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java
index b50392f..44162ae 100644
--- a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java
+++ b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java
@@ -1,7 +1,12 @@
package kz.ilotterytea.frogartha.server;
+import com.badlogic.gdx.math.Vector3;
+import kz.ilotterytea.frogartha.FrogarthaConstants;
import kz.ilotterytea.frogartha.domain.Identity;
+import kz.ilotterytea.frogartha.domain.PlayerState;
+import kz.ilotterytea.frogartha.domain.client.PlayerJumpAction;
import kz.ilotterytea.frogartha.domain.server.Acknowledge;
+import kz.ilotterytea.frogartha.events.PlayerJumpEvent;
import kz.ilotterytea.frogartha.exceptions.PlayerKickException;
import kz.ilotterytea.frogartha.utils.Logger;
@@ -22,4 +27,36 @@ public class ServerHandlers {
player.send(new Acknowledge(Acknowledge.AcknowledgeCode.IDENTIFIED));
log.log("Successfully identified {} for {}", identity, player);
}
+
+ public static void handlePlayerJumpAction(PlayerConnection player, PlayerJumpAction action) {
+ PlayerState state = player.getState();
+
+ float nowTimestamp = System.currentTimeMillis() / 1000f;
+
+ if (state.getNextJumpTimestamp() > nowTimestamp) {
+ throw PlayerKickException.jumpSpam();
+ }
+
+ state.setNextJumpTimestamp(nowTimestamp + action.getJumpStrength());
+
+ // calculating start and end positions
+ Vector3 position = state.getPosition();
+ Vector3 startPosition = position.cpy();
+
+ float jumpDistance = action.getJumpStrength() * FrogarthaConstants.Player.JUMP_SPEED;
+ Vector3 d = new Vector3(state.getDirection()).sub(startPosition).nor();
+
+ Vector3 endPosition = new Vector3(
+ position.x + d.x * jumpDistance,
+ position.y,
+ position.z + d.z * jumpDistance
+ );
+
+ state.setPosition(endPosition.x, endPosition.y, endPosition.z);
+
+ player.send(new PlayerJumpEvent(
+ player.getId(), startPosition, endPosition, action.getJumpStrength()
+ ));
+ log.log("{} jumped from {} to {} with strength {}", player, startPosition, endPosition, action.getJumpStrength());
+ }
}