Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //define method signatures for types of moves that can be assigned to AI
- //NOTE these could be impelemented as coroutines if you wanted them to be async
- public delegate void BasicMove(Actor user);
- public delegate void TargetMove(Actor user, Actor target);
- //one concrete inheritor per enemy type (i.e. OrcActor, GoblinActor, TrollActor, ect)
- public abstract class AIActor : MonoBehaviour
- {
- private Controller _controller;
- //this is where you would call the builder class to create your enemy (could also just implement it here)
- protected abstract Controller BuildController();
- private void Awake()
- {
- _controller = BuildController();
- }
- private void Start()
- {
- _controller.Init();
- }
- private void Update()
- {
- _controller.Update();
- }
- }
- //one concrete implementor per enemy behavior type (i.e. ScoutBrain, GruntBrain, HeavyBrain, CustodianBrain, ect.)
- public abstract class EnemyController
- {
- private FSM _fsm;
- private Actor _target; //this value would be resolved dynamically, probably through some sort of perception system
- private Actor _owner; //this needs to be injected in the constructor
- private Dictionary<string, BasicMove> _basicMoves = new Dictionary<string, BasicMove>();
- private Dictionary<string, TargetMove> _targetMoves = new Dictionary<string, TargetMove>();
- //this method would be where you define the states and transitions
- public abstract FSM CreateStateMachine();
- public void Init()
- {
- //exact syntax for your state machine varies based on your choosen implementation
- //I am assuming that you can assign states as coroutines though
- _fsm = CreateStateMachine();
- _fsm.Start();
- }
- public void Update()
- {
- _fsm.Update();
- }
- public void AssignMove(string move, BasicMove moveDelegate)
- {
- //don't assing same name to more than one move
- if (_basicMoves.ContainsKey(move) || _targetMoves.ContainsKey(move))
- return;
- _basicMoves.Add(move, moveDelegate);
- }
- public void AssignMove(string move, TargetMove moveDelegate)
- {
- //don't assing same name to more than one move
- if (_targetMoves.ContainsKey(move) || _basicMoves.ContainsKey(move))
- return;
- _targetMoves.Add(move, moveDelegate);
- }
- //this method would be used to convert the sync delegate into an async state so it can be added to the state machine
- IEnumerator ExecuteMoveAsState(string move, float cooldown, Action<bool> successCallback)
- {
- if(!ExecuteMove(move))
- successCallback?.Invoke(false);
- yield return new WaitForSeconds(cooldown);
- successCallback?.Invoke(true);
- }
- bool ExecuteMove(string move)
- {
- if (_basicMoves.ContainsKey(move))
- {
- _basicMoves[move](_owner);
- }
- else if (_targetMoves.ContainsKey(move))
- {
- if (_target == null)
- {
- successCallback?.Invoke(false);
- }
- else
- {
- _targetMoves[move](_owner, _target);
- }
- }
- else
- {
- //throw exception because AI is trying to execute a move that it doesn't have
- throw new KeyNotFoundException();
- }
- }
- //could also implement any common enemy functionality here like target selection, perception, movement, pathfinding, ect.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement