83 lines
2.4 KiB
C#
83 lines
2.4 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;
|
|
}
|
|
|
|
// 캐릭터의 기본 방향을 오른쪽으로 설정
|
|
Vector3 initialScale = transform.localScale;
|
|
initialScale.x = -1f;
|
|
transform.localScale = initialScale;
|
|
|
|
_stateMachine = new StateMachine(State.IDLE, this);
|
|
|
|
// AttackArea 게임 오브젝트를 찾고 그 자식에서 Collider2D를 가져옵니다.
|
|
Transform attackAreaTransform = transform.Find("AttackArea");
|
|
if (attackAreaTransform != null)
|
|
{
|
|
attackCollider = attackAreaTransform.GetComponent<Collider2D>();
|
|
}
|
|
|
|
if (attackCollider == null)
|
|
{
|
|
Debug.LogError("AttackArea child object or Collider2D component not found!");
|
|
}
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_stateMachine.Update();
|
|
}
|
|
|
|
void OnCollisionEnter2D(Collision2D collision)
|
|
{
|
|
if (collision.gameObject.CompareTag("Enemy"))
|
|
{
|
|
// 중복 호출 제거, 피격 위치를 전달하는 메서드만 사용
|
|
_stateMachine.SetTransition(State.Damaged, collision.transform.position);
|
|
}
|
|
}
|
|
|
|
// 플레이어 오브젝트를 파괴하는 메서드
|
|
public void Die()
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
} |