< Summary

Class:EnemyAi
Assembly:Test
File(s):D:/--UnityProject/VR/_____ISSTA 26/VGuns-Unity-VR/Assets/Test/__Test/EnemyAi.cs
Covered lines:56
Uncovered lines:17
Coverable lines:73
Total lines:154
Line coverage:76.7% (56 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%00088.89%
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/_____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()
 2390055    {
 2390056        TextMeshPro.text = "Health: " + (health);
 57
 58        //Check for sight and attack range
 2390059        playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
 2390060        playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
 61
 2506562        if(agent.velocity.magnitude < 0.1f && !playerInAttackRange && !playerInAttackRange) SearchWalkPoint();
 63
 3706164        if (!playerInSightRange && !playerInAttackRange) Patroling();
 3021665        if (playerInSightRange && !playerInAttackRange) ChasePlayer();
 2832366        if (playerInAttackRange && playerInSightRange) AttackPlayer();
 2390067    }
 68
 69    private void Patroling()
 1316170    {
 1317271        if (!walkPointSet) SearchWalkPoint();
 72
 1316173        if (walkPointSet)
 1315474            agent.SetDestination(walkPoint);
 75
 1316176        Vector3 distanceToWalkPoint = transform.position - walkPoint;
 77
 78        //Walkpoint reached
 1316179        if (distanceToWalkPoint.magnitude < 1f)
 080            walkPointSet = false;
 1316181    }
 82    private void SearchWalkPoint()
 117683    {
 84        //Calculate random point in range
 117685        float randomZ = Random.Range(-walkPointRange, walkPointRange);
 117686        float randomX = Random.Range(-walkPointRange, walkPointRange);
 87
 117688        walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
 89
 117690        if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
 37191            walkPointSet = true;
 117692    }
 93
 94    private void ChasePlayer()
 631695    {
 631696        agent.SetDestination(player.position);
 97
 631698        transform.LookAt(player, Vector3.up);
 631699        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran
 6316100    }
 101
 102    private void AttackPlayer()
 4423103    {
 104
 105        //Make sure enemy doesn't move
 4423106        agent.SetDestination(this.agent.transform.position);
 107
 4423108        agent.acceleration = 0f;
 109
 4423110        transform.LookAt(player, Vector3.up);
 4423111        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran
 112
 113
 4423114        if (!alreadyAttacked)
 101115        {
 101116            AudioManager.Play("Gun Fire");
 117
 118           ///Attack code here
 101119            Rigidbody rb = Instantiate(projectile, Attackpoint.position, Quaternion.identity).GetComponent<Rigidbody>();
 101120            rb.AddForce(transform.forward * forceforward, ForceMode.Impulse);
 101121            rb.AddForce(transform.up * forceup, ForceMode.Impulse);
 122            ///End of attack code
 123
 101124            alreadyAttacked = true;
 101125            Invoke(nameof(ResetAttack), timeBetweenAttacks);
 101126        }
 4423127    }
 128    private void ResetAttack()
 100129    {
 100130        alreadyAttacked = false;
 100131    }
 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}