Exemple #1
0
    // Load map data from local storage
    public void _LoadFromStorage(string _path = null)
    {
        string chosenPath = path;

        if (_path != null)
        {
            chosenPath = _path;
        }

        if (File.Exists(chosenPath))
        {
            //Debug.Log (chosenPath);
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream      stream    = new FileStream(chosenPath, FileMode.Open);
            if (stream.Length == 0)   // If want to reset data, change this to > 0
            {
                stream.Close();
                this._InitFirstTime();
                //this._SaveToStorage ();
            }
            else
            {
                MapDataSerialized mapDataSerialized = formatter.Deserialize(stream) as MapDataSerialized;
                MapDataSerialized._SerializedToMapData(mapDataSerialized);

                //this._AddMapObject(BuildingFactory.BuildingType.MINE, 1, new Vector2(70, 25));
                stream.Close();
            }
        }
        else
        {
            this._InitFirstTime();
            this._SaveToStorage();
        }
    }
Exemple #2
0
    // Save map data to local storage
    public void _SaveToStorage()
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream      stream    = new FileStream(path, FileMode.Create);

        formatter.Serialize(stream, MapDataSerialized._MapDataToSerialized(this));
        stream.Close();
    }
Exemple #3
0
    public static MapData _SerializedToMapData(MapDataSerialized mapDataSerialized)
    {
        // Use the singleton
        for (int i = 0; i < Number.MAP_SIZE; i++)
        {
            for (int j = 0; j < Number.MAP_SIZE; j++)
            {
                MapData.instance.data[i, j] = mapDataSerialized.data[i, j];
            }
        }
        for (int k = 0; k < mapDataSerialized.objects.Length; k++)
        {
            MapData.instance.objects.Add(MapObjectSerialized._SerializedToMapObject(mapDataSerialized.objects[k]));
        }

        return(MapData.instance);
    }
Exemple #4
0
    public static MapDataSerialized _MapDataToSerialized(MapData mapData)
    {
        // Initialize
        MapDataSerialized mapDataSerialized = new MapDataSerialized();

        mapDataSerialized.data    = new int [Number.MAP_SIZE, Number.MAP_SIZE];
        mapDataSerialized.objects = new MapObjectSerialized[mapData.objects.Count];

        // Deep copy value
        for (int i = 0; i < Number.MAP_SIZE; i++)
        {
            for (int j = 0; j < Number.MAP_SIZE; j++)
            {
                mapDataSerialized.data[i, j] = mapData.data[i, j];
            }
        }
        for (int k = 0; k < mapData.objects.Count; k++)
        {
            mapDataSerialized.objects[k] = MapObjectSerialized._MapObjectToSerialized(mapData.objects[k]);
        }

        return(mapDataSerialized);
    }