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; // 사용하는 컴포넌트들 초기화 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; } _stateMachine = new StateMachine(State.IDLE, this); } private void Update() { _stateMachine.Update(); } void FixedUpdate() { float h = Input.GetAxisRaw("Horizontal"); RigidBody.AddForce(Vector2.right * h, ForceMode2D.Impulse); if (RigidBody.linearVelocity.x > maxSpeed) { RigidBody.linearVelocity = new Vector2(maxSpeed, RigidBody.linearVelocity.y); } else if (RigidBody.linearVelocity.x < -maxSpeed) { RigidBody.linearVelocity = new Vector2(-maxSpeed, RigidBody.linearVelocity.y); } } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Enemy")) { // FSM에 피격 로직 추가 // _stateMachine.SetTransition(new DamagedState(this, collision.transform.position)); } } void OnDamaged(Vector2 targetPosition) { // OnDamaged 로직은 DamagedState의 Enter()로 이동 // 기존 Invoke("OffDamaged", 1)는 DamagedState 내부에서 처리 } void OffDamaged() { // OffDamaged 로직은 DamagedState의 Exit()로 이동 } }