From a14612bfb1a1703f774c92ee5d8e02a65eadb483 Mon Sep 17 00:00:00 2001 From: "v.tarantik" <v.tarantik@gmail.com> Date: Tue, 27 Oct 2015 12:08:32 +0100 Subject: [PATCH] #56 changed gamecontrol system --- .../games/pursuit/action/Interruptable.java | 8 ++++ .../pursuit/action/MovePieceToPosition.java | 13 ++++++- .../games/pursuit/action/MyMoveToAction.java | 33 +++++++++++++++++ .../action/RotateAndMovePieceInPosition.java | 13 ++++++- .../pursuit/helper/ArithmeticsHelper.java | 37 +++++++++---------- .../listener/DragAndRotateActorListener.java | 28 +++++++------- 6 files changed, 95 insertions(+), 37 deletions(-) create mode 100644 core/src/cz/nic/tablexia/game/games/pursuit/action/Interruptable.java create mode 100644 core/src/cz/nic/tablexia/game/games/pursuit/action/MyMoveToAction.java diff --git a/core/src/cz/nic/tablexia/game/games/pursuit/action/Interruptable.java b/core/src/cz/nic/tablexia/game/games/pursuit/action/Interruptable.java new file mode 100644 index 000000000..3c087a9b4 --- /dev/null +++ b/core/src/cz/nic/tablexia/game/games/pursuit/action/Interruptable.java @@ -0,0 +1,8 @@ +package cz.nic.tablexia.game.games.pursuit.action; + +/** + * Created by Václav TarantĂk on 26.10.15. + */ +public interface Interruptable { + void interrupt(); +} diff --git a/core/src/cz/nic/tablexia/game/games/pursuit/action/MovePieceToPosition.java b/core/src/cz/nic/tablexia/game/games/pursuit/action/MovePieceToPosition.java index 23ec4b91f..6bfdcf736 100644 --- a/core/src/cz/nic/tablexia/game/games/pursuit/action/MovePieceToPosition.java +++ b/core/src/cz/nic/tablexia/game/games/pursuit/action/MovePieceToPosition.java @@ -11,7 +11,7 @@ import cz.nic.tablexia.util.listener.DragAndRotateActorListener; /** * Created by Václav TarantĂk on 3.8.15. */ -public class MovePieceToPosition extends SequenceAction { +public class MovePieceToPosition extends SequenceAction implements Interruptable { /* *Moves specified piece in position using built-in actions @@ -19,11 +19,20 @@ public class MovePieceToPosition extends SequenceAction { public MovePieceToPosition(final PuzzlePiece draggedPiece, Point dest){ setActor(draggedPiece); setTarget(draggedPiece); - addAction(Actions.moveTo(dest.x, dest.y, DragAndRotateActorListener.MOVETO_ANIMATION_DURATION)); + addAction(new MyMoveToAction(dest.x, dest.y, DragAndRotateActorListener.MOVETO_ANIMATION_DURATION)); } public MovePieceToPosition(final PuzzlePiece draggedPiece, Point dest,Action finalAction){ this(draggedPiece,dest); addAction(finalAction); } + + @Override + public void interrupt() { + for(Action action: getActions()){ + if(action instanceof Interruptable){ + ((Interruptable)action).interrupt(); + } + } + } } diff --git a/core/src/cz/nic/tablexia/game/games/pursuit/action/MyMoveToAction.java b/core/src/cz/nic/tablexia/game/games/pursuit/action/MyMoveToAction.java new file mode 100644 index 000000000..3735fcc67 --- /dev/null +++ b/core/src/cz/nic/tablexia/game/games/pursuit/action/MyMoveToAction.java @@ -0,0 +1,33 @@ +package cz.nic.tablexia.game.games.pursuit.action; + +import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction; + +import cz.nic.tablexia.util.Log; + +/** + * Created by Václav TarantĂk on 26.10.15. + */ +public class MyMoveToAction extends MoveToAction implements Interruptable { + private boolean interrupted; + + public MyMoveToAction(float x, float y, float duration){ + setPosition(x, y); + setDuration(duration); + } + @Override + protected void update(float percent) { + if(!interrupted){ + super.update(percent); + } + } + + private void setInterrupted(boolean interrupted) { + this.interrupted = interrupted; + } + + @Override + public void interrupt() { + Log.info(getClass(),"Interrupting action"); + setInterrupted(true); + } +} diff --git a/core/src/cz/nic/tablexia/game/games/pursuit/action/RotateAndMovePieceInPosition.java b/core/src/cz/nic/tablexia/game/games/pursuit/action/RotateAndMovePieceInPosition.java index d59a80091..a3beb5645 100644 --- a/core/src/cz/nic/tablexia/game/games/pursuit/action/RotateAndMovePieceInPosition.java +++ b/core/src/cz/nic/tablexia/game/games/pursuit/action/RotateAndMovePieceInPosition.java @@ -1,9 +1,12 @@ package cz.nic.tablexia.game.games.pursuit.action; import com.badlogic.gdx.scenes.scene2d.Action; +import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.actions.Actions; import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction; +import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction; +import cz.nic.tablexia.util.Log; import cz.nic.tablexia.util.Point; import cz.nic.tablexia.game.games.pursuit.model.PuzzlePiece; import cz.nic.tablexia.util.listener.DragAndRotateActorListener; @@ -11,7 +14,7 @@ import cz.nic.tablexia.util.listener.DragAndRotateActorListener; /** * Created by Václav TarantĂk on 3.8.15. */ -public class RotateAndMovePieceInPosition extends SequenceAction { +public class RotateAndMovePieceInPosition extends SequenceAction implements Interruptable{ public RotateAndMovePieceInPosition(PuzzlePiece draggedPiece,float originalRotation, Point position, final DragAndRotateActorListener.IOnRotationFinished iOnRotationFinished){ setActor(draggedPiece); setTarget(draggedPiece); @@ -26,4 +29,12 @@ public class RotateAndMovePieceInPosition extends SequenceAction { addAction(Actions.parallel(new RotatePieceToClosestAngle(draggedPiece,originalRotation),new MovePieceToPosition(draggedPiece,position))); addAction(rotationFinishedAction); } + + public void interrupt(){ + for(Action a: getActions()){ + if(a instanceof Interruptable){ + ((Interruptable)a).interrupt(); + } + } + } } diff --git a/core/src/cz/nic/tablexia/game/games/pursuit/helper/ArithmeticsHelper.java b/core/src/cz/nic/tablexia/game/games/pursuit/helper/ArithmeticsHelper.java index 03cfde42c..3166fcc26 100644 --- a/core/src/cz/nic/tablexia/game/games/pursuit/helper/ArithmeticsHelper.java +++ b/core/src/cz/nic/tablexia/game/games/pursuit/helper/ArithmeticsHelper.java @@ -19,31 +19,26 @@ public class ArithmeticsHelper { return recalcAngle; } - public static float getAngleInRotationDirection(float originalRotation, float currentAngle){ - Log.info(ArithmeticsHelper.class,"ORIGINAL ROTATION: "+originalRotation+", currentRotation: "+currentAngle ); - float rotationAmount = currentAngle-originalRotation; - float angleModule = currentAngle % 90; - float nearestRightAngle; - float roundedAngle = currentAngle - (angleModule); - if(currentAngle<0){ - nearestRightAngle = rotationAmount < -20 ? roundedAngle - 90 : roundedAngle; - }else{ - nearestRightAngle = rotationAmount > 20 ? roundedAngle + 90 : roundedAngle; + public static float getAngleInRotationDirection(float originalRotation, float currentAngle) { + float rotationAmount = currentAngle - originalRotation; + int numOfNinetyDegreesPassed = (int) (rotationAmount / 90); + //original angle added with number of ninety degrees which we rotated piece over + float baseAngle = originalRotation + (numOfNinetyDegreesPassed * 90); + if ((Math.abs(rotationAmount) % 90) > 15) { + baseAngle+= (getRotationDirection(originalRotation, currentAngle) * 90); } - return nearestRightAngle; + return baseAngle; } public static float getClosestRightAngle(float currentAngle) { + int numOfNinetyDegreesInAngle = (int)(currentAngle/90); float angleModule = currentAngle % 90; - float nearestRightAngle = 0; - float roundedAngle = currentAngle - (angleModule); - //rotating actor clockwise - if (currentAngle < 0) { - nearestRightAngle = angleModule < -45 ? roundedAngle - 90 : roundedAngle; - } else { - nearestRightAngle = angleModule > 45 ? roundedAngle + 90 : roundedAngle; + float highestLowerRightAngle = numOfNinetyDegreesInAngle*90; + if(currentAngle<0){ + return angleModule<-45?highestLowerRightAngle-90:highestLowerRightAngle; + }else{ + return angleModule>45?highestLowerRightAngle+90:highestLowerRightAngle; } - return nearestRightAngle; } //returns right angle inside 360 degrees @@ -76,4 +71,8 @@ public class ArithmeticsHelper { return new Point(x, y); } + public static int getRotationDirection(float originalRotation, float currentRotation){ + return (currentRotation<originalRotation)?-1:1; + } + } diff --git a/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java b/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java index 36fd0b976..d91d21fad 100644 --- a/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java +++ b/core/src/cz/nic/tablexia/util/listener/DragAndRotateActorListener.java @@ -33,6 +33,7 @@ public class DragAndRotateActorListener extends InputListener { private float actorOriginalRotation; private boolean performingAction; private boolean pieceAnimationDone; + private RotateAndMovePieceInPosition pieceTranslationAction; private LinkedHashMap<Integer, Point> activePointers; @@ -52,7 +53,7 @@ public class DragAndRotateActorListener extends InputListener { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { - if(button!=1){ + if (button != 1) { activePointers.put(pointer, new Point(x, y)); } if (activePointers.size() == 1) { @@ -74,6 +75,7 @@ public class DragAndRotateActorListener extends InputListener { } else if (activePointers.size() == 2) { initialFingersAngle = (float) ArithmeticsHelper.getAngleBetweenTwoPoints(activePointers.get(activePointers.keySet().toArray()[0]), activePointers.get(activePointers.keySet().toArray()[1])); actorOriginalRotation = draggedActor.getRotation(); + Log.info(getClass(), "Actors iniial angle: " + actorOriginalRotation); } } return true; @@ -108,22 +110,19 @@ public class DragAndRotateActorListener extends InputListener { @Override public void touchUp(InputEvent event, final float x, final float y, int pointer, int button) { - if(button != 1){ + if (button != 1) { if (draggedActor != null && !performingAction) { - Log.info(getClass(), "TOuch up with pointer: " + pointer); boolean draggingFingerLifted = ((Integer) (activePointers.keySet().toArray()[0]) == pointer); boolean rotatingFingerLifted = false; if (activePointers.size() > 1) { rotatingFingerLifted = (Integer) activePointers.keySet().toArray()[1] == pointer; } - Log.info(getClass(), draggingFingerLifted ? "DRAGGING FINGER LIFTED" : rotatingFingerLifted ? "ROTATING FINGER LIFTED" : "WEIIIIRD"); activePointers.remove(pointer); if (activePointers.size() == 0) { - Log.info(getClass(), "DROPPED PIECE: " + draggedActor); - if(!isPieceAnimationDone()){ - draggedActor.clearActions(); - Point pieceOriginalPosition = ((Grid)parentActor).getPieceGridPositionInPixels(((PuzzlePiece)draggedActor).getActualPosition()); - parentActor.addAction(new RotateAndMovePieceInPosition((PuzzlePiece) draggedActor,actorOriginalRotation,pieceOriginalPosition , iOnRotationFinished)); + if (!isPieceAnimationDone()) { + pieceTranslationAction.interrupt(); + Point pieceOriginalPosition = ((Grid) parentActor).getPieceGridPositionInPixels(((PuzzlePiece) draggedActor).getActualPosition()); + parentActor.addAction(new RotateAndMovePieceInPosition((PuzzlePiece) draggedActor, actorOriginalRotation, pieceOriginalPosition, iOnRotationFinished)); } parentActor.addAction(Actions.after(new Action() { @Override @@ -132,21 +131,20 @@ public class DragAndRotateActorListener extends InputListener { return true; } })); + } else if (activePointers.size() > 0) { - setPieceAnimationDone(false); //if user raised the dragging finger, animate piece under second finger Point currentFirstPointer = activePointers.get(activePointers.keySet().toArray()[0]); - Log.info(getClass(),"Current first Pointer: "+currentFirstPointer); if (draggingFingerLifted) { + setPieceAnimationDone(false); Vector2 recalculatedCoords = new Vector2(currentFirstPointer.x, currentFirstPointer.y); draggedActor.stageToLocalCoordinates(recalculatedCoords); float bx = draggedActor.getX() + (recalculatedCoords.x - (draggedActor.getWidth() / 2)); float by = draggedActor.getY() + (recalculatedCoords.y - (draggedActor.getHeight() / 2)); - Log.info(getClass(),"ADDING ROTATE AND MOVE ACTION"); - parentActor.addAction(new RotateAndMovePieceInPosition((PuzzlePiece) draggedActor,actorOriginalRotation, new Point(bx, by), iOnRotationFinished)); + pieceTranslationAction = new RotateAndMovePieceInPosition((PuzzlePiece) draggedActor, actorOriginalRotation, new Point(bx, by), iOnRotationFinished); + parentActor.addAction(pieceTranslationAction); } else if (rotatingFingerLifted) { - Log.info(getClass(),"ADDING ROTATE ACTION"); - parentActor.addAction(new RotatePieceToClosestAngle((PuzzlePiece) draggedActor, actorOriginalRotation,iOnRotationFinished)); + parentActor.addAction(new RotatePieceToClosestAngle((PuzzlePiece) draggedActor, actorOriginalRotation, iOnRotationFinished)); } } } else { -- GitLab