Android Open Source - rock-paper-scissors State Manager






From Project

Back to project page rock-paper-scissors.

License

The source code is released under:

MIT License

If you think the Android project rock-paper-scissors listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package au.com.codeka.rps.game;
//from  w  w w.  jav a2s .com
import java.io.UnsupportedEncodingException;

import au.com.codeka.rps.DebugLog;
import au.com.codeka.rps.WatchConnection;

/**
 * Manages which game state we're currently in.
 */
public class StateManager {
    public static final StateManager i = new StateManager();
    private WatchConnection watchConnection;
    private State currentState;

    private StateManager() {
    }

    public void start(WatchConnection watchConnection) {
        this.watchConnection = watchConnection;
        enterState(new FindingOpponentState(this));
    }

    public WatchConnection getWatchConnection() {
        return watchConnection;
    }

    public void onMessageReceived(String path, String payload) {
        currentState.onMessageReceived(path, payload);
    }

    void enterState(State newState) {
        if (currentState != newState) {
            DebugLog.write("Entering state '%s'.", newState.getClass().getSimpleName());
            currentState = newState;

            try {
                watchConnection.sendMessage(new WatchConnection.Message("/rps/StateChange",
                        currentState.getClass().getSimpleName().getBytes("utf-8")));
            } catch (UnsupportedEncodingException e) {
            }

            currentState.onEnter();
        }
    }
}




Java Source Code List

au.com.codeka.rps.ApplicationTest.java
au.com.codeka.rps.DebugLog.java
au.com.codeka.rps.FindingOpponentFragment.java
au.com.codeka.rps.GameActivity.java
au.com.codeka.rps.GameFragment.java
au.com.codeka.rps.MessageListenerService.java
au.com.codeka.rps.NotificationUpdateService.java
au.com.codeka.rps.PhoneConnection.java
au.com.codeka.rps.ResultFragment.java
au.com.codeka.rps.SplashActivity.java
au.com.codeka.rps.WatchConnection.java
au.com.codeka.rps.game.AwaitingPlayerChoiceState.java
au.com.codeka.rps.game.AwaitingPlayerChoiceState.java
au.com.codeka.rps.game.AwaitingResultState.java
au.com.codeka.rps.game.AwaitingResultState.java
au.com.codeka.rps.game.DisplayingResultState.java
au.com.codeka.rps.game.DisplayingResultState.java
au.com.codeka.rps.game.FindingOpponentState.java
au.com.codeka.rps.game.FindingOpponentState.java
au.com.codeka.rps.game.GameRunningState.java
au.com.codeka.rps.game.GameRunningState.java
au.com.codeka.rps.game.MatchInfo.java
au.com.codeka.rps.game.ResultInfo.java
au.com.codeka.rps.game.StateManager.java
au.com.codeka.rps.game.StateManager.java
au.com.codeka.rps.game.State.java
au.com.codeka.rps.game.State.java