(25/07/05)FSM

허윤·2025년 7월 5일
        public class StateMachine
        {
            StateParent[] states;
            StateParent currStates;
            public Dirrector dirrector;
            public StateMachine()
            {
                List<StateParent> temp = new List<StateParent>();
                
                for (int i = 0; i < Enum.GetValues(typeof(StateType)).Length; i++)
                {
                    //TODO : 애니메이션 타임 리턴하는 함수 작업 필요
                    if (!((int)StateType.HeavyWeapon == i))
                    {
                        temp.Add(new StateParent(dirrector, 30));
                    }
                    else
                    {
                        temp.Add(new StateParent( dirrector, 30));
                    }
                }
            }

            public StateParent Search(StateType type)
            {
                for (int i = 0; i < Enum.GetValues(typeof(StateType)).Length; i++)
                {
                    if (states[i] == null) return null;
                    if (states[i].type == type)
                    {
                        return states[i];
                    }
                }
                Debug.LogError($"{type}스테이트가 존재 하지 않습니다");
                return null;
            }


            public void Change(StateType type)
            {
                if (currStates.Exit())
                {
                    currStates = Search(type);
                    currStates.Enter();
                }
            }
	}
 public enum StateType { Idle, Move, Attack, HeavyWeapon, Damage, Die , Seat , BulletJump ,Jump,Landing }

 public class StateParent : IState
 {

     public StateType type;
     protected Dirrector actor;
     protected float currTime;
     protected float goal;
     protected bool motionCancel;
     public bool MotionCancel { get { return motionCancel; } }

     public StateParent(Dirrector actor, float goal)
     {
         this.actor = actor;
         this.currTime = 0;
         this.goal = goal;
     }

     public virtual void Enter()
     {
         currTime = 0;
         actor.ChangeAnims(type);
     }
     public virtual void Execute()
     {
         currTime += Time.deltaTime;
     }
     public virtual bool Exit()
     {
         if (!MotionCancel && currTime <= goal) return false;
         currTime = 0f;
         return true;
     }
 }

FSM

Finite State Machine
유한 상태머신의 줄임말로 동시에 여러 State가 발생하는것을 방지하기 위해 작업하였다.

public class IdleState : StateParent
        {
            public IdleState(Dirrector actor, float goal, bool motionCancel) : base(actor, goal)
            {
                type = StateType.Idle;
                this.actor = actor;
                this.currTime = 0;
                this.goal = goal;
                this.motionCancel = true;
            }
            public override void Execute()
            {
                base.Execute();
                if (goal <= currTime) actor.stateMachine.Change(StateType.Idle);
            }
        }

StatePare

    1. 오늘 학습 키워드
    • 상태머신
    1. 오늘 학습 한 내용을 나만의 언어로 정리하기
    • 애니메이션 실 적용 이전 FSM 클래스 작성
    1. 학습하며 겪었던 문제점 & 에러
    • 문제에 대한 정의
      • 애니메이션 상태값 중복이슈해결을 위한 FSM
    • 내가 한 시도와 해결 방법
      • 여러 상태값 중 하나의 주 상태를 지정하여 환경을 제어
    • 이 문제&에러를 다시 만나게 되었다면?
      • 학생시절 만났던 이슈이기에 이번 케이스가 문제를 다시 만난 케이스로
        이번과 동일하게 처리할 것 같다.
    1. 내일 학습 할 것은 무엇인지
    • 일부 states를 구현을 아직 안했는데 실 기능 구현 하고 오늘 작업한 FSM을 붙여 볼 것 같다.
      한마디로 해당 기능들을 실 구현할 것 같음
profile
C# 클라이언트 프로그래밍 지망

0개의 댓글