Android Open Source - shapemergency Saved Game






From Project

Back to project page shapemergency.

License

The source code is released under:

GNU General Public License

If you think the Android project shapemergency 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 com.adsg0186.shapemergency.testgame1.config;
/*from w  ww. j ava2  s.c  om*/
import com.github.adsgray.gdxtry1.engine.position.BlobPosition;
import com.github.adsgray.gdxtry1.engine.position.PositionIF;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class SavedGame  {

    protected Context context;
    protected SharedPreferences store;
    protected boolean savedGamePresent; // set to false as soon as a game completes
    protected GameState state;
    
    private static String createKey(String pref) {
        return String.format("savedgame_%s", pref);
    }

    // C struct
    public static class GameState {
        public int hitPoints;
        public int shields;
        public int bossesKilled;
        public int difficulty;
        public int score;
        public PositionIF defenderPos;
    }
    
    public SavedGame(Context context) { 
        this.context = context;
        store = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public SavedGame setGameState(GameState state) {
        this.state = state;
        savedGamePresent = true;
        return this;
    }

    public boolean getSavedGamePresent() { return savedGamePresent; }

    public void clearSavedGame() {
        SharedPreferences.Editor editor = store.edit();
        savedGamePresent = false;
        editor.putInt(createKey("gamepresent"), 0);
        editor.commit();
    }
    
    public GameState getState() {
        return state;
    }

    // call before getting
    public GameState load() {
        savedGamePresent = store.getInt(createKey("gamepresent"), 0) == 1;

        if (!savedGamePresent) {
            return null;
        }

        if (state == null) {
            state = new GameState();
        }

        state.hitPoints = store.getInt(createKey("hitPoints"), 0);
        state.bossesKilled = store.getInt(createKey("bossesKilled"), 0);
        state.shields = store.getInt(createKey("shields"), 0);
        state.difficulty = store.getInt(createKey("difficulty"), 0);
        state.score = store.getInt(createKey("score"), 0);
        state.defenderPos = new BlobPosition(store.getInt(createKey("xpos"), 400), store.getInt(createKey("ypos"), 100)); 

        return state;
    }
    
    
    // call after setting
    public void save() {
        if (state == null) return;

        SharedPreferences.Editor editor = store.edit();
        editor.putInt(createKey("hitPoints"), state.hitPoints);
        editor.putInt(createKey("shields"), state.shields);
        editor.putInt(createKey("bossesKilled"), state.bossesKilled);
        editor.putInt(createKey("difficulty"), state.difficulty);
        editor.putInt(createKey("score"), state.score);
        editor.putInt(createKey("xpos"), state.defenderPos.getX());
        editor.putInt(createKey("ypos"), state.defenderPos.getY());
        editor.putInt(createKey("gamepresent"), 1);
        editor.commit();
    }
   
    protected static SavedGame instance;
    public static SavedGame createInstance(Context context) {
        instance = new SavedGame(context);
        return instance;
    }
    public static SavedGame get() { return instance; }
}




Java Source Code List

com.adsg0186.shapemergency.GameActivity.java
com.adsg0186.shapemergency.GameScreen.java
com.adsg0186.shapemergency.HelpView.java
com.adsg0186.shapemergency.HighScoreView.java
com.adsg0186.shapemergency.MainActivity.java
com.adsg0186.shapemergency.SettingsView.java
com.adsg0186.shapemergency.testgame1.AngryTargetMissileSource.java
com.adsg0186.shapemergency.testgame1.BonusFactory.java
com.adsg0186.shapemergency.testgame1.BossTargetMissileSource.java
com.adsg0186.shapemergency.testgame1.CreateEnemyTrigger.java
com.adsg0186.shapemergency.testgame1.DefenderCollisionTrigger.java
com.adsg0186.shapemergency.testgame1.FiringGameTest.java
com.adsg0186.shapemergency.testgame1.GameSound.java
com.adsg0186.shapemergency.testgame1.MissileBlobSource.java
com.adsg0186.shapemergency.testgame1.MissileCollisionTrigger.java
com.adsg0186.shapemergency.testgame1.ShieldCollisionTrigger.java
com.adsg0186.shapemergency.testgame1.TargetMissileSource.java
com.adsg0186.shapemergency.testgame1.TargetUtils.java
com.adsg0186.shapemergency.testgame1.Vibrate.java
com.adsg0186.shapemergency.testgame1.blobs.BonusDropper.java
com.adsg0186.shapemergency.testgame1.blobs.BonusIF.java
com.adsg0186.shapemergency.testgame1.blobs.BossEnemy.java
com.adsg0186.shapemergency.testgame1.blobs.DamagableIF.java
com.adsg0186.shapemergency.testgame1.blobs.DamagerIF.java
com.adsg0186.shapemergency.testgame1.blobs.DefaultEnemy.java
com.adsg0186.shapemergency.testgame1.blobs.EnemyBomb.java
com.adsg0186.shapemergency.testgame1.blobs.EnemyFactory.java
com.adsg0186.shapemergency.testgame1.blobs.EnemyIF.java
com.adsg0186.shapemergency.testgame1.blobs.FiringBlobDecorator.java
com.adsg0186.shapemergency.testgame1.blobs.FlashMessage.java
com.adsg0186.shapemergency.testgame1.blobs.HitpointBonusDecorator.java
com.adsg0186.shapemergency.testgame1.blobs.ScoreTextDisplay.java
com.adsg0186.shapemergency.testgame1.blobs.ShieldRing.java
com.adsg0186.shapemergency.testgame1.config.BaseGameConfig.java
com.adsg0186.shapemergency.testgame1.config.EasyGameConfig.java
com.adsg0186.shapemergency.testgame1.config.GameConfigIF.java
com.adsg0186.shapemergency.testgame1.config.GameConfig.java
com.adsg0186.shapemergency.testgame1.config.GamePreferences.java
com.adsg0186.shapemergency.testgame1.config.InsaneGameConfig.java
com.adsg0186.shapemergency.testgame1.config.SavedGame.java