Beispiel #1
0
    // Save the current game to a file
    public void Save()
    {
        Environment environment = mMap;

        SaveDataPacket packet = new SaveDataPacket();


        // Player Data
        packet.Money   = shop.GetCurrency();
        packet.Time    = mDayTime;
        packet.PlayerX = mCharacter.CurrentPosition.PositionTile.x;
        packet.PlayerY = mCharacter.CurrentPosition.PositionTile.y;

        packet.Inventory = new ItemSaveData[mCharacter.inventory.Length];
        for (int i = 0; i < mCharacter.inventory.Length; i++)
        {
            if (mCharacter.inventory[i] != null)
            {
                packet.Inventory[i].N = mCharacter.inventory[i].itemName;
                packet.Inventory[i].C = mCharacter.inventory[i].count;
            }
        }

        // Entity Data
        Entity[] entities = environment.GetEntities();
        packet.Entities = new EntitySaveData[entities.Length];
        for (int i = 0; i < entities.Length; i++)
        {
            packet.Entities[i].N = entities[i].entityName;
            packet.Entities[i].X = entities[i].CurrentPosition.PositionTile.x;
            packet.Entities[i].Y = entities[i].CurrentPosition.PositionTile.y;
        }

        // World Data
        environment.Save(ref packet);

        // Save to the JSON file
        string jsonData = JsonUtility.ToJson(packet, false);

        File.WriteAllText("GameSave.json", jsonData);
    }