Files
VRProject/Assets/_Scripts/PlayerStatusManager.cs
ohgushiyuga 52341100ed otamesi
2026-08-27 12:51:15 +09:00

172 lines
4.7 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerStatusManager : MonoBehaviour
{
public static PlayerStatusManager Instance { get; private set; }
[Header("デバッグ設定")]
[SerializeField] private bool ignoreGaugeForDebug = true;
[Header("入力設定")]
[SerializeField] private InputActionProperty zlButtonAction;
[SerializeField] private InputActionProperty zrButtonAction;
[SerializeField] private InputActionProperty moveAction;
[SerializeField] private InputActionProperty turnAction;
[Header("パズル制限時間")]
[SerializeField] private float codingLimitDuration = 15f;
[Header("ゲージ設定")]
[SerializeField] private float maxGauge = 100f;
[SerializeField] private float currentGauge = 0f;
[Header("UI & エフェクト")]
[SerializeField] private GameObject codingModeUI;
[SerializeField] private GameObject successEffectPrefab;
[SerializeField] private Transform effectSpawnPoint;
[Header("バフ機能")]
[SerializeField] private BuffHandler buffHandler = new BuffHandler();
private float codingTimer;
public bool IsCodingMode { get; private set; }
public BuffHandler Buffs => buffHandler;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
private void OnEnable()
{
SetActionsEnabled(true);
}
private void OnDisable()
{
SetActionsEnabled(false);
}
private void SetActionsEnabled(bool enable)
{
SetActionState(zlButtonAction, enable);
SetActionState(zrButtonAction, enable);
SetActionState(moveAction, enable);
SetActionState(turnAction, enable);
}
private void SetActionState(InputActionProperty property, bool enable)
{
if (property.action == null) return;
if (enable) property.action.Enable();
else property.action.Disable();
}
private void Start()
{
if (codingModeUI != null) codingModeUI.SetActive(false);
}
private void Update()
{
CheckComboInput();
if (!IsCodingMode)
{
buffHandler.UpdateTimers(Time.deltaTime);
}
else
{
codingTimer -= Time.deltaTime;
if (codingTimer <= 0f)
{
FailCodingModeByTimeout();
}
}
}
public void AddGauge(float amount)
{
if (IsCodingMode) return;
currentGauge = Mathf.Clamp(currentGauge + amount, 0f, maxGauge);
}
private void CheckComboInput()
{
if (zlButtonAction.action == null || zrButtonAction.action == null) return;
bool isBothHeld = zlButtonAction.action.ReadValue<float>() > 0.5f && zrButtonAction.action.ReadValue<float>() > 0.5f;
bool isAnyTriggered = zlButtonAction.action.triggered || zrButtonAction.action.triggered;
if (isBothHeld && isAnyTriggered)
{
if (!IsCodingMode)
{
if (ignoreGaugeForDebug || currentGauge >= maxGauge)
{
StartCodingMode();
}
}
else
{
CancelCodingMode();
}
}
}
private void StartCodingMode()
{
IsCodingMode = true;
codingTimer = codingLimitDuration;
SetActionState(moveAction, false);
SetActionState(turnAction, false);
PuzzleBlock.ResetAllBlocksInScene();
if (WorldPauseManager.Instance != null)
{
WorldPauseManager.Instance.PauseWorld();
}
if (codingModeUI != null) codingModeUI.SetActive(true);
}
public void ApplyBuffsAndResume(BuffType buff1, BuffType buff2)
{
if (successEffectPrefab != null && effectSpawnPoint != null)
{
Instantiate(successEffectPrefab, effectSpawnPoint.position, effectSpawnPoint.rotation);
}
buffHandler.ActivateBuff(buff1);
buffHandler.ActivateBuff(buff2);
ResumeWorldState(consumeGauge: true);
}
private void FailCodingModeByTimeout() => ResumeWorldState(consumeGauge: true);
public void CancelCodingMode() => ResumeWorldState(consumeGauge: false);
private void ResumeWorldState(bool consumeGauge)
{
if (WorldPauseManager.Instance != null)
{
WorldPauseManager.Instance.ResumeWorld();
}
SetActionState(moveAction, true);
SetActionState(turnAction, true);
IsCodingMode = false;
if (codingModeUI != null) codingModeUI.SetActive(false);
if (consumeGauge) currentGauge = 0f;
}
}