71 lines
2.0 KiB
C#
71 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; }
|
|
|
|
// 다른 클래스에서 접근할 수 있도록 private static 변수와 public 프로퍼티를 분리
|
|
private static int _isJumping;
|
|
private static int _isMoving;
|
|
|
|
public static int IsJumping => _isJumping;
|
|
public static int IsMoving => _isMoving;
|
|
|
|
public float maxSpeed;
|
|
public float jumpPower;
|
|
|
|
// 변수
|
|
[SerializeField]
|
|
public int hp = 5;
|
|
|
|
private StateMachine _stateMachine;
|
|
|
|
// 사용하는 컴포넌트들 초기화
|
|
void Awake()
|
|
{
|
|
// 중복 변수 제거하고 public 프로퍼티에 직접 할당
|
|
RigidBody = GetComponent<Rigidbody2D>();
|
|
SpriteRenderer = GetComponent<SpriteRenderer>();
|
|
Animator = GetComponent<Animator>();
|
|
|
|
// 2. Animator가 초기화된 후에 해시 값 할당
|
|
_isJumping = Animator.StringToHash("isJumping");
|
|
_isMoving = Animator.StringToHash("isMoving");
|
|
|
|
_stateMachine = new StateMachine(new IdleState(this));
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_stateMachine.Update();
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
_stateMachine.FixedUpdate();
|
|
}
|
|
|
|
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()로 이동
|
|
}
|
|
} |