diff --git a/Assets/JAY/Scripts/WeaponController.cs b/Assets/JAY/Scripts/WeaponController.cs index 199dbe04..e5dea921 100644 --- a/Assets/JAY/Scripts/WeaponController.cs +++ b/Assets/JAY/Scripts/WeaponController.cs @@ -49,7 +49,11 @@ public class WeaponController : MonoBehaviour, IObservable _playerController = GetComponent(); if (_playerController == null) { - _playerController = GameObject.FindGameObjectWithTag("Player").GetComponent(); + var player = GameObject.FindGameObjectWithTag("Player"); + if (player != null) + { + _playerController = player.GetComponent(); + } } _previousPositions = new Vector3[_triggerZones.Length]; @@ -106,7 +110,7 @@ public class WeaponController : MonoBehaviour, IObservable if (hit.collider.gameObject.CompareTag("Enemy")) { var enemyController = hit.transform.GetComponent(); - if (enemyController != null) + if (enemyController != null && _playerController != null) { enemyController.TakeDamage(AttackPower * _playerController.attackPower); } diff --git a/Assets/KSH/DungeonLogic.cs b/Assets/KSH/DungeonLogic.cs index 3a8bccc6..834eec9e 100644 --- a/Assets/KSH/DungeonLogic.cs +++ b/Assets/KSH/DungeonLogic.cs @@ -5,6 +5,8 @@ using UnityEngine; public class DungeonLogic : MonoBehaviour { + [SerializeField] private DungeonPanelController _dungeonPanelController; + [NonSerialized] public bool isCompleted = false; // 던전 클리어 여부 [NonSerialized] public bool isFailed = false; // 던전 실패 여부 @@ -24,19 +26,35 @@ public class DungeonLogic : MonoBehaviour // 죽음 이벤트 구독 if (_player != null) { + _player.OnGetHit += OnPlayerGetHit; _player.OnDeath += OnPlayerDeath; } if (_enemy != null) { + _enemy.OnGetHit += OnEnemyGetHit; _enemy.OnDeath += OnEnemyDeath; } } - // 플레이어 사망 처리 - private void OnPlayerDeath(CharacterBase player) + private void OnPlayerGetHit(CharacterBase player) + { + var result = _dungeonPanelController.SetPlayerHealth(); + if (!result) // 하트 모두 소모 + { + player.Die(); + } + } + + private void OnEnemyGetHit(CharacterBase enemy) + { + Debug.Log("Enemy HP: " + enemy.currentHP); + _dungeonPanelController.SetBossHealthBar(enemy.currentHP); + } + + // 플레이어 사망 처리 + private void OnPlayerDeath() { - Debug.Log("player name:" + player.characterName); if (!isFailed) // 중복 실행 방지 { FailDungeon(); @@ -44,9 +62,8 @@ public class DungeonLogic : MonoBehaviour } // 적 사망 처리 - private void OnEnemyDeath(CharacterBase enemy) + private void OnEnemyDeath() { - Debug.Log("enemy name:" + enemy.characterName); if (!isCompleted) // 중복 실행 방지 { CompleteDungeon(); @@ -62,7 +79,7 @@ public class DungeonLogic : MonoBehaviour isCompleted = true; OnDungeonSuccess?.Invoke(); - // 성공 UI 표시 ?? 강화 표기 + _player.SetState(PlayerState.Win); // TODO: 강화 시스템으로 넘어가고 일상 맵으로 이동 } } @@ -76,10 +93,8 @@ public class DungeonLogic : MonoBehaviour isFailed = true; OnDungeonFailure?.Invoke(); - // 죽음 애니메이션 + 실패 UI 표시 ? - // GameManager.Instance.ChangeToHomeScene(); - - StartCoroutine(DelayedSceneChange()); // 테스트를 위해 3초 대기 후 전환 + _player.SetState(PlayerState.Dead); + StartCoroutine(DelayedSceneChange()); // 3초 대기 후 전환 } } diff --git a/Assets/KSH/DungeonPanelController.cs b/Assets/KSH/DungeonPanelController.cs new file mode 100644 index 00000000..800e4690 --- /dev/null +++ b/Assets/KSH/DungeonPanelController.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +public class DungeonPanelController : MonoBehaviour +{ + [SerializeField] private Slider _bossHealthBar; // 0~1 value + [SerializeField] private Image[] _playerHealthImages; // color 값 white / black 로 조정 + private int _countHealth = 0; + + public void SetBossHealthBar(float hp) // hp: 0~100 + { + float normalizedHp = hp / 100f; // 0~1 사이 값으로 조정 + _bossHealthBar.value = normalizedHp; + } + + // false 반환 시 사망 처리 + public bool SetPlayerHealth() + { + if (_countHealth > _playerHealthImages.Length - 1) // out of index error 방지 + { + return false; + } + + _playerHealthImages[_countHealth].color = Color.black; + _countHealth++; + return _countHealth <= _playerHealthImages.Length - 1; + } +} diff --git a/Assets/KSH/DungeonPanelController.cs.meta b/Assets/KSH/DungeonPanelController.cs.meta new file mode 100644 index 00000000..5f53a9f2 --- /dev/null +++ b/Assets/KSH/DungeonPanelController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 75caf6a3958eb0745a00749003e12d73 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KSH/DungeonTestScene.unity b/Assets/KSH/DungeonTestScene.unity index b8ce1c3e..ecfbe121 100644 --- a/Assets/KSH/DungeonTestScene.unity +++ b/Assets/KSH/DungeonTestScene.unity @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a3f988deb686f35d4ca0aeb90dcf4d726ccff34a27d1b5f0e020b0ceeb008606 -size 185731 +oid sha256:ee7536d36b114bd9488ea8f43600194c288f7bb6a77bbb0ba1247b8486a6dd78 +size 236615 diff --git a/Assets/Scripts/Character/CharacterBase.cs b/Assets/Scripts/Character/CharacterBase.cs index 4a35b463..fdf5a623 100644 --- a/Assets/Scripts/Character/CharacterBase.cs +++ b/Assets/Scripts/Character/CharacterBase.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -17,7 +18,8 @@ public abstract class CharacterBase : MonoBehaviour [Header("상태 이상")] public List statusEffects = new List(); - public event System.Action OnDeath; // 사망 이벤트 + public event Action OnDeath; // 사망 이벤트 + public event Action OnGetHit; // 피격 이벤트 protected virtual void Start() { @@ -34,13 +36,15 @@ public abstract class CharacterBase : MonoBehaviour { Die(); } + + OnGetHit?.Invoke(this); } public virtual void Die() { Debug.Log($"{characterName}이 사망했습니다."); // TODO: 사망 처리 - OnDeath?.Invoke(this); + OnDeath?.Invoke(); } // 상태이상 추가 메서드