Files
WildRoot/Assets/Script/PlayerController.cs
T

182 lines
4.8 KiB
C#
Raw Normal View History

2026-04-14 17:06:58 +09:00
using UnityEngine;
using System.Collections;
2026-04-14 17:06:58 +09:00
public class PlayerController : MonoBehaviour
{
[Header("Movement")]
2026-04-14 17:06:58 +09:00
public float speed = 8f;
private Rigidbody playerRigidbody;
public VirtualJoystick joystick;
2026-04-14 17:06:58 +09:00
[Header("Shield System")]
public int shieldCount = 3;
private bool isShieldActive = false;
public GameObject shieldVisual;
2026-04-29 09:22:08 +09:00
public ShieldUIHandler shieldUI;
[Header("Shooting")]
public GameObject bulletPrefab;
public Transform firePoint;
public int maxAmmo = 6;
public float reloadTime = 1.5f;
private int currentAmmo;
private bool isReloading = false;
public ShootButtonUI shootButtonUI;
private Vector3 lastMoveDirection = Vector3.forward;
void Start()
2026-04-14 17:06:58 +09:00
{
playerRigidbody = GetComponent<Rigidbody>();
2026-04-29 09:22:08 +09:00
currentAmmo = maxAmmo;
if (shootButtonUI != null) shootButtonUI.UpdateAmmoUI(currentAmmo, maxAmmo);
if (shieldUI != null) shieldUI.UpdateShieldUI(shieldCount);
2026-04-14 17:06:58 +09:00
}
void Update()
2026-04-14 17:06:58 +09:00
{
HandleMovement();
if (!GameSettings.IsMobile && Input.GetKeyDown(KeyCode.Alpha1))
{
ActivateShieldLogic();
}
2026-04-29 09:22:08 +09:00
if (!GameSettings.IsMobile && Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}
2026-04-14 17:06:58 +09:00
}
void HandleMovement()
2026-04-14 17:06:58 +09:00
{
float x = 0, z = 0;
if (GameSettings.IsMobile && joystick != null)
{
Vector2 input = joystick.GetInput();
2026-04-29 09:22:08 +09:00
x = input.x;
z = input.y;
}
else
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
}
2026-04-14 17:06:58 +09:00
2026-04-29 09:22:08 +09:00
playerRigidbody.linearVelocity = new Vector3(x * speed, playerRigidbody.linearVelocity.y, z * speed);
2026-04-14 17:06:58 +09:00
2026-04-29 09:22:08 +09:00
if (Mathf.Abs(x) > 0.1f || Mathf.Abs(z) > 0.1f)
2026-04-15 12:59:37 +09:00
{
2026-04-29 09:22:08 +09:00
lastMoveDirection = new Vector3(x, 0, z).normalized;
2026-04-14 17:06:58 +09:00
}
2026-04-15 12:59:37 +09:00
2026-04-29 09:22:08 +09:00
2026-04-15 12:59:37 +09:00
Animator anim = GetComponentInChildren<Animator>();
2026-04-29 09:22:08 +09:00
if (anim != null) anim.SetBool("isMoving", (Mathf.Abs(x) > 0.1f || Mathf.Abs(z) > 0.1f));
2026-04-14 17:06:58 +09:00
}
public void ActivateShieldLogic()
2026-04-14 17:06:58 +09:00
{
if (shieldCount > 0 && !isShieldActive)
2026-04-14 17:06:58 +09:00
{
StartCoroutine(ActivateShieldRoutine());
}
}
2026-04-14 17:06:58 +09:00
private IEnumerator ActivateShieldRoutine()
2026-04-14 17:06:58 +09:00
{
isShieldActive = true;
shieldCount--;
if (shieldUI != null) shieldUI.UpdateShieldUI(shieldCount);
if (shieldVisual != null) shieldVisual.SetActive(true);
yield return new WaitForSeconds(2f);
if (shieldVisual != null) shieldVisual.SetActive(false);
isShieldActive = false;
2026-04-14 17:06:58 +09:00
}
public void Die()
{
gameObject.SetActive(false);
FindFirstObjectByType<GameManager>()?.EndGame();
}
2026-04-29 09:22:08 +09:00
public void Shoot(Vector2? inputDirection = null)
{
// 리로드 중이거나 탄이 없으면 발사 불가
if (isReloading || currentAmmo <= 0)
return;
if (bulletPrefab == null || firePoint == null)
return;
// 탄 차감
currentAmmo--;
if (shootButtonUI != null) shootButtonUI.UpdateAmmoUI(currentAmmo, maxAmmo);
// 방향 결정
Vector3 lookDir;
if (inputDirection.HasValue && inputDirection.Value.magnitude > 0.1f)
{
lookDir = new Vector3(inputDirection.Value.x, 0, inputDirection.Value.y).normalized;
lastMoveDirection = lookDir;
}
else
{
lookDir = lastMoveDirection;
}
Quaternion bulletRotation = Quaternion.LookRotation(lookDir);
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, bulletRotation);
bullet.transform.forward = lookDir;
Bullet bulletScript = bullet.GetComponent<Bullet>();
if (bulletScript != null)
{
bulletScript.isPlayerBullet = true;
bulletScript.speed = 8f;
}
Collider bulletCollider = bullet.GetComponent<Collider>();
Collider playerCollider = GetComponent<Collider>();
if (bulletCollider != null && playerCollider != null)
{
Physics.IgnoreCollision(bulletCollider, playerCollider);
}
// 마지막 탄을 쐈으면 자동 리로드 시작
if (currentAmmo <= 0)
{
StartCoroutine(ReloadRoutine());
}
}
private IEnumerator ReloadRoutine()
{
isReloading = true;
if (shootButtonUI != null) shootButtonUI.SetReloading(true);
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isReloading = false;
if (shootButtonUI != null)
{
shootButtonUI.SetReloading(false);
shootButtonUI.UpdateAmmoUI(currentAmmo, maxAmmo);
}
}
void LateUpdate()
{
transform.rotation = Quaternion.LookRotation(lastMoveDirection);
}
}