Files
Knight_Hollow/Assets/Scripts/Player/PlayerMove.cs
2025-08-22 20:33:29 +09:00

69 lines
2.0 KiB
C#

using System;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
// 컴포넌트 프로퍼티
public Rigidbody2D RigidBody { get; private set; }
public SpriteRenderer SpriteRenderer { get; private set; }
public Animator Animator { get; private set; }
public float maxSpeed;
public float jumpPower;
// 변수
[SerializeField]
public int hp = 5;
private StateMachine _stateMachine;
// 공격 콜라이더
public Collider2D attackCollider;
// 사용하는 컴포넌트들 초기화
void Awake()
{
// 중복 변수 제거하고 public 프로퍼티에 직접 할당
RigidBody = GetComponent<Rigidbody2D>();
SpriteRenderer = GetComponent<SpriteRenderer>();
Animator = GetComponent<Animator>();
// Null 체크 추가
if (RigidBody == null || SpriteRenderer == null || Animator == null)
{
Debug.LogError("PlayerMove script requires Rigidbody2D, SpriteRenderer, and Animator components on the same GameObject.");
// 게임 오브젝트 비활성화 또는 스크립트 비활성화
enabled = false;
return;
}
_stateMachine = new StateMachine(State.IDLE, this);
// 공격 콜라이더 참조
attackCollider = GetComponentInChildren<Collider2D>();
}
private void Update()
{
_stateMachine.Update();
}
void FixedUpdate()
{
// FixedUpdate 로직은 이제 사용하지 않습니다.
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
// 중복 호출 제거, 피격 위치를 전달하는 메서드만 사용
_stateMachine.SetTransition(State.Damaged, collision.transform.position);
}
}
// 플레이어 오브젝트를 파괴하는 메서드
public void Die()
{
Destroy(gameObject);
}
}