Exemple #1
0
 public void InitializeInventory(string playerData, PlayerManager player)
 {
     _settings = new JsonSerializerSettings {
         TypeNameHandling = TypeNameHandling.All, ReferenceLoopHandling = ReferenceLoopHandling.Ignore
     };
     NetInventory      = JsonConvert.DeserializeObject(playerData, _settings) as NetInventory;
     _player           = player;
     player.playerData = playerData;
     GetComponent <DressController>().InitializeWear(NetInventory);
 }
Exemple #2
0
    public void InitializeWear(NetInventory playerSaveData)
    {
        List <Wearable> wearables = playerSaveData.Wearables.ToList();
        List <Item>     asItems   = new List <Item>();

        foreach (var wearable in wearables)
        {
            asItems.Add(wearable as Item);
        }

        LoadController.instance.LoadRuntimeItems(asItems, RunItemsLoaded);
    }
 public static void UpdatePlayerInventory(int player, NetInventory inventory)
 {
     using (Packet packet = new Packet((int)ServerPackets.jsonObject))
     {
         packet.Write(player);
         JsonSerializerSettings settings = new JsonSerializerSettings {
             TypeNameHandling = TypeNameHandling.All, ReferenceLoopHandling = ReferenceLoopHandling.Ignore
         };
         string json = JsonConvert.SerializeObject(inventory, settings);
         packet.Write(json);
         SendTCPDataToAll(packet);
     }
 }
Exemple #4
0
    public NetInventory MakeNetCopy()
    {
        NetInventory netCopy = new NetInventory();

        foreach (var item in Inventory)
        {
            if (item.Value.Item is Wearable)
            {
                for (int i = 0; i < netCopy.Wearables.Length; i++)
                {
                    if (netCopy.Wearables[i].Slot == item.Value.Item.Slot)
                    {
                        netCopy.Wearables[i] = item.Value.Item as Wearable;
                    }
                }
            }
            if (item.Value.Item is Holdable)
            {
                for (int i = 0; i < netCopy.Wearables.Length; i++)
                {
                    if (netCopy.Holdables[i].Slot == item.Value.Item.Slot)
                    {
                        netCopy.Holdables[i] = item.Value.Item as Holdable;
                    }
                }
            }
            if (item.Value.Item is Consumable)
            {
                for (int i = 0; i < netCopy.Wearables.Length; i++)
                {
                    if (netCopy.Consumable[i].Slot == item.Value.Item.Slot)
                    {
                        netCopy.Consumable[i] = item.Value.Item as Consumable;
                    }
                }
            }
            if (item.Value.Item is Misc)
            {
                for (int i = 0; i < netCopy.Wearables.Length; i++)
                {
                    if (netCopy.Misc[i].Slot == item.Value.Item.Slot)
                    {
                        netCopy.Misc[i] = item.Value.Item as Misc;
                    }
                }
            }
        }
        return(netCopy);
    }
    public void SaveInventory(Dictionary <string, ItemSlot> slots) // save inherited data for wearable/holdable data as well
    {
        NetInventory      playerSaveData = new NetInventory();
        List <Wearable>   wearables      = new List <Wearable>();
        List <Holdable>   holdables      = new List <Holdable>();
        List <Consumable> consumables    = new List <Consumable>();
        List <Misc>       miscs          = new List <Misc>();

        foreach (var itemSlot in slots)
        {
            if (itemSlot.Value.RuntimeItem.Item is Wearable)
            {
                Wearable wearable = ScriptableObject.CreateInstance(typeof(Wearable)) as Wearable;
                wearable.Initialize(itemSlot.Value.RuntimeItem.Item, itemSlot.Key);
                wearables.Add(wearable);
            }
            if (itemSlot.Value.RuntimeItem.Item is Holdable)
            {
                Holdable holdable = ScriptableObject.CreateInstance(typeof(Holdable)) as Holdable;
                holdable.Initialize(itemSlot.Value.RuntimeItem.Item, itemSlot.Key);
                holdables.Add(holdable);
            }
            if (itemSlot.Value.RuntimeItem.Item is Consumable)
            {
                Consumable consumable = ScriptableObject.CreateInstance(typeof(Consumable)) as Consumable;
                consumable.Initialize(itemSlot.Value.RuntimeItem.Item, itemSlot.Key);
                consumables.Add(consumable);
            }
            if (itemSlot.Value.RuntimeItem.Item is Misc)
            {
                Misc misc = ScriptableObject.CreateInstance(typeof(Misc)) as Misc;
                misc.Initialize(itemSlot.Value.RuntimeItem.Item, itemSlot.Key);
                miscs.Add(misc);
            }
        }

        playerSaveData.Wearables  = wearables.ToArray();
        playerSaveData.Holdables  = holdables.ToArray();
        playerSaveData.Consumable = consumables.ToArray();
        playerSaveData.Misc       = miscs.ToArray();

        JsonSerializerSettings settings = new JsonSerializerSettings {
            TypeNameHandling = TypeNameHandling.All, ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };
        string json = JsonConvert.SerializeObject(playerSaveData, settings);

        File.WriteAllText(Application.persistentDataPath + "/GAPData.json", json);
        Debug.Log($"Saved inventory to: {Application.persistentDataPath}/GAPData.json");
    }
Exemple #6
0
    NetInventory SortAndSerialize(List <Item> items)
    {
        NetInventory      playerSaveData = new NetInventory();
        List <Wearable>   wearables      = new List <Wearable>();
        List <Holdable>   holdables      = new List <Holdable>();
        List <Consumable> consumables    = new List <Consumable>();
        List <Misc>       miscs          = new List <Misc>();

        foreach (var item in items)
        {
            if (item is Wearable)
            {
                wearables.Add(item as Wearable);
            }
            if (item is Holdable)
            {
                holdables.Add(item as Holdable);
            }
            if (item is Consumable)
            {
                consumables.Add(item as Consumable);
            }
            if (item is Misc)
            {
                miscs.Add(item as Misc);
            }
        }

        playerSaveData.Wearables  = wearables.ToArray();
        playerSaveData.Holdables  = holdables.ToArray();
        playerSaveData.Consumable = consumables.ToArray();
        playerSaveData.Misc       = miscs.ToArray();


        return(playerSaveData);
    }
    public Dictionary <string, Item> LoadInventory(string playerData)
    {
        JsonSerializerSettings settings = new JsonSerializerSettings {
            TypeNameHandling = TypeNameHandling.All, ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };
        NetInventory playerSaveData = JsonConvert.DeserializeObject(playerData, settings) as NetInventory;

        Dictionary <string, Item> runTimeDictionary = new Dictionary <string, Item>();

        if (playerSaveData.Wearables != null)
        {
            foreach (var wearable in playerSaveData.Wearables)
            {
                if (!runTimeDictionary.ContainsKey(wearable.Slot))
                {
                    runTimeDictionary.Add(wearable.Slot, wearable as Item);
                }
                else
                {
                    Debug.Log($"Inventory already contains Item {runTimeDictionary[wearable.Slot].Name} on slot: {wearable.Slot}. Trying to also add item: {wearable.Name} to the same slot.");
                }
            }
        }
        if (playerSaveData.Holdables != null)
        {
            foreach (var holdable in playerSaveData.Holdables)
            {
                if (!runTimeDictionary.ContainsKey(holdable.Slot))
                {
                    runTimeDictionary.Add(holdable.Slot, holdable as Item);
                }
                else
                {
                    Debug.Log($"Inventory already contains Item {runTimeDictionary[holdable.Slot].Name} on slot: {holdable.Slot}. Trying to also add item: {holdable.Name} to the same slot.");
                }
            }
        }
        if (playerSaveData.Consumable != null)
        {
            foreach (var consumable in playerSaveData.Consumable)
            {
                if (!runTimeDictionary.ContainsKey(consumable.Slot))
                {
                    runTimeDictionary.Add(consumable.Slot, consumable as Item);
                }
                else
                {
                    Debug.Log($"Inventory already contains Item {runTimeDictionary[consumable.Slot].Name} on slot: {consumable.Slot}. Trying to also add item: {consumable.Name} to the same slot.");
                }
            }
        }
        if (playerSaveData.Misc != null)
        {
            foreach (var misc in playerSaveData.Misc)
            {
                if (!runTimeDictionary.ContainsKey(misc.Slot))
                {
                    runTimeDictionary.Add(misc.Slot, misc as Item);
                }
                else
                {
                    Debug.Log($"Inventory already contains Item {runTimeDictionary[misc.Slot].Name} on slot: {misc.Slot}. Trying to also add item: {misc.Name} to the same slot.");
                }
            }
        }
        Debug.Log($"Loaded inventory with: {runTimeDictionary.Count} Slots taken");
        return(runTimeDictionary);
    }