< Summary

Class:EnemyAi
Assembly:Test
File(s):D:/--UnityProject/VR/VRExplorer_subjects/VGuns-Unity-VR/Assets/Test/Enemy/EnemyAi.cs
Covered lines:45
Uncovered lines:28
Coverable lines:73
Total lines:154
Line coverage:61.6% (45 of 73)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:11
Method coverage:63.6% (7 of 11)

Coverage History

Metrics

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

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
 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()
 798055    {
 798056        TextMeshPro.text = "Health: " + (health);
 57
 58        //Check for sight and attack range
 798059        playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
 798060        playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
 61
 879162        if(agent.velocity.magnitude < 0.1f && !playerInAttackRange && !playerInAttackRange) SearchWalkPoint();
 63
 1495464        if (!playerInSightRange && !playerInAttackRange) Patroling();
 898665        if (playerInSightRange && !playerInAttackRange) ChasePlayer();
 798066        if (playerInAttackRange && playerInSightRange) AttackPlayer();
 798067    }
 68
 69    private void Patroling()
 697470    {
 779771        if (!walkPointSet) SearchWalkPoint();
 72
 697473        if (walkPointSet)
 615374            agent.SetDestination(walkPoint);
 75
 697476        Vector3 distanceToWalkPoint = transform.position - walkPoint;
 77
 78        //Walkpoint reached
 697479        if (distanceToWalkPoint.magnitude < 1f)
 780            walkPointSet = false;
 697481    }
 82    private void SearchWalkPoint()
 163483    {
 84        //Calculate random point in range
 163485        float randomZ = Random.Range(-walkPointRange, walkPointRange);
 163486        float randomX = Random.Range(-walkPointRange, walkPointRange);
 87
 163488        walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
 89
 163490        if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
 3591            walkPointSet = true;
 163492    }
 93
 94    private void ChasePlayer()
 100695    {
 100696        agent.SetDestination(player.position);
 97
 100698        transform.LookAt(player, Vector3.up);
 100699        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran
 1006100    }
 101
 102    private void AttackPlayer()
 0103    {
 104
 105        //Make sure enemy doesn't move
 0106        agent.SetDestination(this.agent.transform.position);
 107
 0108        agent.acceleration = 0f;
 109
 0110        transform.LookAt(player, Vector3.up);
 0111        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran
 112
 113
 0114        if (!alreadyAttacked)
 0115        {
 0116            AudioManager.Play("Gun Fire");
 117
 118           ///Attack code here
 0119            Rigidbody rb = Instantiate(projectile, Attackpoint.position, Quaternion.identity).GetComponent<Rigidbody>();
 0120            rb.AddForce(transform.forward * forceforward, ForceMode.Impulse);
 0121            rb.AddForce(transform.up * forceup, ForceMode.Impulse);
 122            ///End of attack code
 123
 0124            alreadyAttacked = true;
 0125            Invoke(nameof(ResetAttack), timeBetweenAttacks);
 0126        }
 0127    }
 128    private void ResetAttack()
 0129    {
 0130        alreadyAttacked = false;
 0131    }
 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()
 526148    {
 526149        Gizmos.color = Color.red;
 526150        Gizmos.DrawWireSphere(transform.position, attackRange);
 526151        Gizmos.color = Color.yellow;
 526152        Gizmos.DrawWireSphere(transform.position, sightRange);
 526153    }
 154}