Android Open Source - hiddenmarble Save State






From Project

Back to project page hiddenmarble.

License

The source code is released under:

Apache License

If you think the Android project hiddenmarble 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.mygdx.hiddenmarble.utils;
//  w  ww .  ja v  a 2s.com
import java.io.IOException;
import java.io.Serializable;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.mygdx.hiddenmarble.world.GameWorld;

/** Saves and loads game data. */
public class SaveState {
    private final Preferences preferences;
    private Data data;

    /** Creates a save state from the specified save file. */
    public SaveState(String filename) {
        preferences = Gdx.app.getPreferences(filename);
        data = new Data();
    }
    
    /**
     * Attempts to load game data from the save file.
     * 
     * @return true if the data was loaded
     */
    public boolean load() {
        String dataString = preferences.getString("dataString", null);
        if (dataString == null) {
            return false;
        }
        try {
            data = (Data)Serialization.fromString(dataString);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /** Saves data to the save file. */
    public void save() {
        try {
            preferences.putString("dataString", Serialization.toString(data));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        preferences.flush();
    }

    /** Clears all loaded data. */
    public void clear() {
        data = new Data();
    }
    
    /** Erases the save file. */
    public void erase() {
        preferences.clear();
        preferences.flush();
    }
    
    public GameWorld getWorld() {
        return data.world;
    }

    public void setWorld(GameWorld world) {
        data.world = world;
    }
    
    public String getUIState() {
        return data.uiState;
    }
    
    public void setUIState(String state) {
        data.uiState = state;
    }
    
    private static class Data implements Serializable {
        private static final long serialVersionUID = -1210225610405870019L;
        
        GameWorld world;
        String uiState;
    }
}




Java Source Code List

com.mygdx.hiddenmarble.android.AndroidLauncher.java
com.mygdx.hiddenmarble.desktop.DesktopLauncher.java
com.mygdx.hiddenmarble.entities.AbstractDynamicEntity.java
com.mygdx.hiddenmarble.entities.AbstractEntity.java
com.mygdx.hiddenmarble.entities.Borders.java
com.mygdx.hiddenmarble.entities.DefaultBorders.java
com.mygdx.hiddenmarble.entities.DefaultMarble.java
com.mygdx.hiddenmarble.entities.DefaultMazeBox.java
com.mygdx.hiddenmarble.entities.DynamicEntity.java
com.mygdx.hiddenmarble.entities.Entity.java
com.mygdx.hiddenmarble.entities.Marble.java
com.mygdx.hiddenmarble.entities.Material.java
com.mygdx.hiddenmarble.entities.MazeBox.java
com.mygdx.hiddenmarble.entities.MazeFixtureDef.java
com.mygdx.hiddenmarble.ui.GameScreen.java
com.mygdx.hiddenmarble.ui.HiddenMarble.java
com.mygdx.hiddenmarble.ui.WorldRenderer.java
com.mygdx.hiddenmarble.utils.Assets.java
com.mygdx.hiddenmarble.utils.BodyHelper.java
com.mygdx.hiddenmarble.utils.MazeHelper.java
com.mygdx.hiddenmarble.utils.SaveState.java
com.mygdx.hiddenmarble.utils.Serialization.java
com.mygdx.hiddenmarble.utils.SpriteHelper.java
com.mygdx.hiddenmarble.world.GameWorldAdapter.java
com.mygdx.hiddenmarble.world.GameWorldListener.java
com.mygdx.hiddenmarble.world.GameWorld.java