몬스터 스킬 코드 추가
This commit is contained in:
@@ -9,14 +9,21 @@ namespace TON
|
||||
[SerializeField]
|
||||
private MonsterBase _monsterBase;
|
||||
// private bool _isDetect;
|
||||
|
||||
private GameObject skill;
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if (other.CompareTag("Player"))
|
||||
{
|
||||
skill = _monsterBase.smallFirePrefab;
|
||||
GameObject newSkill = Instantiate<GameObject>(skill);
|
||||
newSkill.transform.position = other.transform.position;
|
||||
// newSkill.GetComponent<skill>().Direction = new Vector2(1, 0);
|
||||
// _monsterBase.SetTransition(new AttackState());
|
||||
|
||||
|
||||
// 플레이어 감지하면 따라가기
|
||||
_monsterBase.SetTransition(new ChasingState());
|
||||
// _monsterBase.SetTransition(new ChasingState());
|
||||
Debug.Log("감지됨");
|
||||
|
||||
}
|
||||
|
||||
@@ -68,6 +68,8 @@ namespace TON
|
||||
public float cooldown; // 스킬 쿨다운
|
||||
public float range; // 스킬 범위
|
||||
public string animationName; // 스킬 애니메이션 이름
|
||||
|
||||
public GameObject smallFirePrefab;
|
||||
|
||||
// 첫 번째 프레임 전에 호출됩니다.
|
||||
private void Start()
|
||||
@@ -213,7 +215,7 @@ namespace TON
|
||||
public void PlayerAttack()
|
||||
{
|
||||
var target = GameObject.FindGameObjectWithTag("Player");
|
||||
Attack(target);
|
||||
Attack(target);
|
||||
}
|
||||
|
||||
public void SetOppositionDirection()
|
||||
|
||||
82
Gameton-06/Assets/Gameton/Scripts/Monster/MonsterSkill.cs
Normal file
82
Gameton-06/Assets/Gameton/Scripts/Monster/MonsterSkill.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TON
|
||||
{
|
||||
public class MonsterSkill : MonoBehaviour
|
||||
{
|
||||
public float speed = 3;
|
||||
public float damage = 1;
|
||||
|
||||
Vector2 direction;
|
||||
Transform playerTransform; // 플레이어의 Transform을 저장할 변수
|
||||
[SerializeField] private SpriteRenderer _spriteRenderer; // 스킬의 스프라이트 렌더러
|
||||
|
||||
public Vector2 Direction
|
||||
{
|
||||
set { direction = value.normalized; }
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
_spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
|
||||
// 플레이어 태그로 플레이어 오브젝트를 찾아서 Transform 컴포넌트를 가져옴
|
||||
GameObject player = GameObject.FindGameObjectWithTag("Player");
|
||||
|
||||
if (player != null)
|
||||
{
|
||||
playerTransform = player.transform;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Player 오브젝트를 찾을 수 없습니다. 태그를 확인하세요.");
|
||||
return; // 플레이어 없으면 이후 로직 실행 중지
|
||||
}
|
||||
|
||||
SetDirection(); // 발사 방향 설정
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
transform.Translate(direction * speed * Time.deltaTime);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collision)
|
||||
{
|
||||
if (collision.CompareTag("Player") || collision.CompareTag("Ground"))
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
void SetDirection()
|
||||
{
|
||||
if (playerTransform == null) return; // 플레이어가 없으면 방향 설정 불가
|
||||
|
||||
// MonsterSkill과 Player의 위치 차이 계산
|
||||
Vector2 toPlayer = playerTransform.position - transform.position;
|
||||
|
||||
// 플레이어의 왼쪽/오른쪽 판별
|
||||
if (toPlayer.x < 0)
|
||||
{
|
||||
// 플레이어가 왼쪽에 있으면 왼쪽 방향으로 발사
|
||||
direction = new Vector2(-1, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 플레이어가 오른쪽에 있으면 오른쪽 방향으로 발사
|
||||
direction = new Vector2(1, 0);
|
||||
_spriteRenderer.flipX = true; // 왼쪽 방향일 때 좌우 반전
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 190405d8d5d2c0e4683121e913af8457
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -200,113 +200,142 @@ namespace TON
|
||||
}
|
||||
}
|
||||
|
||||
public class HitState : State
|
||||
public class MonsterSkillState : State
|
||||
{
|
||||
private const string AniHit = "Hit"; // 피격 애니메이션
|
||||
private const string AniAttack = "Attack"; // 공격 애니메이션
|
||||
public GameObject smallFirePrefab;
|
||||
private MonsterBase _monsterBase;
|
||||
private float _hitStunDuration = 0.5f; // 피격 경직 시간
|
||||
private float _hitStartTime;
|
||||
|
||||
|
||||
public void Enter(MonsterBase monsterBase)
|
||||
{
|
||||
_monsterBase = monsterBase;
|
||||
_hitStartTime = Time.time;
|
||||
_monsterBase.ChangeAnimationState(AniHit);
|
||||
|
||||
Debug.Log($"피격 상태 진입! 현재 HP: {_monsterBase.currentHP}");
|
||||
smallFirePrefab = _monsterBase.smallFirePrefab;
|
||||
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// 피격 경직 시간이 지나면 이전 상태로 복귀
|
||||
if (Time.time >= _hitStartTime + _hitStunDuration)
|
||||
{
|
||||
// HP가 0 이하면 죽음 상태로 전환
|
||||
if (_monsterBase.currentHP <= 0)
|
||||
{
|
||||
_monsterBase.SetTransition(new DeadState());
|
||||
}
|
||||
else
|
||||
{
|
||||
// 플레이어가 공격 범위 내에 있으면 공격 상태로, 아니면 추적 상태로
|
||||
var target = GameObject.FindGameObjectWithTag("Player");
|
||||
if (target != null)
|
||||
{
|
||||
float distanceToTarget =
|
||||
Vector2.Distance(_monsterBase.transform.position, target.transform.position);
|
||||
if (distanceToTarget <= _monsterBase.attackRange)
|
||||
{
|
||||
_monsterBase.SetTransition(new AttackState());
|
||||
}
|
||||
else
|
||||
{
|
||||
_monsterBase.SetTransition(new ChasingState());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_monsterBase.ChangeAnimationState(AniAttack);
|
||||
|
||||
}
|
||||
|
||||
public void Exit()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void CheckTransition()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class DeadState : State
|
||||
{
|
||||
private const string AniDead = "Death"; // 죽음 애니메이션
|
||||
private MonsterBase _monsterBase;
|
||||
private float _deathAnimationDuration = 1f; // 죽음 애니메이션 재생 시간
|
||||
private float _deathStartTime;
|
||||
private bool _hasTriggeredDeath = false;
|
||||
|
||||
public void Enter(MonsterBase monsterBase)
|
||||
{
|
||||
_monsterBase = monsterBase;
|
||||
_deathStartTime = Time.time;
|
||||
_monsterBase.ChangeAnimationState(AniDead);
|
||||
|
||||
// 죽음 처리 시작
|
||||
StartDeathProcess();
|
||||
}
|
||||
|
||||
private void StartDeathProcess()
|
||||
{
|
||||
if (_hasTriggeredDeath) return;
|
||||
|
||||
_hasTriggeredDeath = true;
|
||||
|
||||
// 콜라이더 비활성화
|
||||
if (_monsterBase.GetComponent<Collider2D>() != null)
|
||||
{
|
||||
_monsterBase.GetComponent<Collider2D>().enabled = false;
|
||||
}
|
||||
|
||||
Debug.Log($"몬스터 사망: {_monsterBase.name}");
|
||||
|
||||
// 여기에 경험치 드롭, 아이템 드롭 등의 로직 추가 가능
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// 죽음 애니메이션이 끝나면 오브젝트 제거
|
||||
if (Time.time >= _deathStartTime + _deathAnimationDuration)
|
||||
{
|
||||
GameObject.Destroy(_monsterBase.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void Exit()
|
||||
{
|
||||
}
|
||||
|
||||
public void CheckTransition()
|
||||
{
|
||||
// 죽음 상태에서는 다른 상태로 전환되지 않음
|
||||
}
|
||||
}
|
||||
//
|
||||
// public class HitState : State
|
||||
// {
|
||||
// private const string AniHit = "Hit"; // 피격 애니메이션
|
||||
// private MonsterBase _monsterBase;
|
||||
// private float _hitStunDuration = 0.5f; // 피격 경직 시간
|
||||
// private float _hitStartTime;
|
||||
//
|
||||
// public void Enter(MonsterBase monsterBase)
|
||||
// {
|
||||
// _monsterBase = monsterBase;
|
||||
// _hitStartTime = Time.time;
|
||||
// _monsterBase.ChangeAnimationState(AniHit);
|
||||
//
|
||||
// Debug.Log($"피격 상태 진입! 현재 HP: {_monsterBase.currentHP}");
|
||||
// }
|
||||
//
|
||||
// public void Update()
|
||||
// {
|
||||
// // 피격 경직 시간이 지나면 이전 상태로 복귀
|
||||
// if (Time.time >= _hitStartTime + _hitStunDuration)
|
||||
// {
|
||||
// // HP가 0 이하면 죽음 상태로 전환
|
||||
// if (_monsterBase.currentHP <= 0)
|
||||
// {
|
||||
// _monsterBase.SetTransition(new DeadState());
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // 플레이어가 공격 범위 내에 있으면 공격 상태로, 아니면 추적 상태로
|
||||
// var target = GameObject.FindGameObjectWithTag("Player");
|
||||
// if (target != null)
|
||||
// {
|
||||
// float distanceToTarget =
|
||||
// Vector2.Distance(_monsterBase.transform.position, target.transform.position);
|
||||
// if (distanceToTarget <= _monsterBase.attackRange)
|
||||
// {
|
||||
// _monsterBase.SetTransition(new AttackState());
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// _monsterBase.SetTransition(new ChasingState());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void Exit()
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// public void CheckTransition()
|
||||
// {
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public class DeadState : State
|
||||
// {
|
||||
// private const string AniDead = "Death"; // 죽음 애니메이션
|
||||
// private MonsterBase _monsterBase;
|
||||
// private float _deathAnimationDuration = 1f; // 죽음 애니메이션 재생 시간
|
||||
// private float _deathStartTime;
|
||||
// private bool _hasTriggeredDeath = false;
|
||||
//
|
||||
// public void Enter(MonsterBase monsterBase)
|
||||
// {
|
||||
// _monsterBase = monsterBase;
|
||||
// _deathStartTime = Time.time;
|
||||
// _monsterBase.ChangeAnimationState(AniDead);
|
||||
//
|
||||
// // 죽음 처리 시작
|
||||
// StartDeathProcess();
|
||||
// }
|
||||
//
|
||||
// private void StartDeathProcess()
|
||||
// {
|
||||
// if (_hasTriggeredDeath) return;
|
||||
//
|
||||
// _hasTriggeredDeath = true;
|
||||
//
|
||||
// // 콜라이더 비활성화
|
||||
// if (_monsterBase.GetComponent<Collider2D>() != null)
|
||||
// {
|
||||
// _monsterBase.GetComponent<Collider2D>().enabled = false;
|
||||
// }
|
||||
//
|
||||
// Debug.Log($"몬스터 사망: {_monsterBase.name}");
|
||||
//
|
||||
// // 여기에 경험치 드롭, 아이템 드롭 등의 로직 추가 가능
|
||||
// }
|
||||
//
|
||||
// public void Update()
|
||||
// {
|
||||
// // 죽음 애니메이션이 끝나면 오브젝트 제거
|
||||
// if (Time.time >= _deathStartTime + _deathAnimationDuration)
|
||||
// {
|
||||
// GameObject.Destroy(_monsterBase.gameObject);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void Exit()
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// public void CheckTransition()
|
||||
// {
|
||||
// // 죽음 상태에서는 다른 상태로 전환되지 않음
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user