using System.Collections; using System.Collections.Generic; using UnityEngine; using InfallibleCode; using UICode; public class RaycastManager : MonoBehaviour { /*[SerializeField] */private Camera _camera; //A reference to the camera of the ray. public GameObject _currentHitObject; //The object that the ray is currently hitting. [HideInInspector] public RaycastHit hit; LayerMask mask = -1; private static RaycastManager _instance; public static RaycastManager GetInstance() { return _instance; } public bool IsLookingAtInteractable; public bool IsLookingAtGrabbable; public bool RaycastIsActive = true; void Awake() { if (!_instance) { _instance = this; } } private void Start() { _camera = PlayerManager.GetInstance().PlayerMainCamera.GetComponent(); } public void RaycastIsActivated() { RaycastIsActive = true; } public void RaycastIsDisabled() { RaycastIsActive = false; } void Update() { #region UI Raycast //var interactableForUI = _currentHitObjects.GetComponent(); //if (interactableForUI == null) //{ // UIManager.GetInstance().raycastDefault.SetActive(true); // UIManager.GetInstance().raycastInteractabe.SetActive(false); // return; //} //else //{ // UIManager.GetInstance().raycastDefault.SetActive(false); // UIManager.GetInstance().raycastInteractabe.SetActive(true); //} if (RaycastIsActive) { if (_currentHitObject == null) { UIManager.GetInstance().raycastDefault.SetActive(true); UIManager.GetInstance().raycastInteractabe.SetActive(false); UIManager.GetInstance().raycastLocked.SetActive(false); } #endregion //RaycastHit hit; //The object that the raycast will hit. Ray ray = _camera.ScreenPointToRay(Input.mousePosition); //The ray that comes from the camera. float raydistance = 2.5f; // The distance of the ray. if (Physics.Raycast(ray, out hit, raydistance, mask.value, QueryTriggerInteraction.Ignore)) { _currentHitObject = hit.transform.gameObject; Debug.DrawLine(ray.origin, hit.point, Color.red); #region Press E Input if (_currentHitObject == null) return; if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton0)/*A button*/) { if (GameplayController.GetInstance().IsOnState(GameplayController.State.Gameplay)) { var interactable = _currentHitObject.GetComponents(); if (interactable == null) return; foreach (var _interactable in interactable) { _interactable.Interact(); } //interactable.Interact(); } } #endregion #region Draggable Input if (Input.GetMouseButton(0)) //While you are holding down the left mouse: { var draggable = _currentHitObject.GetComponent(); if (draggable == null) return; #region Player Camera //CameraMovement.GetInstance()._cameraSensitivity = 0.5f; #endregion draggable.Drag(); } else if (DraggablesManager.GetInstance().IsDragging) { //draggable.Drag //CameraMovement.GetInstance()._cameraSensitivity = CameraMovement.GetInstance()._mouseSensitivityDefault; //Restores mouse sensitivity back to the deafult value. DraggablesManager.GetInstance().IsDragging = false; print("Default mouse sens"); } #region Discoverable //if (ExaminableItemData.GetInstance().IsExamining) //{ //print("Mphka sthn if mwrh"); var discoverable = _currentHitObject.GetComponents(); if (discoverable == null) return; foreach (var _discoverable in discoverable) { _discoverable.Discover(); print("Discoverable item"); } //} #endregion //// Initialize variables to null //DraggableWindow window = null; //DoorBehaviour door = null; #region UI Raycast var interactableForUI = _currentHitObject.GetComponent(); var interactableForUI_Doors = _currentHitObject.GetComponent(); var interactableForUI_Windows = _currentHitObject.GetComponent(); var interactableForUI_Drawers = _currentHitObject.GetComponent(); //var grabbableForUI = _currentHitObjects.GetComponent(); if (interactableForUI == null) { UIManager.GetInstance().raycastDefault.SetActive(true); UIManager.GetInstance().raycastInteractabe.SetActive(false); UIManager.GetInstance().raycastLocked.SetActive(false); interactableForUI_Doors = null; return; } else { // Check if it's a DoorBehaviour if (interactableForUI_Doors != null) { if (interactableForUI_Doors.CanOpenDoor) { UIManager.GetInstance().raycastDefault.SetActive(false); UIManager.GetInstance().raycastInteractabe.SetActive(true); UIManager.GetInstance().raycastLocked.SetActive(false); } else // Door is locked { UIManager.GetInstance().raycastDefault.SetActive(false); UIManager.GetInstance().raycastInteractabe.SetActive(false); UIManager.GetInstance().raycastLocked.SetActive(true); } } else if (interactableForUI_Windows) { if (interactableForUI_Windows.CanOpenWindow) { UIManager.GetInstance().raycastDefault.SetActive(false); UIManager.GetInstance().raycastInteractabe.SetActive(true); UIManager.GetInstance().raycastLocked.SetActive(false); } else // window is locked { UIManager.GetInstance().raycastDefault.SetActive(false); UIManager.GetInstance().raycastInteractabe.SetActive(false); UIManager.GetInstance().raycastLocked.SetActive(true); } } else if (interactableForUI_Drawers) { if (interactableForUI_Drawers.CanOpenDrawer) { UIManager.GetInstance().raycastDefault.SetActive(false); UIManager.GetInstance().raycastInteractabe.SetActive(true); UIManager.GetInstance().raycastLocked.SetActive(false); } else // drawer is locked { UIManager.GetInstance().raycastDefault.SetActive(false); UIManager.GetInstance().raycastInteractabe.SetActive(false); UIManager.GetInstance().raycastLocked.SetActive(true); } } // It's neither DraggableWindow nor DoorBehaviour else { UIManager.GetInstance().raycastDefault.SetActive(false); UIManager.GetInstance().raycastInteractabe.SetActive(true); UIManager.GetInstance().raycastLocked.SetActive(false); } } //if (grabbableForUI == null && IsLookingAtInteractable == false) //{ // //IsLookingAtInteractable = false; // IsLookingAtGrabbable = false; // UIManager.GetInstance().raycastDefault.SetActive(true); // UIManager.GetInstance().raycastInteractabe.SetActive(false); // return; //} //else //{ // IsLookingAtGrabbable = true; // if (IsLookingAtInteractable == false) // { // UIManager.GetInstance().raycastDefault.SetActive(false); // UIManager.GetInstance().raycastInteractabe.SetActive(true); // } //} #region Polished WIP Implementation //var interactableForUI = _currentHitObjects.GetComponent(); //var grabbableForUI = _currentHitObjects.GetComponent(); //if (interactableForUI == null && IsLookingAtGrabbable == false) //{ // IsLookingAtInteractable = false; // //IsLookingAtGrabbable = false; // UIManager.GetInstance().raycastDefault.SetActive(true); // UIManager.GetInstance().raycastInteractabe.SetActive(false); // return; //} //else //{ // IsLookingAtInteractable = true; // if (IsLookingAtGrabbable == false) // { // UIManager.GetInstance().raycastDefault.SetActive(false); // UIManager.GetInstance().raycastInteractabe.SetActive(true); // } //} //if(grabbableForUI == null && IsLookingAtInteractable == false) //{ // //IsLookingAtInteractable = false; // IsLookingAtGrabbable = false; // UIManager.GetInstance().raycastDefault.SetActive(true); // UIManager.GetInstance().raycastInteractabe.SetActive(false); // return; //} //else //{ // IsLookingAtGrabbable = true; // if (IsLookingAtInteractable == false) // { // UIManager.GetInstance().raycastDefault.SetActive(false); // UIManager.GetInstance().raycastInteractabe.SetActive(true); // } //} #endregion #endregion #region WorldUI Icon switch (hit.collider.tag) { case "Interactable": //_currentHitObjects = hit.transform.gameObject; if (!UIManager.GetInstance().updateUI) { UIManager.GetInstance().updateUI = true; var worldUI = _currentHitObject.GetComponent(); if (worldUI == null) return; worldUI.UpdateWorldUI(); print("UI icon is updating"); } //if (DraggablesManager.GetInstance().IsDragging) //{ // UIManager.GetInstance().updateUI = true; // var worldUI = _currentHitObjects.GetComponent(); // if (worldUI == null) return; // worldUI.UpdateWorldUI(); // print("UI icon is updating"); //} break; default: if (UIManager.GetInstance().updateUI) { UIManager.GetInstance().DisableWorldUIIcon(); print("UI icon is Disabled"); UIManager.GetInstance().updateUI = false; } break; } #endregion #endregion #region old code //switch (hit.collider.tag) //{ // case "Closet": // _currentHitObjects = hit.transform.gameObject; // print("Ray hit the closet"); // if (ClosetHideBehaviour.GetInstance().InCloset == false && ClosetHideBehaviour.GetInstance().OutOfCloset) // { // ClosetHideBehaviour.GetInstance().HideInCloset(); // } // break; // case "LightSwitch": // _currentHitObjects = hit.transform.gameObject; // _currentHitObjects.GetComponent().lightSwitch(); // print("Ray hit the lightSwitch"); // //LightSwitch.GetInstance().lightSwitch(); // LightsManager.GetInstance().CanInteractWithLight = true; // break; // case "Battery": // _currentHitObjects = hit.transform.gameObject; // print("Ray hit a battery"); // BatteryManager.GetInstance().collectBattery(); // if (BatteryManager.GetInstance().BatteryCollected) // { // AudioManager.GetInstance().PickUpItemSoundPlay(); // Destroy(hit.transform.gameObject); // BatteryManager.GetInstance().BatteryCollected = false; // } // break; // case "Pills": // _currentHitObjects = hit.transform.gameObject; // print("Ray hit a pill"); // PlayerHealthManager.GetInstance().collectMedicine(); // if (PlayerHealthManager.GetInstance().PillsCollected) //If you have just collected a medicine: // { // AudioManager.GetInstance().PickUpItemSoundPlay(); // Destroy(hit.transform.gameObject); // PlayerHealthManager.GetInstance().PillsCollected = false; //Defaults the MedicineCollected bool for the next medicine to be picked up. // } // break; // case "Door": // _currentHitObjects = hit.transform.gameObject; // print("Ray hit a door"); // DoorBehaviour.GetInstance().OpenDoor(); // break; // case "ExaminableItem": // _currentHitObjects = hit.transform.gameObject; // print("Ray hit an ExaminableItem"); // ExaminableItemData.GetInstance().CanInteractWithInteractableItem = true; // break; // default: // LightsManager.GetInstance().CanInteractWithLight = false; // ExaminableItemData.GetInstance().CanInteractWithInteractableItem = false; // break; //} #endregion } else { _currentHitObject = null; } } } public void CheckForInteractableObject() { var interactable = _currentHitObject.GetComponents(); if (interactable == null) return; foreach (var _interactable in interactable) { _interactable.Interact(); } } }