Skip to content

Instantly share code, notes, and snippets.

@marinho
Last active August 2, 2023 09:32
Show Gist options
  • Save marinho/9c1313b08d49e3a3d6b1a45b00a94c0b to your computer and use it in GitHub Desktop.
Save marinho/9c1313b08d49e3a3d6b1a45b00a94c0b to your computer and use it in GitHub Desktop.
AIBrain Decision and Action for Character to move back to initial position
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MoreMountains.TopDownEngine;
using MoreMountains.Tools;
namespace Twelve {
public class TwelveAIActionMoveTowardsInitialPosition : AIAction
{
protected CharacterMovement _characterMovement;
protected Vector3 _directionToTarget;
protected Vector2 _movementVector;
protected Vector3 _initialPosition;
private void Start() {
_initialPosition = transform.position;
}
public override void Initialization()
{
_characterMovement = this.gameObject.GetComponentInParent<Character>()?.FindAbility<CharacterMovement>();
}
public override void PerformAction()
{
Move();
}
public Vector3 GetInitialPosition()
{
return _initialPosition;
}
/// <summary>
/// Moves the character towards the target if needed
/// </summary>
protected virtual void Move()
{
_directionToTarget = _initialPosition - this.transform.position;
_movementVector.x = _directionToTarget.x;
_movementVector.y = _directionToTarget.z;
_characterMovement.SetMovement(_movementVector);
}
/// <summary>
/// On exit state we stop our movement
/// </summary>
public override void OnExitState()
{
base.OnExitState();
_characterMovement?.SetHorizontalMovement(0f);
_characterMovement?.SetVerticalMovement(0f);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MoreMountains.TopDownEngine;
using MoreMountains.Tools;
namespace Twelve
{
public class TwelveAIDecisionIsBackToInitialPosition : AIDecision
{
protected TwelveAIActionMoveTowardsInitialPosition actionMoveTowardsInitialPosition;
float distanceTolerance = 0.001f;
public override void Initialization()
{
actionMoveTowardsInitialPosition = gameObject.GetComponent<TwelveAIActionMoveTowardsInitialPosition>();
}
public override bool Decide()
{
return IsBackToInitialPosition();
}
protected virtual bool IsBackToInitialPosition() {
if (actionMoveTowardsInitialPosition == null) {
return false;
}
var initialPosition = actionMoveTowardsInitialPosition.GetInitialPosition();
var distance = new Vector2(initialPosition.x, initialPosition.z) - new Vector2(this.transform.position.x, this.transform.position.z);
return distance.x < distanceTolerance && distance.y < distanceTolerance;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MoreMountains.TopDownEngine;
using MoreMountains.Tools;
namespace Twelve
{
public class TwelveAIDecisionIsFarFromInitialPosition : AIDecision
{
[SerializeField] float maximumDistance = 2f;
[SerializeField] float timeToKeepDecision = 1f;
protected TwelveAIActionMoveTowardsInitialPosition actionMoveTowardsInitialPosition;
float timeCounterToKeepDecision = 0f;
bool latestDecision = false;
private void Update() {
timeCounterToKeepDecision += Time.deltaTime;
}
public override void Initialization()
{
actionMoveTowardsInitialPosition = gameObject.GetComponent<TwelveAIActionMoveTowardsInitialPosition>();
}
public override bool Decide()
{
if (timeCounterToKeepDecision < timeToKeepDecision) {
return latestDecision;
}
bool newDecision = IsFarFromInitialPosition();
if (newDecision != latestDecision) {
timeCounterToKeepDecision = 0f;
latestDecision = newDecision;
}
return newDecision;
}
protected virtual bool IsFarFromInitialPosition() {
if (actionMoveTowardsInitialPosition == null) {
return false;
}
var initialPosition = actionMoveTowardsInitialPosition.GetInitialPosition();
var distance = new Vector2(initialPosition.x, initialPosition.z) - new Vector2(this.transform.position.x, this.transform.position.z);
return distance.x > maximumDistance || distance.y > maximumDistance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment