(25/06/24)FSM틀과 C# 다형성

허윤·2025년 6월 24일
	namespace Data
	{
		public interface IState
		{
			void Enter();
			void Execute();
			void Exit();
        }
		public class Charactors
		{
            public string name;
            private string dataName;//datapool에서 필요한 이름
			public string GetDataName { get { return dataName; } }
            public sbyte hp;
			public sbyte mana;
			public sbyte skillPoints;//
			public sbyte ad;//attackDamage
			public sbyte ap;//abilityPower
			public Charactors(string name, string dataName, sbyte hp, sbyte mana, sbyte skillPoints, sbyte ad, sbyte ap)
            {
                this.name = name;
                this.dataName = dataName;
                this.hp = hp;
                this.mana = mana;
                this.skillPoints = skillPoints;
                this.ad = ad;
                this.ap = ap;
            }
        }
		public enum charactorJob
		{
			warriror,archer,oracle
		}
    }
	namespace Dirrect
	{

        public class Actors
        {
			private SpriteRenderer sr;
			private Animator anim;
            private string srName;
            StateParent[] states;
            StateParent curr;
			public void StateUpdate()
			{
                curr.Execute();
			}
            public void ChangeAnim(StateType sName)
            {
                anim.Play($"{srName}_{sName.ToString()}");
            }
            public void ChangeState(StateType nextState)
            {
                curr.Exit();
                if (curr.type == StateType.Die) return;
                curr = states.Where(x => x.type == nextState).First();
                curr.Enter();
            }

        }
		namespace States
		{
            public enum StateType { Idle,Move,Attack, Damage, Die}

            public class StateParent : IState
            {

                public StateType type;
                protected Actors actor;
                protected float currTime;
                protected float goal;
                public StateParent(Actors actor, float goal)
                {
                    this.actor = actor;
                    this.currTime = 0;
                    this.goal = goal;
                }

                public virtual void Enter()
                {
                    actor.ChangeAnim(type);
                }
                public virtual void Execute()
                {
                    currTime += Time.deltaTime;
                    if (goal <= currTime) actor.ChangeState(StateType.Idle);
                }
                public virtual void Exit()
                {
                    currTime = 0f;
                }
            }
            public class IdleState : StateParent
            {
                public IdleState(Actors actor, float goal) : base(actor, goal)
                {
                    this.type = StateType.Idle;
                    this.actor = actor;
                    this.goal = goal;
                }
            }
            public class MoveState : StateParent
            {
                public MoveState(Actors actor, float goal) : base(actor, goal)
                {
                    this.type = StateType.Move;
                    this.actor = actor;
                    this.goal = goal;
                }
            }
            public class AttackState : StateParent
            {
                public AttackState(Actors actor, float goal) : base(actor, goal)
                {
                    this.type = StateType.Attack;
                    this.actor = actor;
                    this.goal = goal;
                }
            }
            public class DamagedState : StateParent
            {
                public DamagedState(Actors actor, float goal) : base(actor, goal)
                {
                    this.type = StateType.Damage;
                    this.actor = actor;
                    this.goal = goal;
                }
            }

            public class DieState : StateParent
            {
                public DieState(Actors actor, float goal) : base(actor, goal)
                {
                    this.type = StateType.Die;
                    this.actor = actor;
                    this.goal = goal;
                }
                public override void Execute()
                {
                    
                }
            }
        }

    }

TIL

    1. 오늘 학습 키워드
    • 유한 상태머신
    1. 오늘 학습 한 내용을 나만의 언어로 정리하기
    • new,virtual 키워드, interface기반의 stateMachine 틀 구현
    1. 학습하며 겪었던 문제점 & 알게 된 점
    • 문제점
      • new와 virtual 키워드의 상세한 차이점을 잊어버렸었다
    • 복기 포인트
      • new : 업캐스팅시 부모의 함수가, 다운캐스팅시 재선언된 자식의 함수가 실행
      • virtual : 캐스팅 여부와 상관없이 재선언된 함수가 실행
    1. 내일 학습 할 것은 무엇인지
    • 일단 근래에 프로젝트보단 코테와 이론공부만을 했었기에 오랜만에 프로젝트에 집중할 것 같다.
profile
C# 클라이언트 프로그래밍 지망

0개의 댓글