190 lines
5.6 KiB
C#
190 lines
5.6 KiB
C#
|
||
using BehaviorDesigner.Runtime;
|
||
using BehaviorDesigner.Runtime.Tasks;
|
||
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.ChangeState((EnemyState)state.Value);
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class SetPlayerAiState : Action
|
||
{
|
||
public SharedInt state;
|
||
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
PlayerAI playerAi = transform.GetComponent<PlayerAI>();
|
||
if (playerAi == null) return TaskStatus.Failure;
|
||
playerAi.state = (PlayerAIState)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;
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class PlayerAIMoveForward : Action
|
||
{
|
||
public SharedGameObject TargetObj;
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
PlayerAI playerAi = transform.GetComponent<PlayerAI>();
|
||
if (playerAi == null) return TaskStatus.Failure;
|
||
playerAi.ai.isStopped = false;
|
||
playerAi.ai.destination = TargetObj.Value.transform.position;
|
||
if (playerAi.ai.destination == TargetObj.Value.transform.position)
|
||
{
|
||
return TaskStatus.Success;
|
||
}
|
||
return TaskStatus.Running;
|
||
}
|
||
}
|
||
|
||
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;
|
||
//Debug.Log("敌人停止");
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class EnemyAttack : Action
|
||
{
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
enemy.DoAttack();
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class EnemyStopAttack : Action
|
||
{
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Enemy enemy = transform.GetComponent<Enemy>();
|
||
if (enemy == null) return TaskStatus.Failure;
|
||
enemy.StopAttack();
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class PlayerAIAttack : Action
|
||
{
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
PlayerAI playerAi = transform.GetComponent<PlayerAI>();
|
||
if (playerAi == null) return TaskStatus.Failure;
|
||
playerAi.StopAttack();
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class PlayerAIStopAttack: Action
|
||
{
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
PlayerAI playerAi = transform.GetComponent<PlayerAI>();
|
||
if (playerAi == null) return TaskStatus.Failure;
|
||
playerAi.StopAttack();
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
public class EnemyGetTarget : Action
|
||
{
|
||
public SharedVector3 targetPos;
|
||
|
||
// 未命中时的偏移距离(可调大,越大越容易飞出屏幕)
|
||
public float missOffset = 3f;
|
||
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
Vector3 target = transform.forward;
|
||
var tran = GameManager.Ins.GetPlayerPos();
|
||
|
||
if (tran == null)
|
||
{
|
||
targetPos.Value = target;
|
||
return TaskStatus.Success;
|
||
}
|
||
|
||
// 15%~25% 命中率
|
||
float hitRate = Random.Range(0.15f, 0.25f);
|
||
bool isHit = Random.value < hitRate;
|
||
|
||
if (isHit)
|
||
{
|
||
// 命中 -> 瞄准玩家/队友
|
||
targetPos.Value = tran.position;
|
||
}
|
||
else
|
||
{
|
||
// 未命中 -> 在目标点周围随机一个方向偏移
|
||
// 生成随机二维方向(平面上随机360°)
|
||
Vector2 randomCircle = Random.insideUnitCircle.normalized;
|
||
Vector3 offset = new Vector3(randomCircle.x, Random.Range(-0.5f, 0.5f), randomCircle.y) * missOffset;
|
||
|
||
targetPos.Value = tran.position + offset;
|
||
}
|
||
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
} |