/// <summary> /// this allows the player to interact with collidable objects /// </summary> protected virtual void OnControllerColliderHit(ControllerColliderHit hit) { Rigidbody body = hit.collider.attachedRigidbody; if (body == null || body.isKinematic) { return; } vp_Interactable interactable = null; if (!m_Interactables.TryGetValue(hit.collider, out interactable)) { m_Interactables.Add(hit.collider, interactable = hit.collider.GetComponent <vp_Interactable>()); } if (interactable == null) { return; } if (interactable.InteractType != vp_Interactable.vp_InteractType.CollisionTrigger) { return; } hit.gameObject.SendMessage("TryInteract", m_Player, SendMessageOptions.DontRequireReceiver); }
/// <summary> /// handles the displaying of the interactables /// crosshair if any /// </summary> protected virtual void InteractCrosshair() { // return if no player crosshair if (m_Player.Crosshair.Get() == null) { return; } // return if interacting if (m_Player.Interactable.Get() != null) { return; } // if interactable is found vp_Interactable interactable = null; if (FindInteractable(out interactable)) { // if this is our first instance of looking at this interactable if (interactable != m_CurrentCrosshairInteractable) { // return if same interactable as last frame if (CrosshairTimeoutTimer > Time.time && ((m_LastInteractable != null) && (interactable.GetType() == m_LastInteractable.GetType()))) { return; } m_CanInteract = true; // cache this interactable for crosshair checking m_CurrentCrosshairInteractable = interactable; // show the interactables hudtext if any if (interactable.InteractText != "" && !m_ShowTextTimer.Active) { vp_Timer.In(interactable.DelayShowingText, delegate() { m_Player.HUDText.Send(interactable.InteractText); }, m_ShowTextTimer); } // return if interactable doesn't have a crosshair if (interactable.m_InteractCrosshair == null) { return; } // set the crosshair to the interactables m_Player.Crosshair.Set(interactable.m_InteractCrosshair); } } else { m_CanInteract = false; ResetCrosshair(); } }
/// <summary> /// finishes interaction with the current interactable /// </summary> protected virtual bool ShouldFinishInteraction() { if (m_Player.Interactable.Get() != null) { m_CurrentCrosshairInteractable = null; // set this to null to allow the crosshair to change again ResetCrosshair(); // reset the crosshair to default m_Player.Interactable.Get().FinishInteraction(); // end interaction with the active interactable m_Player.Interactable.Set(null); // set this to null to allow new interaction return(true); // don't allow new interaction this time through } return(false); }
/// <summary> /// Resets the crosshair to it's default state. /// </summary> protected virtual void ResetCrosshair(bool reset = true) { if (m_OriginalCrosshair == null || m_Player.Crosshair.Get() == m_OriginalCrosshair) { return; } m_ShowTextTimer.Cancel(); if (reset) { m_Player.Crosshair.Set(m_OriginalCrosshair); } m_CurrentCrosshairInteractable = null; }
/// <summary> /// adds a condition (a rule set) that must be met for the /// event handler 'Interact' activity to successfully activate. /// NOTE: other scripts may have added conditions to this /// activity aswell /// </summary> protected virtual bool CanStart_Interact() { // if we are already interacting, we need to stop interacting so a new interaction can begin. if (ShouldFinishInteraction()) { return(false); } // if the weapon is being set, don't allow interaction if (m_Player.SetWeapon.Active) { return(false); } vp_Interactable interactable = null; if (FindInteractable(out interactable)) { // if the interactable is of type vp_InteractType.Normal, carry on if (interactable.InteractType != vp_Interactable.vp_InteractType.Normal) { return(false); } // check if we can interact with the interactable, if so, carry on if (!interactable.TryInteract(m_Player)) { return(false); } // reset some things ResetCrosshair(false); // cache this interactable m_LastInteractable = interactable; return(true); // allow us interaction } return(false); // if all else fails, don't allow interaction }
/// <summary> /// does a raycast to see if an interactable is in range /// and returns that interactable in the out as well as returns /// true if an interactable was found /// </summary> protected virtual bool FindInteractable(out vp_Interactable interactable) { interactable = null; RaycastHit hit; LayerMask layerMask = ~((1 << vp_Layer.LocalPlayer) | (1 << vp_Layer.Debris) | (1 << vp_Layer.IgnoreRaycast) | (1 << vp_Layer.IgnoreBullets) | (1 << vp_Layer.Trigger) | (1 << vp_Layer.Water)); if (Physics.Raycast(m_Camera.Transform.position, m_Camera.Transform.forward, out hit, MaxInteractDistance, layerMask)) { // test to see if we hit a collider and if that collider contains a vp_Interactable instance if (!m_Interactables.TryGetValue(hit.collider, out interactable)) { m_Interactables.Add(hit.collider, interactable = hit.collider.GetComponent <vp_Interactable>()); } // return if no interactable if (interactable == null) { return(false); } // checks our distance, either from this instance's interactDistance, or if it's overridden on the interactable itself. If the hit is within range, carry on if (interactable.InteractDistance == 0 && hit.distance >= (m_Player.IsFirstPerson.Get() ? InteractDistance : InteractDistance3rdPerson)) { return(false); } // make sure the interact distance isn't higher than the interactables if (interactable.InteractDistance > 0 && hit.distance >= interactable.InteractDistance) { return(false); } } else { return(false); } return(true); }
/// <summary> /// does a raycast to see if an interactable is in range /// and returns that interactable in the out as well as returns /// true if an interactable was found /// </summary> protected virtual bool FindInteractable(out vp_Interactable interactable) { interactable = null; RaycastHit hit; if (Physics.Raycast(m_Camera.Transform.position, m_Camera.Transform.forward, out hit, MaxInteractDistance, vp_Layer.Mask.BulletBlockers)) { // test to see if we hit a collider and if that collider contains a vp_Interactable instance if (!m_Interactables.TryGetValue(hit.collider, out interactable)) { m_Interactables.Add(hit.collider, interactable = hit.collider.GetComponent <vp_Interactable>()); } // return if no interactable if (interactable == null) { return(false); } // checks our distance, either from this instance's interactDistance, or if it's overridden on the interactable itself. If the hit is within range, carry on if (interactable.InteractDistance == 0 && hit.distance >= InteractDistance) { return(false); } // make sure the interact distance isn't higher than the interactables if (interactable.InteractDistance > 0 && hit.distance >= interactable.InteractDistance) { return(false); } } else { return(false); } return(true); }
/// <summary> /// Resets the crosshair to it's default state. /// </summary> protected virtual void ResetCrosshair( bool reset = true ) { if(m_OriginalCrosshair == null || m_Player.Crosshair.Get() == m_OriginalCrosshair) return; m_ShowTextTimer.Cancel(); if(reset) m_Player.Crosshair.Set( m_OriginalCrosshair ); m_CurrentCrosshairInteractable = null; }
/// <summary> /// does a raycast to see if an interactable is in range /// and returns that interactable in the out as well as returns /// true if an interactable was found /// </summary> protected virtual bool FindInteractable( out vp_Interactable interactable ) { interactable = null; RaycastHit hit; if(Physics.Raycast(m_Camera.Transform.position, m_Camera.Transform.forward, out hit, MaxInteractDistance, vp_Layer.Mask.BulletBlockers)) { // test to see if we hit a collider and if that collider contains a vp_Interactable instance if(!m_Interactables.TryGetValue(hit.collider, out interactable)) m_Interactables.Add(hit.collider, interactable = hit.collider.GetComponent<vp_Interactable>()); // return if no interactable if(interactable == null) return false; // checks our distance, either from this instance's interactDistance, or if it's overridden on the interactable itself. If the hit is within range, carry on if(interactable.InteractDistance == 0 && hit.distance >= (m_Player.IsFirstPerson.Get() ? InteractDistance : InteractDistance3rdPerson)) return false; // make sure the interact distance isn't higher than the interactables if(interactable.InteractDistance > 0 && hit.distance >= interactable.InteractDistance) return false; } else return false; return true; }
/// <summary> /// handles the displaying of the interactables /// crosshair if any /// </summary> protected virtual void InteractCrosshair() { // return if no player crosshair if(m_Player.Crosshair.Get() == null) return; // return if interacting if(m_Player.Interactable.Get() != null) return; // if interactable is found vp_Interactable interactable = null; if(FindInteractable( out interactable )) { // if this is our first instance of looking at this interactable if(interactable != m_CurrentCrosshairInteractable) { // return if same interactable as last frame if(CrosshairTimeoutTimer > Time.time && ((m_LastInteractable != null) && (interactable.GetType() == m_LastInteractable.GetType()))) return; m_CanInteract = true; // cache this interactable for crosshair checking m_CurrentCrosshairInteractable = interactable; // show the interactables hudtext if any if(interactable.InteractText != "" && !m_ShowTextTimer.Active) { vp_Timer.In(interactable.DelayShowingText, delegate() { m_Player.HUDText.Send(interactable.InteractText); }, m_ShowTextTimer); } // return if interactable doesn't have a crosshair if(interactable.m_InteractCrosshair == null) return; // set the crosshair to the interactables m_Player.Crosshair.Set( interactable.m_InteractCrosshair ); } } else { m_CanInteract = false; ResetCrosshair(); } }
/// <summary> /// finishes interaction with the current interactable /// </summary> protected virtual bool ShouldFinishInteraction() { if(m_Player.Interactable.Get() != null) { m_CurrentCrosshairInteractable = null; // set this to null to allow the crosshair to change again ResetCrosshair(); // reset the crosshair to default m_Player.Interactable.Get().FinishInteraction(); // end interaction with the active interactable m_Player.Interactable.Set(null); // set this to null to allow new interaction return true; // don't allow new interaction this time through } return false; }
/// <summary> /// adds a condition (a rule set) that must be met for the /// event handler 'Interact' activity to successfully activate. /// NOTE: other scripts may have added conditions to this /// activity aswell /// </summary> protected virtual bool CanStart_Interact() { // if we are already interacting, we need to stop interacting so a new interaction can begin. if(ShouldFinishInteraction()) return false; // if the weapon is being set, don't allow interaction if(m_Player.SetWeapon.Active) return false; vp_Interactable interactable = null; if(FindInteractable( out interactable )) { // if the interactable is of type vp_InteractType.Normal, carry on if(interactable.InteractType != vp_Interactable.vp_InteractType.Normal) return false; // check if we can interact with the interactable, if so, carry on if(!interactable.TryInteract(m_Player)) return false; // reset some things ResetCrosshair(false); // cache this interactable m_LastInteractable = interactable; return true; // allow us interaction } return false; // if all else fails, don't allow interaction }