2026-06-13 04:56:50 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2020-12-22 03:13:43 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-05-12 01:42:13 +01:00
|
|
|
using System.Linq;
|
2026-06-14 10:42:31 +01:00
|
|
|
using GameCult.Aetheria.State.Unity;
|
|
|
|
|
|
2026-06-13 15:02:32 +01:00
|
|
|
[Inspectable]
|
2020-05-12 01:42:13 +01:00
|
|
|
public class StatModifierData : BehaviorData
|
|
|
|
|
{
|
2026-06-13 21:19:53 +01:00
|
|
|
[Inspectable, LegacyPayloadKey(1)]
|
2020-05-12 01:42:13 +01:00
|
|
|
public StatReference Stat = new StatReference();
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2026-06-13 21:19:53 +01:00
|
|
|
[Inspectable, LegacyPayloadKey(2)]
|
2020-05-12 01:42:13 +01:00
|
|
|
public PerformanceStat Modifier = new PerformanceStat();
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2026-06-13 21:19:53 +01:00
|
|
|
[Inspectable, LegacyPayloadKey(3)]
|
2020-05-12 01:42:13 +01:00
|
|
|
public StatModifierType Type;
|
|
|
|
|
|
2026-06-13 21:19:53 +01:00
|
|
|
[InspectableType(typeof(BehaviorData)), LegacyPayloadKey(4)]
|
2026-06-14 09:53:03 +01:00
|
|
|
public string RequireBehavior;
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public override Behavior CreateInstance(EquippedItem item)
|
2020-05-12 01:42:13 +01:00
|
|
|
{
|
2021-04-12 04:20:52 +01:00
|
|
|
return new StatModifier(this, item);
|
2020-05-12 01:42:13 +01:00
|
|
|
}
|
2021-04-18 05:13:20 +01:00
|
|
|
|
|
|
|
|
public override Behavior CreateInstance(ConsumableItemEffect consumable)
|
|
|
|
|
{
|
|
|
|
|
return new StatModifier(this, consumable);
|
|
|
|
|
}
|
2020-05-12 01:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-13 22:35:56 +01:00
|
|
|
[Order(-4)]
|
2021-04-21 01:49:07 +01:00
|
|
|
public class StatModifier : Behavior, IInitializableBehavior, IDisposable, IAlwaysUpdatedBehavior
|
2020-05-12 01:42:13 +01:00
|
|
|
{
|
|
|
|
|
private StatModifierData _data;
|
|
|
|
|
|
|
|
|
|
private PerformanceStat[] _stats;
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-04-21 01:49:07 +01:00
|
|
|
private bool _applied;
|
|
|
|
|
private bool _executed;
|
|
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public StatModifier(StatModifierData data, EquippedItem item) : base(data, item)
|
|
|
|
|
{
|
|
|
|
|
_data = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StatModifier(StatModifierData data, ConsumableItemEffect item) : base(data, item)
|
2020-05-12 01:42:13 +01:00
|
|
|
{
|
|
|
|
|
_data = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
2026-06-14 09:53:03 +01:00
|
|
|
_stats = Entity.Equipment
|
|
|
|
|
.Where(HasRequiredBehavior)
|
2026-06-14 10:38:04 +01:00
|
|
|
.SelectMany(gear => gear.Behaviors ?? Array.Empty<Behavior>())
|
|
|
|
|
.Where(behavior => BehaviorKindMatches(behavior.Data.Kind, _data.Stat.Target))
|
2026-06-14 09:53:03 +01:00
|
|
|
.Select(FindTargetStat)
|
|
|
|
|
.Where(stat => stat != null)
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 10:38:04 +01:00
|
|
|
private PerformanceStat FindTargetStat(Behavior behavior)
|
2026-06-14 09:53:03 +01:00
|
|
|
{
|
2026-06-14 10:38:04 +01:00
|
|
|
var data = behavior.Data;
|
|
|
|
|
var statField = data
|
2026-06-14 09:53:03 +01:00
|
|
|
.GetType()
|
|
|
|
|
.GetFields()
|
|
|
|
|
.Where(f => f.FieldType == typeof(PerformanceStat))
|
|
|
|
|
.FirstOrDefault(f => f.Name == _data.Stat.Stat);
|
|
|
|
|
|
2026-06-14 10:38:04 +01:00
|
|
|
return statField?.GetValue(data) as PerformanceStat;
|
2026-06-14 09:53:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool BehaviorKindMatches(string runtimeKind, string expectedKind)
|
|
|
|
|
{
|
2026-06-14 10:42:31 +01:00
|
|
|
return AetheriaRuntimeBehaviorMetadataCatalog.IsKindOrDescendant(
|
|
|
|
|
runtimeKind,
|
|
|
|
|
NormalizeExpectedBehaviorKind(expectedKind));
|
|
|
|
|
}
|
2026-06-14 09:53:03 +01:00
|
|
|
|
2026-06-14 10:42:31 +01:00
|
|
|
private static string NormalizeExpectedBehaviorKind(string expectedKind)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(expectedKind))
|
2020-05-12 01:42:13 +01:00
|
|
|
{
|
2026-06-14 10:42:31 +01:00
|
|
|
return "";
|
2020-05-12 01:42:13 +01:00
|
|
|
}
|
2026-06-14 09:53:03 +01:00
|
|
|
|
|
|
|
|
const string legacyDataSuffix = "Data";
|
2026-06-14 10:42:31 +01:00
|
|
|
return expectedKind.EndsWith(legacyDataSuffix, StringComparison.Ordinal)
|
|
|
|
|
? expectedKind.Substring(0, expectedKind.Length - legacyDataSuffix.Length)
|
|
|
|
|
: expectedKind;
|
2020-05-12 01:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-14 10:38:04 +01:00
|
|
|
private bool HasRequiredBehavior(EquippedItem gear)
|
2026-06-14 06:02:26 +01:00
|
|
|
{
|
2026-06-14 09:53:03 +01:00
|
|
|
return string.IsNullOrWhiteSpace(_data.RequireBehavior) ||
|
2026-06-14 10:38:04 +01:00
|
|
|
(gear?.Behaviors ?? Array.Empty<Behavior>())
|
|
|
|
|
.Any(behavior => BehaviorKindMatches(behavior.Data.Kind, _data.RequireBehavior));
|
2026-06-14 06:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-21 01:49:07 +01:00
|
|
|
private void ApplyModifier()
|
2020-05-12 01:42:13 +01:00
|
|
|
{
|
2021-04-21 01:49:07 +01:00
|
|
|
_applied = true;
|
2020-05-12 01:42:13 +01:00
|
|
|
foreach (var stat in _stats)
|
|
|
|
|
(_data.Type == StatModifierType.Constant
|
2021-04-18 05:13:20 +01:00
|
|
|
? stat.GetConstantModifiers(Entity)
|
|
|
|
|
: stat.GetScaleModifiers(Entity))[this] = Evaluate(_data.Modifier);
|
2020-05-12 01:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-21 01:49:07 +01:00
|
|
|
private void RemoveModifier()
|
2020-05-12 01:42:13 +01:00
|
|
|
{
|
2021-04-21 01:49:07 +01:00
|
|
|
_applied = false;
|
2020-05-12 01:42:13 +01:00
|
|
|
foreach (var stat in _stats)
|
2021-04-18 05:13:20 +01:00
|
|
|
(_data.Type == StatModifierType.Constant ? stat.GetConstantModifiers(Entity) : stat.GetScaleModifiers(Entity)).Remove(this);
|
2020-05-12 01:42:13 +01:00
|
|
|
}
|
2021-04-21 01:49:07 +01:00
|
|
|
|
|
|
|
|
public override bool Execute(float dt)
|
|
|
|
|
{
|
|
|
|
|
_executed = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if(_applied)
|
|
|
|
|
RemoveModifier();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(float delta)
|
|
|
|
|
{
|
|
|
|
|
if(_executed && !_applied)
|
|
|
|
|
ApplyModifier();
|
|
|
|
|
if(!_executed && _applied)
|
|
|
|
|
RemoveModifier();
|
|
|
|
|
_executed = false;
|
|
|
|
|
}
|
2020-05-12 01:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum StatModifierType
|
|
|
|
|
{
|
|
|
|
|
Constant,
|
|
|
|
|
Multiplier
|
2021-03-22 05:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-13 15:02:32 +01:00
|
|
|
[Inspectable]
|
2021-03-22 05:22:31 +00:00
|
|
|
public class StatReference
|
|
|
|
|
{
|
2026-06-13 21:19:53 +01:00
|
|
|
[InspectableType(typeof(BehaviorData)), LegacyPayloadKey(1)]
|
2021-03-22 05:22:31 +00:00
|
|
|
public string Target;
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2026-06-13 21:19:53 +01:00
|
|
|
[Inspectable, LegacyPayloadKey(2)]
|
2021-03-22 05:22:31 +00:00
|
|
|
public string Stat;
|
2026-06-14 06:02:26 +01:00
|
|
|
}
|