#nullable enable using System.Collections; using System.Collections.Generic; using UnityEngine; public class AIEntity : Entity { [SerializeField] AIStats stats = null!; BaseState currentState = null!; public string ennemyName {get; protected set; } override protected void Awake(){ base.Awake(); currentState = new FindTargetState(this); } override protected void Start(){ base.Start(); currentState.EnterState(); } void Update() { if (currentState.UpdateState() is {} newState) SwitchState(newState); } void FixedUpdate() { if (currentState.FixedUpdateState() is {} newState) SwitchState(newState); } void OnDrawGizmos() => currentState?.OnDrawGizmos(); void SwitchState(BaseState newState) { currentState.LeaveState(); currentState = newState; newState.EnterState(); } abstract class BaseStateAI : BaseState{ protected AIEntity entity; public BaseStateAI(AIEntity entity){ this.entity = entity; } } class SeekState : BaseStateAI{ public SeekState(AIEntity entity) : base(entity){ } public override BaseState? UpdateState(){ Entity targetEntity = entity.GetTarget().GetComponent(); if(targetEntity != null){ if(targetEntity.IsAlive()){//target is alive, keep chasing it return null; }else{//target is dead, go to findTargetState return new FindTargetState(entity);; } } return null; } public override BaseState? FixedUpdateState(){ Transform target = entity.GetTarget(); entity.direction = Vector3.RotateTowards(entity.direction, (target.position - entity.transform.position), entity.rotSpeed*Time.fixedDeltaTime, 0.0f); if(!entity.IsInAttackRange()){ entity.rb.MovePosition(entity.transform.position + entity.direction * entity.movementSpeed * Time.fixedDeltaTime); }else{ return new AttackState(entity); } return null; } } class FindTargetState : BaseStateAI{ float closeEnough; public FindTargetState(AIEntity entity) : base(entity){ } public override BaseState? UpdateState(){ Entity[] entities = FindObjectsOfType(); float lastDist = float.MaxValue; Entity chosenEntity = null!; foreach (Entity other in entities){// Find the closest entity if(other.name == entity.ennemyName && other.IsAlive()){ float distance = Vector2.Distance(other.transform.position, entity.transform.position); if(distance < lastDist){ lastDist = distance; chosenEntity = other; if(lastDist <= entity.stats.closeEnough)break; } } } if(chosenEntity != null){ entity.SetTarget(chosenEntity.transform); }else{ return new RoamState(entity); } return new SeekState(entity); } } class AttackState : BaseStateAI{ public AttackState(AIEntity entity) : base(entity){ } public override BaseState? UpdateState(){ if(entity.IsInAttackRange()){ if(entity.attackTimer >= entity.attackCooldown){ entity.attackTimer = 0; return Attack(); }else{ entity.attackTimer += Time.deltaTime; } }else{ return new SeekState(entity); } return null; } private BaseState? Attack(){ Entity targetEntity = entity.GetTarget().GetComponent(); if(targetEntity != null){ targetEntity.TakeDamage(entity.attackDmg); bool isTargetAlive = targetEntity.IsAlive(); if(!isTargetAlive){ return new FindTargetState(entity); } } return null; } } class RoamState : BaseStateAI{ public RoamState(AIEntity entity) : base(entity){ Debug.Log("Roaming!"); } } }