Esempio n. 1
0
    private void SaveUnit(Unit unit, string fileName)
    {
        // load the old database
        _saveDatabase = JsonSaveLoadIO.LoadAllUnits(UnitsSaveFileName);

        // create new unitSaveData
        List <ItemSlotData>  occupiedItemSlots = unit.unitInventory.Where(x => x.Item != null).ToList();
        IEnumerable <string> unitAbilities     = unit.abilities.Select(x =>
        {
            foreach (KeyValuePair <string, Ability> pair in StaticData.AbilityReference)
            {
                if (pair.Value.Equals(x))
                {
                    return(pair.Key);
                }
            }
            return(null);
        });
        var unitSaveData = new UnitSaveData(unit.characterName, unit.characterClass, unit.level,
                                            occupiedItemSlots, ConvertStatsToBaseInt(unit.stats), unitAbilities.ToList());

        // update old database with new unitSaveData
        _saveDatabase[unit.characterName] = unitSaveData;

        // write out to file
        // Debug.Log(JsonSaveLoadIO.SaveUnit(unitSaveData, fileName));
        JsonSaveLoadIO.SaveAllUnits(_saveDatabase, fileName);
    }
Esempio n. 2
0
    private void SaveUnit(UnitData unit, string fileName)
    {
        // load the old database
        _saveDatabase = JsonSaveLoadIO.LoadAllUnits(UnitsSaveFileName);

        // get old unitSaveData
        UnitSaveData oldUnitSaveData;

        if (!_saveDatabase.ContainsKey(unit.unitID))
        {
            Debug.LogError("Unit not found in database. Updating with firstSave info.");
            _saveDatabase   = JsonSaveLoadIO.LoadAllUnits("firstSave");
            oldUnitSaveData = _saveDatabase[unit.unitID];
        }
        else
        {
            oldUnitSaveData = _saveDatabase[unit.unitID];
        }

        // create new unitSaveData from old unitSaveData
        var unitSaveData = new UnitSaveData(oldUnitSaveData, unit.unitItems, ConvertStatsToBaseInt(unit.stats));

        // update old database with new unitSaveData
        _saveDatabase[unit.unitID] = unitSaveData;

        // write out to file
        // Debug.Log(JsonSaveLoadIO.SaveUnit(unitSaveData, fileName));
        JsonSaveLoadIO.SaveAllUnits(_saveDatabase, fileName);
    }
Esempio n. 3
0
    public void ResetUnitAndInventorySaveData()
    {
        _saveDatabase = JsonSaveLoadIO.LoadAllUnits("first");
        JsonSaveLoadIO.SaveAllUnits(_saveDatabase, UnitsSaveFileName);
        List <Item> defaultItems = LoadInventory("first");

        SaveInventory(defaultItems);
    }
Esempio n. 4
0
    public List <Item> LoadInventory(string fileName)
    {
        ItemContainerSaveData itemContainer = JsonSaveLoadIO.LoadItems(fileName);

        List <Item> items = new List <Item>();

        foreach (ItemSlotSaveData itemSlotSave in itemContainer.savedSlots)
        {
            if (itemSlotSave != null)
            {
                items.Add(itemDataBase.GetItemCopy(itemSlotSave.itemId));
            }
        }
        return(items);
    }
Esempio n. 5
0
    // DANGEROUS BUT EASY DEFAULT TO AMOUNT = 1
    private void SaveItems(IList <Item> items, string fileName)
    {
        var saveData = new ItemContainerSaveData(items.Count);

        for (int i = 0; i < saveData.savedSlots.Length; i++)
        {
            if (items[i] == null)
            {
                saveData.savedSlots[i] = null;
            }
            else
            {
                saveData.savedSlots[i] = new ItemSlotSaveData(items[i].ID, 1);
            }
        }
        JsonSaveLoadIO.SaveItems(saveData, fileName);
    }
Esempio n. 6
0
    private void SaveItems(IList <ItemSlot> itemSlots, string fileName)
    {
        var saveData = new ItemContainerSaveData(itemSlots.Count);

        for (int i = 0; i < saveData.savedSlots.Length; i++)
        {
            ItemSlot currSlot = itemSlots[i];

            if (currSlot.Item == null)
            {
                saveData.savedSlots[i] = null;
            }
            else
            {
                saveData.savedSlots[i] = new ItemSlotSaveData(currSlot.Item.ID, currSlot.Amount);
            }
        }
        JsonSaveLoadIO.SaveItems(saveData, fileName);
    }
Esempio n. 7
0
    public UnitLoadData LoadUnit(string unitId, string filename, bool enemy = false)
    {
        // minor difference for loading enemy units
        _saveDatabase = enemy ? JsonSaveLoadIO.LoadAllEnemyUnits(filename) : JsonSaveLoadIO.LoadAllUnits(filename);

        UnitSaveData saveUnit;

        if (!_saveDatabase.ContainsKey(unitId))
        {
            Debug.LogError($"{unitId} Unit not found in database. Updating with firstSave info.");
            _saveDatabase = JsonSaveLoadIO.LoadAllUnits("firstSave");
            saveUnit      = _saveDatabase[unitId];
        }
        else
        {
            saveUnit = _saveDatabase[unitId];
        }

        return(new UnitLoadData(saveUnit, itemDataBase));
    }
Esempio n. 8
0
    public UnitData LoadUnitData(string unitId)
    {
        _saveDatabase = JsonSaveLoadIO.LoadAllUnits(UnitsSaveFileName);
        UnitSaveData saveUnit;

        if (!_saveDatabase.ContainsKey(unitId))
        {
            Debug.LogError($"{unitId} Unit not found in database. Updating with firstSave info.");
            _saveDatabase = JsonSaveLoadIO.LoadAllUnits("firstSave");
            saveUnit      = _saveDatabase[unitId];
        }
        else
        {
            saveUnit = _saveDatabase[unitId];
        }

        List <Item> items = saveUnit.unitInventory.GetCopyOfItems(itemDataBase);

        UnitData loadedUnitData = new UnitData(saveUnit.unitName, ConvertStatsToUnitStat(saveUnit.unitStats), items,
                                               saveUnit.unitAbilities, saveUnit.unitLevel, saveUnit.unitExp);

        return(loadedUnitData);
    }