< Summary

Class:KnifeBehaviour
Assembly:Test
File(s):E:/Unity/Unity Project/VR-Cooking-Demo/Assets/_Scripts/KnifeBehaviour.cs
Covered lines:28
Uncovered lines:45
Coverable lines:73
Total lines:146
Line coverage:38.3% (28 of 73)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:11
Method coverage:54.5% (6 of 11)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
KnifeBehaviour()0%000100%
Awake()0%000100%
OnDestroy()0%000100%
Start()0%000100%
Update()0%00033.33%
Left_cut(...)0%0000%
Right_cut(...)0%0000%
ConfirmValidCut(...)0%0000%
InitiateCut()0%00040%
ResetCutter()0%0000%
PlayCutSounds()0%0000%

File(s)

E:/Unity/Unity Project/VR-Cooking-Demo/Assets/_Scripts/KnifeBehaviour.cs

#LineLine coverage
 1/*
 2 *
 3 * Code by:
 4 *      Dimitrios Vlachos
 5 *      djv1@student.london.ac.uk
 6 *      dimitri.j.vlachos@gmail.com
 7 *
 8 * References:
 9 *       https://forum.unity.com/threads/how-to-get-which-hand-is-grabbing-an-xr-grab-interactable-object.946045/
 10 */
 11
 12using DynamicMeshCutter;
 13using System.Collections;
 14using System.Collections.Generic;
 15using UnityEngine;
 16using UnityEngine.InputSystem;
 17using UnityEngine.XR.Interaction.Toolkit;
 18
 19[RequireComponent(typeof(XRGrabInteractable))]
 20[RequireComponent(typeof(AudioSource))]
 21public class KnifeBehaviour : MonoBehaviour
 22{
 23    [Header("Cutting")]
 24    [SerializeField] private XRDirectInteractor rHand;
 25    [SerializeField] private XRDirectInteractor lHand;
 226    [SerializeField] private InputActionReference right_cut_event = null;
 227    [SerializeField] private InputActionReference left_cut_event = null;
 28    [SerializeField] private KnifePlaneBehaviour cutter;
 229    [SerializeField] private float cutCooldownTimer = 0.5f;
 30
 31    [Header("Knife Behaviour")]
 32    [SerializeField] private XRGrabInteractable grabInteractable;
 33
 34    [Header("Sounds")]
 35    [SerializeField, Tooltip("List of cutting sounds to use")]
 36    List<AudioClip> cuttingSounds;
 37    [SerializeField] AudioSource audioSource;
 38
 39    private Collider collider;
 40    private bool isSelected;
 41
 42    private bool has_cut_recently;
 43
 44    private void Awake()
 145    {
 146        right_cut_event.action.started += Right_cut;
 147        left_cut_event.action.started += Left_cut;
 148    }
 49
 50    private void OnDestroy()
 151    {
 152        right_cut_event.action.started -= Right_cut;
 153        left_cut_event.action.started -= Left_cut;
 154    }
 55
 56    private void Start()
 157    {
 158        has_cut_recently = false;
 159        grabInteractable = GetComponent<XRGrabInteractable>();
 160        collider = GetComponent<Collider>();
 161        isSelected = false;
 162        audioSource = GetComponent<AudioSource>();
 163    }
 64
 65    private void Update()
 168266    {
 67        // Check if object is grabbed, if so, then make collider a trigger
 168268        if(grabInteractable.isSelected)
 069        {
 070            isSelected = true;
 071            collider.isTrigger = true;
 072        }
 168273        else if(collider.isTrigger)
 074        {
 075            isSelected = false;
 076            collider.isTrigger = false;
 077        }
 168278    }
 79
 80    private void Left_cut(InputAction.CallbackContext context)
 081    {
 082        List<IXRSelectInteractable> interactables = lHand.interactablesSelected;
 83
 084        ConfirmValidCut(interactables);
 085    }
 86
 87    private void Right_cut(InputAction.CallbackContext context)
 088    {
 089        List<IXRSelectInteractable> interactables = rHand.interactablesSelected;
 90
 091        ConfirmValidCut(interactables);
 092    }
 93
 94    private void ConfirmValidCut(List<IXRSelectInteractable> interactables)
 095    {
 096        foreach (IXRSelectInteractable Interactable in interactables)
 097        {
 098            if (Interactable.transform.CompareTag("Knife"))
 099            {
 0100                InitiateCut();
 0101            }
 0102        }
 0103    }
 104
 105    public void InitiateCut()
 7106    {
 107        // Guard statements
 7108        if (!isSelected)
 7109        {
 7110            Debug.Log("Can't cut: not selected!");
 7111            return;
 112        }
 0113        if (has_cut_recently)
 0114        {
 0115            Debug.Log("Can't cut: in cooldown!");
 0116            return;
 117        }
 0118        Debug.Log("Attempting cut");
 119        // Set the cut value to true
 0120        has_cut_recently = true;
 121
 122        // Perform the cut
 0123        cutter.Cut();
 0124        PlayCutSounds();
 125
 126        // Begin cut cooldown timer
 0127        StartCoroutine(ResetCutter());
 7128    }
 129
 0130    IEnumerator ResetCutter() {
 131        // Wait for cooldown time
 0132        yield return new WaitForSeconds(cutCooldownTimer);
 0133        Debug.Log("Cooldown complete: cut ready");
 0134        has_cut_recently = false;
 0135    }
 136
 137    void PlayCutSounds()
 0138    {
 139        // Select a sound from the list at random
 0140        int randomSound = Random.Range(0, cuttingSounds.Count);
 141        // Pull that clip from the list
 0142        AudioClip clip = cuttingSounds[randomSound];
 0143        audioSource.clip = clip;
 0144        audioSource.Play();
 0145    }
 146}