< Summary

Class:EnemyAi
Assembly:Test
File(s):D:/--UnityProject/VR/_____ISSTA 26/VGuns-Unity-VR/Assets/Test/__Test/EnemyAi.cs
Covered lines:63
Uncovered lines:10
Coverable lines:73
Total lines:154
Line coverage:86.3% (63 of 73)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:11
Method coverage:81.8% (9 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%000100%

File(s)

D:/--UnityProject/VR/_____ISSTA 26/VGuns-Unity-VR/Assets/Test/__Test/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
 540    void Start () {
 541        UnityEngine.AI.NavMeshAgent agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
 42
 543        self.transform.position = startpoint.position;
 44
 545    }
 46    private void Awake()
 547    {
 548        AudioManager = FindObjectOfType<AudioManager>();
 549        player = GameObject.Find("Camera Offset").transform;
 550        agent = GetComponent<NavMeshAgent>();
 551        alreadyAttacked = false;
 552    }
 53
 54    private void Update()
 868555    {
 868556        TextMeshPro.text = "Health: " + (health);
 57
 58        //Check for sight and attack range
 868559        playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
 868560        playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
 61
 877162        if(agent.velocity.magnitude < 0.1f && !playerInAttackRange && !playerInAttackRange) SearchWalkPoint();
 63
 1535764        if (!playerInSightRange && !playerInAttackRange) Patroling();
 1010765        if (playerInSightRange && !playerInAttackRange) ChasePlayer();
 927666        if (playerInAttackRange && playerInSightRange) AttackPlayer();
 868567    }
 68
 69    private void Patroling()
 667270    {
 668671        if (!walkPointSet) SearchWalkPoint();
 72
 667273        if (walkPointSet)
 666374            agent.SetDestination(walkPoint);
 75
 667276        Vector3 distanceToWalkPoint = transform.position - walkPoint;
 77
 78        //Walkpoint reached
 667279        if (distanceToWalkPoint.magnitude < 1f)
 380            walkPointSet = false;
 667281    }
 82    private void SearchWalkPoint()
 10083    {
 84        //Calculate random point in range
 10085        float randomZ = Random.Range(-walkPointRange, walkPointRange);
 10086        float randomX = Random.Range(-walkPointRange, walkPointRange);
 87
 10088        walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
 89
 10090        if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
 2891            walkPointSet = true;
 10092    }
 93
 94    private void ChasePlayer()
 142295    {
 142296        agent.SetDestination(player.position);
 97
 142298        transform.LookAt(player, Vector3.up);
 142299        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran
 1422100    }
 101
 102    private void AttackPlayer()
 591103    {
 104
 105        //Make sure enemy doesn't move
 591106        agent.SetDestination(this.agent.transform.position);
 107
 591108        agent.acceleration = 0f;
 109
 591110        transform.LookAt(player, Vector3.up);
 591111        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran
 112
 113
 591114        if (!alreadyAttacked)
 29115        {
 29116            AudioManager.Play("Gun Fire");
 117
 118           ///Attack code here
 29119            Rigidbody rb = Instantiate(projectile, Attackpoint.position, Quaternion.identity).GetComponent<Rigidbody>();
 29120            rb.AddForce(transform.forward * forceforward, ForceMode.Impulse);
 29121            rb.AddForce(transform.up * forceup, ForceMode.Impulse);
 122            ///End of attack code
 123
 29124            alreadyAttacked = true;
 29125            Invoke(nameof(ResetAttack), timeBetweenAttacks);
 29126        }
 591127    }
 128    private void ResetAttack()
 29129    {
 29130        alreadyAttacked = false;
 29131    }
 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()
 165148    {
 165149        Gizmos.color = Color.red;
 165150        Gizmos.DrawWireSphere(transform.position, attackRange);
 165151        Gizmos.color = Color.yellow;
 165152        Gizmos.DrawWireSphere(transform.position, sightRange);
 165153    }
 154}