Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game Speed 3 levels, 1-4-8 #162

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class GameSpeed {

private static final int FAST_FORWARD_SPEED = 4;
private static final int SPEED_LEVELS[] = {1, 4, 8};

public interface Listener {
void gameSpeedChanged();
Expand All @@ -17,14 +17,14 @@ public interface Listener {
private final GameEngine mGameEngine;
private final List<Listener> mListeners = new CopyOnWriteArrayList<>();

private boolean mFastForwardActive = false;
private int mCurrentSpeed = SPEED_LEVELS[0];

public GameSpeed(GameEngine gameEngine) {
mGameEngine = gameEngine;
}

public boolean isFastForwardActive() {
return mFastForwardActive;
public int getCurrentSpeed() {
return mCurrentSpeed;
}

public void toggleFastForward() {
Expand All @@ -38,7 +38,15 @@ public void execute() {
return;
}

setFastForwardActive(!mFastForwardActive);
int index = 0;
for (int level : SPEED_LEVELS) {
if (level == mCurrentSpeed) {
index++;
break;
}
index++;
}
setFastForwardActive(SPEED_LEVELS[index % SPEED_LEVELS.length]);
}

public void addListener(Listener listener) {
Expand All @@ -49,10 +57,10 @@ public void removeListener(Listener listener) {
mListeners.remove(listener);
}

private void setFastForwardActive(boolean fastForwardActive) {
if (mFastForwardActive != fastForwardActive) {
mFastForwardActive = fastForwardActive;
mGameEngine.setTicksPerLoop(mFastForwardActive ? FAST_FORWARD_SPEED : 1);
private void setFastForwardActive(int currentSpeed) {
if (mCurrentSpeed != currentSpeed) {
mCurrentSpeed = currentSpeed;
mGameEngine.setTicksPerLoop(mCurrentSpeed);

for (Listener listener : mListeners) {
listener.gameSpeedChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
txt_credits.setText(getString(R.string.credits) + ": " + StringUtils.formatSuffix(mScoreBoard.getCredits()));
txt_lives.setText(getString(R.string.lives) + ": " + mScoreBoard.getLives());
txt_bonus.setText(getString(R.string.bonus) + ": " + StringUtils.formatSuffix(mScoreBoard.getWaveBonus() + mScoreBoard.getEarlyBonus()));
btn_fast_forward.setText(getString(mSpeedManager.isFastForwardActive() ? R.string.fast_speed : R.string.normal_speed));
btn_fast_forward.setText(mSpeedManager.getCurrentSpeed() + "x");

final List<TowerView> towerViews = new ArrayList<>();
towerViews.add((TowerView) v.findViewById(R.id.view_tower_1));
Expand Down Expand Up @@ -226,7 +226,7 @@ public void gameSpeedChanged() {
mHandler.post(new Runnable() {
@Override
public void run() {
btn_fast_forward.setText(getString(mSpeedManager.isFastForwardActive() ? R.string.fast_speed : R.string.normal_speed));
btn_fast_forward.setText(mSpeedManager.getCurrentSpeed() + "x");
}
});
}
Expand Down