몬스터 코드 구조 변경
This commit is contained in:
@@ -13,7 +13,8 @@ namespace TON
|
|||||||
{
|
{
|
||||||
if (other.CompareTag("Player"))
|
if (other.CompareTag("Player"))
|
||||||
{
|
{
|
||||||
_monsterBase.Attack(other.gameObject);
|
_monsterBase.SetTransition(new AttackState());
|
||||||
|
// _monsterBase.Attack(other.gameObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ namespace TON
|
|||||||
{
|
{
|
||||||
if (other.CompareTag("Player"))
|
if (other.CompareTag("Player"))
|
||||||
{
|
{
|
||||||
// TODO : 플레이어 감지하면 따라가기
|
// 플레이어 감지하면 따라가기
|
||||||
|
_monsterBase.SetTransition(new ChasingState());
|
||||||
_monsterBase.IsDetect = true;
|
_monsterBase.IsDetect = true;
|
||||||
Debug.Log("감지됨");
|
Debug.Log("감지됨");
|
||||||
|
|
||||||
@@ -29,22 +29,15 @@ namespace TON
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 단순 플레이어 태그 기준 따라가는 코드
|
|
||||||
private void FixedUpdate()
|
|
||||||
{
|
|
||||||
// 타겟의 위치에서 내 현제 위치를 뺌
|
|
||||||
// UnityEngine.Vector2 direction = target.transform.position - transform.position;
|
|
||||||
|
|
||||||
// 방향 * 속도 * 시간간격
|
|
||||||
// transform.Translate(direction.normalized * speed * Time.deltaTime);
|
|
||||||
// animator.SetBool("Iidle", true);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnTriggerExit2D(Collider2D other)
|
private void OnTriggerExit2D(Collider2D other)
|
||||||
{
|
{
|
||||||
_monsterBase.IsDetect = false;
|
_monsterBase.SetTransition(new IdleState());
|
||||||
_monsterBase.IsWalking = true;
|
|
||||||
|
// _monsterBase.IsDetect = false;
|
||||||
|
// _monsterBase.IsWalking = true;
|
||||||
|
// _monsterBase.ResetTime();
|
||||||
|
// _monsterBase.ChangeAnimationState(MonsterBase.AniIdle);
|
||||||
|
Debug.Log("감지 벗어남");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,8 +23,7 @@ namespace TON
|
|||||||
// public int damage; // 공격력
|
// public int damage; // 공격력
|
||||||
public float speed = 2; // 몬스터의 이동 속도
|
public float speed = 2; // 몬스터의 이동 속도
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField] private SpriteRenderer _spriteRenderer; // 몬스터의 스프라이트 렌더러
|
||||||
private SpriteRenderer _spriteRenderer; // 몬스터의 스프라이트 렌더러
|
|
||||||
private Animator _animator; // 몬스터 애니메이터
|
private Animator _animator; // 몬스터 애니메이터
|
||||||
|
|
||||||
private Vector3 _direction; // 몬스터의 이동 방향
|
private Vector3 _direction; // 몬스터의 이동 방향
|
||||||
@@ -33,27 +32,31 @@ namespace TON
|
|||||||
private bool _isDetect; // 몬스터가 대상을 인식했는지 여부
|
private bool _isDetect; // 몬스터가 대상을 인식했는지 여부
|
||||||
private float _currentTime; // 현재 시간
|
private float _currentTime; // 현재 시간
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField] private int _idleTime = 2; // 대기 시간
|
||||||
private int _idleTime = 2; // 대기 시간
|
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField] private int walkingTime = 2; // 걷기 시간
|
||||||
private int walkingTime = 2; // 걷기 시간
|
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField] private GameObject _target; // 몬스터의 타겟
|
||||||
private GameObject _target; // 몬스터의 타겟
|
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField] private Collider2D _collider; // 몬스터의 콜라이더
|
||||||
private Collider2D _collider; // 몬스터의 콜라이더
|
|
||||||
|
|
||||||
public float defencePower; // 몬스터의 방어력
|
public float defencePower; // 몬스터의 방어력
|
||||||
|
|
||||||
// 애니메이션 관련 선언
|
// 애니메이션 관련 선언
|
||||||
private string currentState; // 현재 애니메이션 상태
|
private string currentState; // 현재 애니메이션 상태
|
||||||
|
|
||||||
const string AniIdle = "Idle"; // 대기 애니메이션
|
|
||||||
const string AniWalk = "Walk"; // 걷기 애니메이션
|
|
||||||
const string AniAttack = "Attack"; // 공격 애니메이션
|
const string AniAttack = "Attack"; // 공격 애니메이션
|
||||||
|
|
||||||
|
StateMachine _stateMachine;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 추적 관련 선언
|
||||||
|
private float _detectStartTime; // 추적 시작 시간
|
||||||
|
private bool _isTracking; // 추적 중인지 여부
|
||||||
|
private const float DETECT_DURATION = 5f; // 추적 지속 시간
|
||||||
|
|
||||||
public bool IsWalking
|
public bool IsWalking
|
||||||
{
|
{
|
||||||
get => _isWalking;
|
get => _isWalking;
|
||||||
@@ -67,16 +70,17 @@ namespace TON
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 첫 번째 프레임 전에 호출됩니다.
|
// 첫 번째 프레임 전에 호출됩니다.
|
||||||
void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
|
_animator = GetComponent<Animator>(); // 애니메이터 컴포넌트 초기화
|
||||||
|
|
||||||
|
_stateMachine = new StateMachine(new IdleState(), this);
|
||||||
// 몬스터 데이터 로드 (테스트용, 첫 번째 몬스터 데이터만 로드)
|
// 몬스터 데이터 로드 (테스트용, 첫 번째 몬스터 데이터만 로드)
|
||||||
MonsterData monsterData = MonsterDataManager.Singleton.monstersData[0];
|
MonsterData monsterData = MonsterDataManager.Singleton.monstersData[0];
|
||||||
Debug.Log(monsterData.name); // 몬스터 ID 출력
|
Debug.Log(monsterData.name); // 몬스터 ID 출력
|
||||||
|
|
||||||
_currentTime = Time.realtimeSinceStartup; // 시작 시간 기록
|
_currentTime = Time.realtimeSinceStartup; // 시작 시간 기록
|
||||||
|
|
||||||
_animator = GetComponent<Animator>(); // 애니메이터 컴포넌트 초기화
|
|
||||||
|
|
||||||
_direction = new Vector3(1, 0, 0); // 초기 이동 방향 (x 축 양의 방향)
|
_direction = new Vector3(1, 0, 0); // 초기 이동 방향 (x 축 양의 방향)
|
||||||
|
|
||||||
_spriteRenderer.flipX = !(_direction.x > 0); // 이동 방향에 따라 스프라이트 플립
|
_spriteRenderer.flipX = !(_direction.x > 0); // 이동 방향에 따라 스프라이트 플립
|
||||||
@@ -88,7 +92,7 @@ namespace TON
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 애니메이션 상태를 변경하는 메서드
|
// 애니메이션 상태를 변경하는 메서드
|
||||||
void ChangeAnimationState(string newState)
|
public void ChangeAnimationState(string newState)
|
||||||
{
|
{
|
||||||
// 현재 상태와 동일한 상태일 경우, 애니메이션을 변경하지 않음
|
// 현재 상태와 동일한 상태일 경우, 애니메이션을 변경하지 않음
|
||||||
if (currentState == newState) return;
|
if (currentState == newState) return;
|
||||||
@@ -97,57 +101,45 @@ namespace TON
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 매 프레임 호출됩니다.
|
// 매 프레임 호출됩니다.
|
||||||
void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
// TODO: 시야를 벗어났으면 idle 상태로 전환하는 기능 추가 예정
|
_stateMachine.Update();
|
||||||
|
|
||||||
// 몬스터가 걷고 있는 경우
|
// // TODO: 시야를 벗어났으면 idle 상태로 전환하는 기능 추가 예정
|
||||||
if (_isWalking)
|
//
|
||||||
{
|
// // 몬스터가 걷고 있는 경우
|
||||||
transform.Translate(_direction * speed * Time.deltaTime); // 몬스터를 이동시킴
|
// if (_isWalking)
|
||||||
|
// {
|
||||||
// 걷기 시간을 초과하면 대기 상태로 전환
|
// transform.Translate(_direction * speed * Time.deltaTime); // 몬스터를 이동시킴
|
||||||
if (Time.realtimeSinceStartup - _currentTime >= walkingTime)
|
//
|
||||||
{
|
// // 걷기 시간을 초과하면 대기 상태로 전환
|
||||||
_isWalking = false;
|
// if (Time.realtimeSinceStartup - _currentTime >= walkingTime)
|
||||||
ChangeAnimationState(AniIdle); // 애니메이션을 Idle로 변경
|
// {
|
||||||
_currentTime = Time.realtimeSinceStartup; // 시간 갱신
|
// _isWalking = false;
|
||||||
}
|
// ChangeAnimationState(AniIdle); // 애니메이션을 Idle로 변경
|
||||||
}
|
// _currentTime = Time.realtimeSinceStartup; // 시간 갱신
|
||||||
else
|
// }
|
||||||
{
|
// }
|
||||||
// 대기 시간이 초과되면 걷기 시작
|
// else
|
||||||
if (Time.realtimeSinceStartup - _currentTime >= _idleTime)
|
// {
|
||||||
{
|
//
|
||||||
_currentTime = Time.realtimeSinceStartup;
|
// }
|
||||||
|
//
|
||||||
// 걷기 상태가 아니라면 방향을 반대로 바꿔서 걷기 시작
|
// // 대상을 인식하고 있으면 공격 또는 추적 처리
|
||||||
if (_isWalking == false)
|
// if (_isDetect && _target != null)
|
||||||
{
|
// {
|
||||||
_direction *= -1; // 이동 방향 반전
|
// // 몬스터와 타겟 간의 거리 계산
|
||||||
_spriteRenderer.flipX = !(_direction.x > 0); // 스프라이트 방향 반전
|
// float distance = Vector3.Distance(transform.position, _target.transform.position);
|
||||||
}
|
//
|
||||||
|
// if (distance < 1.5f) // 일정 거리 이내에서 공격
|
||||||
_isWalking = true;
|
// {
|
||||||
ChangeAnimationState(AniWalk); // 애니메이션을 Walk로 변경
|
// Attack(_target);
|
||||||
}
|
// }
|
||||||
}
|
// else
|
||||||
|
// {
|
||||||
// 대상을 인식하고 있으면 공격 또는 추적 처리
|
// Detect(_target); // 타겟이 멀어지면 추적
|
||||||
if (_isDetect && _target != null)
|
// }
|
||||||
{
|
// }
|
||||||
// 몬스터와 타겟 간의 거리 계산
|
|
||||||
float distance = Vector3.Distance(transform.position, _target.transform.position);
|
|
||||||
|
|
||||||
if (distance < 1.5f) // 일정 거리 이내에서 공격
|
|
||||||
{
|
|
||||||
Attack(_target);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Detect(_target); // 타겟이 멀어지면 추적
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 피해를 적용하는 메서드
|
// 피해를 적용하는 메서드
|
||||||
@@ -189,18 +181,53 @@ namespace TON
|
|||||||
// 타겟을 추적하는 메서드
|
// 타겟을 추적하는 메서드
|
||||||
public void Detect(GameObject target)
|
public void Detect(GameObject target)
|
||||||
{
|
{
|
||||||
if (target == null) return; // 타겟이 없으면 리턴
|
if (target == null)
|
||||||
|
return; // 타겟이 없으면 리턴
|
||||||
|
|
||||||
|
// 추적 시작 시 시간 기록
|
||||||
|
if (!_isTracking)
|
||||||
|
{
|
||||||
|
_detectStartTime = Time.time;
|
||||||
|
_isTracking = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5초가 지났는지 확인
|
||||||
|
if (Time.time - _detectStartTime >= DETECT_DURATION)
|
||||||
|
{
|
||||||
|
_isTracking = false;
|
||||||
|
_isDetect = false; // 추적 상태 해제
|
||||||
|
// ChangeAnimationState(AniIdle); // 대기 상태로 전환
|
||||||
|
_spriteRenderer.flipX = !(_direction.x > 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
UnityEngine.Vector2 direction = target.transform.position - transform.position; // 타겟과의 방향 계산
|
UnityEngine.Vector2 direction = target.transform.position - transform.position; // 타겟과의 방향 계산
|
||||||
|
|
||||||
if (direction.magnitude > 0) // 타겟이 존재하면 이동
|
if (direction.magnitude > 0) // 타겟이 존재하면 이동
|
||||||
{
|
{
|
||||||
transform.Translate(direction.normalized * speed * Time.deltaTime); // 타겟 방향으로 이동
|
transform.Translate(direction.normalized * speed * Time.deltaTime); // 타겟 방향으로 이동
|
||||||
ChangeAnimationState(AniWalk); // 걷기 애니메이션으로 변경
|
// ChangeAnimationState(AniWalk); // 걷기 애니메이션으로 변경
|
||||||
|
|
||||||
// 타겟 방향에 따라 스프라이트 방향 즉시 변경
|
// 타겟 방향에 따라 스프라이트 방향 즉시 변경
|
||||||
_spriteRenderer.flipX = direction.x < 0; // 타겟이 왼쪽에 있으면 반전, 오른쪽에 있으면 정방향
|
_spriteRenderer.flipX = direction.x < 0; // 타겟이 왼쪽에 있으면 반전, 오른쪽에 있으면 정방향
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetOppositionDirection()
|
||||||
|
{
|
||||||
|
_direction *= -1; // 이동 방향 반전
|
||||||
|
_spriteRenderer.flipX = !(_direction.x > 0); // 스프라이트 방향 반전
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Move()
|
||||||
|
{
|
||||||
|
transform.Translate(_direction * speed * Time.deltaTime); // 몬스터를 이동시킴
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetTransition(State state)
|
||||||
|
{
|
||||||
|
_stateMachine.SetTransition(state);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
159
Gameton-06/Assets/Gameton/Scripts/Monster/StateMachine.cs
Normal file
159
Gameton-06/Assets/Gameton/Scripts/Monster/StateMachine.cs
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEditor.UI;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace TON
|
||||||
|
{
|
||||||
|
public class StateMachine
|
||||||
|
{
|
||||||
|
private Animator animator;
|
||||||
|
private State _state;
|
||||||
|
private MonsterBase _monsterBase;
|
||||||
|
|
||||||
|
public StateMachine(State state, MonsterBase monsterBase)
|
||||||
|
{
|
||||||
|
// 초기 상태 객체 생성
|
||||||
|
_monsterBase = monsterBase;;
|
||||||
|
|
||||||
|
_state = state;
|
||||||
|
_state.Enter(_monsterBase);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
_state.Update();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetTransition(State state)
|
||||||
|
{
|
||||||
|
// 다음음 상태로 전환
|
||||||
|
_state.Exit();
|
||||||
|
|
||||||
|
_state = state;
|
||||||
|
_state.Enter(_monsterBase);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface State
|
||||||
|
{
|
||||||
|
void Enter(MonsterBase _monsterBase);
|
||||||
|
|
||||||
|
void Update();
|
||||||
|
|
||||||
|
void Exit();
|
||||||
|
void CheckTransition();
|
||||||
|
// TODO : 트리거 조건일 경우 다음 상태로 전환
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class IdleState : State
|
||||||
|
{
|
||||||
|
private const string AniIdle = "Idle"; // 대기 애니메이션
|
||||||
|
private const string AniWalk = "Walk"; // 걷기 애니메이션
|
||||||
|
|
||||||
|
private MonsterBase _monsterBase;
|
||||||
|
private float _currentTime;
|
||||||
|
private float _idleTime = 2;
|
||||||
|
|
||||||
|
private bool _isWalking;
|
||||||
|
private int _walkingTime = 2; // 걷기 시간
|
||||||
|
|
||||||
|
public void Enter(MonsterBase monsterBase)
|
||||||
|
{
|
||||||
|
_monsterBase = monsterBase;
|
||||||
|
_currentTime = Time.realtimeSinceStartup;
|
||||||
|
_monsterBase.ChangeAnimationState(AniIdle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
// Idle
|
||||||
|
if (_isWalking == false)
|
||||||
|
{
|
||||||
|
// 대기 시간이 초과되면 걷기 시작
|
||||||
|
if (Time.realtimeSinceStartup - _currentTime >= _idleTime)
|
||||||
|
{
|
||||||
|
_currentTime = Time.realtimeSinceStartup;
|
||||||
|
|
||||||
|
// 걷기 상태가 아니라면 방향을 반대로 바꿔서 걷기 시작
|
||||||
|
_monsterBase.SetOppositionDirection();
|
||||||
|
_monsterBase.ChangeAnimationState(AniWalk);
|
||||||
|
|
||||||
|
_isWalking = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Walking
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_monsterBase.Move(); // 몬스터를 이동시킴
|
||||||
|
|
||||||
|
// 대기 시간이 초과되면 걷기 시작
|
||||||
|
if (Time.realtimeSinceStartup - _currentTime >= _walkingTime)
|
||||||
|
{
|
||||||
|
_currentTime = Time.realtimeSinceStartup;
|
||||||
|
|
||||||
|
_isWalking = false;
|
||||||
|
_monsterBase.ChangeAnimationState(AniIdle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Exit()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CheckTransition()
|
||||||
|
{
|
||||||
|
// TODO : 데미지 받을 때
|
||||||
|
// TODO : 추적 범위에 들어왔을 때
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ChasingState : State
|
||||||
|
{
|
||||||
|
public void Enter(MonsterBase _monsterBase)
|
||||||
|
{
|
||||||
|
// 추적 상태 초기화
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
// 추적
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Exit()
|
||||||
|
{
|
||||||
|
// 추적 끝났을때
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CheckTransition()
|
||||||
|
{
|
||||||
|
// Idle로 변경
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AttackState : State
|
||||||
|
{
|
||||||
|
public void Enter(MonsterBase _monsterBase)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Exit()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CheckTransition()
|
||||||
|
{
|
||||||
|
// 공격이 끝나면 다음 상태 전환
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: aa8c61fbcbdd4654b9e6e8c259811599
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Reference in New Issue
Block a user