159 lines
6.0 KiB
C#
159 lines
6.0 KiB
C#
![]() |
using System;
|
||
|
using System.Collections;
|
||
|
using BehaviorDesigner.Runtime;
|
||
|
using BehaviorDesigner.Runtime.Tasks;
|
||
|
using Sirenix.OdinInspector;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.AI;
|
||
|
using Action = BehaviorDesigner.Runtime.Tasks.Action;
|
||
|
|
||
|
// ReSharper disable once CheckNamespace
|
||
|
namespace BlueWaterProject
|
||
|
{
|
||
|
[TaskCategory("Custom/Action")]
|
||
|
public class FindTarget : Action
|
||
|
{
|
||
|
[Title("Common variable")]
|
||
|
[RequiredField] public SharedAiStat aiStat;
|
||
|
[RequiredField] public SharedNavMeshAgent navMeshAgent;
|
||
|
[RequiredField] public SharedTransform targetTransform;
|
||
|
|
||
|
[Title("Offense Variable")]
|
||
|
public SharedIslandInfo islandInfo;
|
||
|
|
||
|
[Title("Defense Variable")]
|
||
|
public SharedVector3 defensePos;
|
||
|
|
||
|
private AiController aiController;
|
||
|
private bool isFindTargetCoroutine;
|
||
|
|
||
|
private static readonly WaitForSeconds FindTargetWaitTime = new(0.5f);
|
||
|
|
||
|
public override void OnAwake()
|
||
|
{
|
||
|
aiController = GetComponent<AiController>();
|
||
|
}
|
||
|
|
||
|
public override void OnStart()
|
||
|
{
|
||
|
switch (aiStat.Value.AttackerType)
|
||
|
{
|
||
|
case EAttackerType.NONE:
|
||
|
Debug.Log("AiStat.AttackerType == NONE Error");
|
||
|
break;
|
||
|
case EAttackerType.OFFENSE:
|
||
|
isFindTargetCoroutine = true;
|
||
|
StartCoroutine(nameof(FindTargetInOffense));
|
||
|
break;
|
||
|
case EAttackerType.DEFENSE:
|
||
|
isFindTargetCoroutine = true;
|
||
|
StartCoroutine(nameof(FindTargetInDefense));
|
||
|
break;
|
||
|
default:
|
||
|
throw new ArgumentOutOfRangeException();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public override TaskStatus OnUpdate()
|
||
|
{
|
||
|
return isFindTargetCoroutine ? TaskStatus.Running : TaskStatus.Success;
|
||
|
}
|
||
|
|
||
|
public IEnumerator FindTargetInOffense()
|
||
|
{
|
||
|
switch (aiStat.Value.OffenseType)
|
||
|
{
|
||
|
case EOffenseType.NONE:
|
||
|
Debug.Log("AiStat.OffenseType == NONE Error");
|
||
|
break;
|
||
|
case EOffenseType.NORMAL:
|
||
|
if (islandInfo.Value.EnemyList.Count > 0)
|
||
|
{
|
||
|
aiController.SetNearestTargetInOffense(islandInfo.Value.EnemyList);
|
||
|
}
|
||
|
else if (islandInfo.Value.HouseList.Count > 0)
|
||
|
{
|
||
|
aiController.SetNearestTargetInOffense(islandInfo.Value.HouseList);
|
||
|
}
|
||
|
break;
|
||
|
case EOffenseType.ONLY_HOUSE:
|
||
|
if (navMeshAgent.Value.pathStatus == NavMeshPathStatus.PathPartial)
|
||
|
{
|
||
|
aiController.SetNearestTargetInOffense(islandInfo.Value.TargetAllList);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (islandInfo.Value.HouseList.Count > 0)
|
||
|
{
|
||
|
aiController.SetNearestTargetInOffense(islandInfo.Value.HouseList);
|
||
|
}
|
||
|
else if (islandInfo.Value.EnemyList.Count > 0)
|
||
|
{
|
||
|
aiController.SetNearestTargetInOffense(islandInfo.Value.EnemyList);
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
throw new ArgumentOutOfRangeException();
|
||
|
}
|
||
|
|
||
|
if (targetTransform.Value && navMeshAgent.Value.enabled)
|
||
|
{
|
||
|
var distanceToTarget = Vector3.Distance(transform.position, targetTransform.Value.position);
|
||
|
|
||
|
navMeshAgent.Value.isStopped = distanceToTarget <= navMeshAgent.Value.stoppingDistance;
|
||
|
if (distanceToTarget > navMeshAgent.Value.stoppingDistance)
|
||
|
{
|
||
|
navMeshAgent.Value.SetDestination(targetTransform.Value.position);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
yield return FindTargetWaitTime;
|
||
|
isFindTargetCoroutine = false;
|
||
|
}
|
||
|
|
||
|
public IEnumerator FindTargetInDefense()
|
||
|
{
|
||
|
switch (aiStat.Value.DefenseType)
|
||
|
{
|
||
|
case EDefenseType.NONE:
|
||
|
Debug.Log("AiStat.DefenseType == NONE Error");
|
||
|
break;
|
||
|
case EDefenseType.STRIKER:
|
||
|
aiController.SetNearestTargetInDefense(transform.position, aiStat.Value.ViewRange);
|
||
|
break;
|
||
|
case EDefenseType.MIDFIELDER:
|
||
|
aiController.SetNearestTargetInDefense(transform.position, aiStat.Value.ViewRange);
|
||
|
break;
|
||
|
case EDefenseType.DEFENDER:
|
||
|
aiController.SetNearestTargetInDefense(defensePos.Value, aiStat.Value.DefenseRange);
|
||
|
break;
|
||
|
case EDefenseType.KEEPER:
|
||
|
aiController.SetNearestTargetInDefense(transform.position, aiStat.Value.ViewRange);
|
||
|
break;
|
||
|
default:
|
||
|
throw new ArgumentOutOfRangeException();
|
||
|
}
|
||
|
|
||
|
if (targetTransform.Value && navMeshAgent.Value.enabled)
|
||
|
{
|
||
|
var distanceToTarget = Vector3.Distance(transform.position, targetTransform.Value.position);
|
||
|
|
||
|
navMeshAgent.Value.isStopped = distanceToTarget <= navMeshAgent.Value.stoppingDistance;
|
||
|
if (distanceToTarget > navMeshAgent.Value.stoppingDistance)
|
||
|
{
|
||
|
navMeshAgent.Value.SetDestination(targetTransform.Value.position);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
yield return FindTargetWaitTime;
|
||
|
isFindTargetCoroutine = false;
|
||
|
}
|
||
|
|
||
|
public override void OnEnd()
|
||
|
{
|
||
|
StopAllCoroutines();
|
||
|
}
|
||
|
}
|
||
|
}
|