diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-11-10 11:11:26 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-11-10 11:11:26 +0100 |
commit | 2cfbb884e942492d1389fbd47daeeb74451235c8 (patch) | |
tree | ce494391cc47a94b5c4c85c1c8c1a32d9c5c1526 /core/src | |
parent | e70469fb5a0bd150427f8af1108e48b8f4625376 (diff) | |
download | RustAndDust-2cfbb884e942492d1389fbd47daeeb74451235c8.zip RustAndDust-2cfbb884e942492d1389fbd47daeeb74451235c8.tar.gz |
use ActionButtons
Diffstat (limited to 'core/src')
10 files changed, 60 insertions, 165 deletions
diff --git a/core/src/ch/asynk/tankontank/game/Ctrl.java b/core/src/ch/asynk/tankontank/game/Ctrl.java index 9092471..24e9344 100644 --- a/core/src/ch/asynk/tankontank/game/Ctrl.java +++ b/core/src/ch/asynk/tankontank/game/Ctrl.java @@ -134,7 +134,14 @@ public class Ctrl implements Disposable public void setState(State.StateType state) { - setState(state, true); + if (state == State.StateType.ABORT) { + hud.notify("Action canceled"); + this.state.abort(); + } + else if (state == State.StateType.DONE) + this.state.done(); + else + setState(state, true); } public void setState(State.StateType state, boolean normal) @@ -177,16 +184,6 @@ public class Ctrl implements Disposable this.state.enter(normal); } - public void abort() - { - state.abort(); - } - - public void done() - { - state.done(); - } - public void touchDown(float x, float y) { if (!blockMap && state.downInMap(x, y)) diff --git a/core/src/ch/asynk/tankontank/game/Hud.java b/core/src/ch/asynk/tankontank/game/Hud.java index adf8fc6..9cad8aa 100644 --- a/core/src/ch/asynk/tankontank/game/Hud.java +++ b/core/src/ch/asynk/tankontank/game/Hud.java @@ -15,7 +15,7 @@ import ch.asynk.tankontank.engine.gfx.Image; import ch.asynk.tankontank.game.State.StateType; import ch.asynk.tankontank.game.hud.Msg; import ch.asynk.tankontank.game.hud.Bg; -import ch.asynk.tankontank.game.hud.Button; +import ch.asynk.tankontank.game.hud.ActionButtons; import ch.asynk.tankontank.game.hud.LabelImage; import ch.asynk.tankontank.game.hud.UnitDock; import ch.asynk.tankontank.game.hud.OkCancel; @@ -31,17 +31,9 @@ public class Hud implements Disposable private final TankOnTank game; private final Ctrl ctrl; - private Bg actionsBg; private Msg msg; - private Button btn; private Object hit; - public Button moveBtn; - public Button rotateBtn; - public Button promoteBtn; - public Button attackBtn; - public Button checkBtn; - public Button cancelBtn; private Image flag; private Image usFlag; @@ -51,13 +43,13 @@ public class Hud implements Disposable private LabelImage reinforcement; private UnitDock unitDock; + public ActionButtons actionButtons; + private BitmapFont font; private OkCancel okCancel; private OkCancelAction okCancelAction; - private Vector2 corner; - enum OkCancelAction { END_TURN, @@ -67,18 +59,12 @@ public class Hud implements Disposable { this.game = game; this.ctrl = ctrl; - this.corner = new Vector2((Gdx.graphics.getWidth() - OFFSET), OFFSET); font = game.skin.getFont("default-font"); TextureAtlas atlas = game.factory.hudAtlas; - moveBtn = new Button(atlas, "btn-move"); - rotateBtn = new Button(atlas, "btn-rotate"); - promoteBtn = new Button(atlas, "btn-promote"); - attackBtn = new Button(atlas, "btn-attack"); - checkBtn = new Button(atlas, "btn-check"); - cancelBtn = new Button(atlas, "btn-cancel"); - actionsBg = new Bg(atlas.findRegion("disabled")); + actionButtons = new ActionButtons(ctrl, atlas.findRegion("disabled"), atlas, 10f); + actionButtons.hide(); usFlag = new Image(atlas.findRegion("us-flag")); geFlag = new Image(atlas.findRegion("ge-flag")); @@ -100,8 +86,6 @@ public class Hud implements Disposable reinforcement.setPosition(x, usFlag.getY() - reinforcement.getHeight() - 0); reinforcement.setLabelPosition(Position.TOP_LEFT); unitDock.setTopLeft(OFFSET, reinforcement.getY() - 5); - - hide(); } @Override @@ -109,13 +93,7 @@ public class Hud implements Disposable { font.dispose(); - moveBtn.dispose(); - rotateBtn.dispose(); - promoteBtn.dispose(); - attackBtn.dispose(); - checkBtn.dispose(); - cancelBtn.dispose(); - actionsBg.dispose(); + actionButtons.dispose(); usFlag.dispose(); geFlag.dispose(); @@ -172,13 +150,7 @@ public class Hud implements Disposable reinforcement.draw(batch); unitDock.draw(batch); - actionsBg.draw(batch); - moveBtn.draw(batch); - rotateBtn.draw(batch); - promoteBtn.draw(batch); - attackBtn.draw(batch); - checkBtn.draw(batch); - cancelBtn.draw(batch); + actionButtons.draw(batch); msg.draw(batch); okCancel.draw(batch); @@ -192,13 +164,7 @@ public class Hud implements Disposable reinforcement.drawDebug(debugShapes); unitDock.drawDebug(debugShapes); - actionsBg.drawDebug(debugShapes); - moveBtn.drawDebug(debugShapes); - rotateBtn.drawDebug(debugShapes); - promoteBtn.drawDebug(debugShapes); - attackBtn.drawDebug(debugShapes); - checkBtn.drawDebug(debugShapes); - cancelBtn.drawDebug(debugShapes); + actionButtons.drawDebug(debugShapes); msg.drawDebug(debugShapes); okCancel.drawDebug(debugShapes); @@ -225,46 +191,6 @@ public class Hud implements Disposable else msg.write(s, 1, position); } - private float setButton(Button btn, float x, float y) - { - btn.setUp(); - btn.visible = true; - btn.setPosition(x, y); - return (y + btn.getHeight() + PADDING); - } - - public void show(boolean promote, boolean rotate, boolean move, boolean attack, boolean check, boolean cancel) - { - float x = (corner.x - checkBtn.getWidth()); - float y = corner.y; - - if (move) y = setButton(moveBtn, x, y); - else moveBtn.hide(); - if (rotate) y = setButton(rotateBtn, x, y); - else rotateBtn.hide(); - if (attack) y = setButton(attackBtn, x, y); - else attackBtn.hide(); - if (promote) y = setButton(promoteBtn, x, y); - else promoteBtn.hide(); - if (cancel) y = setButton(cancelBtn, x, y); - else cancelBtn.hide(); - if (check) y = setButton(checkBtn, x, y); - else checkBtn.hide(); - - actionsBg.set((x - PADDING), (corner.y - PADDING), (checkBtn.getWidth() + (2 * PADDING)), (y - corner.y + PADDING)); - } - - public void hide() - { - moveBtn.hide(); - rotateBtn.hide(); - promoteBtn.hide(); - attackBtn.hide(); - checkBtn.hide(); - cancelBtn.hide(); - actionsBg.set(0, 0, 0, 0); - } - public void hideUnitDock() { unitDock.hide(); @@ -272,10 +198,11 @@ public class Hud implements Disposable public boolean touchDown(float x, float y) { - btn = null; hit = null; - if (turns.hit(x,y)) + if (actionButtons.touchDown(x, y)) + hit = actionButtons; + else if (turns.hit(x,y)) hit = turns; else if (unitDock.hit(x, y)) hit = unitDock; @@ -283,63 +210,30 @@ public class Hud implements Disposable hit = reinforcement; else if (okCancel.hit(x, y)) hit = okCancel; - else if (actionsBg.hit(x,y)) { - if (moveBtn.hit(x, y)) - btn = moveBtn; - else if (rotateBtn.hit(x, y)) - btn = rotateBtn; - else if (promoteBtn.hit(x, y)) - btn = promoteBtn; - else if (attackBtn.hit(x, y)) - btn = attackBtn; - else if (checkBtn.hit(x, y)) - btn = checkBtn; - else if (cancelBtn.hit(x, y)) - btn = cancelBtn; - } else + else return false; - if (btn != null) - btn.setDown(); - return true; } public boolean touchUp(float x, float y) { - if (btn != null) { - if (actionsBg.hit(x, y)) { - if ((btn == moveBtn) && moveBtn.hit(x, y)) - ctrl.setState(StateType.MOVE); - else if ((btn == rotateBtn) && rotateBtn.hit(x, y)) - ctrl.setState(StateType.ROTATE); - else if ((btn == promoteBtn) && promoteBtn.hit(x, y)) - ctrl.setState(StateType.PROMOTE); - else if ((btn == attackBtn) && attackBtn.hit(x, y)) - ctrl.setState(StateType.ATTACK); - else if ((btn == checkBtn) && checkBtn.hit(x, y)) - ctrl.done(); - else if ((btn == cancelBtn) && cancelBtn.hit(x, y)) { - notify("Action canceled"); - ctrl.abort(); - } else - btn.setUp(); - } else - btn.setUp(); - btn = null; - } else if (hit != null) { - if ((hit == turns) && turns.hit(x, y)) - askEndTurn(); - else if ((hit == reinforcement) && reinforcement.hit(x, y)) - unitDock.toggle(); - else if ((hit == unitDock) && unitDock.hit(x, y)) - ctrl.setState(StateType.ENTRY); - else if ((hit == okCancel) && okCancel.hit(x, y)) - closeOkCancel(); - hit = null; - } else + if (hit == null) return false; + if (hit == actionButtons) + actionButtons.touchUp(x, y); + else if ((hit == turns) && turns.hit(x, y)) + askEndTurn(); + else if ((hit == reinforcement) && reinforcement.hit(x, y)) + unitDock.toggle(); + else if ((hit == unitDock) && unitDock.hit(x, y)) + ctrl.setState(StateType.ENTRY); + else if ((hit == okCancel) && okCancel.hit(x, y)) + closeOkCancel(); + + hit = null; + return true; } diff --git a/core/src/ch/asynk/tankontank/game/states/StateAttack.java b/core/src/ch/asynk/tankontank/game/states/StateAttack.java index 80c240b..e2ed73e 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateAttack.java +++ b/core/src/ch/asynk/tankontank/game/states/StateAttack.java @@ -1,6 +1,7 @@ package ch.asynk.tankontank.game.states; import ch.asynk.tankontank.game.Unit; +import ch.asynk.tankontank.game.hud.ActionButtons.Buttons; import ch.asynk.tankontank.TankOnTank; @@ -10,8 +11,8 @@ public class StateAttack extends StateCommon public void enter(boolean fromSelect) { map.possibleTargets.clear(); - ctrl.hud.show(false, false, false, true, false, ctrl.cfg.canCancel); - ctrl.hud.attackBtn.setOn(); + ctrl.hud.actionButtons.show(Buttons.ATTACK.b | ((ctrl.cfg.canCancel) ? Buttons.ABORT.b : 0)); + ctrl.hud.actionButtons.setOn(Buttons.ATTACK); // activeUnit is the target if (fromSelect) { @@ -61,7 +62,7 @@ public class StateAttack extends StateCommon activeUnit.showTarget(); map.collectAttackAssists(selectedUnit, activeUnit, ctrl.player.unitIterator()); map.showAttackAssists(); - ctrl.hud.show(false, false, false, true, true, ctrl.cfg.canCancel); + ctrl.hud.actionButtons.show(Buttons.ATTACK.b | Buttons.DONE.b | ((ctrl.cfg.canCancel) ? Buttons.ABORT.b : 0)); } if ((activeUnit != null) && map.attackAssists.contains(unit)) { diff --git a/core/src/ch/asynk/tankontank/game/states/StateBreak.java b/core/src/ch/asynk/tankontank/game/states/StateBreak.java index fa3023f..be7f460 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateBreak.java +++ b/core/src/ch/asynk/tankontank/game/states/StateBreak.java @@ -2,6 +2,7 @@ package ch.asynk.tankontank.game.states; import ch.asynk.tankontank.engine.Orientation; import ch.asynk.tankontank.game.Unit; +import ch.asynk.tankontank.game.hud.ActionButtons.Buttons; import ch.asynk.tankontank.TankOnTank; @@ -15,7 +16,7 @@ public class StateBreak extends StateCommon { done = false; activeUnit = null; - ctrl.hud.show(false, false, false, false, true, false); + ctrl.hud.actionButtons.show(Buttons.DONE.b); map.showBreakPawns(); } @@ -54,7 +55,7 @@ public class StateBreak extends StateCommon if (ctrl.cfg.mustValidate) { map.hideDirections(to); map.showOrientation(to, o); - ctrl.hud.show(false, false, false, false, true, false); + ctrl.hud.actionButtons.show(Buttons.DONE.b); } else doRotation(o); } diff --git a/core/src/ch/asynk/tankontank/game/states/StateCommon.java b/core/src/ch/asynk/tankontank/game/states/StateCommon.java index 9bf6ac7..314801b 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateCommon.java +++ b/core/src/ch/asynk/tankontank/game/states/StateCommon.java @@ -68,7 +68,7 @@ public abstract class StateCommon implements State private void goToNextState() { - ctrl.hud.hide(); + ctrl.hud.actionButtons.hide(); StateType next = nextState; nextState = StateType.SELECT; ctrl.setState(next, (next == StateType.SELECT)); diff --git a/core/src/ch/asynk/tankontank/game/states/StateEntry.java b/core/src/ch/asynk/tankontank/game/states/StateEntry.java index dc4acc9..d166a69 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateEntry.java +++ b/core/src/ch/asynk/tankontank/game/states/StateEntry.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Vector3; import ch.asynk.tankontank.engine.TileSet; import ch.asynk.tankontank.engine.Orientation; import ch.asynk.tankontank.game.Hex; +import ch.asynk.tankontank.game.hud.ActionButtons.Buttons; public class StateEntry extends StateCommon { @@ -19,7 +20,7 @@ public class StateEntry extends StateCommon activeUnit = ctrl.hud.getDockUnit(); entryPoint = ctrl.battle.getEntryPoint(activeUnit); entryPoint.enable(Hex.AREA, true); - ctrl.hud.show(false, false, false, false, false, ctrl.cfg.canCancel); + ctrl.hud.actionButtons.show(((ctrl.cfg.canCancel) ? Buttons.ABORT.b : 0)); x = activeUnit.getPosition().x; y = activeUnit.getPosition().x; z = activeUnit.getPosition().y; diff --git a/core/src/ch/asynk/tankontank/game/states/StateMove.java b/core/src/ch/asynk/tankontank/game/states/StateMove.java index 657c9bd..2f672c9 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateMove.java +++ b/core/src/ch/asynk/tankontank/game/states/StateMove.java @@ -2,6 +2,7 @@ package ch.asynk.tankontank.game.states; import ch.asynk.tankontank.game.Hex; import ch.asynk.tankontank.game.Unit; +import ch.asynk.tankontank.game.hud.ActionButtons.Buttons; public class StateMove extends StateCommon { @@ -9,8 +10,8 @@ public class StateMove extends StateCommon public void enter(boolean fromSelect) { boolean moreThanOne = ((map.moveablePawns.size() + map.activatedPawns.size()) > 1); - ctrl.hud.show(false, true, true, false, moreThanOne, ctrl.cfg.canCancel); - ctrl.hud.moveBtn.setOn(); + ctrl.hud.actionButtons.show(Buttons.ROTATE.b | Buttons.MOVE.b | ((moreThanOne) ? Buttons.DONE.b : 0) | ((ctrl.cfg.canCancel) ? Buttons.ABORT.b : 0)); + ctrl.hud.actionButtons.setOn(Buttons.MOVE); map.possiblePaths.clear(); if (fromSelect) { diff --git a/core/src/ch/asynk/tankontank/game/states/StatePromote.java b/core/src/ch/asynk/tankontank/game/states/StatePromote.java index 28fe2b5..6366d74 100644 --- a/core/src/ch/asynk/tankontank/game/states/StatePromote.java +++ b/core/src/ch/asynk/tankontank/game/states/StatePromote.java @@ -7,7 +7,7 @@ public class StatePromote extends StateCommon @Override public void enter(boolean flag) { - ctrl.hud.hide(); + ctrl.hud.actionButtons.hide(); Unit unit = ctrl.player.promote(selectedUnit); if (unit != null) { ctrl.hud.notify(unit + " has been promoted"); diff --git a/core/src/ch/asynk/tankontank/game/states/StateRotate.java b/core/src/ch/asynk/tankontank/game/states/StateRotate.java index b35a856..a5547f9 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateRotate.java +++ b/core/src/ch/asynk/tankontank/game/states/StateRotate.java @@ -1,6 +1,7 @@ package ch.asynk.tankontank.game.states; import ch.asynk.tankontank.engine.Orientation; +import ch.asynk.tankontank.game.hud.ActionButtons.Buttons; import ch.asynk.tankontank.TankOnTank; @@ -15,8 +16,8 @@ public class StateRotate extends StateCommon { this.rotateOnly = rotateOnly; - ctrl.hud.show(false, true, false, false, false, ctrl.cfg.canCancel); - ctrl.hud.rotateBtn.setOn(); + ctrl.hud.actionButtons.show(Buttons.ROTATE.b | ((ctrl.cfg.canCancel) ? Buttons.ABORT.b : 0)); + ctrl.hud.actionButtons.setOn(Buttons.ROTATE); if (rotateOnly) { // rotateBtn from Select state @@ -64,7 +65,7 @@ public class StateRotate extends StateCommon if (ctrl.cfg.mustValidate) { map.hideDirections(to); map.showOrientation(to, o); - ctrl.hud.show(false, true, false, false, true, ctrl.cfg.canCancel); + ctrl.hud.actionButtons.show(Buttons.ROTATE.b | Buttons.DONE.b | ((ctrl.cfg.canCancel) ? Buttons.ABORT.b : 0)); } else doRotation(o); } @@ -72,7 +73,7 @@ public class StateRotate extends StateCommon @Override public void abort() { - ctrl.hud.hide(); + ctrl.hud.actionButtons.hide(); if (activeUnit.move.entryMove) { map.leaveBoard(activeUnit); ctrl.player.revertUnitEntry(activeUnit); diff --git a/core/src/ch/asynk/tankontank/game/states/StateSelect.java b/core/src/ch/asynk/tankontank/game/states/StateSelect.java index 9d07c6a..19dbc73 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateSelect.java +++ b/core/src/ch/asynk/tankontank/game/states/StateSelect.java @@ -2,6 +2,7 @@ package ch.asynk.tankontank.game.states; import ch.asynk.tankontank.game.Map; import ch.asynk.tankontank.game.Ctrl; +import ch.asynk.tankontank.game.hud.ActionButtons.Buttons; public class StateSelect extends StateCommon { @@ -62,16 +63,14 @@ public class StateSelect extends StateCommon if (moves > 0) map.collectMoveablePawns(selectedUnit); showPossibleTargetsMovesAssists(selectedUnit); - ctrl.hud.show( - ctrl.player.canPromote(selectedUnit), - selectedUnit.canMove(), - (moves > 0), - (targets > 0), - false, - false + ctrl.hud.actionButtons.show( + ((ctrl.player.canPromote(selectedUnit)) ? Buttons.PROMOTE.b : 0 ) | + ((selectedUnit.canMove()) ? Buttons.ROTATE.b : 0 ) | + ((moves > 0) ? Buttons.MOVE.b : 0 ) | + ((targets > 0) ? Buttons.ATTACK.b : 0) ); } else { - ctrl.hud.hide(); + ctrl.hud.actionButtons.hide(); map.clearAll(); } if (selectedUnit != null) ctrl.hud.notify(selectedUnit.toString()); |