Simple Pigman AI

This commit is contained in:
Michal Pikulski
2025-12-04 08:52:59 +01:00
committed by Michal Pikulski
parent 9b71b00441
commit a463e099b4
27 changed files with 7452 additions and 428 deletions

View File

@@ -56,6 +56,7 @@ namespace Minigames.FortFight.Fort
public float CurrentHp => currentHp;
public float MaxHp => maxHp;
public float HpPercentage => maxHp > 0 ? (currentHp / maxHp) * 100f : 0f;
public bool IsDestroyed => isDestroyed;
#endregion

View File

@@ -18,9 +18,6 @@ namespace Minigames.FortFight.Fort
[Header("Fort Configuration")]
[SerializeField] private string fortName = "Unnamed Fort";
[Header("Debug")]
[SerializeField] private bool showDebugInfo = true;
#endregion
#region Events
@@ -45,26 +42,26 @@ namespace Minigames.FortFight.Fort
#region Properties
public string FortName => fortName;
public float MaxFortHp => maxFortHp;
public float CurrentFortHp => currentFortHp;
public float HpPercentage => maxFortHp > 0 ? (currentFortHp / maxFortHp) * 100f : 0f;
public int TotalBlockCount => blocks.Count;
public int InitialBlockCount => initialBlockCount;
public float MaxFortHp => _maxFortHp;
public float CurrentFortHp => _currentFortHp;
public float HpPercentage => _maxFortHp > 0 ? (_currentFortHp / _maxFortHp) * 100f : 0f;
public int TotalBlockCount => _blocks.Count;
public int InitialBlockCount => _initialBlockCount;
public bool IsDefeated { get; private set; }
// Aliases for consistency
public float MaxHp => maxFortHp;
public float CurrentHp => currentFortHp;
public float MaxHp => _maxFortHp;
public float CurrentHp => _currentFortHp;
#endregion
#region Private State
private List<FortBlock> blocks = new List<FortBlock>();
private float maxFortHp = 0f;
private float currentFortHp = 0f;
private int initialBlockCount = 0;
private bool isInitialized = false;
private List<FortBlock> _blocks = new List<FortBlock>();
private float _maxFortHp = 0f;
private float _currentFortHp = 0f;
private int _initialBlockCount = 0;
private bool _isInitialized = false;
// Cached settings
private IFortFightSettings _cachedSettings;
@@ -80,6 +77,21 @@ namespace Minigames.FortFight.Fort
}
}
private FortFightDeveloperSettings _cachedDevSettings;
private FortFightDeveloperSettings CachedDevSettings
{
get
{
if (_cachedDevSettings == null)
{
_cachedDevSettings = GameManager.GetDeveloperSettings<FortFightDeveloperSettings>();
}
return _cachedDevSettings;
}
}
private bool ShowDebugInfo => CachedDevSettings?.FortShowDebugInfo ?? false;
#endregion
#region Lifecycle
@@ -97,7 +109,7 @@ namespace Minigames.FortFight.Fort
base.OnManagedDestroy();
// Unsubscribe from all block events
foreach (FortBlock block in blocks)
foreach (FortBlock block in _blocks)
{
if (block != null)
{
@@ -106,7 +118,7 @@ namespace Minigames.FortFight.Fort
}
}
blocks.Clear();
_blocks.Clear();
// Clear events
OnFortDamaged = null;
@@ -124,7 +136,7 @@ namespace Minigames.FortFight.Fort
/// </summary>
private void InitializeFort()
{
if (isInitialized)
if (_isInitialized)
{
Logging.Warning($"[FortController] {fortName} already initialized!");
return;
@@ -138,7 +150,7 @@ namespace Minigames.FortFight.Fort
// Step 2: Register with central manager
RegisterWithManager();
isInitialized = true;
_isInitialized = true;
Logging.Debug($"[FortController] {fortName} - Initialization complete");
}
@@ -176,10 +188,10 @@ namespace Minigames.FortFight.Fort
}
// Step 3: Initialize current HP to match max HP (sum of all blocks)
currentFortHp = maxFortHp;
_currentFortHp = _maxFortHp;
initialBlockCount = blocks.Count;
Logging.Debug($"[FortController] {fortName} - Initialized and registered {blocks.Count} blocks, Total HP: {maxFortHp:F0}");
_initialBlockCount = _blocks.Count;
Logging.Debug($"[FortController] {fortName} - Initialized and registered {_blocks.Count} blocks, Total HP: {_maxFortHp:F0}");
}
/// <summary>
@@ -211,18 +223,18 @@ namespace Minigames.FortFight.Fort
{
if (block == null) return;
if (!blocks.Contains(block))
if (!_blocks.Contains(block))
{
blocks.Add(block);
_blocks.Add(block);
// Only add to max HP, current HP will be calculated once at end of initialization
maxFortHp += block.MaxHp;
_maxFortHp += block.MaxHp;
// Subscribe to block events
block.OnBlockDestroyed += HandleBlockDestroyed;
block.OnBlockDamaged += HandleBlockDamaged;
if (showDebugInfo)
if (ShowDebugInfo)
{
Logging.Debug($"[FortController] Registered block: {block.gameObject.name} ({block.Material} {block.Size}, HP: {block.MaxHp})");
}
@@ -234,7 +246,7 @@ namespace Minigames.FortFight.Fort
/// </summary>
public List<FortBlock> GetWeakPoints()
{
return blocks.Where(b => b != null && b.IsWeakPoint).ToList();
return _blocks.Where(b => b != null && b.IsWeakPoint).ToList();
}
/// <summary>
@@ -242,7 +254,7 @@ namespace Minigames.FortFight.Fort
/// </summary>
public List<FortBlock> GetAllBlocks()
{
return new List<FortBlock>(blocks);
return new List<FortBlock>(_blocks);
}
/// <summary>
@@ -250,8 +262,8 @@ namespace Minigames.FortFight.Fort
/// </summary>
public FortBlock GetRandomBlock()
{
if (blocks.Count == 0) return null;
return blocks[UnityEngine.Random.Range(0, blocks.Count)];
if (_blocks.Count == 0) return null;
return _blocks[UnityEngine.Random.Range(0, _blocks.Count)];
}
#endregion
@@ -265,7 +277,7 @@ namespace Minigames.FortFight.Fort
Logging.Debug($"[FortController] {fortName} - Block destroyed: {block.gameObject.name}");
// Remove from list
blocks.Remove(block);
_blocks.Remove(block);
// Recalculate HP by summing all remaining blocks (consistent calculation method)
RecalculateFortHp();
@@ -277,9 +289,9 @@ namespace Minigames.FortFight.Fort
// Check defeat condition
CheckDefeatCondition();
if (showDebugInfo)
if (ShowDebugInfo)
{
Logging.Debug($"[FortController] {fortName} - HP: {currentFortHp:F0}/{maxFortHp:F0} ({HpPercentage:F1}%), Blocks: {blocks.Count}/{initialBlockCount}");
Logging.Debug($"[FortController] {fortName} - HP: {_currentFortHp:F0}/{_maxFortHp:F0} ({HpPercentage:F1}%), Blocks: {_blocks.Count}/{_initialBlockCount}");
}
}
@@ -305,18 +317,18 @@ namespace Minigames.FortFight.Fort
/// </summary>
private void RecalculateFortHp()
{
currentFortHp = 0f;
foreach (var block in blocks)
_currentFortHp = 0f;
foreach (var block in _blocks)
{
if (block != null)
{
currentFortHp += block.CurrentHp;
_currentFortHp += block.CurrentHp;
}
}
if (showDebugInfo)
if (ShowDebugInfo)
{
Logging.Debug($"[FortController] {fortName} - HP recalculated: {currentFortHp:F0}/{maxFortHp:F0} ({HpPercentage:F1}%)");
Logging.Debug($"[FortController] {fortName} - HP recalculated: {_currentFortHp:F0}/{_maxFortHp:F0} ({HpPercentage:F1}%)");
}
}
@@ -335,7 +347,7 @@ namespace Minigames.FortFight.Fort
float defeatThreshold = CachedSettings?.FortDefeatThreshold ?? 0.3f;
float defeatThresholdPercent = defeatThreshold * 100f;
Logging.Debug($"[FortController] {fortName} - Checking defeat: HP={currentFortHp:F1}/{maxFortHp:F1} ({HpPercentage:F1}%) vs threshold={defeatThresholdPercent:F1}%");
Logging.Debug($"[FortController] {fortName} - Checking defeat: HP={_currentFortHp:F1}/{_maxFortHp:F1} ({HpPercentage:F1}%) vs threshold={defeatThresholdPercent:F1}%");
// Defeat if HP at or below threshold
if (HpPercentage <= defeatThresholdPercent)
@@ -361,7 +373,7 @@ namespace Minigames.FortFight.Fort
private void OnGUI()
{
if (!showDebugInfo || !Application.isPlaying) return;
if (!ShowDebugInfo || !Application.isPlaying) return;
// Display fort HP in scene view (for testing)
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position + Vector3.up * 2f);