Advertisement
TheLowestAnimal

GenericEnemy

May 23rd, 2022
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using StateMachineLib;
  6. public class GenericEnemy : MonoBehaviour
  7. {
  8.     private struct NPCDummyStats
  9.     {
  10.         int health;
  11.         float speed;
  12.         float localGravity;
  13.     }
  14.  
  15.     [SerializeField] private GameObject _target;
  16.  
  17.     [SerializeField] private CharacterController _characterController;
  18.     [SerializeField] private Animator _anim;
  19.     [SerializeField] private bool _forceStateTransitionDebug = false;
  20.     private StateMachine _stateMachine;
  21.  
  22.     private PlayerHealth _playerHealth;
  23.  
  24.  
  25.     private NPCDummyStats _dummyStats;
  26.  
  27.  
  28.     //Defined State Types being Initialized
  29.     IState Idle;
  30.     IState Null;
  31.     void Awake()
  32.     {
  33.         if (_characterController == null)
  34.             GetComponent<CharacterController>();
  35.  
  36.         if (_stateMachine == null)
  37.             if (TryGetComponent<StateMachine>(out _stateMachine))
  38.             {
  39.                 _stateMachine = new StateMachine();
  40.             }
  41.  
  42.         _playerHealth = PlayerHealth.Instance;
  43.  
  44.  
  45.         void At(IState from, IState to, Func<bool> condition) => _stateMachine.AddStateTransition(from, to, condition);
  46.         //Generic Transition Conditionals
  47.         #region
  48.         Func<bool> HasTarget() => () => _target != null;
  49.         Func<bool> HasNoTarget() => () => _target == null;
  50.         Func<bool> Timer(float timeLimit, float timeElapsed) => () => (timeElapsed >= timeLimit);
  51.         Func<bool> TargetReached(float targetThresholdRange) => () => _target != null
  52.                                                                       && Vector3.Distance(transform.position, _target.transform.position) < targetThresholdRange;
  53.         Func<bool> ForceDebugTransition() => () => _forceStateTransitionDebug;
  54.         Func<bool> ForceDebug01() => () => false;
  55.  
  56.         #endregion
  57.  
  58.         //Assigning the prior defined states
  59.         Idle = new SM_Idle(_anim);
  60.         Null = new SM_Null();
  61.  
  62.         //Assigning transitions
  63.         At(Null, Idle, HasTarget());
  64.         At(Idle, Null, HasNoTarget());
  65.     }
  66.  
  67.     void Start()
  68.     {
  69.         _stateMachine.SetState(Null);
  70.         //_stateMachine._stateChanged(Null);
  71.     }
  72.  
  73.     private void Update()
  74.     {
  75.         _stateMachine.Tick();
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement