public void DropItem(int index, bool removeFromInventory = true) { ItemEntry itemEntry = this[index]; if (!itemEntry.IsDroppable) { return; } ItemPickup pickup = ItemPickup.CreateFromItemEntry(itemEntry); Vector2 dropLocation = new Vector2( this.transform.position.x + UnityEngine.Random.Range(-this.dropOffsetRange.x, this.dropOffsetRange.x), this.transform.position.y + UnityEngine.Random.Range(-this.dropOffsetRange.y, this.dropOffsetRange.y) ); pickup.transform.position = dropLocation; if (removeFromInventory) { if (this.onItemRemoval != null) { this.onItemRemoval.Invoke(itemEntry); } this.items.RemoveAt(index); } }
public void DropItemYield() { if (this.IsReadyForHarvest) { Collider2D collider = GetComponent <Collider2D>(); foreach (ItemEntry yieldEntry in this.itemYield) { ItemPickup yield = ItemPickup.CreateFromItemEntry(yieldEntry); Vector3 dropOffset = new Vector3( UnityEngine.Random.Range(-0.5f, 0.5f), UnityEngine.Random.Range(0, 0.5f), 0); yield.transform.position = collider.bounds.max + dropOffset; } } this.cropBlock.Crop = null; this.cropBlock.Unwater(); PoolManager.Despawn(this.gameObject); }
public static ItemPickup CreateFromItemEntry(ItemEntry itemEntry) { if (poolObject == null) { Debug.Log("Creating ItemPickup blueprint."); poolObject = new GameObject("Item Pickup"); poolObject.AddComponent<SpriteRenderer>(); poolObject.AddComponent<CameraVisibleTrigger>(); poolObject.SetupRigidbody(); poolObject.AddComponent<ItemPickup>(); poolObject.SetActive(false); } GameObject itemPickupObject = PoolManager.Spawn(poolObject); ItemPickup itemPickup = itemPickupObject.GetComponent<ItemPickup>(); itemPickup.item = itemEntry; itemPickup.SpriteRenderer.sprite = itemEntry.ItemData.Icon; Destroy(itemPickupObject.GetComponent<Collider2D>()); BoxCollider2D collider = itemPickupObject.AddComponent<BoxCollider2D>(); collider.isTrigger = true; return itemPickup; }