From b3739521c1dcaed9bd451e00067b7d304521745f Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 22 Jan 2025 01:00:49 +0500 Subject: feat: player direction is now calculated by server --- .../frogartha/entities/PlayerEntity.java | 7 ++++- .../frogartha/sessions/SessionClient.java | 13 ++++++-- .../frogartha/sessions/SessionHandlers.java | 16 ++++++++++ .../frogartha/server/FrogarthaServer.java | 3 ++ .../frogartha/server/ServerHandlers.java | 11 +++++++ .../ilotterytea/frogartha/domain/PlayerState.java | 2 +- .../domain/client/ChangedDirectionAction.java | 32 ++++++++++++++++++++ .../frogartha/events/ChangedDirectionEvent.java | 35 ++++++++++++++++++++++ .../frogartha/utils/SerializerUtils.java | 4 +++ 9 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 shared/src/main/java/kz/ilotterytea/frogartha/domain/client/ChangedDirectionAction.java create mode 100644 shared/src/main/java/kz/ilotterytea/frogartha/events/ChangedDirectionEvent.java diff --git a/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java b/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java index 76f7f4c..518c7c6 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java @@ -9,6 +9,7 @@ import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.collision.Ray; import kz.ilotterytea.frogartha.FrogarthaConstants; import kz.ilotterytea.frogartha.FrogarthaGame; +import kz.ilotterytea.frogartha.domain.client.ChangedDirectionAction; import kz.ilotterytea.frogartha.domain.client.PlayerJumpAction; public class PlayerEntity extends RenderableEntity { @@ -80,7 +81,11 @@ public class PlayerEntity extends RenderableEntity { Ray ray = camera.getPickRay(Gdx.input.getX(), Gdx.input.getY()); final float distance = -ray.origin.y / ray.direction.y; Vector3 point = new Vector3(ray.direction).scl(distance).add(ray.origin); - super.setDirection(point.x, position.y, point.z); + Vector3 direction = new Vector3(point.x, position.y, point.z); + + if (direction.x != this.direction.x || direction.z != this.direction.z) { + FrogarthaGame.getInstance().getSessionClient().send(new ChangedDirectionAction(direction)); + } } private void setCameraPosition() { diff --git a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java index 73dfd3c..7b3ae4e 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java @@ -9,6 +9,7 @@ import kz.ilotterytea.frogartha.FrogarthaConstants; import kz.ilotterytea.frogartha.FrogarthaGame; import kz.ilotterytea.frogartha.domain.Identity; import kz.ilotterytea.frogartha.domain.server.Acknowledge; +import kz.ilotterytea.frogartha.events.ChangedDirectionEvent; import kz.ilotterytea.frogartha.events.PlayerJumpEvent; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; import kz.ilotterytea.frogartha.utils.Logger; @@ -60,9 +61,15 @@ public class SessionClient implements WebSocketListener { throw new RuntimeException("Deserialized packet is null"); } - if (obj instanceof Acknowledge) SessionHandlers.handleAcknowledge((Acknowledge) obj); - else if (obj instanceof PlayerJumpEvent) SessionHandlers.handlePlayerJumpEvent((PlayerJumpEvent) obj); - else if (obj instanceof PlayerKickException) throw (PlayerKickException) obj; + if (obj instanceof Acknowledge) { + SessionHandlers.handleAcknowledge((Acknowledge) obj); + } else if (obj instanceof PlayerJumpEvent) { + SessionHandlers.handlePlayerJumpEvent((PlayerJumpEvent) obj); + } else if (obj instanceof ChangedDirectionEvent) { + SessionHandlers.handleChangedDirectionEvent((ChangedDirectionEvent) obj); + } else if (obj instanceof PlayerKickException) { + throw (PlayerKickException) obj; + } } catch (PlayerKickException e) { log.log("Kicked out: {}", e.getMessage()); } catch (Exception e) { diff --git a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java index c4d4237..21bf8d9 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java @@ -1,8 +1,10 @@ package kz.ilotterytea.frogartha.sessions; +import com.badlogic.gdx.math.Vector3; import kz.ilotterytea.frogartha.FrogarthaGame; import kz.ilotterytea.frogartha.domain.server.Acknowledge; import kz.ilotterytea.frogartha.entities.PlayerEntity; +import kz.ilotterytea.frogartha.events.ChangedDirectionEvent; import kz.ilotterytea.frogartha.events.PlayerJumpEvent; import kz.ilotterytea.frogartha.screens.GameScreen; import kz.ilotterytea.frogartha.utils.Logger; @@ -34,4 +36,18 @@ public class SessionHandlers { entity.setEndPosition(event.getEndPosition()); entity.setJumping(true); } + + public static void handleChangedDirectionEvent(ChangedDirectionEvent event) { + if (!game.getScreen().getClass().equals(GameScreen.class)) { + log.log("The screen is not GameScreen, but the session received ChangedDirectionEvent"); + return; + } + + GameScreen screen = (GameScreen) game.getScreen(); + PlayerEntity entity = screen.getPlayerEntity(); + + Vector3 direction = event.getDirection(); + + entity.setDirection(direction.x, entity.getPosition().y, direction.z); + } } diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java index c141bd9..1a94970 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java @@ -2,6 +2,7 @@ package kz.ilotterytea.frogartha.server; import com.github.czyzby.websocket.serialization.impl.ManualSerializer; import kz.ilotterytea.frogartha.domain.Identity; +import kz.ilotterytea.frogartha.domain.client.ChangedDirectionAction; import kz.ilotterytea.frogartha.domain.client.PlayerJumpAction; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; import kz.ilotterytea.frogartha.utils.Logger; @@ -81,6 +82,8 @@ public class FrogarthaServer extends WebSocketServer { ServerHandlers.handleIdentity(player, (Identity) obj); } else if (obj instanceof PlayerJumpAction) { ServerHandlers.handlePlayerJumpAction(player, (PlayerJumpAction) obj); + } else if (obj instanceof ChangedDirectionAction) { + ServerHandlers.handleChangedDirectionAction(player, (ChangedDirectionAction) obj); } else { throw PlayerKickException.internalServerError(); } 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 44162ae..b6dcc3c 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java @@ -4,8 +4,10 @@ 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.ChangedDirectionAction; import kz.ilotterytea.frogartha.domain.client.PlayerJumpAction; import kz.ilotterytea.frogartha.domain.server.Acknowledge; +import kz.ilotterytea.frogartha.events.ChangedDirectionEvent; import kz.ilotterytea.frogartha.events.PlayerJumpEvent; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; import kz.ilotterytea.frogartha.utils.Logger; @@ -59,4 +61,13 @@ public class ServerHandlers { )); log.log("{} jumped from {} to {} with strength {}", player, startPosition, endPosition, action.getJumpStrength()); } + + public static void handleChangedDirectionAction(PlayerConnection player, ChangedDirectionAction action) { + PlayerState state = player.getState(); + Vector3 direction = action.getDirection(); + + state.setDirection(direction.x, state.getPosition().y, direction.z); + + player.send(new ChangedDirectionEvent(player.getId(), state.getDirection())); + } } diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/domain/PlayerState.java b/shared/src/main/java/kz/ilotterytea/frogartha/domain/PlayerState.java index 15ba015..d3bd18c 100644 --- a/shared/src/main/java/kz/ilotterytea/frogartha/domain/PlayerState.java +++ b/shared/src/main/java/kz/ilotterytea/frogartha/domain/PlayerState.java @@ -37,6 +37,6 @@ public class PlayerState { } public void setDirection(float x, float y, float z) { - this.position.set(x, y, z); + this.direction.set(x, y, z); } } diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/domain/client/ChangedDirectionAction.java b/shared/src/main/java/kz/ilotterytea/frogartha/domain/client/ChangedDirectionAction.java new file mode 100644 index 0000000..9c64c56 --- /dev/null +++ b/shared/src/main/java/kz/ilotterytea/frogartha/domain/client/ChangedDirectionAction.java @@ -0,0 +1,32 @@ +package kz.ilotterytea.frogartha.domain.client; + +import com.badlogic.gdx.math.Vector3; +import com.github.czyzby.websocket.serialization.SerializationException; +import com.github.czyzby.websocket.serialization.Transferable; +import com.github.czyzby.websocket.serialization.impl.Deserializer; +import com.github.czyzby.websocket.serialization.impl.Serializer; + +public class ChangedDirectionAction implements Transferable { + private Vector3 direction; + + public ChangedDirectionAction() { + } + + public ChangedDirectionAction(Vector3 direction) { + this.direction = direction; + } + + public Vector3 getDirection() { + return direction; + } + + @Override + public void serialize(Serializer serializer) throws SerializationException { + serializer.serializeFloatArray(new float[]{direction.x, direction.y, direction.z}); + } + + @Override + public ChangedDirectionAction deserialize(Deserializer deserializer) throws SerializationException { + return new ChangedDirectionAction(new Vector3(deserializer.deserializeFloatArray())); + } +} diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/events/ChangedDirectionEvent.java b/shared/src/main/java/kz/ilotterytea/frogartha/events/ChangedDirectionEvent.java new file mode 100644 index 0000000..eefe20a --- /dev/null +++ b/shared/src/main/java/kz/ilotterytea/frogartha/events/ChangedDirectionEvent.java @@ -0,0 +1,35 @@ +package kz.ilotterytea.frogartha.events; + +import com.badlogic.gdx.math.Vector3; +import com.github.czyzby.websocket.serialization.SerializationException; +import com.github.czyzby.websocket.serialization.Transferable; +import com.github.czyzby.websocket.serialization.impl.Deserializer; +import com.github.czyzby.websocket.serialization.impl.Serializer; + +public class ChangedDirectionEvent extends Event implements Transferable { + private Vector3 direction; + + public ChangedDirectionEvent() { + } + + public ChangedDirectionEvent(int playerId, Vector3 direction) { + super(playerId); + this.direction = direction; + } + + public Vector3 getDirection() { + return direction; + } + + @Override + public void serialize(Serializer serializer) throws SerializationException { + serializer + .serializeInt(playerId) + .serializeFloatArray(new float[]{direction.x, direction.y, direction.z}); + } + + @Override + public ChangedDirectionEvent deserialize(Deserializer deserializer) throws SerializationException { + return new ChangedDirectionEvent(deserializer.deserializeInt(), new Vector3(deserializer.deserializeFloatArray())); + } +} diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java b/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java index 1c62e0d..a00c0ff 100644 --- a/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java +++ b/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java @@ -2,8 +2,10 @@ package kz.ilotterytea.frogartha.utils; import com.github.czyzby.websocket.serialization.impl.ManualSerializer; import kz.ilotterytea.frogartha.domain.Identity; +import kz.ilotterytea.frogartha.domain.client.ChangedDirectionAction; import kz.ilotterytea.frogartha.domain.client.PlayerJumpAction; import kz.ilotterytea.frogartha.domain.server.Acknowledge; +import kz.ilotterytea.frogartha.events.ChangedDirectionEvent; import kz.ilotterytea.frogartha.events.PlayerJumpEvent; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; @@ -14,5 +16,7 @@ public class SerializerUtils { serializer.register(new PlayerKickException()); serializer.register(new PlayerJumpAction()); serializer.register(new PlayerJumpEvent()); + serializer.register(new ChangedDirectionAction()); + serializer.register(new ChangedDirectionEvent()); } } -- cgit v1.2.3