diff options
| author | ilotterytea <iltsu@alright.party> | 2025-01-22 02:35:59 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-01-22 02:35:59 +0500 |
| commit | b96a6d7005fcb90a21aeb21ddebe8bb7b82a7ef5 (patch) | |
| tree | f28ec334a0eb426a92d3403bf4f3708d66a32124 | |
| parent | 86e91616e8585e05656bac7be356f342be006952 (diff) | |
upd: separated frog logic from input logic in PlayerEntity
3 files changed, 77 insertions, 61 deletions
diff --git a/core/src/main/java/kz/ilotterytea/frogartha/entities/LocalPlayerEntity.java b/core/src/main/java/kz/ilotterytea/frogartha/entities/LocalPlayerEntity.java new file mode 100644 index 0000000..4971635 --- /dev/null +++ b/core/src/main/java/kz/ilotterytea/frogartha/entities/LocalPlayerEntity.java @@ -0,0 +1,69 @@ +package kz.ilotterytea.frogartha.entities; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.graphics.Camera; +import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.math.collision.Ray; +import kz.ilotterytea.frogartha.FrogarthaGame; +import kz.ilotterytea.frogartha.domain.actions.ChangedDirectionAction; +import kz.ilotterytea.frogartha.domain.actions.PlayerJumpAction; + +public class LocalPlayerEntity extends PlayerEntity { + private final Camera camera; + private boolean isJumpKeyPressed, lockJump; + + public LocalPlayerEntity(Camera camera) { + super(); + this.camera = camera; + setCameraPosition(); + } + + @Override + public void update(float delta) { + super.update(delta); + if (lockJump && !isJumping) lockJump = false; + if (!lockJump) handlePlayerJump(delta); + + updatePlayerLook(); + } + + @Override + public void setPosition(float x, float y, float z) { + super.setPosition(x, y, z); + setCameraPosition(); + } + + private void handlePlayerJump(float delta) { + int jumpKey = Input.Keys.SPACE; + + if (Gdx.input.isKeyPressed(jumpKey)) { + isJumpKeyPressed = true; + setJumpStrength(jumpStrength + delta); + } else if (isJumpKeyPressed) { + isJumpKeyPressed = false; + lockJump = true; + FrogarthaGame.getInstance().getSessionClient().send(new PlayerJumpAction(jumpStrength)); + } + } + + private void updatePlayerLook() { + // cv pasted from https://stackoverflow.com/a/18552800 + 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); + 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() { + float zoomScale = 4f; + + this.camera.position.set(position.x + zoomScale, position.y + zoomScale / 2f, position.z); + this.camera.lookAt(position); + this.camera.update(); + } +} 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 94f5e28..8cc70bc 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java @@ -1,58 +1,26 @@ package kz.ilotterytea.frogartha.entities; 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.actions.ChangedDirectionAction; -import kz.ilotterytea.frogartha.domain.actions.PlayerJumpAction; public class PlayerEntity extends RenderableEntity { - private final Camera camera; - private Vector3 startPosition, endPosition; + protected Vector3 startPosition, endPosition; - private float jumpStrength, elapsedTime; - private boolean isJumpKeyPressed, isJumping, lockJump; + protected float jumpStrength, elapsedTime; + protected boolean isJumping; - public PlayerEntity(Camera camera) { + public PlayerEntity() { super(new Texture(Gdx.files.internal("sprites/player/player.png"))); - this.camera = camera; this.endPosition = Vector3.Zero; this.startPosition = Vector3.Zero; - setCameraPosition(); } @Override public void update(float delta) { - if (!lockJump) handlePlayerJump(delta); - if (isJumping) performJump(delta); - - updatePlayerLook(); - } - - @Override - public void setPosition(float x, float y, float z) { - super.setPosition(x, y, z); - setCameraPosition(); - } - - private void handlePlayerJump(float delta) { - int jumpKey = Input.Keys.SPACE; - - if (Gdx.input.isKeyPressed(jumpKey)) { - isJumpKeyPressed = true; - setJumpStrength(jumpStrength + delta); - } else if (isJumpKeyPressed) { - isJumpKeyPressed = false; - lockJump = true; - FrogarthaGame.getInstance().getSessionClient().send(new PlayerJumpAction(jumpStrength)); - } } private void performJump(float delta) { @@ -68,7 +36,6 @@ public class PlayerEntity extends RenderableEntity { if (elapsedTime >= jumpStrength) { elapsedTime = 0f; - lockJump = false; setStartPosition(Vector3.Zero); setEndPosition(Vector3.Zero); setJumping(false); @@ -76,26 +43,6 @@ public class PlayerEntity extends RenderableEntity { } } - private void updatePlayerLook() { - // cv pasted from https://stackoverflow.com/a/18552800 - 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); - 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() { - float zoomScale = 4f; - - this.camera.position.set(position.x + zoomScale, position.y + zoomScale / 2f, position.z); - this.camera.lookAt(position); - this.camera.update(); - } - public float getJumpStrength() { return jumpStrength; } 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 51f7287..3da9d55 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/screens/GameScreen.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/screens/GameScreen.java @@ -11,7 +11,7 @@ import com.badlogic.gdx.graphics.g3d.decals.CameraGroupStrategy; import com.badlogic.gdx.graphics.g3d.decals.DecalBatch; import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; import com.badlogic.gdx.math.Vector3; -import kz.ilotterytea.frogartha.entities.PlayerEntity; +import kz.ilotterytea.frogartha.entities.LocalPlayerEntity; import net.mgsx.gltf.scene3d.attributes.PBRCubemapAttribute; import net.mgsx.gltf.scene3d.attributes.PBRTextureAttribute; import net.mgsx.gltf.scene3d.lights.DirectionalShadowLight; @@ -26,13 +26,13 @@ public class GameScreen implements Screen { private SceneManager sceneManager; private DecalBatch decalBatch; - private PlayerEntity playerEntity; + private LocalPlayerEntity playerEntity; @Override public void show() { create3D(); - playerEntity = new PlayerEntity(camera); + playerEntity = new LocalPlayerEntity(camera); playerEntity.setPosition(0f, 1f, 0f); } @@ -127,7 +127,7 @@ public class GameScreen implements Screen { decalBatch = new DecalBatch(new CameraGroupStrategy(camera)); } - public PlayerEntity getPlayerEntity() { + public LocalPlayerEntity getPlayerEntity() { return playerEntity; } } |
