< Summary

Class:EnemyAi
Assembly:Test
File(s):D:/--UnityProject/VR/VRExplorer_subjects/VGuns-Unity-VR/Assets/Test/Enemy/EnemyAi.cs
Covered lines:34
Uncovered lines:39
Coverable lines:73
Total lines:154
Line coverage:46.5% (34 of 73)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:11
Method coverage:45.4% (5 of 11)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%000100%
Awake()0%000100%
Update()0%00084.62%
Patroling()0%000100%
SearchWalkPoint()0%000100%
ChasePlayer()0%0000%
AttackPlayer()0%0000%
ResetAttack()0%0000%
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
 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()
 488555    {
 488556        TextMeshPro.text = "Health: " + (health);
 57
 58        //Check for sight and attack range
 488559        playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
 488560        playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
 61
 494562        if(agent.velocity.magnitude < 0.1f && !playerInAttackRange && !playerInAttackRange) SearchWalkPoint();
 63
 977064        if (!playerInSightRange && !playerInAttackRange) Patroling();
 488565        if (playerInSightRange && !playerInAttackRange) ChasePlayer();
 488566        if (playerInAttackRange && playerInSightRange) AttackPlayer();
 488567    }
 68
 69    private void Patroling()
 488570    {
 489771        if (!walkPointSet) SearchWalkPoint();
 72
 488573        if (walkPointSet)
 487874            agent.SetDestination(walkPoint);
 75
 488576        Vector3 distanceToWalkPoint = transform.position - walkPoint;
 77
 78        //Walkpoint reached
 488579        if (distanceToWalkPoint.magnitude < 1f)
 280            walkPointSet = false;
 488581    }
 82    private void SearchWalkPoint()
 7283    {
 84        //Calculate random point in range
 7285        float randomZ = Random.Range(-walkPointRange, walkPointRange);
 7286        float randomX = Random.Range(-walkPointRange, walkPointRange);
 87
 7288        walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
 89
 7290        if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
 2191            walkPointSet = true;
 7292    }
 93
 94    private void ChasePlayer()
 095    {
 096        agent.SetDestination(player.position);
 97
 098        transform.LookAt(player, Vector3.up);
 099        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran
 0100    }
 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()
 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}