/// <summary> /// Create a portable item from the given details. /// </summary> /// <param name="details">The item details to use.</param> /// <returns>An item based on the given details.</returns> public PortableItem CreateItem(PortableItemDetails details) { PortableItem item = ScriptableObject.CreateInstance <PortableItem>(); item.details = details; return(item); }
/// <summary> /// Clear and repopulate the houses. /// </summary> public void Repopulate() { // Generate enough plants to fill the planter of each house with the // same type of plant. List <PortableItem> plants = new List <PortableItem>(); for (int h = 0; h < houses.Length; ++h) { PortableItem plant = planterFactory.CreateRandomItem(); for (int i = 0; i < houses[h].Planter.Capacity; ++i) { plants.Add(planterFactory.CreateItem(plant)); } } // All plants have been made, so shuffle the array. plants.Shuffle(); // Add the plants to the houses. foreach (SuburbHouseState house in this.houses) { house.Repopulate(plants); } // Repopulated and ready to be worked. this.done = false; }
private PortableItem CreateShovel() { PortableItem item = ScriptableObject.CreateInstance <PortableItem>(); item.details = shovel; return(item); }
/// <summary> /// Only take items we can afford. /// </summary> /// <param name="item">The item of interest.</param> /// <returns>True if we can afford the item, false otherwise.</returns> public override bool CanTakeItem(PortableItem item) { if (item.price <= this.playerWallet.value) { return(true); } this.SayInsufficientFunds(); return(false); }
/// <inheritdoc /> /// <remarks> /// Don't allow an item to be dragged out of this inventory unless the /// player is carrying a shovel. /// </remarks> public override bool CanTakeItem(PortableItem item) { if (!this.PlayerHasShovel()) { this.SayNeedShovel(); return(false); } return(true); }
/// <summary> /// Initializes the controller with the necessary information to convert an /// item's state into a functional game object. /// </summary> /// <param name="item"> /// The portable item to wrap this controller around in order to transform /// it into a valid game object. /// </param> /// <param name="inventory"> /// The optional inventory controller this item belongs to. It's perfectly /// valid for an item to not belong to an inventory. /// </param> public void Initialize(PortableItem item, IInventoryController inventory = null) { if (item == null) { throw new System.ArgumentNullException("item cannot be null"); } this.item = item; this.inventory = inventory; this.Orientation = PortableObjectOrientation.Inventory; }
/// <inheritdoc /> /// <remarks> /// Takes the dragged object and attempts to delete it. If the object cannot /// be deleted the <c>HandleDropError</c> method will be called with the /// appropriate <c>InventoryError</c> value. /// </remarks> public void OnDrop(PointerEventData eventData) { PortableItemController obj = eventData.pointerDrag?.GetComponent <PortableItemController>(); PortableItem item = obj?.Item; if (item == null || !whitelist.Matches(item.details)) { this.HandleDropError(InventoryError.InvalidItem); return; } Destroy(eventData.pointerDrag); item.inventory.Remove(item); }
/// <summary> /// Take the fruit; charge the player; delete the game object. /// </summary> protected override void ValidateLayout() { foreach (Transform child in this.rectTransform) { PortableItem item = child.gameObject.GetComponent <PortableItemController>()?.Item; if (!this.inventory.Contains(item)) { child.SetParent(null); GameObject.Destroy(child.gameObject); if (item.price != 0) { this.playerWallet.value -= item.price; } } } }
public void PutInHand( PortableItem item ) { ClearHand(); switch( item ) { case PortableItem.IMPORTANT_PACKAGE: InstantiateInHand( _ImportantPackage ); break; case PortableItem.CELL_PHONE: InstantiateInHand( _CellPhone ); break; case PortableItem.GARBAGE: InstantiateInHand( _Garbage[ Random.Range(0, _Garbage.Count) ] ); break; } }
/// <summary> /// Show the given item. /// </summary> /// <param name="item">The item to show.</param> public void Show(PortableItem item) { this.canvas.SetActive(true); this.movieDetails.gameObject.SetActive(false); this.itemDetails.gameObject.SetActive(false); if (item?.details is MovieDetails movie) { this.movieDetails.gameObject.SetActive(true); this.movieDetails.Movie = movie; } else { this.itemDetails.gameObject.SetActive(true); this.itemDetails.Item = item; } }
/// <inheritdoc /> void Start() { // Clear existing weeds. for (int i = 0; i < this.rectTransform.childCount; ++i) { GameObject child = this.rectTransform.GetChild(i).gameObject; if (LawnController.IsWeed(child)) { Destroy(child); } } // Add new weeds. for (int weedIndex = 0; weedIndex < this.lawn.Capacity; ++weedIndex) { PortableItem weed = this.lawn[weedIndex]; if (weed == null) { continue; } Vector2 position; if (layout.FindPosition(this.rectTransform, out position)) { PortableItemController obj = GameObject.Instantiate(this.prefab, this.rectTransform); obj.Initialize(weed); // Set the anchor to the center because rect.min, rect.max, and the // collider are all relative to the center. RectTransform itemTransform = obj.transform as RectTransform; itemTransform.anchorMin.Set(0.5f, 0.5f); itemTransform.anchorMax.Set(0.5f, 0.5f); itemTransform.anchoredPosition = position; } else { // Couldn't place it after 100 attempts; clear it from the inventory so // we don't count it in the score. this.lawn[weedIndex] = null; } } this.OrderLawn(); }
/// <inheritdoc /> /// <remarks> /// In addition to performing all of the usual inventory checks this will /// also make sure the item physically fits using the item's width and the /// remaining inventory width. /// </remarks> public override InventoryError Add(PortableItem item, bool ignoreFilters = false) { if (!ignoreFilters && !this.Supports(item)) { return(InventoryError.InvalidItem); } if (this.Contains(item)) { return(InventoryError.AlreadyExists); } // Check for space on the shelf. float spaceNeeded = item.worldSprite.rect.width; foreach (PortableItem i in this.items) { if (i != null) { spaceNeeded += physicalItemGap + i.worldSprite.rect.width; if (physicalSpaceAvailable < spaceNeeded) { return(InventoryError.OutOfSpace); } } } // It's a valid item and everything will fit. Now put it in the first // available slot. for (int i = 0; i < this.items.Count; ++i) { if (this.items[i] == null) { item.inventory?.Remove(item); this.UpdateIndex(i, item); return(InventoryError.NoError); } } // If we couldn't find an empty slot it means we don't have space. return(InventoryError.OutOfSpace); }
/// <summary> /// Logic for re-validating the view of this inventory whenever the /// underlying inventory is changed. /// </summary> protected override void ValidateLayout() { // clear existing children for (int i = 0; i < this.rectTransform.childCount; ++i) { Destroy(this.rectTransform.GetChild(i).gameObject); } // add a new one PortableItem item = this.inventory[this.index]; if (item != null) { PortableItemController obj = Instantiate(this.prefab, Vector3.zero, Quaternion.identity, this.rectTransform); obj.Initialize(item, this); RectTransform itemTransform = obj.transform as RectTransform; // Center the object in the cell. itemTransform.pivot = new Vector2(0.5f, 0.5f); itemTransform.anchorMin = new Vector2(0.5f, 0.5f); itemTransform.anchorMax = new Vector2(0.5f, 0.5f); itemTransform.anchoredPosition = Vector2.zero; } }
/// <inheritdoc /> private void Start() { // Clear all existing children. for (int i = 0; i < this.rectTransform.childCount; ++i) { Destroy(this.rectTransform.GetChild(i).gameObject); } // Layout the items. for (int itemIndex = 0; itemIndex < this.inventory.Capacity; ++itemIndex) { PortableItem item = this.inventory[itemIndex]; if (item == null) { continue; } Vector2 position; if (this.layout.FindPosition(this.rectTransform, out position)) { PortableItemController obj = Instantiate(this.prefab, this.rectTransform); obj.Initialize(item, this); // Set the anchor to the center because rect.min, rect.max, and the // collider are all relative to the center. RectTransform itemTransform = obj.transform as RectTransform; itemTransform.anchorMin.Set(0.5f, 0.5f); itemTransform.anchorMax.Set(0.5f, 0.5f); itemTransform.anchoredPosition = position; itemTransform.pivot.Set(0.5f, 0.5f); } else { // Couldn't place it after 100 attempts; clear it from the inventory so // we don't count it in the score. this.inventory[itemIndex] = null; } } }
/// <see cref="Add" /> /// <remarks> /// This type of inventory doesn't really support setting an item by the /// index so this just proxies to <c>Add</c> and drops the index. /// </remarks> public override InventoryError Set(int index, PortableItem item, bool ignoreFilters = false) { return(this.Add(item, ignoreFilters)); }
/// <inheritdoc /> public virtual bool CanTakeItem(PortableItem item) { return(true); }
/// <summary> /// Check if the given item (based on the details) is present. /// </summary> /// <param name="item">The item to compare to.</param> /// <returns>Boolean representing if the item is present.</returns> public bool Contains(PortableItem item) { return(item != null && this.Contains(item.details)); }
/// <summary> /// Broadcast a portable item to be handled by the portable item handler. /// </summary> /// <param name="item">The item to broadcast.</param> public void Raise(PortableItem item) { this.broadcast?.Invoke(item); }
/// <summary> /// Create a copy of an item. /// </summary> /// <param name="item">The item to copy.</param> /// <returns>A new portable item based on the provided item.</returns> public PortableItem CreateItem(PortableItem item) { return(this.CreateItem(item.details)); }