Skip to content
Snippets Groups Projects
Commit 5be8d9d9 authored by v.tarantik's avatar v.tarantik
Browse files

#32 Bux fixes for rotating and piece animation, GameRulesHelper and progress counting

parent 9ecf36ba
No related branches found
No related tags found
No related merge requests found
......@@ -14,7 +14,9 @@ import cz.nic.tablexia.TablexiaSettings;
import cz.nic.tablexia.game.AbstractTablexiaGame;
import cz.nic.tablexia.game.games.pursuit.environment.Grid;
import cz.nic.tablexia.game.games.pursuit.environment.PuzzlePiece;
import cz.nic.tablexia.game.games.pursuit.helper.GameRulesHelper;
import cz.nic.tablexia.game.games.pursuit.helper.TextureHelper;
import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.listener.DragAndRotateActorListener;
/**
......@@ -32,17 +34,25 @@ public class PursuitGame extends AbstractTablexiaGame<Void> {
private Image backgroundImage;
private DragAndRotateActorListener dragAndRotateActorListener;
private int movesCounter;
private float startTime;
private float endTime;
@Override
protected void gameLoaded(Map<String, String> gameState) {
contentGroup = new Group();
contentGroup.setSize(SCREEN_WIDTH, SCREEN_MIN_HEIGHT);
getStage().addActor(contentGroup);
startTime = System.currentTimeMillis();
prepareBackground();
prepareGrid();
dragAndRotateActorListener = new DragAndRotateActorListener() {
dragAndRotateActorListener = new DragAndRotateActorListener(new DragAndRotateActorListener.IOnRotationFinished() {
@Override
public void onRotationFinished() {
movesCounter++;
}
}) {
@Override
public void selected(float x, float y) {
if (dragAndRotateActorListener.getActor() == null) {
......@@ -74,7 +84,10 @@ public class PursuitGame extends AbstractTablexiaGame<Void> {
movesCounter++;
dragAndRotateActorListener.setActor(null);
if (grid.allPiecesInCorrectPositions()) {
gameComplete(movesCounter);
endTime = System.currentTimeMillis();
Log.info(getClass(),"Moves: "+movesCounter);
int starsCount = GameRulesHelper.getNumberOfStarsForMoves(getGameDifficulty().ordinal(),endTime-startTime);
gameComplete(starsCount);
}
return true;
}
......
package cz.nic.tablexia.game.games.pursuit.helper;
/**
* Created by Václav Tarantík on 27.7.15.
*/
public class GameRulesHelper {
public static int getNumberOfStarsForMoves(int difficulty,float millis){
float seconds = millis/1000;
switch (difficulty) {
case 0:
if (seconds >= 300) {
return 0;
} else if (seconds >= 160) {
return 1;
} else if (seconds >= 80) {
return 2;
} else {
return 3;
}
case 1:
if (seconds >= 600) {
return 0;
} else if (seconds >= 360) {
return 1;
} else if (seconds >= 160) {
return 2;
} else {
return 3;
}
case 2:
if (seconds >= 800) {
return 0;
} else if (seconds >= 460) {
return 1;
} else if (seconds >= 320) {
return 2;
} else {
return 3;
}
}
return 0;
}
}
......@@ -8,6 +8,7 @@ import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import net.dermetfan.utils.Pair;
import java.util.LinkedHashMap;
import java.util.TreeMap;
import cz.nic.tablexia.game.games.pursuit.helper.ArithmeticsHelper;
......@@ -23,11 +24,13 @@ public class DragAndRotateActorListener extends InputListener {
private float grabX, grabY;
private float initialFingersAngle;
private Actor actor;
private IOnRotationFinished iOnRotationFinished;
private TreeMap<Integer, Pair<Float, Float>> activePointers;
private LinkedHashMap<Integer, Pair<Float, Float>> activePointers;
public DragAndRotateActorListener() {
activePointers = new TreeMap<Integer, Pair<Float, Float>>();
public DragAndRotateActorListener(IOnRotationFinished iOnRotationFinished) {
this.iOnRotationFinished = iOnRotationFinished;
activePointers = new LinkedHashMap<Integer, Pair<Float, Float>>();
}
public void selected(float x, float y) {
......@@ -56,7 +59,7 @@ public class DragAndRotateActorListener extends InputListener {
grabY = recalculatedCoords.y;
} else if (activePointers.size() == 2) {
initialFingersAngle = (float) ArithmeticsHelper.getAngleBetweenTwoPoints(activePointers.get(activePointers.firstKey()), activePointers.get(activePointers.higherKey(activePointers.firstKey())));
initialFingersAngle = (float) ArithmeticsHelper.getAngleBetweenTwoPoints(activePointers.get(activePointers.keySet().toArray()[0]), activePointers.get(activePointers.keySet().toArray()[1]));
}
}
return true;
......@@ -77,11 +80,11 @@ public class DragAndRotateActorListener extends InputListener {
actor.setPosition(bx, by);
moved(bx, by, actor);
} else if (activePointers.size() > 1) {
if (pointer != activePointers.firstKey()) {
if (pointer != activePointers.keySet().toArray()[0]) {
activePointers.put(pointer, new Pair(x, y));
}
Pair<Float, Float> firstFingerCoordinates = activePointers.get(activePointers.firstKey());
Pair<Float, Float> secondFingerCoordinates = activePointers.get(activePointers.higherKey(activePointers.firstKey()));
Pair<Float, Float> firstFingerCoordinates = activePointers.get(activePointers.keySet().toArray()[0]);
Pair<Float, Float> secondFingerCoordinates = activePointers.get(activePointers.keySet().toArray()[1]);
double angle = ArithmeticsHelper.getAngleBetweenTwoPoints(firstFingerCoordinates, secondFingerCoordinates);
actor.setRotation(actor.getRotation() + (float) (angle - initialFingersAngle));
initialFingersAngle = (float) angle;
......@@ -92,30 +95,36 @@ public class DragAndRotateActorListener extends InputListener {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
if(actor!=null){
boolean draggingFingerLifted = activePointers.firstKey() == pointer;
boolean draggingFingerLifted = ((Integer)(activePointers.keySet().toArray()[0]) == pointer);
boolean rotatingFingerLifted = false;
if (activePointers.higherKey(activePointers.firstKey()) != null) {
rotatingFingerLifted = activePointers.higherKey(activePointers.firstKey()) == pointer;
if (activePointers.size()>1&&activePointers.keySet().toArray()[1] != null) {
rotatingFingerLifted = (Integer)activePointers.keySet().toArray()[1] == pointer;
}
activePointers.remove(pointer);
if (activePointers.size() == 0) {
dropped(x, y, actor);
} else if (activePointers.size() >= 1) {
//if user raised the dragging finger, animate piece under second finger
Pair<Float, Float> currentFirstPointer = activePointers.get(activePointers.firstKey());
Pair<Float, Float> currentFirstPointer = activePointers.get(activePointers.keySet().toArray()[0]);
if (draggingFingerLifted) {
Vector2 recalculatedCoords = new Vector2(currentFirstPointer.getKey(), currentFirstPointer.getValue());
actor.stageToLocalCoordinates(recalculatedCoords);
float bx = actor.getX() + (recalculatedCoords.x-(actor.getWidth()/2));
float by = actor.getY() + (recalculatedCoords.y-(actor.getHeight()/2));
actor.addAction(Actions.parallel(Actions.rotateTo(ArithmeticsHelper.getClosestRightAngle(actor.getRotation()), ROTATION_ANIMATION_DURATION), Actions.moveTo(bx,by, MOVETO_ANIMATION_DURATION)));
} else if (rotatingFingerLifted) {
actor.addAction(Actions.rotateTo(ArithmeticsHelper.getClosestRightAngle(actor.getRotation()), ROTATION_ANIMATION_DURATION));
iOnRotationFinished.onRotationFinished();
}
}
}
}
public interface IOnRotationFinished{
void onRotationFinished();
}
public void setActor(Actor actor) {
this.actor = actor;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment