public override void InitBehaviours(AiBehaviourController controller) { var stateMachine = controller.stateMachine; var inputHolder = controller.GetComponentInParent <InputHolder>(); var transform = controller.transform; FocusPriority priorityFocus = controller.focusPriority; AttentionMode enemyAttention = AiBehaviourPackUtility.GetAttentionEnemy(controller); var tExecute = new Timer(); var lookAroundMethod = new LookAround(new RangedFloat(0.5f, 0.8f), new RangedFloat(25, 50)); // TODO if seeker is not defined create MoveToDestination instead var moveToDestination = new MoveToDestinationNavigation(controller.GetComponent <Seeker>()); var velocityAccumulator = new VelocityAccumulator(0.89f, 0.125f); var destination = controller.InitBlackboardValue <Vector2>("destination"); //////////////// /// idle var lookAtTarget = enemyAttention.AddNewState(true) .AddOnBegin(inputHolder.ResetInput) .AddOnBegin(() => tExecute.RestartRandom(1f, 2f)) .AddOnUpdate(() => { velocityAccumulator.Update(transform, priorityFocus.currentFocus.GetTarget(), 0.01225f); inputHolder.rotationInput = velocityAccumulator.position - (Vector2)transform.position; inputHolder.directionInput = velocityAccumulator.position - (Vector2)transform.position; }) .AddShallReturn(() => tExecute.IsReady()) .SetUtility(() => 10) ; var lookAround = enemyAttention.AddNewState(true) .AddOnBegin(() => lookAroundMethod.Begin(transform)) .AddOnBegin(inputHolder.ResetInput) .AddOnBegin(() => tExecute.RestartRandom(0.6f, 1.5f)) .AddOnUpdate(() => { velocityAccumulator.Update(transform, priorityFocus.currentFocus.GetTarget(), 0.01225f); Vector2 targetPosition = velocityAccumulator.position - (Vector2)transform.position; float angle = Vector2.SignedAngle(Vector2.up, targetPosition); inputHolder.rotationInput = lookAroundMethod.GetRotationInput(transform, angle, 0.1f); inputHolder.directionInput = velocityAccumulator.position - (Vector2)transform.position; }) .AddShallReturn(() => tExecute.IsReady() || priorityFocus.currentFocus.GetTarget().velocity.sqrMagnitude > 20) .SetUtility(() => 15f - 0.225f * priorityFocus.currentFocus.GetTarget().velocity.sqrMagnitude) ; /// ideas: /// looks a little bit to sides sometimes /// tries to keep certain distance from enemy /// Input Accumulation (aka velocity ?) /*/// lookAround * var lookAround = enemyAttention.AddNewState(true) * .AddOnBegin(() => lookAroundMethod.Begin(transform)) * .AddOnBegin(inputHolder.ResetInput) * .AddOnBegin(() => tExecute.RestartRandom(0.25f, 0.4f)) * .AddOnUpdate(() => * { * inputHolder.rotationInput = lookAroundMethod.GetRotationInput(transform); * }) * .AddShallReturn(() => tExecute.IsReady()) * .SetUtility(6) * ; * * /// randomMovement * var randomMovement = enemyAttention.AddNewState(true) * .AddOnBegin(() => moveToDestination.SetDestination_Search(priorityFocus, 20)) * .AddOnBegin(inputHolder.ResetInput) * .AddOnBegin(() => tExecute.RestartRandom(0.75f, 1.5f)) * .AddOnUpdate(() => * { * inputHolder.positionInput = moveToDestination.ToDestination(); * //inputHolder.positionInput = Vector2.Lerp(inputHolder.positionInput, moveToDestination.ToDestination(), 0.985f ); * }) * .AddShallReturn(() => tExecute.IsReady()) * .SetUtility(2f) * ;*/ }
/*public const int awarenessEnemy = 0; * public const int awarenessUnknown = 1; * * public const int awarenessWantsToTalk = 2;*/ #endregion // TODO setup attention utility params public static FocusPriority GetFocusPriority(AiBehaviourController controller) { // check if focus was already created and if soo return it FocusPriority focusPriority = (FocusPriority)controller.GetBlackboardValue <FocusOwned>("priorityFocus")?.value; if (focusPriority != null) { return(focusPriority); } // otherwise create a new one FocusOwned CreatePriorityFocusDelegate() => new FocusPriority(controller.transform); focusPriority = (FocusPriority)controller.InitBlackboardValue("priorityFocus", CreatePriorityFocusDelegate).value; focusPriority.SetSustainUtility(0.1f); // prepare partial focuses FocusOwned enemyFocus = GetFocusEnemy(controller); FocusOwned painFocus = GetFocusPain(controller); FocusOwned touchFocus = GetFocusTouch(controller); FocusOwned noiseFocus = GetFocusNoise(controller); if (enemyFocus != null) { /// enemy AttentionMode CreateAttentionMode() => focusPriority.AddAttentionMode(new AttentionMode(enemyFocus)); AttentionMode attentionMode = controller.InitBlackboardValue("attentionEnemy", CreateAttentionMode).value; AttentionUtility attentionUtility = new AttentionUtility() { attackTime = 0.5f, attackPeek = 1.0f, sustainValue = 0.5f, }; attentionMode.SetUtility(() => attentionMode.focus.HasTarget() ? attentionUtility.GetUtility(attentionMode.activeTime) : 0); } // noise if (noiseFocus != null) { /// enemy AttentionMode CreateAttentionMode() => focusPriority.AddAttentionMode(new AttentionMode(noiseFocus)); AttentionMode attentionMode = controller.InitBlackboardValue("attentionNoise", CreateAttentionMode).value; AttentionUtility attentionUtility = new AttentionUtility() { attackTime = 0.25f, attackPeek = 0.85f, sustainValue = 0.45f, }; attentionMode.SetUtility(() => attentionMode.hadTarget ? attentionUtility.GetUtility(attentionMode.activeTime) : 0); } // pain if (painFocus != null) { /// enemy AttentionMode CreateAttentionMode() => focusPriority.AddAttentionMode(new AttentionMode(painFocus)); AttentionMode attentionMode = controller.InitBlackboardValue("attentionPain", CreateAttentionMode).value; AttentionUtility attentionUtility = new AttentionUtility() { attackTime = 0.45f, attackPeek = 0.8f, sustainValue = 0.4f, }; attentionMode.SetUtility(() => attentionMode.focus.HasTarget() ? attentionUtility.GetUtility(attentionMode.activeTime) : 0);; } // touch if (touchFocus != null) { /// enemy AttentionMode CreateAttentionMode() => focusPriority.AddAttentionMode(new AttentionMode(touchFocus)); AttentionMode attentionMode = controller.InitBlackboardValue("attentionTouch", CreateAttentionMode).value; AttentionUtility attentionUtility = new AttentionUtility() { attackTime = 0.25f, attackPeek = 0.65f, sustainValue = 0.3f, }; attentionMode.SetUtility(() => attentionMode.hadTarget ? attentionUtility.GetUtility(attentionMode.activeTime) : 0); } // idle { /// enemy AttentionMode CreateAttentionMode() => focusPriority.AddAttentionMode(new AttentionMode()); AttentionMode attentionMode = controller.InitBlackboardValue("attentionIdle", CreateAttentionMode).value; attentionMode.SetUtility(() => 0.15f); } return(focusPriority); }
public override void InitBehaviours(AiBehaviourController controller) { var stateMachine = controller.stateMachine; var inputHolder = controller.GetComponentInParent <InputHolder>(); var transform = controller.transform; FocusPriority priorityFocus = controller.focusPriority; AttentionMode painAttention = AiBehaviourPackUtility.GetAttentionPain(controller); var tExecute = new Timer(); var lookAroundMethod = new LookAround(new RangedFloat(0.5f, 0.8f), new RangedFloat(25, 50)); // TODO if seeker is not defined create MoveToDestination instead var moveToDestination = new MoveToDestinationNavigation(controller.GetComponent <Seeker>()); var velocityAccumulator = new VelocityAccumulator(0.89f, 0.075f); var destination = controller.InitBlackboardValue <Vector2>("destination"); //////////////// var idle = painAttention.AddNewState(true) .AddOnBegin(() => tExecute.RestartRandom(0.15f, 0.3f)) .AddOnBegin(inputHolder.ResetInput) .AddShallReturn(tExecute.IsReady) .SetUtility(0.1f); ; var moveAway = painAttention.AddNewState(true) .AddOnBegin(inputHolder.ResetInput) .AddOnBegin(() => tExecute.RestartRandom(0.25f, 0.325f)) .AddOnUpdate(() => { Vector2 toTarget = painAttention.focus.ToTarget(); inputHolder.positionInput = -toTarget; inputHolder.rotationInput = toTarget; inputHolder.directionInput = toTarget; //Debug.Log(painAttention.activeTime); }) .AddShallReturn(() => tExecute.IsReady()) .SetUtility(() => 10) .AddCanEnter(() => painAttention.activeTime < 0.1f) ; BoxValue <Vector2> initialDirection = new BoxValue <Vector2>(Vector2.zero); var hit = painAttention.AddNewState(true) .AddOnBegin(inputHolder.ResetInput) .AddOnBegin(() => tExecute.RestartRandom(1.0f, 1.5f)) .AddOnBegin(() => inputHolder.directionInput = transform.up) .AddOnUpdate(() => { Vector2 toTarget = painAttention.focus.ToTarget(); inputHolder.positionInput = tExecute.IsReady(0.2f) ? Vector2.zero : -toTarget; if (tExecute.IsReady(0.2f)) { inputHolder.directionInput = PolarVector2.MoveTowards( inputHolder.directionInput.GetPolarVector(), toTarget.GetPolarVector(), 3.5f, 0.25f); inputHolder.keys[0] = true; } //Debug.Log(painAttention.activeTime); }) .AddShallReturn(() => tExecute.IsReady()) .SetUtility(() => 15) .AddCanEnter(() => painAttention.activeTime < 0.1f) ; /// TODO: /// later (if have not switched to another event) ai will have search behaviours /// look around ( standard 360 degrees with agressive switches) /// move randomly around /// search manager (which will somehow generate points to check) /// once in a while look at random point close to the hit position }
private void Start() { focusPriority = AiBehaviourPackUtility.GetFocusPriority(this); stateMachine.SetNextStateMethod(focusPriority.GetTransitionByRandomUtility); InitBehaviourPacks(); }