190 lines
5.7 KiB
C#
190 lines
5.7 KiB
C#
|
||
using BehaviorDesigner.Runtime;
|
||
using BehaviorDesigner.Runtime.Tasks;
|
||
using DragonLi.Core;
|
||
using UnityEngine;
|
||
|
||
public class Actions
|
||
{
|
||
public class SetEnemyState : Action
|
||
{
|
||
public SharedInt state;
|
||
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
enemy.state = (EnemyState)state.Value;
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class SetEnemyDestination : Action
|
||
{
|
||
public SharedVector3 targetPos;
|
||
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
enemy.ai.isStopped = false;
|
||
enemy.ai.destination = targetPos.Value;
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class SetEnemyDestination2 : Action
|
||
{
|
||
public SharedGameObject target;
|
||
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
enemy.ai.isStopped = false;
|
||
enemy.ai.destination = target.Value.transform.position;
|
||
if (enemy.ai.reachedDestination)
|
||
{
|
||
return TaskStatus.Success;
|
||
}
|
||
else
|
||
{
|
||
return TaskStatus.Running;
|
||
}
|
||
}
|
||
}
|
||
|
||
public class SetEnemyTarget: Action
|
||
{
|
||
public SharedVector3 tempVec3;
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
tempVec3.Value = GetRandomPointInFront(enemy.startPos,enemy.transform.forward, 5, 10);
|
||
return TaskStatus.Success;
|
||
}
|
||
/// <summary>
|
||
/// 获取随机点(XZ方向随机,Y浮动±2)
|
||
/// </summary>
|
||
Vector3 GetRandomPointInFront(Vector3 center, Vector3 forward, float minDist, float maxDist, float halfAngle = 90f)
|
||
{
|
||
Vector3 finalPos = center;
|
||
int tryCount = 0;
|
||
bool found = false;
|
||
|
||
while (tryCount < 10 && !found)
|
||
{
|
||
// 在巨龙前方 ±halfAngle 范围内随机角度
|
||
float angle = Random.Range(-halfAngle, halfAngle);
|
||
float distance = Random.Range(minDist, maxDist);
|
||
float offsetY = Random.Range(-2f, 2f);
|
||
|
||
// 根据forward方向旋转
|
||
Quaternion rot = Quaternion.AngleAxis(angle, Vector3.up);
|
||
Vector3 dir = rot * forward;
|
||
|
||
Vector3 point = center + dir.normalized * distance;
|
||
point.y += offsetY;
|
||
|
||
// 检查是否被其他小龙占用
|
||
Collider[] hits = Physics.OverlapSphere(point, 2f);
|
||
bool blocked = false;
|
||
foreach (var hit in hits)
|
||
{
|
||
if (hit.GetComponent<GuardEnemy>())
|
||
{
|
||
blocked = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (!blocked)
|
||
{
|
||
finalPos = point;
|
||
found = true;
|
||
}
|
||
|
||
tryCount++;
|
||
}
|
||
|
||
return finalPos;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
public class SetEnemyDestinationTarget : Action
|
||
{
|
||
public SharedVector3 tempVec3;
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
enemy.isMove = true;
|
||
enemy.ai.isStopped = false;
|
||
enemy.ai.destination = tempVec3.Value;
|
||
if (Vector3.Distance(enemy.transform.position.ReflectVectorXOZ(),tempVec3.Value.ReflectVectorXOZ())<1f)
|
||
{
|
||
enemy.isMove = false;
|
||
return TaskStatus.Success;
|
||
}
|
||
else
|
||
{
|
||
return TaskStatus.Running;
|
||
}
|
||
}
|
||
}
|
||
|
||
public class EnemyMoveForward : Action
|
||
{
|
||
public SharedFloat dis;
|
||
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
Vector3 targetPos = transform.position + transform.forward * dis.Value;
|
||
enemy.ai.isStopped = false;
|
||
enemy.ai.destination = targetPos;
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class EnemyMoveToward : Action
|
||
{
|
||
public SharedFloat dis;
|
||
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
Vector3 targetPos = transform.position + new Vector3(0, 0, -1) * dis.Value;
|
||
enemy.ai.isStopped = false;
|
||
enemy.ai.destination = targetPos;
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class EnemyStop : Action
|
||
{
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
enemy.ai.isStopped = true;
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class EnemyShoot : Action
|
||
{
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
enemy.Shoot();
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
} |