diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/src/ch/asynk/tankontank/game/Hud.java | 154 |
1 files changed, 141 insertions, 13 deletions
diff --git a/core/src/ch/asynk/tankontank/game/Hud.java b/core/src/ch/asynk/tankontank/game/Hud.java index 8fa2b32..2f0e0c6 100644 --- a/core/src/ch/asynk/tankontank/game/Hud.java +++ b/core/src/ch/asynk/tankontank/game/Hud.java @@ -2,32 +2,160 @@ package ch.asynk.tankontank.game; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.scenes.scene2d.Stage; -import com.badlogic.gdx.scenes.scene2d.ui.Label; +import com.badlogic.gdx.utils.Disposable; -import com.badlogic.gdx.utils.viewport.Viewport; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.TextureAtlas; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.math.Rectangle; + +import ch.asynk.tankontank.engine.gfx.Image; import ch.asynk.tankontank.TankOnTank; -public class Hud extends Stage +class ActionBtn implements Disposable +{ + boolean enabled; + private Image on; + private Image off; + + public ActionBtn(TextureAtlas atlas, String off, String on) + { + this.enabled = false; + this.on = new Image(atlas.findRegion(on)); + this.off = new Image(atlas.findRegion(off)); + } + + public void toggle() + { + enabled = !enabled; + } + + public Image getImage() + { + return (enabled ? on : off); + } + + public void setPosition(float x, float y) + { + on.setPosition(x, y); + off.setPosition(x, y); + } + + public boolean hit(float x, float y) + { + return ((x > on.getX()) && (x < on.getX() + on.getWidth()) && (y > on.getY()) && (y < on.getY() + on.getHeight())); + } + + @Override + public void dispose() + { + on.dispose(); + off.dispose(); + } + + public float getX() { return on.getX(); } + public float getY() { return on.getY(); } + public float getWidth() { return on.getWidth(); } + public float getHeight() { return on.getHeight(); } +} + +public class Hud implements Disposable { private final TankOnTank game; - private Label fps; - public Hud(final TankOnTank game, Viewport viewport) + private ActionBtn flagAct; + private ActionBtn moveAct; + private ActionBtn rotateAct; + private ActionBtn attackAct; + private ActionBtn cancelAct; + + private Rectangle rect; + private float elapsed; + + public Hud(final TankOnTank game) { - super(viewport); this.game = game; - fps = new Label("FPS: 0", game.skin); - fps.setPosition( 10, Gdx.graphics.getHeight() - 40); - addActor(fps); + TextureAtlas atlas = game.manager.get("data/assets.atlas", TextureAtlas.class); + + flagAct = new ActionBtn(atlas, "us-flag", "ge-flag"); + moveAct = new ActionBtn(atlas, "act-move", "act-move-on"); + rotateAct = new ActionBtn(atlas, "act-rotate", "act-rotate-on"); + attackAct = new ActionBtn(atlas, "act-attack", "act-attack-on"); + cancelAct = new ActionBtn(atlas, "act-cancel", "act-cancel-on"); + + flagAct.setPosition(5, (Gdx.graphics.getHeight() - flagAct.getHeight() - 5)); + moveAct.setPosition(flagAct.getX(), ( flagAct.getY() - moveAct.getHeight() - 5)); + rotateAct.setPosition(flagAct.getX(), ( moveAct.getY() - rotateAct.getHeight() - 5)); + attackAct.setPosition(flagAct.getX(), ( rotateAct.getY() - attackAct.getHeight() - 5)); + cancelAct.setPosition(flagAct.getX(), ( attackAct.getY() - cancelAct.getHeight() - 5)); + + rect = new Rectangle(cancelAct.getX(), cancelAct.getY(), flagAct.getWidth(), + (flagAct.getY() + flagAct.getHeight() - cancelAct.getY())); + + elapsed = 0f; } @Override - public void act(float delta) + public void dispose() + { + flagAct.dispose(); + moveAct.dispose(); + rotateAct.dispose(); + attackAct.dispose(); + cancelAct.dispose(); + } + + public void animate(float delta) { - super.act(delta); - fps.setText("FPS: " + Gdx.graphics.getFramesPerSecond()); + elapsed += delta; + if (elapsed > 5f) { + elapsed = 0f; + flagAct.toggle(); + } + } + + public void draw(Batch batch) + { + flagAct.getImage().draw(batch); + moveAct.getImage().draw(batch); + rotateAct.getImage().draw(batch); + attackAct.getImage().draw(batch); + cancelAct.getImage().draw(batch); + } + + public boolean touchDown(float x, float y) + { + if (!rect.contains(x,y)) return false; + + if (moveAct.hit(x, y)) { + moveAct.toggle(); + } else if (rotateAct.hit(x, y)) { + rotateAct.toggle(); + } else if (attackAct.hit(x, y)) { + attackAct.toggle(); + } else if (cancelAct.hit(x, y)) { + cancelAct.toggle(); + } + + return true; + } + + public boolean touchUp(float x, float y) + { + if (!rect.contains(x,y)) return false; + + if (moveAct.hit(x, y)) { + moveAct.toggle(); + } else if (rotateAct.hit(x, y)) { + rotateAct.toggle(); + } else if (attackAct.hit(x, y)) { + attackAct.toggle(); + } else if (cancelAct.hit(x, y)) { + cancelAct.toggle(); + } + + return true; } } |