public void SyncInventoryState(String state, InventoryDatabase inventoryDatabase) { String[] stateInfo = state.Split(";"); int stateIndex = 0; // Set the current inventory count int inventoryCount = int.Parse(stateInfo[stateIndex]); _availableCapacity = _items.Count - inventoryCount; stateIndex++; // Clean up the inventory for (int index = 0; index < _items.Count; index++) { _items[index] = null; } for (int index = 0; index < inventoryCount; index++) { int inventoryInex = int.Parse(stateInfo[stateIndex]); stateIndex++; _items[inventoryInex] = inventoryDatabase.GetItemByID(stateInfo[stateIndex]); stateIndex++; } // sync used item int usedIndex = int.Parse(stateInfo[stateIndex]); stateIndex++; // Clean up the used index _usedIndex.Clear(); for (int index = 0; index < usedIndex; index++) { _usedIndex.Add(int.Parse(stateInfo[stateIndex])); stateIndex++; } int equipCount = int.Parse(stateInfo[stateIndex]); stateIndex++; _equipmentIndex.Clear(); for (int index = 0; index < equipCount; index++) { String weaponKey = stateInfo[stateIndex]; stateIndex++; int itemIndex = int.Parse(stateInfo[stateIndex]); stateIndex++; int ammo = int.Parse(stateInfo[stateIndex]); stateIndex++; Weapon.WeaponOrder weaponOrder = (Weapon.WeaponOrder) int.Parse(weaponKey.Split("_")[0]); int weaponIndex = int.Parse(weaponKey.Split("_")[1]); if (itemIndex != -1) { _equipmentIndex.Add(weaponKey, itemIndex); if (_agent.GetWeapons(weaponOrder)[weaponIndex] == null || _agent.GetWeapons(weaponOrder)[weaponIndex].ItemResourceID != _items[itemIndex].ItemID) { // Client is not equip same as server, try to enforced it _agent.UnequipWeapon(weaponOrder, weaponIndex); _agent.EquipWeapon(_items[itemIndex].ReferencePackedScene, weaponOrder, weaponIndex); _agent.GetWeapons(weaponOrder)[weaponIndex].SetAmmo(ammo); EmitSignal(nameof(WeaponChangeSignal), weaponOrder, weaponIndex); } } else { if (_agent.GetWeapons(weaponOrder)[weaponIndex] != null) { // Client is equip while server does not, try to enforced it _agent.UnequipWeapon(weaponOrder, weaponIndex); EmitSignal(nameof(WeaponChangeSignal), weaponOrder, weaponIndex); } } } EmitSignal(nameof(InventoryChangeSignal)); }
public ItemResource GetPurchasableItemByID(String itemID) { return(_inventoryDatabase.GetItemByID(itemID)); }