diff options
| author | ilotterytea <iltsu@alright.party> | 2025-01-22 00:40:57 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-01-22 00:41:39 +0500 |
| commit | a7f77c115bc95eb8a667df1146cc26dc17367879 (patch) | |
| tree | 77b08b0cb4f07f9b3d21c277d5621bd0ee5a488c /core | |
| parent | e4997681a2d08adc9b9055b0b2a1dc52d54edd47 (diff) | |
feat: player jumps like a frog!!! (FROGartha reference) + player state
Diffstat (limited to 'core')
5 files changed, 85 insertions, 26 deletions
diff --git a/core/src/main/java/kz/ilotterytea/frogartha/FrogarthaConstants.java b/core/src/main/java/kz/ilotterytea/frogartha/FrogarthaConstants.java deleted file mode 100644 index f80948a..0000000 --- a/core/src/main/java/kz/ilotterytea/frogartha/FrogarthaConstants.java +++ /dev/null @@ -1,7 +0,0 @@ -package kz.ilotterytea.frogartha; - -public class FrogarthaConstants { - public static class URLS { - public static String SESSION_WSS = "ws://localhost:20015"; - } -} 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 da100fa..76f7f4c 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java @@ -4,23 +4,34 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.Camera; import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.math.MathUtils; 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.PlayerJumpAction; public class PlayerEntity extends RenderableEntity { + private final Vector3 startPosition, endPosition; private final Camera camera; - private final float velocity; + + private float jumpStrength, elapsedTime; + private boolean isJumpKeyPressed, isJumping, lockJump; public PlayerEntity(Camera camera) { super(new Texture(Gdx.files.internal("sprites/player/player.png"))); this.camera = camera; - this.velocity = 8f; + this.endPosition = Vector3.Zero; + this.startPosition = Vector3.Zero; setCameraPosition(); } @Override public void update(float delta) { - updatePlayerPosition(); + if (!lockJump) handlePlayerJump(delta); + + if (isJumping) performJump(delta); + updatePlayerLook(); } @@ -30,27 +41,38 @@ public class PlayerEntity extends RenderableEntity { setCameraPosition(); } - private void updatePlayerPosition() { - float xMovement = 0f, zMovement = 0f; + private void handlePlayerJump(float delta) { + int jumpKey = Input.Keys.SPACE; - if (Gdx.input.isKeyPressed(Input.Keys.W)) { - xMovement = -1f; - } else if (Gdx.input.isKeyPressed(Input.Keys.S)) { - xMovement = 1f; + if (Gdx.input.isKeyPressed(jumpKey)) { + isJumpKeyPressed = true; + setJumpStrength(jumpStrength + delta); + } else if (isJumpKeyPressed) { + isJumpKeyPressed = false; + lockJump = true; + FrogarthaGame.getInstance().getSessionClient().send(new PlayerJumpAction(jumpStrength)); } + } - if (Gdx.input.isKeyPressed(Input.Keys.A)) { - zMovement = 1f; - } else if (Gdx.input.isKeyPressed(Input.Keys.D)) { - zMovement = -1f; - } + private void performJump(float delta) { + elapsedTime += delta; - xMovement *= velocity / 100f; - zMovement *= velocity / 100f; + float t = elapsedTime / jumpStrength; - setPosition(position.x + xMovement, position.y, position.z + zMovement); + float x = MathUtils.lerp(startPosition.x, endPosition.x, t); + float z = MathUtils.lerp(startPosition.z, endPosition.z, t); - if (xMovement != 0f || zMovement != 0f) setCameraPosition(); + float y = FrogarthaConstants.Player.MAX_JUMP_HEIGHT - 4 * FrogarthaConstants.Player.MAX_JUMP_HEIGHT * (t - 0.5f) * (t - 0.5f); + setPosition(x, y, z); + + if (elapsedTime >= jumpStrength) { + elapsedTime = 0f; + lockJump = false; + setStartPosition(Vector3.Zero); + setEndPosition(Vector3.Zero); + setJumping(false); + setJumpStrength(0f); + } } private void updatePlayerLook() { @@ -58,7 +80,7 @@ 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, 1f, point.z); + super.setDirection(point.x, position.y, point.z); } private void setCameraPosition() { @@ -68,4 +90,24 @@ public class PlayerEntity extends RenderableEntity { this.camera.lookAt(position); this.camera.update(); } + + public float getJumpStrength() { + return jumpStrength; + } + + public void setJumpStrength(float jumpStrength) { + this.jumpStrength = Math.min(jumpStrength, FrogarthaConstants.Player.MAX_JUMP_STRENGTH); + } + + public void setJumping(boolean jumping) { + isJumping = jumping; + } + + public void setStartPosition(Vector3 startPosition) { + this.startPosition.set(startPosition.x, startPosition.y, startPosition.z); + } + + public void setEndPosition(Vector3 endPosition) { + this.endPosition.set(endPosition.x, endPosition.y, endPosition.z); + } } diff --git a/core/src/main/java/kz/ilotterytea/frogartha/screens/GameScreen.java b/core/src/main/java/kz/ilotterytea/frogartha/screens/GameScreen.java index d8f9b36..24fc00c 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/screens/GameScreen.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/screens/GameScreen.java @@ -125,4 +125,8 @@ public class GameScreen implements Screen { decalBatch = new DecalBatch(new CameraGroupStrategy(camera)); } + + public PlayerEntity getPlayerEntity() { + return playerEntity; + } } 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 c775e57..73dfd3c 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.PlayerJumpEvent; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; import kz.ilotterytea.frogartha.utils.Logger; import kz.ilotterytea.frogartha.utils.SerializerUtils; @@ -60,6 +61,7 @@ public class SessionClient implements WebSocketListener { } 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; } catch (PlayerKickException e) { log.log("Kicked out: {}", e.getMessage()); 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 383640b..c4d4237 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java @@ -2,6 +2,9 @@ package kz.ilotterytea.frogartha.sessions; import kz.ilotterytea.frogartha.FrogarthaGame; import kz.ilotterytea.frogartha.domain.server.Acknowledge; +import kz.ilotterytea.frogartha.entities.PlayerEntity; +import kz.ilotterytea.frogartha.events.PlayerJumpEvent; +import kz.ilotterytea.frogartha.screens.GameScreen; import kz.ilotterytea.frogartha.utils.Logger; public class SessionHandlers { @@ -16,4 +19,19 @@ public class SessionHandlers { game.getIdentityClient().setAuthorized(true); } } + + public static void handlePlayerJumpEvent(PlayerJumpEvent event) { + if (!game.getScreen().getClass().equals(GameScreen.class)) { + log.log("The screen is not GameScreen, but the session received PlayerJumpEvent"); + return; + } + + GameScreen screen = (GameScreen) game.getScreen(); + PlayerEntity entity = screen.getPlayerEntity(); + + entity.setJumpStrength(event.getJumpStrength()); + entity.setStartPosition(event.getStartPosition()); + entity.setEndPosition(event.getEndPosition()); + entity.setJumping(true); + } } |
