diff options
Diffstat (limited to 'core')
4 files changed, 136 insertions, 2 deletions
diff --git a/core/src/main/java/kz/ilotterytea/frogartha/entities/Entity.java b/core/src/main/java/kz/ilotterytea/frogartha/entities/Entity.java new file mode 100644 index 0000000..f8a7ce5 --- /dev/null +++ b/core/src/main/java/kz/ilotterytea/frogartha/entities/Entity.java @@ -0,0 +1,4 @@ +package kz.ilotterytea.frogartha.entities; + +public abstract class Entity { +} diff --git a/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java b/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java new file mode 100644 index 0000000..da100fa --- /dev/null +++ b/core/src/main/java/kz/ilotterytea/frogartha/entities/PlayerEntity.java @@ -0,0 +1,71 @@ +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.Vector3; +import com.badlogic.gdx.math.collision.Ray; + +public class PlayerEntity extends RenderableEntity { + private final Camera camera; + private final float velocity; + + public PlayerEntity(Camera camera) { + super(new Texture(Gdx.files.internal("sprites/player/player.png"))); + this.camera = camera; + this.velocity = 8f; + setCameraPosition(); + } + + @Override + public void update(float delta) { + updatePlayerPosition(); + updatePlayerLook(); + } + + @Override + public void setPosition(float x, float y, float z) { + super.setPosition(x, y, z); + setCameraPosition(); + } + + private void updatePlayerPosition() { + float xMovement = 0f, zMovement = 0f; + + if (Gdx.input.isKeyPressed(Input.Keys.W)) { + xMovement = -1f; + } else if (Gdx.input.isKeyPressed(Input.Keys.S)) { + xMovement = 1f; + } + + if (Gdx.input.isKeyPressed(Input.Keys.A)) { + zMovement = 1f; + } else if (Gdx.input.isKeyPressed(Input.Keys.D)) { + zMovement = -1f; + } + + xMovement *= velocity / 100f; + zMovement *= velocity / 100f; + + setPosition(position.x + xMovement, position.y, position.z + zMovement); + + if (xMovement != 0f || zMovement != 0f) setCameraPosition(); + } + + 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); + super.setDirection(point.x, 1f, point.z); + } + + 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/RenderableEntity.java b/core/src/main/java/kz/ilotterytea/frogartha/entities/RenderableEntity.java new file mode 100644 index 0000000..39cc866 --- /dev/null +++ b/core/src/main/java/kz/ilotterytea/frogartha/entities/RenderableEntity.java @@ -0,0 +1,52 @@ +package kz.ilotterytea.frogartha.entities; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.graphics.g3d.decals.Decal; +import com.badlogic.gdx.graphics.g3d.decals.DecalBatch; +import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.math.collision.BoundingBox; + +public abstract class RenderableEntity extends Entity { + protected final Vector3 position, direction; + protected final Decal decal; + protected final BoundingBox boundingBox; + + public RenderableEntity(Texture texture) { + this.decal = Decal.newDecal(1f, 1f, new TextureRegion(texture), true); + this.boundingBox = new BoundingBox(); + this.position = new Vector3(); + this.direction = new Vector3(); + } + + public void render(DecalBatch decalBatch) { + decalBatch.add(decal); + } + + public Vector3 getPosition() { + return position; + } + + public void setPosition(float x, float y, float z) { + position.set(x, y, z); + decal.setPosition(position); + + float xSize = 2.5f, ySize = 1f; + + boundingBox.set( + new Vector3(position.x - xSize, position.y, position.z - xSize), + new Vector3(position.x + xSize, position.y + ySize, xSize + 2.5f) + ); + } + + public Vector3 getDirection() { + return direction; + } + + public void setDirection(float x, float y, float z) { + direction.set(x, y, z); + decal.lookAt(direction, Vector3.Y); + } + + public abstract void update(float delta); +} 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 509566e..d8f9b36 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/screens/GameScreen.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/screens/GameScreen.java @@ -11,6 +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 net.mgsx.gltf.scene3d.attributes.PBRCubemapAttribute; import net.mgsx.gltf.scene3d.attributes.PBRTextureAttribute; import net.mgsx.gltf.scene3d.lights.DirectionalShadowLight; @@ -25,9 +26,14 @@ public class GameScreen implements Screen { private SceneManager sceneManager; private DecalBatch decalBatch; + private PlayerEntity playerEntity; + @Override public void show() { create3D(); + + playerEntity = new PlayerEntity(camera); + playerEntity.setPosition(0f, 1f, 0f); } @Override @@ -36,6 +42,9 @@ public class GameScreen implements Screen { sceneManager.update(delta); sceneManager.render(); + playerEntity.update(delta); + playerEntity.render(decalBatch); + decalBatch.flush(); } @@ -78,8 +87,6 @@ public class GameScreen implements Screen { camera = new PerspectiveCamera(60f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.near = 1f; camera.far = 300f; - camera.position.set(0f, 8f, 15f); - camera.lookAt(planeScene.modelInstance.transform.getTranslation(new Vector3())); camera.update(); sceneManager.setCamera(camera); |
