diff options
| author | ilotterytea <iltsu@alright.party> | 2025-01-22 01:00:49 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-01-22 01:00:49 +0500 |
| commit | b3739521c1dcaed9bd451e00067b7d304521745f (patch) | |
| tree | 04403d23708e2cdfa35452243323c9f24cde5e26 /core/src | |
| parent | a7f77c115bc95eb8a667df1146cc26dc17367879 (diff) | |
feat: player direction is now calculated by server
Diffstat (limited to 'core/src')
3 files changed, 32 insertions, 4 deletions
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); + } } |
