2026-04-14 17:06:58 +09:00
|
|
|
using UnityEngine;
|
2026-04-22 13:56:03 +09:00
|
|
|
using System.Collections;
|
2026-04-14 17:06:58 +09:00
|
|
|
|
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
|
|
|
{
|
2026-04-22 13:56:03 +09:00
|
|
|
[Header("Movement")]
|
2026-04-14 17:06:58 +09:00
|
|
|
public float speed = 8f;
|
2026-04-22 13:56:03 +09:00
|
|
|
private Rigidbody playerRigidbody;
|
|
|
|
|
public VirtualJoystick joystick;
|
2026-04-14 17:06:58 +09:00
|
|
|
|
2026-04-22 13:56:03 +09:00
|
|
|
[Header("Shield System")]
|
2026-04-21 17:45:51 +09:00
|
|
|
public int shieldCount = 3;
|
|
|
|
|
private bool isShieldActive = false;
|
|
|
|
|
public GameObject shieldVisual;
|
2026-04-22 13:56:03 +09:00
|
|
|
public ShieldUIHandler shieldUI; // UI 전용 스크립트 참조
|
2026-04-21 17:45:51 +09:00
|
|
|
|
2026-04-22 13:56:03 +09:00
|
|
|
void Start()
|
2026-04-14 17:06:58 +09:00
|
|
|
{
|
2026-04-22 13:56:03 +09:00
|
|
|
playerRigidbody = GetComponent<Rigidbody>();
|
|
|
|
|
|
|
|
|
|
// 초기 UI 상태 반영
|
|
|
|
|
if (shieldUI != null) shieldUI.UpdateShieldUI(shieldCount);
|
2026-04-14 17:06:58 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-22 13:56:03 +09:00
|
|
|
void Update()
|
2026-04-14 17:06:58 +09:00
|
|
|
{
|
2026-04-22 13:56:03 +09:00
|
|
|
HandleMovement();
|
2026-04-21 17:45:51 +09:00
|
|
|
|
2026-04-22 13:56:03 +09:00
|
|
|
// PC 입력 처리 (모바일 아닐 때만)
|
|
|
|
|
if (!GameSettings.IsMobile && Input.GetKeyDown(KeyCode.Alpha1))
|
2026-04-21 17:45:51 +09:00
|
|
|
{
|
2026-04-22 13:56:03 +09:00
|
|
|
ActivateShieldLogic();
|
2026-04-21 17:45:51 +09:00
|
|
|
}
|
2026-04-14 17:06:58 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-22 13:56:03 +09:00
|
|
|
void HandleMovement()
|
2026-04-14 17:06:58 +09:00
|
|
|
{
|
2026-04-22 13:56:03 +09:00
|
|
|
float x = 0, z = 0;
|
2026-04-21 17:45:51 +09:00
|
|
|
|
|
|
|
|
if (GameSettings.IsMobile && joystick != null)
|
|
|
|
|
{
|
|
|
|
|
Vector2 input = joystick.GetInput();
|
2026-04-22 13:56:03 +09:00
|
|
|
x = input.x; z = input.y;
|
2026-04-21 17:45:51 +09:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
x = Input.GetAxis("Horizontal");
|
|
|
|
|
z = Input.GetAxis("Vertical");
|
|
|
|
|
}
|
2026-04-14 17:06:58 +09:00
|
|
|
|
2026-04-22 13:56:03 +09:00
|
|
|
playerRigidbody.linearVelocity = new Vector3(x * speed, 0f, z * speed);
|
2026-04-14 17:06:58 +09:00
|
|
|
|
2026-04-22 13:56:03 +09:00
|
|
|
if (x != 0 || z != 0)
|
2026-04-15 12:59:37 +09:00
|
|
|
{
|
2026-04-22 13:56:03 +09:00
|
|
|
transform.rotation = Quaternion.LookRotation(new Vector3(x, 0f, z));
|
2026-04-14 17:06:58 +09:00
|
|
|
}
|
2026-04-15 12:59:37 +09:00
|
|
|
|
2026-04-22 13:56:03 +09:00
|
|
|
// 애니메이션
|
2026-04-15 12:59:37 +09:00
|
|
|
Animator anim = GetComponentInChildren<Animator>();
|
2026-04-22 13:56:03 +09:00
|
|
|
if (anim != null) anim.SetBool("isMoving", (x != 0 || z != 0));
|
2026-04-14 17:06:58 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-21 17:45:51 +09:00
|
|
|
public void ActivateShieldLogic()
|
2026-04-14 17:06:58 +09:00
|
|
|
{
|
2026-04-21 17:45:51 +09:00
|
|
|
if (shieldCount > 0 && !isShieldActive)
|
2026-04-14 17:06:58 +09:00
|
|
|
{
|
2026-04-22 13:56:03 +09:00
|
|
|
StartCoroutine(ActivateShieldRoutine());
|
2026-04-21 17:45:51 +09:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-14 17:06:58 +09:00
|
|
|
|
2026-04-22 13:56:03 +09:00
|
|
|
private IEnumerator ActivateShieldRoutine()
|
2026-04-14 17:06:58 +09:00
|
|
|
{
|
2026-04-21 17:45:51 +09:00
|
|
|
isShieldActive = true;
|
|
|
|
|
shieldCount--;
|
2026-04-22 13:56:03 +09:00
|
|
|
|
|
|
|
|
// UI 업데이트 위임
|
|
|
|
|
if (shieldUI != null) shieldUI.UpdateShieldUI(shieldCount);
|
2026-04-21 17:45:51 +09:00
|
|
|
|
|
|
|
|
if (shieldVisual != null) shieldVisual.SetActive(true);
|
|
|
|
|
yield return new WaitForSeconds(2f);
|
|
|
|
|
if (shieldVisual != null) shieldVisual.SetActive(false);
|
2026-04-22 13:56:03 +09:00
|
|
|
|
2026-04-21 17:45:51 +09:00
|
|
|
isShieldActive = false;
|
2026-04-14 17:06:58 +09:00
|
|
|
}
|
2026-04-22 13:56:03 +09:00
|
|
|
|
|
|
|
|
public void Die()
|
|
|
|
|
{
|
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
FindFirstObjectByType<GameManager>()?.EndGame();
|
|
|
|
|
}
|
2026-04-21 17:45:51 +09:00
|
|
|
}
|