/// <summary> /// Remove an InventorySpace from the view. /// </summary> public void Remove(InventorySpace space) { int index = -1; for (int i = 0; i < _sets.Length; ++i) { if (_sets[i].space == space) { index = i; break; } } if (index != -1) { _sets[index].Clear(); if (index != _sets.Length - 1 && _sets[index + 1].space != null) { for (int i = index + 1; i < _sets.Length; ++i) { _sets[i - 1] = _sets[i]; _sets[i].Clear(); } } --_count; } }
public void OnAfterDeserialize() { _views.Clear(); foreach (SerializableInventoryView data in serializedViews) { InventoryView view = new InventoryView(data.name, data.spaces.Length); // Add all the spaces to the view. for (int i = 0; i < data.spaces.Length; ++i) { InventorySpace space = FindSpaceByName(data.spaces[i]); if (space != null) { view.Add(space); } } // Add all the filters to the view. for (int i = 0; i < data.filters.Length; ++i) { view.AddFilter(data.filters[i]); } // Set the sorting method. view.Sorting = data.sortBy; } serializedViews.Clear(); }
/// <summary> /// Adds a new InventorySpace to the list of spaces in this /// InventoryView. /// </summary> public void Add(InventorySpace space) { int index = -1; for (int i = 0; i < _sets.Length; ++i) { if (_sets[i].space == null) { _sets[i].Set(space); index = i; break; } } if (index != -1) { index = _sets.Length; // Otherwise, have to grow array. This should happen rarely, if ever. Debug.LogWarning("Growing array in InventoryView from [" + index + "]"); _sets = AHArray.Added(_sets, new InventoryViewSet()); _sets[index].Set(space); } ++_count; if (Active) { Filter(_sets[index]); Sort(_sets[index]); } }
/// <summary> /// Try and take the specified number of items from a specific inventory space. /// </summary> /// <returns>An InventoryItem, with number of items taken (0 if none).</returns> public InventoryItem TakeFrom(string name, IGameItem item, uint count = 1) { InventorySpace space = FindSpaceByName(name); if (space != null) { return(space.Take(item, count)); } else { return(item.CreateInventoryItem()); /* Creates empty inventory item. */ } }
/// <summary>Get the total number of `item` in the specified InventorySpace.</summary> public ulong CountIn(string name, IGameItem item) { InventorySpace space = FindSpaceByName(name); if (space != null) { return(space.Count(item)); } else { return(0); } }
/// <summary> /// Remove and returns the InventorySpace with the provided name. /// </summary> public InventorySpace RemoveSpace(string name) { InventorySpace space = FindSpaceByName(name); if (space != null) { stores.Remove(space); } // Make sure the InventorySpace is removed from all // InventoryViews. foreach (InventoryView view in _views) { view.Remove(space); } return(space); }
/// <summary> /// Try and store the items into the specified inventory space. /// </summary> public InventoryResult StoreIn(string name, InventoryItem item, uint count = 0) { if (count != 0) { Debug.Assert(item.count == 0, "[InventoryComponent] Overwriting count of InventoryItem with count > 0 on StoreIn!"); item.count = count; } InventorySpace space = FindSpaceByName(name); if (space != null) { return(space.Store(item)); } else { return(InventoryResult.InvalidSpace); } }
/// <summary> /// Adds a new InventorySpace to the list of spaces. /// If an InventorySpace already exists with the provided name, it will /// return `SpaceAlreadyExists`, otherwise will return `Success`. /// </summary> public InventoryResult AddSpace(string name, float capacity) { if (FindSpaceByName(name) != null) { return(InventoryResult.SpaceAlreadyExists); } InventorySpace space = new InventorySpace(name, capacity); stores.Add(space); // Make sure if there are any InventoryViews active, then we // update them with the new InventorySpace. foreach (InventoryView view in _views) { if (view.Active) { view.Add(space); } } return(InventoryResult.Success); }
public void Set(InventorySpace space) { this.space = space; _items = null; }
public void Clear() { space = null; _items = null; }