[Add] Dice & Refactor private names
This commit is contained in:
@@ -45,11 +45,11 @@ namespace YachtDice.Modifiers.Core
|
||||
{
|
||||
return new ScoreResult
|
||||
{
|
||||
BaseScore = BaseScore,
|
||||
FlatBonus = FlatBonus,
|
||||
Multiplier = Multiplier * PostMultiplier,
|
||||
DiceValues = DiceValues,
|
||||
Category = Category,
|
||||
baseScore = BaseScore,
|
||||
flatBonus = FlatBonus,
|
||||
multiplier = Multiplier * PostMultiplier,
|
||||
diceValues = DiceValues,
|
||||
category = Category,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -9,13 +9,13 @@ namespace YachtDice.Modifiers.Pipeline
|
||||
{
|
||||
public class ModifierPipeline
|
||||
{
|
||||
private readonly ModifierRegistry registry;
|
||||
private readonly List<EffectEntry> effectBuffer = new();
|
||||
private bool isExecuting;
|
||||
private readonly Queue<(TriggerType trigger, ModifierContext context)> deferredQueue = new();
|
||||
private readonly ModifierRegistry _registry;
|
||||
private readonly List<EffectEntry> _effectBuffer = new();
|
||||
private bool _isExecuting;
|
||||
private readonly Queue<(TriggerType trigger, ModifierContext context)> _deferredQueue = new();
|
||||
|
||||
private const int MaxRecursionDepth = 1;
|
||||
private int currentDepth;
|
||||
private int _currentDepth;
|
||||
|
||||
public bool TracingEnabled { get; set; }
|
||||
#if UNITY_EDITOR || DEVELOPMENT_BUILD
|
||||
@@ -24,23 +24,23 @@ namespace YachtDice.Modifiers.Pipeline
|
||||
|
||||
public ModifierPipeline(ModifierRegistry registry)
|
||||
{
|
||||
this.registry = registry;
|
||||
this._registry = registry;
|
||||
}
|
||||
|
||||
public async UniTask<ModifierContext> Execute(TriggerType trigger, ModifierContext context)
|
||||
{
|
||||
if (isExecuting)
|
||||
if (_isExecuting)
|
||||
{
|
||||
if (currentDepth >= MaxRecursionDepth)
|
||||
if (_currentDepth >= MaxRecursionDepth)
|
||||
{
|
||||
Debug.LogWarning($"[ModifierPipeline] Max recursion depth reached for trigger {trigger}. Dropping.");
|
||||
return context;
|
||||
}
|
||||
|
||||
currentDepth++;
|
||||
_currentDepth++;
|
||||
}
|
||||
|
||||
isExecuting = true;
|
||||
_isExecuting = true;
|
||||
context.Trigger = trigger;
|
||||
|
||||
PipelineTrace trace = null;
|
||||
@@ -50,10 +50,10 @@ namespace YachtDice.Modifiers.Pipeline
|
||||
context.DebugLog ??= new List<string>();
|
||||
}
|
||||
|
||||
effectBuffer.Clear();
|
||||
_effectBuffer.Clear();
|
||||
|
||||
// Snapshot active modifiers to avoid modification during iteration
|
||||
var activeSnapshot = registry.Active;
|
||||
var activeSnapshot = _registry.Active;
|
||||
|
||||
// Gather eligible effects
|
||||
for (int i = 0; i < activeSnapshot.Count; i++)
|
||||
@@ -93,7 +93,7 @@ namespace YachtDice.Modifiers.Pipeline
|
||||
for (int e = 0; e < effects.Count; e++)
|
||||
{
|
||||
if (effects[e] == null) continue;
|
||||
effectBuffer.Add(new EffectEntry
|
||||
_effectBuffer.Add(new EffectEntry
|
||||
{
|
||||
Effect = effects[e],
|
||||
Instance = inst,
|
||||
@@ -103,12 +103,12 @@ namespace YachtDice.Modifiers.Pipeline
|
||||
}
|
||||
|
||||
// Sort by Phase -> Priority -> Id
|
||||
effectBuffer.Sort(ModifierComparer.Default);
|
||||
_effectBuffer.Sort(ModifierComparer.Default);
|
||||
|
||||
// Execute sequentially
|
||||
for (int i = 0; i < effectBuffer.Count; i++)
|
||||
for (int i = 0; i < _effectBuffer.Count; i++)
|
||||
{
|
||||
var entry = effectBuffer[i];
|
||||
var entry = _effectBuffer[i];
|
||||
await entry.Effect.Apply(context, entry.Instance);
|
||||
|
||||
if (trace != null)
|
||||
@@ -122,8 +122,8 @@ namespace YachtDice.Modifiers.Pipeline
|
||||
Debug.Log(traceStr);
|
||||
}
|
||||
|
||||
isExecuting = false;
|
||||
currentDepth = 0;
|
||||
_isExecuting = false;
|
||||
_currentDepth = 0;
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -7,29 +7,29 @@ namespace YachtDice.Modifiers.Runtime
|
||||
{
|
||||
public class ModifierRegistry
|
||||
{
|
||||
private readonly List<ModifierInstance> instances = new();
|
||||
private readonly List<ModifierInstance> activeCache = new();
|
||||
private int maxActiveSlots;
|
||||
private bool activeCacheDirty = true;
|
||||
private readonly List<ModifierInstance> _instances = new();
|
||||
private readonly List<ModifierInstance> _activeCache = new();
|
||||
private int _maxActiveSlots;
|
||||
private bool _activeCacheDirty = true;
|
||||
|
||||
public event Action OnChanged;
|
||||
public event Action<IReadOnlyList<ModifierInstance>> OnActiveModifiersChanged;
|
||||
|
||||
public IReadOnlyList<ModifierInstance> All => instances;
|
||||
public int MaxActiveSlots => maxActiveSlots;
|
||||
public IReadOnlyList<ModifierInstance> All => _instances;
|
||||
public int MaxActiveSlots => _maxActiveSlots;
|
||||
|
||||
public ModifierRegistry(int maxActiveSlots = 5)
|
||||
{
|
||||
this.maxActiveSlots = maxActiveSlots;
|
||||
this._maxActiveSlots = maxActiveSlots;
|
||||
}
|
||||
|
||||
public IReadOnlyList<ModifierInstance> Active
|
||||
{
|
||||
get
|
||||
{
|
||||
if (activeCacheDirty)
|
||||
if (_activeCacheDirty)
|
||||
RebuildActiveCache();
|
||||
return activeCache;
|
||||
return _activeCache;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,34 +38,34 @@ namespace YachtDice.Modifiers.Runtime
|
||||
get
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0; i < instances.Count; i++)
|
||||
if (instances[i].IsActive) count++;
|
||||
for (int i = 0; i < _instances.Count; i++)
|
||||
if (_instances[i].IsActive) count++;
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetMaxActiveSlots(int slots)
|
||||
{
|
||||
maxActiveSlots = slots;
|
||||
_maxActiveSlots = slots;
|
||||
}
|
||||
|
||||
public ModifierInstance Add(ModifierDefinition definition)
|
||||
{
|
||||
var instance = new ModifierInstance(definition);
|
||||
instances.Add(instance);
|
||||
activeCacheDirty = true;
|
||||
_instances.Add(instance);
|
||||
_activeCacheDirty = true;
|
||||
OnChanged?.Invoke();
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void Remove(ModifierInstance instance)
|
||||
{
|
||||
if (!instances.Contains(instance)) return;
|
||||
if (!_instances.Contains(instance)) return;
|
||||
|
||||
bool wasActive = instance.IsActive;
|
||||
instance.IsActive = false;
|
||||
instances.Remove(instance);
|
||||
activeCacheDirty = true;
|
||||
_instances.Remove(instance);
|
||||
_activeCacheDirty = true;
|
||||
|
||||
if (wasActive)
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
@@ -76,11 +76,11 @@ namespace YachtDice.Modifiers.Runtime
|
||||
public bool TryActivate(ModifierInstance instance)
|
||||
{
|
||||
if (instance.IsActive) return false;
|
||||
if (!instances.Contains(instance)) return false;
|
||||
if (ActiveCount >= maxActiveSlots) return false;
|
||||
if (!_instances.Contains(instance)) return false;
|
||||
if (ActiveCount >= _maxActiveSlots) return false;
|
||||
|
||||
instance.IsActive = true;
|
||||
activeCacheDirty = true;
|
||||
_activeCacheDirty = true;
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
OnChanged?.Invoke();
|
||||
return true;
|
||||
@@ -91,7 +91,7 @@ namespace YachtDice.Modifiers.Runtime
|
||||
if (!instance.IsActive) return;
|
||||
|
||||
instance.IsActive = false;
|
||||
activeCacheDirty = true;
|
||||
_activeCacheDirty = true;
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
OnChanged?.Invoke();
|
||||
}
|
||||
@@ -100,9 +100,9 @@ namespace YachtDice.Modifiers.Runtime
|
||||
{
|
||||
bool changed = false;
|
||||
|
||||
for (int i = instances.Count - 1; i >= 0; i--)
|
||||
for (int i = _instances.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var inst = instances[i];
|
||||
var inst = _instances[i];
|
||||
if (!inst.IsActive) continue;
|
||||
if (!inst.Definition.HasLimitedUses) continue;
|
||||
|
||||
@@ -110,14 +110,14 @@ namespace YachtDice.Modifiers.Runtime
|
||||
|
||||
if (inst.IsExpired)
|
||||
{
|
||||
instances.RemoveAt(i);
|
||||
_instances.RemoveAt(i);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed)
|
||||
{
|
||||
activeCacheDirty = true;
|
||||
_activeCacheDirty = true;
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
OnChanged?.Invoke();
|
||||
}
|
||||
@@ -126,23 +126,23 @@ namespace YachtDice.Modifiers.Runtime
|
||||
public List<ModifierSaveEntry> GetSaveData()
|
||||
{
|
||||
var entries = new List<ModifierSaveEntry>();
|
||||
for (int i = 0; i < instances.Count; i++)
|
||||
for (int i = 0; i < _instances.Count; i++)
|
||||
{
|
||||
var inst = instances[i];
|
||||
var inst = _instances[i];
|
||||
var entry = new ModifierSaveEntry
|
||||
{
|
||||
ModifierId = inst.Definition.Id,
|
||||
IsActive = inst.IsActive,
|
||||
RemainingUses = inst.RemainingUses,
|
||||
Stacks = inst.Stacks,
|
||||
modifierId = inst.Definition.Id,
|
||||
isActive = inst.IsActive,
|
||||
remainingUses = inst.RemainingUses,
|
||||
stacks = inst.Stacks,
|
||||
};
|
||||
|
||||
foreach (var kvp in inst.CustomState)
|
||||
{
|
||||
entry.CustomState.Add(new CustomStateEntry
|
||||
entry.customState.Add(new CustomStateEntry
|
||||
{
|
||||
Key = kvp.Key,
|
||||
Value = kvp.Value,
|
||||
key = kvp.Key,
|
||||
value = kvp.Value,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -153,36 +153,36 @@ namespace YachtDice.Modifiers.Runtime
|
||||
|
||||
public void LoadSaveData(List<ModifierSaveEntry> entries, ModifierCatalog catalog)
|
||||
{
|
||||
instances.Clear();
|
||||
activeCacheDirty = true;
|
||||
_instances.Clear();
|
||||
_activeCacheDirty = true;
|
||||
|
||||
if (entries == null) return;
|
||||
|
||||
for (int i = 0; i < entries.Count; i++)
|
||||
{
|
||||
var entry = entries[i];
|
||||
var definition = catalog.FindById(entry.ModifierId);
|
||||
var definition = catalog.FindById(entry.modifierId);
|
||||
|
||||
if (definition == null)
|
||||
{
|
||||
Debug.LogWarning($"Modifier '{entry.ModifierId}' not found in catalog, skipping.");
|
||||
Debug.LogWarning($"Modifier '{entry.modifierId}' not found in catalog, skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
var instance = new ModifierInstance(definition)
|
||||
{
|
||||
IsActive = entry.IsActive,
|
||||
RemainingUses = entry.RemainingUses,
|
||||
Stacks = entry.Stacks,
|
||||
IsActive = entry.isActive,
|
||||
RemainingUses = entry.remainingUses,
|
||||
Stacks = entry.stacks,
|
||||
};
|
||||
|
||||
if (entry.CustomState != null)
|
||||
if (entry.customState != null)
|
||||
{
|
||||
foreach (var cs in entry.CustomState)
|
||||
instance.CustomState[cs.Key] = cs.Value;
|
||||
foreach (var cs in entry.customState)
|
||||
instance.CustomState[cs.key] = cs.value;
|
||||
}
|
||||
|
||||
instances.Add(instance);
|
||||
_instances.Add(instance);
|
||||
}
|
||||
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
@@ -191,21 +191,21 @@ namespace YachtDice.Modifiers.Runtime
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
instances.Clear();
|
||||
activeCacheDirty = true;
|
||||
_instances.Clear();
|
||||
_activeCacheDirty = true;
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
OnChanged?.Invoke();
|
||||
}
|
||||
|
||||
private void RebuildActiveCache()
|
||||
{
|
||||
activeCache.Clear();
|
||||
for (int i = 0; i < instances.Count; i++)
|
||||
_activeCache.Clear();
|
||||
for (int i = 0; i < _instances.Count; i++)
|
||||
{
|
||||
if (instances[i].IsActive)
|
||||
activeCache.Add(instances[i]);
|
||||
if (_instances[i].IsActive)
|
||||
_activeCache.Add(_instances[i]);
|
||||
}
|
||||
activeCacheDirty = false;
|
||||
_activeCacheDirty = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace YachtDice.Modifiers.Runtime
|
||||
{
|
||||
[Serializable]
|
||||
public class ModifierSaveEntry
|
||||
{
|
||||
public string ModifierId;
|
||||
public bool IsActive;
|
||||
public int RemainingUses;
|
||||
public int Stacks;
|
||||
public List<CustomStateEntry> CustomState = new();
|
||||
[FormerlySerializedAs("ModifierId")] public string modifierId;
|
||||
[FormerlySerializedAs("IsActive")] public bool isActive;
|
||||
[FormerlySerializedAs("RemainingUses")] public int remainingUses;
|
||||
[FormerlySerializedAs("Stacks")] public int stacks;
|
||||
[FormerlySerializedAs("CustomState")] public List<CustomStateEntry> customState = new();
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class CustomStateEntry
|
||||
{
|
||||
public string Key;
|
||||
public float Value;
|
||||
[FormerlySerializedAs("Key")] public string key;
|
||||
[FormerlySerializedAs("Value")] public float value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user