Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using StateMachineLib;
- public class GenericEnemy : MonoBehaviour
- {
- private struct NPCDummyStats
- {
- int health;
- float speed;
- float localGravity;
- }
- [SerializeField] private GameObject _target;
- [SerializeField] private CharacterController _characterController;
- [SerializeField] private Animator _anim;
- [SerializeField] private bool _forceStateTransitionDebug = false;
- private StateMachine _stateMachine;
- private PlayerHealth _playerHealth;
- private NPCDummyStats _dummyStats;
- //Defined State Types being Initialized
- IState Idle;
- IState Null;
- void Awake()
- {
- if (_characterController == null)
- GetComponent<CharacterController>();
- if (_stateMachine == null)
- if (TryGetComponent<StateMachine>(out _stateMachine))
- {
- _stateMachine = new StateMachine();
- }
- _playerHealth = PlayerHealth.Instance;
- void At(IState from, IState to, Func<bool> condition) => _stateMachine.AddStateTransition(from, to, condition);
- //Generic Transition Conditionals
- #region
- Func<bool> HasTarget() => () => _target != null;
- Func<bool> HasNoTarget() => () => _target == null;
- Func<bool> Timer(float timeLimit, float timeElapsed) => () => (timeElapsed >= timeLimit);
- Func<bool> TargetReached(float targetThresholdRange) => () => _target != null
- && Vector3.Distance(transform.position, _target.transform.position) < targetThresholdRange;
- Func<bool> ForceDebugTransition() => () => _forceStateTransitionDebug;
- Func<bool> ForceDebug01() => () => false;
- #endregion
- //Assigning the prior defined states
- Idle = new SM_Idle(_anim);
- Null = new SM_Null();
- //Assigning transitions
- At(Null, Idle, HasTarget());
- At(Idle, Null, HasNoTarget());
- }
- void Start()
- {
- _stateMachine.SetState(Null);
- //_stateMachine._stateChanged(Null);
- }
- private void Update()
- {
- _stateMachine.Tick();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement