< Summary

Class:EnemyAi
Assembly:Test
File(s):D:/--UnityProject/VR/VRExplorer_subjects/VGuns-Unity-VR/Assets/Test/Enemy/EnemyAi.cs
Covered lines:57
Uncovered lines:16
Coverable lines:73
Total lines:154
Line coverage:78% (57 of 73)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:11
Method coverage:72.7% (8 of 11)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%000100%
Awake()0%000100%
Update()0%000100%
Patroling()0%000100%
SearchWalkPoint()0%000100%
ChasePlayer()0%000100%
AttackPlayer()0%000100%
ResetAttack()0%000100%
TakeDamage(...)0%0000%
DestroyEnemy()0%0000%
OnDrawGizmosSelected()0%0000%

File(s)

D:/--UnityProject/VR/VRExplorer_subjects/VGuns-Unity-VR/Assets/Test/Enemy/EnemyAi.cs

#LineLine coverage
 1using TMPro;
 2using UnityEngine;
 3using UnityEngine.AI;
 4
 5public class EnemyAi : MonoBehaviour
 6{
 7    public NavMeshAgent agent;
 8
 9    public Transform player;
 10
 11    public LayerMask whatIsGround, whatIsPlayer;
 12
 13    public float forceup;
 14    public float forceforward;
 15
 16    public Transform Attackpoint;
 17
 18    public float health;
 19
 20    public AudioManager AudioManager;
 21
 22    public GameObject self;
 23    public Transform startpoint;
 24    public TextMeshPro TextMeshPro;
 25
 26    //Patroling
 27    public Vector3 walkPoint;
 28    bool walkPointSet;
 29    public float walkPointRange;
 30
 31    //Attacking
 32    public float timeBetweenAttacks;
 33    bool alreadyAttacked;
 34    public GameObject projectile;
 35
 36    //States
 37    public float sightRange, attackRange;
 38    public bool playerInSightRange, playerInAttackRange;
 39
 2040    void Start () {
 2041        UnityEngine.AI.NavMeshAgent agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
 42
 2043        self.transform.position = startpoint.position;
 44
 2045    }
 46    private void Awake()
 2047    {
 2048        AudioManager = FindObjectOfType<AudioManager>();
 2049        player = GameObject.Find("Camera Offset").transform;
 2050        agent = GetComponent<NavMeshAgent>();
 2051        alreadyAttacked = false;
 2052    }
 53
 54    private void Update()
 4498555    {
 4498556        TextMeshPro.text = "Health: " + (health);
 57
 58        //Check for sight and attack range
 4498559        playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
 4498560        playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
 61
 4551962        if(agent.velocity.magnitude < 0.1f && !playerInAttackRange && !playerInAttackRange) SearchWalkPoint();
 63
 8818964        if (!playerInSightRange && !playerInAttackRange) Patroling();
 4557065        if (playerInSightRange && !playerInAttackRange) ChasePlayer();
 4618166        if (playerInAttackRange && playerInSightRange) AttackPlayer();
 4498567    }
 68
 69    private void Patroling()
 4320470    {
 4323571        if (!walkPointSet) SearchWalkPoint();
 72
 4320473        if (walkPointSet)
 4319274            agent.SetDestination(walkPoint);
 75
 4320476        Vector3 distanceToWalkPoint = transform.position - walkPoint;
 77
 78        //Walkpoint reached
 4320479        if (distanceToWalkPoint.magnitude < 1f)
 680            walkPointSet = false;
 4320481    }
 82    private void SearchWalkPoint()
 56583    {
 84        //Calculate random point in range
 56585        float randomZ = Random.Range(-walkPointRange, walkPointRange);
 56586        float randomX = Random.Range(-walkPointRange, walkPointRange);
 87
 56588        walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
 89
 56590        if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
 12391            walkPointSet = true;
 56592    }
 93
 94    private void ChasePlayer()
 58595    {
 58596        agent.SetDestination(player.position);
 97
 58598        transform.LookAt(player, Vector3.up);
 58599        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran
 585100    }
 101
 102    private void AttackPlayer()
 1196103    {
 104
 105        //Make sure enemy doesn't move
 1196106        agent.SetDestination(this.agent.transform.position);
 107
 1196108        agent.acceleration = 0f;
 109
 1196110        transform.LookAt(player, Vector3.up);
 1196111        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran
 112
 113
 1196114        if (!alreadyAttacked)
 22115        {
 22116            AudioManager.Play("Gun Fire");
 117
 118           ///Attack code here
 22119            Rigidbody rb = Instantiate(projectile, Attackpoint.position, Quaternion.identity).GetComponent<Rigidbody>();
 22120            rb.AddForce(transform.forward * forceforward, ForceMode.Impulse);
 22121            rb.AddForce(transform.up * forceup, ForceMode.Impulse);
 122            ///End of attack code
 123
 22124            alreadyAttacked = true;
 22125            Invoke(nameof(ResetAttack), timeBetweenAttacks);
 22126        }
 1196127    }
 128    private void ResetAttack()
 21129    {
 21130        alreadyAttacked = false;
 21131    }
 132
 133
 134    public void TakeDamage(int damage)
 0135    {
 0136        health -= damage;
 137
 0138        if (health <= 0) Invoke(nameof(DestroyEnemy), 0.5f);
 0139    }
 140    private void DestroyEnemy()
 0141    {
 0142        AudioManager.Play("EnemyDie");
 0143        walkPoint = startpoint.position;
 0144        agent.acceleration = 0f;
 0145        self.transform.position = startpoint.position;
 0146    }
 147    private void OnDrawGizmosSelected()
 0148    {
 0149        Gizmos.color = Color.red;
 0150        Gizmos.DrawWireSphere(transform.position, attackRange);
 0151        Gizmos.color = Color.yellow;
 0152        Gizmos.DrawWireSphere(transform.position, sightRange);
 0153    }
 154}