Skip to content
Snippets Groups Projects
Commit 02d0436b authored by Vitaliy Vashchenko's avatar Vitaliy Vashchenko
Browse files

#145 Effect state is now saved and restored when interruption occurred.

parent c6167d86
No related branches found
No related tags found
No related merge requests found
......@@ -51,6 +51,9 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
private static final Color SCORE_COUNTER_TEXT_COLOR = Color.WHITE;
private static final int SCORE_BOTTOM_PAD = 10;
private static final int SCORE_HORIZONTAL_PAD = 50;
private static final int NO_EFFECT = -1;
private static final int SLOW_DOWN_EFFECT = 0;
private static final int SPEED_UP_EFFECT = 1;
private GfxLibrary gfxLibrary = new GfxLibrary(this, TextureType.values());
......@@ -64,10 +67,10 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
private TargetPositionController targetPositionController;
private HitEvaluator hitEvaluator;
private long slowDownEffectDuration = 0;
private long speedUpEffectDuration = 0;
private long slowDownEffectEntry = 0;
private long speedUpEffectEntry = 0;
private int currentEffect = NO_EFFECT;
private long effectEntry = 0;
private long effectDuration = 0;
private boolean effectInterrupted = false;
/**
* Most important listener deciding every hit on target
......@@ -96,16 +99,13 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
target.addAction(Actions.scaleTo(1, 0, 0.1f));
switch (BoxType.BAD_BOXES[getRandom().nextInt(BoxType.BAD_BOXES.length)]) {
case SPEED_UP:
// TODO: 3.2.16 slow timer in pause mod
if (slowDownEffectDuration != 0) {
slowDownEffectDuration = 0;
slowDownEffectEntry = 0;
//reset slow state
if (currentEffect == SPEED_UP_EFFECT) {
effectDuration += Properties.GAME_SPEED_TIMEOUT;
} else {
currentEffect = SPEED_UP_EFFECT;
effectEntry = TimeUtils.millis();
effectDuration = Properties.GAME_SPEED_TIMEOUT;
}
if (speedUpEffectDuration == 0) {
speedUpEffectEntry = TimeUtils.millis();
}
speedUpEffectDuration += Properties.GAME_SPEED_TIMEOUT;
getData().setGameSpeed(Properties.GAME_SPEED_FAST);
sfxLibrary.getSound(SoundType.BOX_FAST).play();
scene.addActor(new Effect(gfxLibrary.getTextureRegion(TextureType.BOX_SPEED_UP), target.getX(), target.getY()));
......@@ -128,16 +128,13 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
target.addAction(Actions.scaleTo(1, 0, 0.1f));
switch (BoxType.GOOD_BOXES[getRandom().nextInt(BoxType.GOOD_BOXES.length)]) {
case SLOW_DOWN:
// TODO game speed timeout
if (speedUpEffectDuration != 0) {
speedUpEffectDuration = 0;
speedUpEffectEntry = 0;
//reset speedUp
}
if (slowDownEffectDuration == 0) {
slowDownEffectEntry = TimeUtils.millis();
if (currentEffect == SLOW_DOWN_EFFECT) {
effectDuration = +Properties.GAME_SPEED_TIMEOUT;
} else {
currentEffect = SLOW_DOWN_EFFECT;
effectDuration = Properties.GAME_SPEED_TIMEOUT;
effectEntry = TimeUtils.millis();
}
slowDownEffectDuration += Properties.GAME_SPEED_TIMEOUT;
getData().setGameSpeed(Properties.GAME_SPEED_SLOW);
scene.addActor(new Effect(gfxLibrary.getTextureRegion(TextureType.BOX_SPEED_DOWN), target.getX(), target.getY()));
sfxLibrary.getSound(SoundType.BOX_SLOW).play();
......@@ -224,6 +221,26 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
})));
}
@Override
protected void gamePaused(Map<String, String> gameState) {
//save remaining effect time. if any
if (currentEffect != NO_EFFECT || effectDuration > TimeUtils.timeSinceMillis(effectEntry)) {
effectDuration = effectDuration - TimeUtils.timeSinceMillis(effectEntry);
effectInterrupted = true;
}
super.gamePaused(gameState);
}
@Override
protected void gameResumed() {
//restore remaining effect time, if any
if (currentEffect != NO_EFFECT) {
effectEntry = TimeUtils.millis();
effectInterrupted = false;
}
super.gameResumed();
}
/**
* Based on game difficulty create randome hit limit for current target
* Current it 1-4 hit for all difficulties
......@@ -271,29 +288,19 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
}
getData().addTime(delta);
targetPositionController.onUpdate(delta * getData().getGameSpeed());
}
watch.setTime((int) getData().getTime());
score.setText(String.valueOf(getData().getScore()));
if (!isScreenPaused()) {
if (slowDownEffectDuration > 0 && TimeUtils.timeSinceMillis(slowDownEffectEntry) > slowDownEffectDuration || speedUpEffectDuration > 0 && TimeUtils.timeSinceMillis(speedUpEffectEntry) > speedUpEffectDuration) {
resetGameSpeed();
if (currentEffect!=NO_EFFECT && TimeUtils.timeSinceMillis(effectEntry)>effectDuration && !effectInterrupted) {
//back to normal
// TODO: 3.2.16 fix screen resizing
currentEffect = NO_EFFECT;
effectDuration = 0;
effectEntry = 0;
getData().setGameSpeed(Properties.GAME_SPEED_NORMAL);
}
}
watch.setTime((int) getData().getTime());
score.setText(String.valueOf(getData().getScore()));
}
}
private void resetGameSpeed(){
slowDownEffectEntry = 0;
slowDownEffectDuration = 0;
speedUpEffectEntry =0;
speedUpEffectDuration =0;
getData().setGameSpeed(Properties.GAME_SPEED_NORMAL);
}
private void finishGame() {
getData().setRunning(false);
scene.setTouchable(Touchable.disabled);
......@@ -338,6 +345,7 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
*/
@Override
protected void screenResized(int width, int height) {
// TODO: 3.2.16 fix screen resizing
super.screenResized(width, height);
scene.setBounds(0, getStage().getCamera().position.y - getStage().getHeight() / 2, getStage().getWidth(), getStage().getHeight()); // scaling viewport camera y-position adjustment
watch.setPosition(0, getStage().getCamera().position.y - getStage().getHeight() / 2);
......
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