| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using UnityEngine; |
| | | 4 | | using UnityEngine.UI; |
| | | 5 | | using UnityEngine.Events; |
| | | 6 | | |
| | | 7 | | public class Collectable : MonoBehaviour { |
| | | 8 | | public Item item; // The object that will go into the inventory when this is picked up |
| | | 9 | | private Inventory inventory; |
| | | 10 | | public Material inactiveMaterial; // Default material for item (when not pointing at it) |
| | | 11 | | public Material gazedAtMaterial; // Highlight material for item (to show it's inspectable) |
| | | 12 | | public UnityEvent dispatch; // Callback |
| | | 13 | | |
| | 20 | 14 | | void Awake() { |
| | 20 | 15 | | Debug.Log("init collectable"); |
| | 20 | 16 | | inventory = FindObjectOfType<Inventory>(); |
| | 20 | 17 | | if (inventory == null) { |
| | 0 | 18 | | Debug.Log("Couldn't find inventory!"); |
| | 0 | 19 | | } |
| | 20 | 20 | | } |
| | | 21 | | |
| | 18 | 22 | | void Start() { |
| | 18 | 23 | | SetGazedAt(false); |
| | 18 | 24 | | } |
| | | 25 | | |
| | 6 | 26 | | public void Collect() { |
| | 8 | 27 | | if (item) { |
| | 2 | 28 | | Debug.Log("Picked up " + item.name + "!"); |
| | | 29 | | // Add item to inventory and remove item from scene |
| | 2 | 30 | | inventory.AddItem(item); |
| | 0 | 31 | | GameObject.Destroy(gameObject); |
| | 0 | 32 | | } |
| | 8 | 33 | | if (dispatch != null) dispatch.Invoke(); // Callback |
| | 4 | 34 | | } |
| | | 35 | | |
| | 21 | 36 | | public void SetGazedAt(bool gazedAt) { |
| | | 37 | | // Show material corresponding to controller's laser pointer location |
| | 42 | 38 | | if (inactiveMaterial != null && gazedAtMaterial != null) { |
| | 21 | 39 | | GetComponent<Renderer>().material = gazedAt ? gazedAtMaterial : inactiveMaterial; |
| | 21 | 40 | | return; |
| | | 41 | | } |
| | 21 | 42 | | } |
| | | 43 | | } |