/// <summary> /// Triggers the tile rto be destroyed /// </summary> public void OnHammerHit() { // if there are things on this tile, try hitting them first List <IHammerHittable> hitables = new List <IHammerHittable>(); m_killables.ForEach(k => { IHammerHittable hitable = k.SourceGO.GetComponent <IHammerHittable>(); if (hitable != null) { hitables.Add(hitable); } }); // Don't destroy the tile but whats on it instead if (hitables.Count > 0) { hitables.ForEach(h => { // Only if they can be hit if (h.CanBeHit()) { h.OnHammerHit(); } }); // Have to process the hit otherwise the player sits there and waits StartCoroutine(HitProcessDelay()); } else { m_manager.DestroyTile(this); } }
/// <summary> /// Triggers the hammer animation and hitting the target /// </summary> /// <param name="target"></param> /// <returns></returns> IEnumerator HammerActionRoutine(IHammerHittable target) { IsMovementDisabled = true; m_canHammer = false; m_hammerHasHit = false; // Before hammering, if we are moving we must wait until reaching the destination while (m_isMoving) { yield return(null); } // If the target can still be hit, then hit it if (target.CanBeHit()) { m_animator.SetTrigger("Hammer"); target.OnHammerHit(); // Wait until the hammer connects and triggers the expected result before resuming control while (!target.HitProcessed()) { yield return(null); } } m_canHammer = true; m_hammerTriggered = false; IsMovementDisabled = false; }
/// <summary> /// Process the request to hit the given object /// Disables player movement /// /// </summary> /// <param name="target"></param> public void TriggerHammerAction(IHammerHittable target) { if (m_canHammer) { m_hammerTriggered = true; m_hammerButtonReleased = false; StartCoroutine(HammerActionRoutine(target)); } }
/// <summary> /// Handles player mouse inputs /// </summary> void HandleMouseInput() { if (!ControlsEnabled) { return; } // Not clicking on anything if (m_mouseToggle && !Input.GetButtonDown("Fire1")) { return; } else if (!m_mouseToggle && !Input.GetButton("Fire1")) { return; } else { m_hammerButtonReleased = true; } // Already triggered - wait until it is done if (!m_hammerTriggered && m_hammerButtonReleased) { // Create a ray from the mouse cursor on screen in the direction of the camera. Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition); // Create a RaycastHit variable to store information about what was hit by the ray. RaycastHit hit; Debug.DrawRay(camRay.origin, camRay.direction); // Perform the raycast and if it hits something on the floor layer... if (Physics.Raycast(camRay, out hit, m_camRayLength, m_hamerHitLayer)) { IHammerHittable target = hit.collider.GetComponent <IHammerHittable>(); Tile tile = hit.collider.GetComponent <Tile>(); if (target != null && tile != null && target.CanBeHit()) { TriggerHammerAction(target); } } } }