This repository has been archived on 2024-07-09. You can view files and clone it, but cannot push or open issues or pull requests.
everellia/SheKnowsWhatYouAreToHerGame/Assets/Scripts/HerController.cs

127 lines
3.5 KiB
C#

/*
* author: mark joshwel
* date: 19/6/2024
* description: enemy AI based off <https://youtu.be/UjkSFoLxesw>
*/
using UnityEngine;
using UnityEngine.AI;
/// <summary>
/// AI patrolling, chasing and capturing behaviour for the enemy
/// </summary>
public class HerController : MonoBehaviour
{
/// <summary>
/// cache variable for the walking parameter in the animator
/// </summary>
private static readonly int IsMoving = Animator.StringToHash("isMoving");
/// <summary>
/// cache variable for the chasing parameter in the animator
/// </summary>
private static readonly int IsChasing = Animator.StringToHash("isChasing");
/// <summary>
/// variable for the nav mesh agent that determines where the enemy can move
/// </summary>
public NavMeshAgent agent;
/// <summary>
/// variable for the player's position
/// </summary>
public Transform player;
/// <summary>
/// variables to distinguish player for sensing
/// </summary>
public LayerMask playerLayerMask;
/// <summary>
/// variable specifying the capture range of the enemy
/// </summary>
public float captureRange;
/// <summary>
/// boolean variable for the player being in attack range
/// </summary>
public bool playerInCaptureRange;
/// <summary>
/// variable for the animator
/// </summary>
private Animator _anim;
/// <summary>
/// variable to store game manager
/// </summary>
private GameManager _game;
/// <summary>
/// function to set any initial values
/// </summary>
private void Awake()
{
// get the player
player = GameObject.FindGameObjectWithTag("Player").transform;
// get the nav mesh agent
agent = GetComponent<NavMeshAgent>();
// get the animator
_anim = GetComponent<Animator>();
// get the game manager
_game = GameManager.Instance;
}
/// <summary>
/// function to update the enemy's behaviour
/// </summary>
private void Update()
{
// check for sight and attack range
playerInCaptureRange = Physics.CheckSphere(transform.position, captureRange, playerLayerMask);
if (!playerInCaptureRange) ChasePlayer();
else CapturePlayer();
}
/// <summary>
/// function to draw/visualize the sight and attack range of the enemy
/// </summary>
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, captureRange);
}
/// <summary>
/// function to chase the player
/// </summary>
private void ChasePlayer()
{
// start chasing animation
_anim.SetBool(IsMoving, true);
_anim.SetBool(IsChasing, true);
// aim at the player
agent.SetDestination(player.position);
}
/// <summary>
/// function that captures the player and signals the game manager appropriately
/// </summary>
private void CapturePlayer()
{
// signal the game manager to show the 'caught!' menu
if (_game.Paused) return;
Debug.Log("captured...");
_game.SetDisplayState(GameManager.DisplayState.ScreenCaughtMenu);
// // stop any moving animations
// _anim.SetBool(IsChasing, false);
// _anim.SetBool(IsMoving, false);
// // don't move the enemy
// agent.SetDestination(transform.position);
// // look at me!
// transform.LookAt(player);
}
}