2026-01-21 12:37:37 +00:00
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using TreeEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
|
|
public class PlayerController2 : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private Rigidbody2D rbody;
|
|
|
|
|
private Vector2 moveInput;
|
|
|
|
|
[SerializeField] private float speed = 3.0f; // 이동 속도
|
|
|
|
|
[SerializeField] private float dashspeed = 5.4f;
|
|
|
|
|
[SerializeField] private float jumpForce = 5.0f; // 점프력
|
|
|
|
|
|
|
|
|
|
private float axisH = 0.0f; // 좌우 입력 값 저장
|
|
|
|
|
bool isDash = false; // 대시 플래그
|
|
|
|
|
bool goJump = false; // 점프 개시 플래그
|
|
|
|
|
bool isDead = false; // 사망 플래그
|
2026-01-23 08:56:21 +00:00
|
|
|
bool issAtk = false; // 약공 플래그
|
|
|
|
|
bool isaAtk = false; // 강공 플래그
|
2026-01-21 12:37:37 +00:00
|
|
|
bool onGround = false; // 지면에 서 있는 플래그
|
|
|
|
|
public LayerMask groundLayer; // 착지할 수 있는 레이어
|
|
|
|
|
|
|
|
|
|
private Animator animator;
|
2026-01-26 08:52:26 +00:00
|
|
|
private PlayerAttacks playerAttacks;
|
2026-01-21 12:37:37 +00:00
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
//Rigidbody2D 가져오기
|
|
|
|
|
rbody = GetComponent<Rigidbody2D>();
|
|
|
|
|
//Animator 가져오기
|
|
|
|
|
animator = GetComponent<Animator>();
|
2026-01-26 08:52:26 +00:00
|
|
|
playerAttacks = GetComponent<PlayerAttacks>();
|
2026-01-21 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
void OnMove(InputValue value)
|
|
|
|
|
{
|
|
|
|
|
// Vector2 값 중 x축(좌우) 값만 가져옴
|
|
|
|
|
Vector2 inputVector = value.Get<Vector2>();
|
|
|
|
|
axisH = inputVector.x;
|
|
|
|
|
}
|
|
|
|
|
void OnDash(InputValue value)
|
|
|
|
|
{
|
|
|
|
|
isDash = value.isPressed;
|
|
|
|
|
|
|
|
|
|
if (isDash) Debug.Log("대시 중...");
|
|
|
|
|
else Debug.Log("대시 중단");
|
|
|
|
|
}
|
|
|
|
|
public void OnJump(InputValue value)
|
|
|
|
|
{
|
|
|
|
|
// 버튼이 눌린 순간(isPressed)이고 지면 위라면 점프 예약
|
|
|
|
|
if (value.isPressed && onGround)
|
|
|
|
|
{
|
|
|
|
|
goJump = true;
|
|
|
|
|
animator.SetBool("jump", true);
|
|
|
|
|
Debug.Log(" 점프 버튼 눌림! ");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-26 08:52:26 +00:00
|
|
|
|
|
|
|
|
public void OnSAtk(InputValue value)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log(" s 버튼 눌림! ");
|
|
|
|
|
if (value.isPressed && !playerAttacks.weaponInfos[0].isUse)
|
|
|
|
|
{
|
|
|
|
|
playerAttacks.weaponInfos[0].isUse = true;
|
|
|
|
|
StartCoroutine(playerAttacks.ableAtk(0));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnAAtk(InputValue value)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log(" a 버튼 눌림! ");
|
|
|
|
|
if (value.isPressed && !playerAttacks.weaponInfos[1].isUse)
|
|
|
|
|
{
|
|
|
|
|
playerAttacks.weaponInfos[1].isUse = true;
|
|
|
|
|
StartCoroutine(playerAttacks.ableAtk(1));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-01-21 12:37:37 +00:00
|
|
|
void Update()
|
|
|
|
|
{
|
2026-01-22 13:08:10 +00:00
|
|
|
if (isDead) return;
|
2026-01-21 12:37:37 +00:00
|
|
|
|
2026-01-22 13:08:10 +00:00
|
|
|
bool isMoving = axisH != 0;
|
|
|
|
|
// 지면 상태 확인
|
2026-01-21 12:37:37 +00:00
|
|
|
animator.SetBool("jump", !onGround);
|
2026-01-23 08:56:21 +00:00
|
|
|
|
2026-01-22 13:08:10 +00:00
|
|
|
animator.SetBool("Dash", isDash);
|
|
|
|
|
animator.SetBool("Move", isMoving);
|
|
|
|
|
|
2026-01-23 08:56:21 +00:00
|
|
|
// 플래그 초기화
|
|
|
|
|
if (Keyboard.current.leftShiftKey.wasReleasedThisFrame) isDash = false;
|
2026-01-26 08:52:26 +00:00
|
|
|
//if (Keyboard.current.sKey.wasReleasedThisFrame) issAtk = false;
|
|
|
|
|
//if (Keyboard.current.aKey.wasReleasedThisFrame) isaAtk = false;
|
2026-01-21 12:37:37 +00:00
|
|
|
|
|
|
|
|
// 방향 조절
|
2026-01-22 13:08:10 +00:00
|
|
|
if (isMoving)
|
2026-01-21 12:37:37 +00:00
|
|
|
{
|
|
|
|
|
float direction = axisH > 0 ? 1 : -1;
|
|
|
|
|
transform.localScale = new Vector2(direction, 1);
|
|
|
|
|
}
|
2026-01-22 13:08:10 +00:00
|
|
|
|
|
|
|
|
else animator.SetBool("Dash", false);
|
2026-01-21 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
|
{
|
|
|
|
|
onGround = Physics2D.Linecast(transform.position,
|
|
|
|
|
transform.position - (transform.up * 0.1f),
|
|
|
|
|
groundLayer);
|
|
|
|
|
|
2026-01-22 13:08:10 +00:00
|
|
|
if (isDead) return;
|
2026-01-21 12:37:37 +00:00
|
|
|
|
2026-01-26 08:52:26 +00:00
|
|
|
// 공격중인지 확인
|
|
|
|
|
if (playerAttacks.weaponInfos[0].isUse)
|
|
|
|
|
{
|
|
|
|
|
rbody.linearVelocity = new Vector2(0, rbody.linearVelocity.y); // 정지
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 12:37:37 +00:00
|
|
|
float currentSpeed = (isDash && onGround) ? dashspeed : speed;
|
|
|
|
|
rbody.linearVelocity = new Vector2(axisH * currentSpeed, rbody.linearVelocity.y);
|
|
|
|
|
|
|
|
|
|
if (goJump)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("점프!");
|
|
|
|
|
rbody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
|
|
|
|
|
goJump = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void OnTriggerEnter2D(Collider2D collision)
|
|
|
|
|
{
|
|
|
|
|
if (collision.CompareTag("Goal"))
|
|
|
|
|
{
|
|
|
|
|
GameStop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (collision.CompareTag("Dead"))
|
|
|
|
|
{
|
|
|
|
|
GameOver();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GameStop()
|
|
|
|
|
{
|
|
|
|
|
rbody.linearVelocity = Vector2.zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GameOver()
|
|
|
|
|
{
|
|
|
|
|
isDead = true;
|
|
|
|
|
animator.SetBool("Move", false);
|
|
|
|
|
GameStop();
|
|
|
|
|
}
|
|
|
|
|
}
|