using BehaviorDesigner.Runtime; using BehaviorDesigner.Runtime.Tasks; using DragonLi.Core; using DragonLi.Frame; using UnityEngine; using UnityEngine.AI; namespace MechanicalAge { public class Flash2Pos : Action { public SharedGameObject targetGameObject; //[SharedRequired] // public SharedVector3 destination; public SharedString pathId; // cache the navmeshagent component private NavMeshAgent navMeshAgent; private GameObject prevGameObject; protected Vector3[] router; protected int currentIndex = 0; protected bool isStay = false; public override void OnStart() { var currentGameObject = GetDefaultGameObject(targetGameObject.Value); if (currentGameObject != prevGameObject) { navMeshAgent = currentGameObject.GetComponent(); prevGameObject = currentGameObject; } if (GetPath()) { isStay = false; if (navMeshAgent.isActiveAndEnabled && currentIndex >= 0 && currentIndex < router.Length) { navMeshAgent?.SetDestination(router[currentIndex]); } } } public override TaskStatus OnUpdate() { if (router == null || router.Length == 0) { return TaskStatus.Failure; } if (currentIndex >= router.Length || currentIndex == -1) { return TaskStatus.Failure; } if (navMeshAgent == null) { Debug.LogWarning("NavMeshAgent is null"); return TaskStatus.Failure; } if (navMeshAgent.isActiveAndEnabled && !isStay && !navMeshAgent.pathPending && navMeshAgent.remainingDistance < 0.5f) { currentIndex++; isStay = true; if (currentIndex >= router.Length) { return TaskStatus.Success; } else { MonoSingleton.Instance.WaitSecondTodo(() => { if (navMeshAgent.isActiveAndEnabled) navMeshAgent.SetDestination(router[currentIndex]); isStay = false; }, 0.2f, null); } } return TaskStatus.Running; } public override void OnReset() { targetGameObject = null; // destination = Vector3.zero; } private bool GetPath() { if (string.IsNullOrEmpty(pathId.ToString())) { return false; } DragonLiAgentPath pathById = DragonLiAgentPathHelper.GetPathById(pathId.ToString()); if (pathById != null) { router = pathById.GetPath(); return true; } this.LogEditorOnly("Failed to get path."); return false; } } public class EskyFlash2Pos : Action { public SharedGameObject targetGameObject; //[SharedRequired] // public SharedVector3 destination; public SharedString pathId; // cache the navmeshagent component private NavMeshAgent navMeshAgent; private GameObject prevGameObject; protected Vector3[] router; protected int currentIndex = 0; protected bool isStay = false; public override void OnStart() { var currentGameObject = GetDefaultGameObject(targetGameObject.Value); if (currentGameObject != prevGameObject) { navMeshAgent = currentGameObject.GetComponent(); prevGameObject = currentGameObject; } if (GetPath()) { isStay = false; if (navMeshAgent.isActiveAndEnabled && currentIndex >= 0 && currentIndex < router.Length) { navMeshAgent?.SetDestination(router[currentIndex]); } } } public override TaskStatus OnUpdate() { if (router == null || router.Length == 0) { return TaskStatus.Failure; } if (currentIndex >= router.Length || currentIndex == -1) { return TaskStatus.Failure; } if (navMeshAgent == null) { Debug.LogWarning("NavMeshAgent is null"); return TaskStatus.Failure; } if (navMeshAgent.isActiveAndEnabled && !isStay && !navMeshAgent.pathPending && navMeshAgent.remainingDistance < 0.5f) { currentIndex++; isStay = true; if (currentIndex >= router.Length) { return TaskStatus.Success; } else { MonoSingleton.Instance.WaitSecondTodo(() => { if (navMeshAgent.isActiveAndEnabled) navMeshAgent.SetDestination(router[currentIndex]); isStay = false; }, 0.1f, null); } } return TaskStatus.Running; } public override void OnReset() { targetGameObject = null; // destination = Vector3.zero; } private bool GetPath() { if (string.IsNullOrEmpty(pathId.ToString())) { return false; } DragonLiAgentPath pathById = DragonLiAgentPathHelper.GetPathById(pathId.ToString()); if (pathById != null) { router = pathById.GetPath(); return true; } this.LogEditorOnly("Failed to get path."); return false; } } public class Flash2PosLoop : Action { public SharedGameObject targetGameObject; //[SharedRequired] // public SharedVector3 destination; public SharedString pathId; // cache the navmeshagent component private NavMeshAgent navMeshAgent; private GameObject prevGameObject; protected Vector3[] router; protected int currentIndex = 0; protected bool isStay = false; protected bool isReverse = false; public override void OnStart() { var currentGameObject = GetDefaultGameObject(targetGameObject.Value); if (currentGameObject != prevGameObject) { navMeshAgent = currentGameObject.GetComponent(); prevGameObject = currentGameObject; } if (GetPath()) { isStay = false; if (navMeshAgent.isActiveAndEnabled) navMeshAgent?.SetDestination(router[currentIndex]); } } public override TaskStatus OnUpdate() { if (router == null || router.Length == 0) { return TaskStatus.Failure; } if (currentIndex >= router.Length - 1 || currentIndex == 0) { isReverse = !isReverse; //return TaskStatus.Failure; } if (navMeshAgent == null) { Debug.LogWarning("NavMeshAgent is null"); return TaskStatus.Failure; } if (navMeshAgent.isActiveAndEnabled && !isStay && !navMeshAgent.pathPending && navMeshAgent.remainingDistance < 0.1f) { //currentIndex++; currentIndex = (isReverse ? (--currentIndex) : (++currentIndex)); //isStay = true; if (currentIndex >= router.Length || currentIndex == -1) { currentIndex = Mathf.Clamp(currentIndex, 0, router.Length - 1); //return TaskStatus.Failure; } isStay = true; MonoSingleton.Instance.WaitSecondTodo(() => { if (navMeshAgent.isActiveAndEnabled) navMeshAgent.SetDestination(router[currentIndex]); isStay = false; }, 0.1f, null); } return TaskStatus.Running; } public override void OnReset() { targetGameObject = null; // destination = Vector3.zero; } private bool GetPath() { if (string.IsNullOrEmpty(pathId.ToString())) { return false; } DragonLiAgentPath pathById = DragonLiAgentPathHelper.GetPathById(pathId.ToString()); if (pathById != null) { router = pathById.GetPath(); return true; } this.LogEditorOnly("Failed to get path."); return false; } } public class Move2Pos : Action { public SharedGameObject moveGameObject; public SharedTransform target; private NavMeshAgent navMeshAgent; protected bool isStay = false; public override void OnStart() { var currentGameObject = GetDefaultGameObject(moveGameObject.Value); navMeshAgent = currentGameObject.GetComponent(); isStay = false; if (navMeshAgent.isActiveAndEnabled) { navMeshAgent.SetDestination(target.Value.position); } } public override TaskStatus OnUpdate() { if (navMeshAgent == null) { Debug.LogWarning("NavMeshAgent is null"); return TaskStatus.Failure; } // Debug.Log(navMeshAgent.remainingDistance); if (!navMeshAgent.pathPending && navMeshAgent.isActiveAndEnabled && !isStay && navMeshAgent.remainingDistance < 0.5f) { isStay = true; return TaskStatus.Success; } return TaskStatus.Running; } } public class MoveTargetPoints : Action { public SharedGameObject moveGameObject; public SharedTransform target; private NavMeshAgent navMeshAgent; protected bool isStay = false; public override void OnStart() { var currentGameObject = GetDefaultGameObject(moveGameObject.Value); navMeshAgent = currentGameObject.GetComponent(); isStay = false; if (navMeshAgent.isActiveAndEnabled) { navMeshAgent.SetDestination(target.Value.position); } } public override TaskStatus OnUpdate() { if (navMeshAgent == null) { Debug.LogWarning("NavMeshAgent is null"); return TaskStatus.Failure; } // Debug.Log(navMeshAgent.remainingDistance); if (navMeshAgent.isActiveAndEnabled && !isStay && navMeshAgent.remainingDistance < 0.5f) { isStay = true; return TaskStatus.Success; } return TaskStatus.Running; } } //behaviorTree.SetVariable("ReadEnd", (SharedBool)true); public class CheckSharedBool : Conditional { public SharedGameObject self; private BehaviorTree behavior; public string value; public override void OnStart() { base.OnStart(); behavior = self.Value.GetComponent(); //behaviorTree.SetVariable("ReadEnd", (SharedBool)true); } public override TaskStatus OnUpdate() { Debug.Log((bool)(behavior.GetVariable(value).GetValue())); if ((bool)(behavior.GetVariable(value).GetValue())) { return TaskStatus.Success; } else { return TaskStatus.Failure; } } } //检测Boss处于什么阶段 public class CheckEnemyPhase1 : Conditional { public SharedGameObject self; //Boss在第几阶段 public SharedInt Phase; private int index; public override TaskStatus OnUpdate() { // if (GameManager.Ins.players.Count == 0) // { // return TaskStatus.Failure; // } float HpUpperLimit = self.Value.GetComponent().xHealth; float CurrentHp = self.Value.GetComponent().health; if (CurrentHp / HpUpperLimit > 0.7) { index = 1; //return TaskStatus.Success; // Debug.Log("怪物处于第一阶段"); } else if (CurrentHp / HpUpperLimit <= 0.7 && CurrentHp / HpUpperLimit >= 0.4) { // Debug.Log("怪物处于第二阶段"); index = 2; //return TaskStatus.Success; } else if (CurrentHp / HpUpperLimit < 0.4) { // Debug.Log("怪物处于第三阶段"); index = 3; //return TaskStatus.Success; } if (Phase.Value == index) { // Debug.Log("TRUE"); return TaskStatus.Success; } else { // Debug.Log("FALSE"); return TaskStatus.Failure; } } } //检测召唤怪物的剩余的数量 public class CheckSummonAmount : Conditional { public override TaskStatus OnUpdate() { if (MRNetworkManager.Ins.roomSlots.Count == 0) { return TaskStatus.Failure; } if (GameManager.Ins.SummonAmount <= 0) { return TaskStatus.Success; } else { return TaskStatus.Failure; } } } //检测Boss处于什么阶段 public class CheckEnemyPhase2 : Conditional { public SharedGameObject self; //Boss在第几阶段 public SharedInt Phase; private int index; public override TaskStatus OnUpdate() { if (GameManager.Ins.players.Count == 0) { return TaskStatus.Failure; } EnemyInfo enemyInfo; GameManager.Ins.EnemyDescInfos.TryGetValue(7, out enemyInfo); float HpUpperLimit = enemyInfo.Hp; float CurrentHp = self.Value.GetComponent().health; if (CurrentHp / HpUpperLimit <= 0.7 && CurrentHp / HpUpperLimit >= 0.4) { Debug.Log("怪物处于第一阶段" + HpUpperLimit + "|" + CurrentHp); index = 2; } if (Phase.Value == index) { Debug.Log("TRUE"); return TaskStatus.Success; } else { Debug.Log("FALSE"); return TaskStatus.Failure; } } } public class IsPlayerNearby : Conditional { public SharedGameObject self; public SharedFloat range; public SharedBool judge; private NavMeshAgent navMeshAgent; public override TaskStatus OnUpdate() { if (GameManager.Ins.players.Count == 0) { return TaskStatus.Failure; } Transform player1 = GameManager.Ins.players[0]; //计算自身到目标点之间XZ的平面距离 float dis = MathExtension.GetDistanceXOZ(self.Value.transform.position, player1.position); if (judge.Value) { //return dis < range.Value ? TaskStatus.Success : TaskStatus.Failure; if (dis < range.Value) { //navMeshAgent = self.Value.GetComponent(); //navMeshAgent.isStopped = true; return TaskStatus.Success; } else { return TaskStatus.Failure; } } else { return dis >= range.Value ? TaskStatus.Success : TaskStatus.Failure; } } } /// /// 是否有玩家在护盾内 /// public class IsPlayerInShield : Conditional { public override TaskStatus OnUpdate() { return GameManager.Ins.IsPlayerInShield() ? TaskStatus.Success : TaskStatus.Failure; } } public class IsPlayerNearby2 : Conditional { public SharedGameObject self; public SharedFloat range; public SharedBool judge; private NavMeshAgent navMeshAgent; public override TaskStatus OnUpdate() { if (GameManager.Ins.players.Count == 0) { return TaskStatus.Failure; } Transform player1 = GameManager.Ins.players[0]; //计算自身到目标点之间XZ的平面距离 //float dis = MathExtension.GetDistanceXOZ(self.Value.transform.position, player1.position); float dis = Vector3.Distance(self.Value.transform.position, player1.transform.position); Debug.Log("DIS" + dis); return dis < range.Value ? TaskStatus.Success : TaskStatus.Failure; //if (dis < range.Value) //{ // return TaskStatus.Success; //} //else //{ // return TaskStatus.Failure; //} } } public class IsCanAttackShield : Conditional { public SharedBool judge; public SharedGameObject self; public SharedFloat attackArea; public override TaskStatus OnUpdate() { return GameManager.Ins.IsCanAttackShield(self.Value.transform, 0, attackArea.Value) == judge.Value ? TaskStatus.Success : TaskStatus.Failure; } } public class IsCanAttackPlayer : Conditional { public SharedBool judge; public SharedGameObject self; public SharedFloat attackArea; public override TaskStatus OnUpdate() { return GameManager.Ins.IsCanAttackPlayer(self.Value.transform, 0, attackArea.Value) == judge.Value ? TaskStatus.Success : TaskStatus.Failure; } } public class CanSeePlayer : Conditional { public SharedFloat angle; public SharedFloat distance; public SharedFloat agentHeight; public override TaskStatus OnUpdate() { Transform agentTransform = transform; // ��ȡ��ǰ�����Transform Transform playerTransform = GameManager.Ins.GetPlayer1(); // ��ȡ��ҵ�Transform if (playerTransform == null) { Debug.Log("��Ҳ����ڣ�����ʧ��"); return TaskStatus.Failure; // ��Ҳ����ڣ�����ʧ�� } // ����Ƕ� float angleToPlayer = Vector3.Angle(agentTransform.forward, playerTransform.position - agentTransform.position); // ���Ƕ� if (angleToPlayer > angle.Value) { Debug.Log("�Ƕ����������㣬ʧ��"); return TaskStatus.Failure; // �Ƕ����������㣬ʧ�� } // ������ float distanceToPlayer = Vector3.Distance(agentTransform.position, playerTransform.position); if (distanceToPlayer > distance.Value) { Debug.Log("�������������㣬ʧ��"); return TaskStatus.Failure; // �������������㣬ʧ�� } // ���߼�� Ray ray = new Ray(agentTransform.position + Vector3.up * agentHeight.Value, agentTransform.forward); RaycastHit hit; if (Physics.Raycast(ray, out hit, distance.Value, LayerMask.GetMask("Default"))) { return TaskStatus.Failure; // �������У�����ʧ�� } return TaskStatus.Success; // �������������㣬�ɹ� } } public class MoveToPlayerNearby : Action { public SharedGameObject self; public SharedFloat range; private NavMeshAgent navMeshAgent; private Vector3 targetPosition; public override void OnStart() { base.OnStart(); navMeshAgent = self.Value.GetComponent(); targetPosition = GetTargetPosition(MRNetworkManager.Ins.roomSlots[0].transform); targetPosition = Utils.GetNearRandomPositionOnNavmesh(targetPosition, 0.1f); if (navMeshAgent.isActiveAndEnabled) navMeshAgent.SetDestination(targetPosition); } public override TaskStatus OnUpdate() { return TaskStatus.Success; } private Vector3 GetTargetPosition(Transform player) { Vector3 dir = -self.Value.transform.position - player.position; Vector3 val = Vector3.Cross(dir.ReflectVectorXOZ(), Vector3.up); val *= (float)((!(Random.Range(-1f, 1f) < 0f)) ? 1 : (-1)); return player.position + (dir.normalized * range.Value + (val.normalized * Random.Range(-range.Value, range.Value))); } } public class MoveToPlayerNearby2 : Action { public SharedGameObject self; public SharedFloat range; private NavMeshAgent navMeshAgent; private Vector3 targetPosition; protected bool isStay = true; public override void OnStart() { base.OnStart(); navMeshAgent = self.Value.GetComponent(); // Utils.GetNearRandomPositionOnNavmesh(); targetPosition = self.Value.transform.position; if (GameManager.Ins.players.Count > 0) { targetPosition = GetTargetPosition(); } if (!navMeshAgent.isActiveAndEnabled) return; navMeshAgent.SetDestination(targetPosition); isStay = false; } public override TaskStatus OnUpdate() { if (navMeshAgent.isActiveAndEnabled && !isStay && navMeshAgent.remainingDistance < 0.5f) { isStay = true; return TaskStatus.Success; } return TaskStatus.Running; } private Vector3 GetTargetPosition() { Transform player = GameManager.Ins.players[0]; Vector3 dir = self.Value.transform.position - player.position; Vector3 val = Vector3.Cross(dir.ReflectVectorXOZ(), Vector3.up); val *= (float)((!(Random.Range(-1f, 1f) < 0f)) ? 1 : (-1)); return player.position + (dir.normalized * range.Value); //+ (dir.normalized * range.Value + (val.normalized * Random.Range(-range.Value, range.Value))); } } public class Attack : Action { public SharedGameObject targetGameObject; // cache the navmeshagent component private NavMeshAgent navMeshAgent; private GameObject prevGameObject; private Animator animator; public override void OnStart() { var currentGameObject = GetDefaultGameObject(targetGameObject.Value); if (currentGameObject != prevGameObject) { navMeshAgent = currentGameObject.GetComponent(); prevGameObject = currentGameObject; } animator = currentGameObject.GetComponent(); } public override TaskStatus OnUpdate() { if (navMeshAgent == null) { Debug.LogWarning("NavMeshAgent is null"); return TaskStatus.Failure; } animator.SetBool("", true); return TaskStatus.Success; } public override void OnReset() { targetGameObject = null; // destination = Vector3.zero; } } public class InHpRange : Conditional { public SharedInt minHpPrecent; public SharedInt maxHpPrecent; public SharedBool judge; public SharedGameObject self; public override TaskStatus OnUpdate() { TaskStatus status = TaskStatus.Failure; Agent agent = self.Value.GetComponent(); if (agent) { if (agent.Health >= minHpPrecent.Value / 100f * agent.OriginHealth && agent.Health <= maxHpPrecent.Value / 100f * agent.OriginHealth) { status = TaskStatus.Success; } } if (!judge.Value) { status = status == TaskStatus.Success ? TaskStatus.Failure : TaskStatus.Success; } return status; } } }