몬스터 공격시 캐릭터 데미지 대상 지정 및 몬스터 피격 시 HP바 감소

This commit is contained in:
Mingu Kim
2025-02-28 02:15:32 +09:00
parent b96aac99f7
commit 5e893a029f
7 changed files with 750 additions and 93 deletions

View File

@@ -7,6 +7,11 @@ namespace TON
[SerializeField]
private MonsterBase _monsterBase;
public void SetMonsterBase(MonsterBase monsterBase)
{
_monsterBase = monsterBase;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))

View File

@@ -7,6 +7,11 @@ namespace TON
[SerializeField]
private MonsterBase _monsterBase;
public void SetMonsterBase(MonsterBase monsterBase)
{
_monsterBase = monsterBase;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))

View File

@@ -38,6 +38,8 @@ namespace TON
public int Gold = 0;
public int Exp = 0;
public int Score = 0;
private CharacterBase _characterBase;
private void Start()
{
@@ -56,6 +58,21 @@ namespace TON
_spriteRenderer.flipX = !(_direction.x > 0); // 이동 방향에 따라 스프라이트 플립
_collider = GetComponent<Collider2D>(); // 콜라이더 컴포넌트 초기화
// CharacterBase 참조 설정
_characterBase = GameObject.Find("TON.Player").GetComponentInChildren<CharacterBase>();
// HP 바 초기화
// HP 바 초기화
if (_hpBarImage != null)
{
RectTransform rectTransform = _hpBarImage.GetComponent<RectTransform>();
hpMaxWidth = rectTransform.sizeDelta.x; // 초기 최대 너비 저장
_maxHP = _monsterData.hp;
currentHP = _maxHP;
Debug.Log($"Initial HP Bar Width: {hpMaxWidth}, Max HP: {_maxHP}");
}
}
private void InitializeMonsterData()
@@ -64,7 +81,8 @@ namespace TON
if (_monsterData != null)
{
currentHP = _monsterData.hp;
_maxHP = _monsterData.hp;
currentHP = _maxHP;
defencePower = _monsterData.defencePower;
Gold = _monsterData.Gold;
Exp = _monsterData.Exp;
@@ -93,7 +111,11 @@ namespace TON
private void Update()
{
_stateMachine.Update();
_skillPattern.Update();
if (_monsterData.monsterSkillID != 0)
{
_skillPattern.Update();
}
}
public void FinishAttack()
@@ -123,10 +145,29 @@ namespace TON
{
if (_hpBarImage != null)
{
float minHPBarWidth = 5f; // 최소 HP 바 길이 (원하는 값으로 설정)
float hpBarWidth = Mathf.Max(currentHP / _maxHP * hpMaxWidth, minHPBarWidth); // 최소 길이 적용
_hpBarImage.GetComponent<RectTransform>().sizeDelta = new Vector2(hpBarWidth, _hpBarImage.GetComponent<RectTransform>().sizeDelta.y);
// 현재 HP가 0 이하로 내려가지 않도록 보정
currentHP = Mathf.Max(0, currentHP);
// HP 비율 계산 (0~1 사이 값)
float hpRatio = currentHP / _maxHP;
// RectTransform 컴포넌트 가져오기
RectTransform rectTransform = _hpBarImage.GetComponent<RectTransform>();
// 현재 크기 가져오기
Vector2 sizeDelta = rectTransform.sizeDelta;
// x 크기를 HP 비율에 따라 조정
sizeDelta.x = hpMaxWidth * hpRatio;
// 변경된 크기 적용
rectTransform.sizeDelta = sizeDelta;
Debug.Log($"Current HP: {currentHP}, Max HP: {_maxHP}, Ratio: {hpRatio}, Bar Width: {sizeDelta.x}");
}
else
{
Debug.LogWarning("HP Bar Image is not assigned!");
}
}
@@ -142,6 +183,8 @@ namespace TON
// 기본 데미지 계산 (치명타 없음)
float damage = damageCalculator.CalculateBaseDamage(baseAttack, equipmentAttack, defense);
_characterBase.ApplyDamage(damage);
Debug.Log($" 몬스터 공격! 최종 데미지: {damage}"); // 데미지 출력
}

View File

@@ -19,6 +19,9 @@ namespace TON
private const int TOTAL_WAVES = 10;
private const int NORMAL_MONSTER_COUNT = 6;
private float initialDelay = 5f; // 게임 시작 후 첫 웨이브 시작까지의 대기 시간
private bool gameStarted = false;
private float nextWaveDelay = 5f; // 다음 웨이브 시작 전 대기 시간
private bool isWaitingForNextWave = false;
@@ -44,12 +47,39 @@ namespace TON
availableSpawnPoints = new List<int>();
activeMonsters = new List<GameObject>();
// 5초 후에 첫 웨이브 시작
StartCoroutine(StartGameWithDelay());
}
private IEnumerator StartGameWithDelay()
{
// 초기 카운트다운 표시
float timer = initialDelay;
while (timer > 0)
{
if (waveCounter != null)
{
waveCounter.text = Mathf.CeilToInt(timer).ToString();
}
timer -= Time.deltaTime;
yield return null;
}
if (waveCounter != null)
{
waveCounter.text = null;
}
gameStarted = true;
StartNextWave();
}
// Update is called once per frame
void Update()
{
// 게임이 시작되지 않았다면 업데이트 건너뛰기
if (!gameStarted) return;
// 활성화된 몬스터 리스트에서 파괴된 몬스터 제거
activeMonsters.RemoveAll(monster => monster == null);
@@ -77,10 +107,10 @@ namespace TON
private void StartNextWave()
{
currentWave++;
StageManager.Singleton.SetWaveData(currentWave); // 웨이브 정보 전달.
currentWave++;
if (currentWave > TOTAL_WAVES)
{
// Debug.Log("모든 웨이브 완료!");
@@ -130,9 +160,26 @@ namespace TON
{
foreach (Transform spawnPoint in spawnPoints)
{
GameObject normalMonster = Instantiate(GetNormalMonsterPrefab(), spawnPoint.position, Quaternion.identity);
monsterPool.Add(normalMonster);
activeMonsters.Add(normalMonster);
GameObject monsterPrefab = GetNormalMonsterPrefab();
Vector3 spawnPosition = spawnPoint.position;
GameObject monster = Instantiate(monsterPrefab, spawnPosition, Quaternion.identity);
monster.transform.parent = transform;
// MonsterBase 컴포넌트 가져오기
MonsterBase monsterBase = monster.GetComponent<MonsterBase>();
// Attack과 Eyesight 컴포넌트 찾아서 MonsterBase 참조 설정
Attack attackComponent = monster.GetComponentInChildren<Attack>();
Eyesight eyesightComponent = monster.GetComponentInChildren<Eyesight>();
if (attackComponent != null)
attackComponent.SetMonsterBase(monsterBase);
if (eyesightComponent != null)
eyesightComponent.SetMonsterBase(monsterBase);
monsterPool.Add(monster);
activeMonsters.Add(monster);
}
}
}
@@ -143,9 +190,26 @@ namespace TON
{
foreach (Transform spawnPoint in spawnPoints)
{
GameObject normalMonster = Instantiate(GetNormalMonsterPrefab(), spawnPoint.position, Quaternion.identity);
monsterPool.Add(normalMonster);
activeMonsters.Add(normalMonster);
GameObject monsterPrefab = GetNormalMonsterPrefab();
Vector3 spawnPosition = spawnPoint.position;
GameObject monster = Instantiate(monsterPrefab, spawnPosition, Quaternion.identity);
monster.transform.parent = transform;
// MonsterBase 컴포넌트 가져오기
MonsterBase monsterBase = monster.GetComponent<MonsterBase>();
// Attack과 Eyesight 컴포넌트 찾아서 MonsterBase 참조 설정
Attack attackComponent = monster.GetComponentInChildren<Attack>();
Eyesight eyesightComponent = monster.GetComponentInChildren<Eyesight>();
if (attackComponent != null)
attackComponent.SetMonsterBase(monsterBase);
if (eyesightComponent != null)
eyesightComponent.SetMonsterBase(monsterBase);
monsterPool.Add(monster);
activeMonsters.Add(monster);
}
}
}

View File

@@ -23,45 +23,34 @@ namespace TON
public class Monster1SkillPattern : SkillPattern
{
private float _skill1CoolTime;
// private float _skill2CoolTime;
private MonsterSkillData _monsterSkillData;
// private MonsterSkillData _monsterSkillDataTwo;
private float _skillCoolTime;
private MonsterSkill _skill1;
// private MonsterSkill _skill2;
private MonsterSkillData _monsterSkillData;
private MonsterSkill _skill;
private Vector3 _skillOffset = new Vector3(0, -0.5f, 0); // 스킬 생성 위치 조정값
public Monster1SkillPattern(MonsterData monsterData, MonsterBase monsterBase) : base(monsterData, monsterBase)
{
_monsterSkillData = MonsterSkillDataManager.Singleton.GetMonsterSkillData(_monsterData.monsterSkillID);
// if (_monsterData.monsterSkillIDTwo > -1)
// {
// _monsterSkillDataTwo = MonsterSkillDataManager.Singleton.GetMonsterSkillData(_monsterData.monsterSkillIDTwo);
// }
// if (_monsterSkillData != null && _monsterSkillDataTwo != null)
if (_monsterSkillData != null)
{
Debug.Log($"몬스터 {_monsterSkillData.skillName} 데이터 로드 완료");
// Debug.Log($"몬스터 {_monsterSkillDataTwo.skillName} 데이터 로드 완료");
// 프리팹을 연결한 코드
_skill1 = Resources.Load<MonsterSkill>($"MonsterSkillPrefabs/{_monsterSkillData.skillName}");
// _skill2 = Resources.Load<MonsterSkill>($"MonsterSkillPrefabs/{_monsterSkillDataTwo.skillName}");
_skill = Resources.Load<MonsterSkill>($"MonsterSkillPrefabs/{_monsterSkillData.skillName}");
}
else
{
Debug.LogError($"몬스터 스킬 ID {_monsterSkillData.skillId}에 대한 데이터를 찾을 수 없습니다.");
// Debug.LogError($"몬스터 스킬 ID {_monsterSkillDataTwo.skillId}에 대한 데이터를 찾을 수 없습니다.");
}
_skill1CoolTime = Time.realtimeSinceStartup;
// _skill2CoolTime = Time.realtimeSinceStartup;
_skillCoolTime = _monsterSkillData.cooldown;
}
public override void Attack(GameObject target)
@@ -74,58 +63,16 @@ namespace TON
Vector3 spawnPosition = _monsterBase.transform.position - _skillOffset;
// 프리팹을 지정된 위치에 생성
Object.Instantiate(_skill1, spawnPosition, Quaternion.identity);
Object.Instantiate(_skill, spawnPosition, Quaternion.identity);
}
public override void Update()
{
if (Time.realtimeSinceStartup - _skill1CoolTime >= _monsterSkillData.cooldown)
if (Time.realtimeSinceStartup - _skillCoolTime >= _monsterSkillData.cooldown)
{
// TODO : 범위 체크
IsAttackable = true;
}
// if (Time.realtimeSinceStartup - _skill2CoolTime >= _monsterSkillDataTwo.cooldown)
// {
// // TODO : 범위 체크
// IsAttackable = true;
// }
}
}
// public class Monster2AttackPattern : AttackPattern
// {
//
//
// public Monster2AttackPattern(MonsterBase monsterBase) : base(monsterBase)
// {
//
// }
//
// public override void Attack()
// {
//
// Skill1();
//
// Skill2();
//
// MeleeAttack();
//
// }
//
// private void Skill1()
// {
//
// }
//
// private void Skill2()
// {
//
// }
//
// private void MeleeAttack()
// {
// _monsterBase.PlayerAttack();
// }
// }
}

View File

@@ -245,7 +245,7 @@ namespace TON
private const string AniAttack = "Attack"; // 공격 애니메이션
private MonsterBase _monsterBase;
private float _skillAttackAnimationDuration = 0.5f; // 공격 애니메이션 지속 시간
private float _skillAttackDelayTime = 2f; // 공격 딜레이 시간
private float _skillAttackDelayTime = 10f; // 공격 딜레이 시간
private float _lastSkillAttackTime; // 마지막 공격 시간
private bool _isSkillAttacking;