/// <summary> /// Checks where the player camera is focusing and /// display a message about it. /// </summary> void CheckPlayerFocus() { if (UnityEngine.Physics.Raycast(_cam.position, _cam.forward, out _hit, 5, _focusLayers.value)) { if (_hit.transform.gameObject.tag == "Resource") { Resource resource = _hit.transform.gameObject.GetComponent <Resource>(); _actionText.Text = "Resource: " + resource.Item.name; } else if (_hit.transform.gameObject.tag == "Container" || _hit.transform.gameObject.tag == "Trader") { if (_hit.transform.gameObject.tag == "Container") { _actionText.Text = "Press (E) to open Container"; } else { _actionText.Text = "Press (E) to trade"; } _interaction = Interaction.OpenContainer; _interactionObj = _hit.transform.gameObject; } else if (_hit.transform.gameObject.tag == "Interactable") { _actionText.Text = _hit.transform.gameObject.name; _interaction = Interaction.None; _interactionObj = _hit.transform.gameObject; } else if (_hit.transform.gameObject.GetComponent <ItemReference>() != null) { ItemReference itemRef = _hit.transform.gameObject.GetComponent <ItemReference>(); if (itemRef != null) { if (itemRef.Data.CanBePicked) { _actionText.Text = "Press (E) to grab " + itemRef.Data.ItemName + " x " + itemRef.Amount; if (itemRef.Data.GetType() == typeof(Tool)) { _interaction = Interaction.GrabTool; } else { _interaction = Interaction.GrabItem; } _interactionObj = _hit.transform.gameObject; } } } else { _interactionObj = null; _actionText.Text = ""; } } else { _interactionObj = null; _actionText.Text = ""; } }
void Update() { if (Input.GetKeyDown(KeyCode.Escape) && !_inventoryUI.IsActive()) { ShowPauseMenu(); } if ((Input.GetKeyDown(KeyCode.I) || Input.GetKeyDown(KeyCode.Tab)) && !_showingMenu) { _inventoryUI.Toogle(); _trading = false; } if (_showingMenu || _inventoryUI.IsActive()) { return; } //Enter/Exit the Crafting Mode if (Input.GetKeyDown(KeyCode.B) || Input.GetKeyDown(KeyCode.C)) { _craftingMode = !_craftingMode; if (_itemObj == null) { _itemObj = Manager.GetCraftableItemObj(_craftTypeIdx, _itemIdx); } if (_itemObj != null) { _itemObj.SetActive(_craftingMode); _actionText.Text = ""; _craftCostUI.gameObject.SetActive(_craftingMode); _currCraftItem = Manager.GetCraftableItem(_craftTypeIdx, _itemIdx); _craftCostUI.DrawCostView(_currCraftItem, _inventory); _craftCostUI.setTypeText(_craftTypeIdx); } } if (Input.GetKeyDown(KeyCode.G)) { if (_toolHandler.CurrentTool != null) { _currItem = _toolHandler.CurrentTool; Drop(); _currItem = null; } } if (_craftingMode) { OnCraftingMode(); } else { if (Input.GetButtonDown("Fire1") && _toolHandler.CurrentTool != null) { _toolHandler.Attack(); } //Perform some action when E is pressed if ((Input.GetKeyDown(KeyCode.E) || Input.GetButton("Fire2")) && _interactionObj != null) { if (_interaction == Interaction.GrabTool) { ItemReference tool = _interactionObj.GetComponent <ItemReference> () as ItemReference; Tool item = _interactionObj.GetComponent <ItemReference>().Data as Tool; _inventory.Add(item, tool.Amount, this); if (_toolHandler.CurrentTool == null) { _toolHandler.ChangeTool(item); Destroy(_interactionObj); } else { Destroy(_interactionObj); } _interactionObj = null; } else if (_interaction == Interaction.GrabItem) { ItemReference item = _interactionObj.GetComponent <ItemReference> (); float amountTaken = _inventory.Add(item.Data, item.Amount, this); if (amountTaken == item.Amount) { Destroy(_interactionObj); } else { item.Amount -= amountTaken; } } else if (_interaction == Interaction.OpenContainer) { //get the container inventory Inventory inventory = _interactionObj.GetComponent <Inventory> (); //draw the player's inventory _inventoryUI.Draw(_inventory); _trading = (inventory.InvType == Inventory.Type.Store); //draw the container inventory _inventoryUI.Draw(inventory); } } CheckPlayerFocus(); } }