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(); SpriteRenderer = GetComponent(); Animator = GetComponent(); // 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; } // 캐릭터의 기본 방향을 오른쪽으로 설정 Vector3 initialScale = transform.localScale; initialScale.x = -1f; transform.localScale = initialScale; _stateMachine = new StateMachine(State.IDLE, this); // 공격 콜라이더 참조 attackCollider = GetComponentInChildren(); } void Start() { } 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); } }