Files
VRProject/Assets/Scripts/PlayerStatusManager.cs
2026-06-09 12:37:09 +09:00

195 lines
7.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections;
/// <summary>
/// プレイヤーの共通ゲージ、時間停止Codingモード、UI、および各種バフの制限時間を管理するクラス
/// </summary>
public class PlayerStatusManager : MonoBehaviour
{
public static PlayerStatusManager Instance { get; private set; }
[Header("Codingモード用 入力設定")]
[SerializeField] private InputActionProperty zlButtonAction;
[SerializeField] private InputActionProperty zrButtonAction;
[Header("ゲージ設定")]
[SerializeField] private float maxGauge = 100f;
[SerializeField] private float currentGauge = 0f;
[Header("表示UI設定")]
[Tooltip("画面全体を追従させる親オブジェクトWorld Space Canvasや3D台座を指定してください")]
[SerializeField] private GameObject codingModeUI;
// ─── ★ここからバフ設定を新しく追加 ───
[Header("バフパラメータ設定")]
[Tooltip("攻撃力UP・防御力UPの持続時間 (秒)")]
[SerializeField] private float buffDuration = 10f;
[Tooltip("攻撃力UP時のダメージ倍率 (例: 1.5倍)")]
[SerializeField] private float attackBuffMultiplier = 1.5f;
[Tooltip("防御力UP時の被ダメージ軽減率 (例: 0.5ならダメージ50%カット)")]
[SerializeField] private float defenseBuffMultiplier = 0.5f;
[Tooltip("HP回復バフ発動時の回復量")]
[SerializeField] private float hpHealAmount = 30f;
// 各バフの残り時間を管理するタイマー0より大きければバフ有効
private float attackBuffTimer = 0f;
private float defenseBuffTimer = 0f;
// 現在Codingモード時間停止中かどうかのフラグ
private bool isCodingMode = false;
// ─────────────────────────────────────
#region
/// <summary> 現在攻撃力UPバフが有効かどうか </summary>
public bool IsAttackBuffActive => attackBuffTimer > 0f;
/// <summary> 現在防御力UPバフが有効かどうか </summary>
public bool IsDefenseBuffActive => defenseBuffTimer > 0f;
/// <summary> 現在の攻撃力倍率を返すバフなしなら1.0倍) </summary>
public float CurrentAttackMultiplier => IsAttackBuffActive ? attackBuffMultiplier : 1.0f;
/// <summary> 現在の防御倍率を返すバフなしなら1.0倍、バフありなら0.5倍など) </summary>
public float CurrentDefenseMultiplier => IsDefenseBuffActive ? defenseBuffMultiplier : 1.0f;
#endregion
private void Awake()
{
if (Instance == null) { Instance = this; }
else { Destroy(gameObject); }
}
private void OnEnable()
{
if (zlButtonAction.action != null) zlButtonAction.action.Enable();
if (zrButtonAction.action != null) zrButtonAction.action.Enable();
}
private void OnDisable()
{
if (zlButtonAction.action != null) zlButtonAction.action.Disable();
if (zrButtonAction.action != null) zrButtonAction.action.Disable();
}
private void Start()
{
if (codingModeUI != null) codingModeUI.SetActive(false);
}
private void Update()
{
CheckComboInput();
// ★新設:ゲームプレイ中(時間停止中でないとき)のみ、バフのタイマーを減算する
if (!isCodingMode)
{
UpdateBuffTimers();
}
}
public void AddGauge(float amount)
{
if (isCodingMode) return;
currentGauge += amount;
currentGauge = Mathf.Clamp(currentGauge, 0f, maxGauge);
Debug.Log($"🔋 共通ゲージ増加: {currentGauge} / {maxGauge}");
}
private void CheckComboInput()
{
if (zlButtonAction.action == null || zrButtonAction.action == null) return;
bool zlPressed = zlButtonAction.action.triggered && zlButtonAction.action.ReadValue<float>() > 0.5f;
bool zrPressed = zrButtonAction.action.triggered && zrButtonAction.action.ReadValue<float>() > 0.5f;
if (!isCodingMode && currentGauge >= maxGauge)
{
if ((zlPressed && zrButtonAction.action.ReadValue<float>() > 0.5f) ||
(zrPressed && zlButtonAction.action.ReadValue<float>() > 0.5f))
{
StartCodingMode();
}
}
}
private void StartCodingMode()
{
isCodingMode = true;
Time.timeScale = 0f; // 時間を完全停止
if (codingModeUI != null) codingModeUI.SetActive(true);
}
/// <summary>
/// ★役割拡張パズル完了時に呼び出され、指定された2つのバフを発動して時間を再開する
/// </summary>
public void ApplyBuffsAndResume(string buff1, string buff2)
{
// 1. 選択された2つのバフをそれぞれ発動重複なしの仕様を適用
ActivateBuffByName(buff1);
ActivateBuffByName(buff2);
// 2. Codingモードの終了処理
isCodingMode = false;
Time.timeScale = 1f; // 時間を再開
if (codingModeUI != null) codingModeUI.SetActive(false);
currentGauge = 0f; // ゲージを消費
}
/// <summary>
/// 文字列に応じて対象のバフを起動する内部メソッド
/// </summary>
private void ActivateBuffByName(string buffName)
{
switch (buffName)
{
case "AttackUp":
attackBuffTimer = buffDuration; // タイマーをリセット(上書き補給)
Debug.Log($"⚔️ 攻撃力UPバフ発動 倍率: {attackBuffMultiplier} / 効果時間: {buffDuration}秒");
break;
case "DefenseUp":
defenseBuffTimer = buffDuration; // タイマーをリセット(上書き補給)
Debug.Log($"🛡️ 防御力UPバフ発動 被ダメージ倍率: {defenseBuffMultiplier} / 効果時間: {buffDuration}秒");
break;
case "Heal":
ExecuteHeal(); // HP回復は即時発動
break;
default:
Debug.LogWarning($"未定義のバフ名が検出されました: {buffName}");
break;
}
}
/// <summary>
/// HP回復の即時実行処理
/// </summary>
private void ExecuteHeal()
{
Debug.Log($"💚 HP回復バフ発動 回復量: {hpHealAmount}");
// 【将来の拡張】プレイヤーのHealthスクリプトが完成したら、ここに回復処理を記述します
// PlayerHealth.Instance.Heal(hpHealAmount);
}
/// <summary>
/// 毎フレーム、有効なバフタイマーをリアルタイムで減算する
/// </summary>
private void UpdateBuffTimers()
{
if (attackBuffTimer > 0f)
{
attackBuffTimer -= Time.deltaTime;
if (attackBuffTimer <= 0f) Debug.Log("⚔️ 攻撃力UPバフの効果が終了しました。");
}
if (defenseBuffTimer > 0f)
{
defenseBuffTimer -= Time.deltaTime;
if (defenseBuffTimer <= 0f) Debug.Log("🛡️ 防御力UPバフの効果が終了しました。");
}
}
}