From ae5913ec0bd3ce0bf83b37ffe78eaa23d4574189 Mon Sep 17 00:00:00 2001 From: Mingu Kim Date: Wed, 19 Feb 2025 00:44:53 +0900 Subject: [PATCH] =?UTF-8?q?=EB=AA=AC=EC=8A=A4=ED=84=B0=20Hit,=20Death=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=20=ED=8B=80=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/Gameton/Scripts/Monster/Attack.cs | 11 ++- .../Gameton/Scripts/Monster/AttackPattern.cs | 3 +- .../Gameton/Scripts/Monster/MonsterBase.cs | 12 ++- .../Gameton/Scripts/Monster/StateMachine.cs | 75 +++++++++++++++++++ 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/Gameton-06/Assets/Gameton/Scripts/Monster/Attack.cs b/Gameton-06/Assets/Gameton/Scripts/Monster/Attack.cs index 62912f75..d9828e01 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Monster/Attack.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Monster/Attack.cs @@ -15,17 +15,16 @@ namespace TON if (other.CompareTag("Player")) { _monsterBase.IsAttacking = true; - // _monsterBase.SetTransition(new AttackState()); - // _monsterBase.SetTransition(new MonsterSkillState()); } } private void OnTriggerExit2D(Collider2D other) { - _monsterBase.IsAttacking = false; - // _monsterBase.SetTransition(new ChasingState()); - - // Debug.Log("감지 벗어남"); + if (other.CompareTag("Player")) + { + _monsterBase.IsAttacking = false; + _monsterBase.IsFisnishAttack = true; // 공격 종료 상태로 설정 + } } } } diff --git a/Gameton-06/Assets/Gameton/Scripts/Monster/AttackPattern.cs b/Gameton-06/Assets/Gameton/Scripts/Monster/AttackPattern.cs index de291f8e..4d62d5fc 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Monster/AttackPattern.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Monster/AttackPattern.cs @@ -7,8 +7,7 @@ namespace TON public class AttackPattern { protected MonsterBase _monsterBase; - - + public AttackPattern(MonsterBase monsterBase) { _monsterBase = monsterBase; diff --git a/Gameton-06/Assets/Gameton/Scripts/Monster/MonsterBase.cs b/Gameton-06/Assets/Gameton/Scripts/Monster/MonsterBase.cs index 16157c44..0495eb05 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Monster/MonsterBase.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Monster/MonsterBase.cs @@ -36,6 +36,8 @@ namespace TON public bool IsDetect { get; set; } // 몬스터가 대상을 인식했는지 여부 public bool IsAttacking { get; set; } // 몬스터가 공격했는지 여부 public bool IsFisnishAttack { get; set; } // 몬스터 공격 모션이 끝났는지 여부 + public bool IsHit { get; set; } // 몬스터 공격 모션이 끝났는지 여부 + public bool IsDead { get; set; } // 몬스터 공격 모션이 끝났는지 여부 [SerializeField] private GameObject _target; // 몬스터의 타겟 @@ -162,7 +164,8 @@ namespace TON if (prevHP > 0 && currentHP <= 0) { // 몬스터가 죽었을 때 처리 (죽는 애니메이션은 주석 처리됨) - Destroy(gameObject); // 몬스터 파괴 + // Destroy(gameObject); // 몬스터 파괴 + DestroyMonster(); } else if (prevHP > 0 && currentHP > 0) { @@ -248,5 +251,10 @@ namespace TON newSkill.transform.position = transform.position + new Vector3(0, 1f, 0); newSkill.GetComponent().Direction = new Vector2(0, 1); } + + public void DestroyMonster() + { + Destroy(gameObject); // 몬스터 파괴 + } } -} +} \ No newline at end of file diff --git a/Gameton-06/Assets/Gameton/Scripts/Monster/StateMachine.cs b/Gameton-06/Assets/Gameton/Scripts/Monster/StateMachine.cs index 1ed272c8..182e87cc 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Monster/StateMachine.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Monster/StateMachine.cs @@ -291,4 +291,79 @@ namespace TON return this; } } + + public class HitState : IState + { + private const string AniHit = "Hit"; + private MonsterBase _monsterBase; + private float _hitDuration = 0.5f; + private float _hitStartTime; + + public void Enter(MonsterBase monsterBase) + { + _monsterBase = monsterBase; + _monsterBase.ChangeAnimationState(AniHit); + _hitStartTime = Time.time; + } + + public void Update() + { + if (Time.time >= _hitStartTime + _hitDuration) + { + _monsterBase.IsHit = false; + } + } + + public void Exit() + { + } + + public IState CheckTransition() + { + if (_monsterBase.IsHit) + return new HitState(); + + if (_monsterBase.IsDead) + return new DeathState(); + + if (Time.time >= _hitStartTime + _hitDuration) + return new IdleState(); + + return this; + } + } + + public class DeathState : IState + { + private const string AniDeath = "Death"; + private MonsterBase _monsterBase; + private float _deathDuration = 1f; + private float _deathStartTime; + private bool _deathAnimationStarted = false; + + public void Enter(MonsterBase monsterBase) + { + _monsterBase = monsterBase; + _monsterBase.ChangeAnimationState(AniDeath); + _deathStartTime = Time.time; + _deathAnimationStarted = true; + } + + public void Update() + { + if (_deathAnimationStarted && Time.time >= _deathStartTime + _deathDuration) + { + _monsterBase.DestroyMonster(); + } + } + + public void Exit() + { + } + + public IState CheckTransition() + { + return this; // Death는 다른 상태로 전환되지 않음 + } + } }