From 88049ac97c5fb9f97d3f6a22e516e23b717625f8 Mon Sep 17 00:00:00 2001 From: Michal Pikulski Date: Wed, 3 Dec 2025 22:45:22 +0100 Subject: [PATCH] Data refactor --- .../FortFight/Core/AmmunitionManager.cs | 115 ++++++------- .../FortFight/Data/PlayerAmmoState.cs | 156 ++++++++++++++++++ .../FortFight/Data/PlayerAmmoState.cs.meta | 3 + 3 files changed, 207 insertions(+), 67 deletions(-) create mode 100644 Assets/Scripts/Minigames/FortFight/Data/PlayerAmmoState.cs create mode 100644 Assets/Scripts/Minigames/FortFight/Data/PlayerAmmoState.cs.meta diff --git a/Assets/Scripts/Minigames/FortFight/Core/AmmunitionManager.cs b/Assets/Scripts/Minigames/FortFight/Core/AmmunitionManager.cs index c3400867..694d8398 100644 --- a/Assets/Scripts/Minigames/FortFight/Core/AmmunitionManager.cs +++ b/Assets/Scripts/Minigames/FortFight/Core/AmmunitionManager.cs @@ -78,11 +78,8 @@ namespace Minigames.FortFight.Core #region State - // Per-player turn-based cooldowns: projectileType -> playerIndex -> turnsRemaining - private Dictionary> cooldowns = new Dictionary>(); - - // Per-player selected ammo: playerIndex -> ProjectileType - private Dictionary selectedAmmoByPlayer = new Dictionary(); + // Per-player ammunition state (encapsulates cooldowns, selection, usage) + private Dictionary playerStates = new Dictionary(); #endregion @@ -92,21 +89,21 @@ namespace Minigames.FortFight.Core { base.OnManagedStart(); - // Initialize per-player turn-based cooldowns to 0 for all players + // Initialize player states var configs = AvailableConfigs; - foreach (var config in configs) - { - cooldowns[config.projectileType] = new Dictionary(); - - for (int playerIndex = 0; playerIndex < MaxPlayers; playerIndex++) - { - cooldowns[config.projectileType][playerIndex] = 0; - } - } - // Select default ammo for all players for (int playerIndex = 0; playerIndex < MaxPlayers; playerIndex++) { + // Create player state with default ammo + playerStates[playerIndex] = new PlayerAmmoState(playerIndex, defaultProjectileType); + + // Initialize cooldowns for all projectile types + foreach (var config in configs) + { + playerStates[playerIndex].InitializeCooldown(config.projectileType); + } + + // Select default ammo (triggers event) SelectAmmo(defaultProjectileType, playerIndex); } } @@ -117,22 +114,15 @@ namespace Minigames.FortFight.Core /// public void DecrementCooldowns(int playerIndex) { - List completedCooldowns = new List(); - List projectileTypes = new List(cooldowns.Keys); - - foreach (var type in projectileTypes) + if (!playerStates.ContainsKey(playerIndex)) { - if (cooldowns[type].ContainsKey(playerIndex) && cooldowns[type][playerIndex] > 0) - { - cooldowns[type][playerIndex]--; - - if (cooldowns[type][playerIndex] == 0) - { - completedCooldowns.Add(type); - } - } + Logging.Warning($"[AmmunitionManager] Player {playerIndex} state not found!"); + return; } + // Decrement cooldowns and get completed types + List completedCooldowns = playerStates[playerIndex].DecrementCooldowns(); + // Fire events for completed cooldowns var settings = CachedSettings; foreach (var type in completedCooldowns) @@ -157,6 +147,12 @@ namespace Minigames.FortFight.Core /// public bool SelectAmmo(ProjectileType type, int playerIndex) { + if (!playerStates.ContainsKey(playerIndex)) + { + Logging.Warning($"[AmmunitionManager] Player {playerIndex} state not found!"); + return false; + } + var settings = CachedSettings; var config = settings?.GetProjectileConfig(type); @@ -172,7 +168,7 @@ namespace Minigames.FortFight.Core return false; } - selectedAmmoByPlayer[playerIndex] = type; + playerStates[playerIndex].SelectedAmmo = type; if (showDebugLogs) Logging.Debug($"[AmmunitionManager] Player {playerIndex} selected: {config.displayName}"); OnAmmoSelected?.Invoke(type, playerIndex); @@ -184,9 +180,9 @@ namespace Minigames.FortFight.Core /// public ProjectileType GetSelectedAmmoType(int playerIndex) { - if (selectedAmmoByPlayer.ContainsKey(playerIndex)) + if (playerStates.ContainsKey(playerIndex)) { - return selectedAmmoByPlayer[playerIndex]; + return playerStates[playerIndex].SelectedAmmo; } return defaultProjectileType; } @@ -201,21 +197,16 @@ namespace Minigames.FortFight.Core } /// - /// Check if ammo is available for a specific player (not on cooldown) + /// Check if ammunition type is available for a specific player (not on cooldown) /// public bool IsAmmoAvailable(ProjectileType type, int playerIndex) { - if (!cooldowns.ContainsKey(type)) + if (!playerStates.ContainsKey(playerIndex)) { - return true; + return false; } - if (!cooldowns[type].ContainsKey(playerIndex)) - { - return true; - } - - return cooldowns[type][playerIndex] <= 0; + return playerStates[playerIndex].IsAmmoAvailable(type); } /// @@ -223,17 +214,12 @@ namespace Minigames.FortFight.Core /// public int GetCooldownRemaining(ProjectileType type, int playerIndex) { - if (!cooldowns.ContainsKey(type)) + if (!playerStates.ContainsKey(playerIndex)) { return 0; } - if (!cooldowns[type].ContainsKey(playerIndex)) - { - return 0; - } - - return cooldowns[type][playerIndex]; + return playerStates[playerIndex].GetCooldown(type); } #endregion @@ -245,6 +231,12 @@ namespace Minigames.FortFight.Core /// public void UseAmmo(ProjectileType type, int playerIndex) { + if (!playerStates.ContainsKey(playerIndex)) + { + Logging.Warning($"[AmmunitionManager] Player {playerIndex} state not found!"); + return; + } + var settings = CachedSettings; var config = settings?.GetProjectileConfig(type); @@ -254,19 +246,9 @@ namespace Minigames.FortFight.Core return; } - // Initialize cooldowns dict if needed - if (!cooldowns.ContainsKey(type)) - { - cooldowns[type] = new Dictionary(); - - for (int i = 0; i < MaxPlayers; i++) - { - cooldowns[type][i] = 0; - } - } - - // Start turn-based cooldown for this player - cooldowns[type][playerIndex] = config.cooldownTurns; + // Set cooldown and record usage + playerStates[playerIndex].SetCooldown(type, config.cooldownTurns); + playerStates[playerIndex].RecordUsage(type); if (showDebugLogs) Logging.Debug($"[AmmunitionManager] Player {playerIndex}: {config.displayName} used - cooldown: {config.cooldownTurns} turns"); @@ -282,14 +264,13 @@ namespace Minigames.FortFight.Core /// public void ResetAllCooldowns() { - foreach (var projectileType in cooldowns.Keys) + var configs = AvailableConfigs; + + foreach (var playerState in playerStates.Values) { - for (int playerIndex = 0; playerIndex < MaxPlayers; playerIndex++) + foreach (var config in configs) { - if (cooldowns[projectileType].ContainsKey(playerIndex)) - { - cooldowns[projectileType][playerIndex] = 0; - } + playerState.SetCooldown(config.projectileType, 0); } } diff --git a/Assets/Scripts/Minigames/FortFight/Data/PlayerAmmoState.cs b/Assets/Scripts/Minigames/FortFight/Data/PlayerAmmoState.cs new file mode 100644 index 00000000..c33a3c95 --- /dev/null +++ b/Assets/Scripts/Minigames/FortFight/Data/PlayerAmmoState.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; + +namespace Minigames.FortFight.Data +{ + /// + /// Encapsulates all ammunition state for a single player. + /// Tracks cooldowns, selection, and usage history per player. + /// + [Serializable] + public class PlayerAmmoState + { + #region Properties + + public int PlayerIndex { get; private set; } + public ProjectileType SelectedAmmo { get; set; } + + #endregion + + #region State + + // Cooldowns per projectile type (turns remaining) + private Dictionary cooldowns; + + // Optional: Track usage for statistics/analytics + private Dictionary usageCount; + private ProjectileType lastUsedProjectile; + + #endregion + + #region Constructor + + public PlayerAmmoState(int playerIndex, ProjectileType defaultAmmo) + { + PlayerIndex = playerIndex; + SelectedAmmo = defaultAmmo; + cooldowns = new Dictionary(); + usageCount = new Dictionary(); + lastUsedProjectile = defaultAmmo; + } + + #endregion + + #region Cooldown Management + + /// + /// Initialize cooldown for a specific projectile type. + /// + public void InitializeCooldown(ProjectileType type) + { + if (!cooldowns.ContainsKey(type)) + { + cooldowns[type] = 0; + } + } + + /// + /// Set cooldown for a specific projectile type. + /// + public void SetCooldown(ProjectileType type, int turns) + { + cooldowns[type] = turns; + } + + /// + /// Get remaining cooldown turns for a projectile type. + /// + public int GetCooldown(ProjectileType type) + { + return cooldowns.ContainsKey(type) ? cooldowns[type] : 0; + } + + /// + /// Check if projectile type is available (not on cooldown). + /// + public bool IsAmmoAvailable(ProjectileType type) + { + return GetCooldown(type) == 0; + } + + /// + /// Decrement all cooldowns by 1 turn. + /// Returns list of projectile types that completed cooldown this turn. + /// + public List DecrementCooldowns() + { + List completedCooldowns = new List(); + List types = new List(cooldowns.Keys); + + foreach (var type in types) + { + if (cooldowns[type] > 0) + { + cooldowns[type]--; + + if (cooldowns[type] == 0) + { + completedCooldowns.Add(type); + } + } + } + + return completedCooldowns; + } + + #endregion + + #region Usage Tracking + + /// + /// Record that a projectile type was used. + /// + public void RecordUsage(ProjectileType type) + { + lastUsedProjectile = type; + + if (!usageCount.ContainsKey(type)) + { + usageCount[type] = 0; + } + usageCount[type]++; + } + + /// + /// Get usage count for a projectile type. + /// + public int GetUsageCount(ProjectileType type) + { + return usageCount.ContainsKey(type) ? usageCount[type] : 0; + } + + /// + /// Get the last projectile type used by this player. + /// + public ProjectileType LastUsedProjectile => lastUsedProjectile; + + /// + /// Get total number of projectiles used by this player. + /// + public int TotalUsageCount + { + get + { + int total = 0; + foreach (var count in usageCount.Values) + { + total += count; + } + return total; + } + } + + #endregion + } +} + diff --git a/Assets/Scripts/Minigames/FortFight/Data/PlayerAmmoState.cs.meta b/Assets/Scripts/Minigames/FortFight/Data/PlayerAmmoState.cs.meta new file mode 100644 index 00000000..fec3aec3 --- /dev/null +++ b/Assets/Scripts/Minigames/FortFight/Data/PlayerAmmoState.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fd7545bfc92d4096b53954bab9884b15 +timeCreated: 1764797211 \ No newline at end of file