diff options
| author | ilotterytea <iltsu@alright.party> | 2024-04-27 23:55:50 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-04-27 23:55:50 +0500 |
| commit | ffee28043fc5acf03a94966f57745a0596bebff5 (patch) | |
| tree | 7e363b80c25ea7c0ba6c415a78ccb8ee90bfa638 /core | |
| parent | 064a2728b4b4b606a0a0ee718431cb1472229e7e (diff) | |
feat: decal player
Diffstat (limited to 'core')
| -rw-r--r-- | core/src/com/ilotterytea/maxoning/player/DecalPlayer.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/core/src/com/ilotterytea/maxoning/player/DecalPlayer.java b/core/src/com/ilotterytea/maxoning/player/DecalPlayer.java new file mode 100644 index 0000000..30ee447 --- /dev/null +++ b/core/src/com/ilotterytea/maxoning/player/DecalPlayer.java @@ -0,0 +1,71 @@ +package com.ilotterytea.maxoning.player; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Camera; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.graphics.g3d.decals.Decal; +import com.badlogic.gdx.math.Intersector; +import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.math.collision.BoundingBox; +import com.badlogic.gdx.math.collision.Ray; + +public class DecalPlayer { + private final TextureRegion[] regions; + private int regionIndex; + private final Decal decal; + private final BoundingBox box; + + public DecalPlayer(TextureRegion[] regions) { + this.regions = regions; + this.regionIndex = 0; + + this.decal = Decal.newDecal(this.regions[this.regionIndex]); + this.decal.setScale(0.025f); + this.decal.setPosition(-4.0f, 1.75f, -4.0f); + + float width = this.decal.getWidth() / (this.decal.getScaleX() * 1000f); + float height = this.decal.getHeight() / (this.decal.getScaleY() * 1000f); + + Vector3 position = this.decal.getPosition(); + Vector3 minBox = new Vector3(position.x - width / 3, position.y - height / 3, position.z - width / 3); + Vector3 maxBox = new Vector3(position.x + width / 3, position.y + height / 3, position.z + width / 3); + + this.box = new BoundingBox(minBox, maxBox); + } + + public void render(Camera camera) { + checkCollisions(camera); + } + + private void checkCollisions(Camera camera) { + Ray ray = null; + + if (Gdx.input.justTouched()) { + ray = camera.getPickRay(Gdx.input.getX(), Gdx.input.getY()); + } + + if (ray == null) { + return; + } + + Vector3 intersection = new Vector3(); + + if (Intersector.intersectRayBounds(ray, box, intersection)) { + updateTextureRegion(); + } + } + + private void updateTextureRegion() { + this.regionIndex++; + + if (this.regions[this.regionIndex] == null) { + this.regionIndex = 0; + } + + this.decal.setTextureRegion(this.regions[this.regionIndex]); + } + + public Decal getDecal() { + return this.decal; + } +} |
