private void UpdateMovementTask(MAnimalBrain brain, int index) { switch (task) { case MoveType.MoveToCurrentTarget: //MoveToTarget(brain); break; case MoveType.Flee: { if (UpdateFleeMovingTarget) { Flee(brain, index); } else { ArriveToFleePoint(brain, index); } break; } case MoveType.KeepDistance: KeepDistance(brain, index); break; case MoveType.CircleAround: CircleAround(brain, index); break; default: break; } }
public override void DrawGizmos(MAnimalBrain brain) { foreach (var d in decisions) { d.DrawGizmos(brain); } }
public override void ExitTask(MAnimalBrain brain, int index) { if (Play == ExecuteTask.OnExit) //If the animal is in range of the Target { StateActivate(brain); } }
public bool LookForUnityTags(MAnimalBrain brain) { if (string.IsNullOrEmpty(UnityTag)) { return(false); } var AllTags = GameObject.FindGameObjectsWithTag(UnityTag); float minDistance = float.MaxValue; GameObject closestTag = null; foreach (var way in AllTags) { var Distance = Vector3.Distance(way.transform.position, brain.Eyes.position); if (Distance < minDistance && Distance < LookRange) { minDistance = Distance; closestTag = way; } } if (closestTag) { return(IsInFieldOfView(brain, closestTag.transform.position, closestTag.transform)); //Find if is inside the Field of view } return(false); }
public override void PrepareDecision(MAnimalBrain brain, int Index) { foreach (var d in decisions) { d.PrepareDecision(brain, Index); } }
public override void UpdateTask(MAnimalBrain brain, int index) { if (MTools.ElapsedTime(brain.TasksTime[index], brain.TasksVars[index].floatValue)) { brain.TaskDone(index); } }
public bool LookForZones(MAnimalBrain brain) { var AllZones = Zone.Zones; float minDistance = float.MaxValue; Zone FoundZone = null; foreach (var zone in AllZones) { if (zone.zoneType == zoneType && ZoneID < 1 || zone.GetID == ZoneID) { if (zone.zoneType == ZoneType.Mode && ZoneModeIndex != -1 && zone.ModeIndex != ZoneModeIndex) { continue; //IMPORTANT! Search for an Specific Mode Zone like Sleep) } var Distance = Vector3.Distance(zone.transform.position, brain.Eyes.position); //Get the Distance between the Eyes and the Zone if (Distance < minDistance && Distance < LookRange) { minDistance = Distance; FoundZone = zone; } } } if (FoundZone) { return(IsInFieldOfView(brain, FoundZone.transform.position, FoundZone.transform)); //Find if is inside the Field of view } return(false); }
/// <summary>When a new State starts this method is called for each Tasks</summary> public void Start_Taks(MAnimalBrain brain) { for (int i = 0; i < tasks.Length; i++) { tasks[i].StartTask(brain, i); } }
private void Update_Tasks(MAnimalBrain brain) { for (int i = 0; i < tasks.Length; i++) { tasks[i].UpdateTask(brain, i); } }
public void OnPositionArrived(MAnimalBrain brain, Vector3 Position) { for (int i = 0; i < tasks.Length; i++) { tasks[i].OnPositionArrived(brain, Position, i); } }
private void Update_Transitions(MAnimalBrain brain) { for (int i = 0; i < transitions.Length; i++) { if (this != brain.currentState) { return; //BUG BUG BUG FIXed } var transition = transitions[i]; var decision = transition.decision; if (decision.interval > 0) { if (brain.CheckIfDecisionsCountDownElapsed(decision.interval, i)) { brain.ResetDecisionTimeElapsed(i); Decide(brain, i, transition); } } else { Decide(brain, i, transition); } } }
//public void RemoveListeners_Decisions(MAnimalBrain brain) //{ // for (int i = 0; i < transitions.Length; i++) // transitions[i].decision.RemoveListeners(brain,i); //} #region Target Event Listeners for Tasks public void OnTargetArrived(MAnimalBrain brain, Transform target) { for (int i = 0; i < tasks.Length; i++) { tasks[i].OnTargetArrived(brain, target, i); } }
public override void FinishDecision(MAnimalBrain brain, int Index) { //Reset all variables Bool = null; Int = null; Float = null; }
public override void StartTask(MAnimalBrain brain, int index) { Transform child; if (SetLookAtOn == Affected.Self) { if (LookAtTargetS == LookAtOption1.CurrentTarget) { child = UseTag ? GetGameObjectByTag(brain.Target) : GetChildName(brain.Target); } else { child = UseTag ? GetGameObjectByTag(TargetVar.Value) : GetChildName(TargetVar.Value); } brain.GetComponentInParent <IAim>()?.SetTarget(child); } else { if (LookAtTargetT == LookAtOption2.AIAnimal) { child = UseTag ? GetGameObjectByTag(brain.Animal.transform) : GetChildName(brain.Animal.transform); } else { child = UseTag ? GetGameObjectByTag(TargetVar.Value) : GetChildName(TargetVar.Value); } brain.Target?.GetComponentInParent <IAim>()?.SetTarget(child); } brain.TaskDone(index); }
public override void UpdateTask(MAnimalBrain brain, int index) { if (when == ExecuteTask.OnUpdate) { Execute_Task(brain); } }
public void Finish_Tasks(MAnimalBrain brain) { for (int i = 0; i < tasks.Length; i++) { tasks[i].ExitTask(brain, i); } }
public override void PrepareDecision(MAnimalBrain brain, int Index) { brain.DecisionsVars[Index].MonoBehaviours = null; MonoBehaviour[] monoValue = null; var objectives = GetObjective(brain); if (objectives != null && objectives.Length > 0) { foreach (var targets in objectives) { // Debug.Log("targets = " + targets); switch (varType) { case VarType.Bool: monoValue = GetComponents <BoolVarListener>(targets.gameObject); break; case VarType.Int: monoValue = GetComponents <IntVarListener>(targets.gameObject); break; case VarType.Float: monoValue = GetComponents <FloatVarListener>(targets.gameObject); break; } } } brain.DecisionsVars[Index].MonoBehaviours = monoValue; }
public void Finish_Decisions(MAnimalBrain brain) { for (int i = 0; i < transitions.Length; i++) { transitions[i].decision.FinishDecision(brain, i); } }
public override void StartTask(MAnimalBrain brain, int index) { Transform child = null; if (SetLookAtOn == Affected.Self) { if (UseTag) { child = GetGameObjectByTag(brain.Target); } else { child = GetChildName(brain.Target); } brain.GetComponentInParent <IAim>()?.SetTarget(child); } else { if (UseTag) { child = GetGameObjectByTag(brain.Animal.transform); } else { child = GetChildName(brain.Animal.transform); } brain.Target?.GetComponentInParent <IAim>()?.SetTarget(child); } }
private void CircleAround(MAnimalBrain brain, int index) { if (brain.AIMovement.HasArrived) //Means that we have arrived to the point so set the next point { brain.TasksVars[index].intValue++; brain.TasksVars[index].intValue = brain.TasksVars[index].intValue % arcsCount; brain.TasksVars[index].boolValue = true; //Set this so we can seek for the next point } if (brain.TasksVars[index].boolValue || brain.AIMovement.TargetIsMoving) { int pointIndex = brain.TasksVars[index].intValue; float arcDegree = 360.0f / arcsCount; int Dir = direction == CircleDirection.Right ? 1 : -1; Quaternion rotation = Quaternion.Euler(0, Dir * arcDegree * pointIndex, 0); Vector3 currentDirection = Vector3.forward; currentDirection = rotation * currentDirection; Vector3 CurrentPoint = brain.Target.position + (currentDirection.normalized * distance); Debug.DrawRay(CurrentPoint, Vector3.up, Color.green, interval); brain.AIMovement.UpdateTargetPosition = false; //Means the Animal Wont Update the Destination Position with the Target position. brain.AIMovement.SetDestination(CurrentPoint); brain.TasksVars[index].boolValue = false; //Set this so we can seak for the next point } }
public bool LookForTags(MAnimalBrain brain) { var AllTagsHolders = Tags.TagsHolders; float minDistance = float.MaxValue; Tags FoundTagHolder = null; foreach (var tagsH in AllTagsHolders) { var Distance = Vector3.Distance(tagsH.transform.position, brain.Eyes.position); if (Distance < minDistance && Distance < LookRange) { if (tagsH.HasTag(tags)) { minDistance = Distance; FoundTagHolder = tagsH; } } } if (FoundTagHolder) { return(IsInFieldOfView(brain, FoundTagHolder.transform.position, FoundTagHolder.transform)); //Find if is inside the Field of view } return(false); }
/// <summary>Locks the Movement forward but it still tries to go forward. Only Rotates</summary> private void RotateInPlace(MAnimalBrain brain) { // brain.Animal.LockForwardMovement = true; brain.AIMovement.StoppingDistance = 100f; //Set the Stopping Distance to almost nothing that way the animal keeps trying to go towards the target brain.AIMovement.LookAtTargetOnArrival = true; //Set the Animal to look Forward to the Target brain.AIMovement.Stop(); }
private bool Look_For(MAnimalBrain brain) { switch (lookFor) { case LookFor.AnimalPlayer: return(LookForAnimalPlayer(brain)); case LookFor.Tags: return(LookForTags(brain)); case LookFor.UnityTags: return(LookForUnityTags(brain)); case LookFor.Zones: return(LookForZones(brain)); case LookFor.GameObject: return(LookForGameObject(brain)); case LookFor.ClosestWayPoint: return(LookForClosestWaypoint(brain)); case LookFor.Target: return(LookForTarget(brain)); default: return(false); } }
private void ArriveToFleePoint(MAnimalBrain brain, int index) { if (brain.AIMovement.HasArrived) { brain.TaskDone(index); //IF we arrived to the Point we set on the Start Task then set this task as done } }
public override void FinishDecision(MAnimalBrain brain, int Index) { foreach (var d in decisions) { d.FinishDecision(brain, Index); } }
public override void ExitTask(MAnimalBrain brain, int index) { brain.AIMovement.MoveAgent = true; brain.AIMovement.UpdateTargetPosition = true; switch (task) { case MoveType.MoveToTarget: brain.AIMovement.UpdateTargetPosition = false; // Make Sure to update the Target position on the Animal brain.AIMovement.MoveAgentOnMovingTarget = false; // If the Target is mooving then Move/Resume the Agent... break; case MoveType.StopAgent: brain.AIMovement.MoveAgent = true; break; case MoveType.Flee: brain.AIMovement.UpdateTargetPosition = true; break; case MoveType.RotateInPlace: brain.Animal.LockForwardMovement = false; break; case MoveType.StopAnimal: brain.Animal.LockMovement = false; break; default: break; } }
private void PlayMode(MAnimalBrain brain) { switch (affect) { case Affected.Self: if (brain.Animal.Mode_TryActivate(modeID, AbilityID)) { if (lookAtAlign && brain.Target) { brain.StartCoroutine(MTools.AlignLookAtTransform(brain.transform, brain.Target, alignTime)); } } break; case Affected.Target: if (brain.TargetAnimal && brain.TargetAnimal.Mode_TryActivate(modeID, AbilityID)) { if (lookAtAlign && brain.Target) { brain.StartCoroutine(MTools.AlignLookAtTransform(brain.TargetAnimal.transform, brain.transform, alignTime)); } } break; default: break; } }
public override void DrawGizmos(MAnimalBrain brain) { if (task == MoveType.Flee) { UnityEditor.Handles.color = debugColor; UnityEditor.Handles.DrawSolidDisc(brain.AIMovement.Agent.transform.position, Vector3.up, distance); } else if (task == MoveType.KeepDistance) { UnityEditor.Handles.color = debugColor; UnityEditor.Handles.color = new Color(debugColor.r, debugColor.g, debugColor.b, 1f); UnityEditor.Handles.DrawWireDisc(brain.transform.position, Vector3.up, distance - distanceThreshold); UnityEditor.Handles.DrawWireDisc(brain.transform.position, Vector3.up, distance + distanceThreshold); //UnityEditor.Handles.DrawWireDisc(brain.AIMovement.Agent.transform.position, Vector3.up, distance); } else if (task == MoveType.CircleAround && brain.Target) { UnityEditor.Handles.color = new Color(debugColor.r, debugColor.g, debugColor.b, 1f); UnityEditor.Handles.DrawWireDisc(brain.AIMovement.Agent.transform.position, Vector3.up, distance); float arcDegree = 360.0f / arcsCount; Quaternion rotation = Quaternion.Euler(0, -arcDegree, 0); Vector3 currentDirection = Vector3.forward; for (int i = 0; i < arcsCount; ++i) { var CurrentPoint = brain.Target.position + (currentDirection.normalized * distance); Gizmos.DrawWireSphere(CurrentPoint, 0.1f); currentDirection = rotation * currentDirection; } } }
/// <summary>When a new State starts this method is called for each Decisions</summary> public void Prepare_Decisions(MAnimalBrain brain) { for (int i = 0; i < transitions.Length; i++) { transitions[i].decision?.PrepareDecision(brain, i); } }
public override void FinishDecision(MAnimalBrain brain, int Index) { //Reset all variables boolListener = null; intListener = null; floatListener = null; }