Add FlatStatModifier and PercentStatModifier classes; introduce Stat enum for character attributes
This commit is contained in:
@@ -4,6 +4,20 @@ using Sirenix.Serialization;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public enum Stat
|
||||
{
|
||||
Health,
|
||||
MaxHealth,
|
||||
MoveSpeed,
|
||||
Luck,
|
||||
Armor,
|
||||
Damage,
|
||||
RangedDamage,
|
||||
MeleeDamage,
|
||||
AttackRange,
|
||||
AttackSpeed
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class CharacterAttributes
|
||||
{
|
||||
|
3
Assets/Scripts/Modifiers.meta
Normal file
3
Assets/Scripts/Modifiers.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6c74671c1ef5489984cf2ed331bd4877
|
||||
timeCreated: 1752266781
|
69
Assets/Scripts/Modifiers/FlatStatModifier.cs
Normal file
69
Assets/Scripts/Modifiers/FlatStatModifier.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Interfaces;
|
||||
|
||||
namespace Modifiers
|
||||
{
|
||||
[Serializable]
|
||||
public class FlatStatModifier : IStatModifier
|
||||
{
|
||||
public Stat stat;
|
||||
public float value;
|
||||
public string Description => $"{stat} +{value}";
|
||||
|
||||
public void Apply(CharacterAttributes attributes)
|
||||
{
|
||||
ModifyAttributes(attributes, value);
|
||||
}
|
||||
|
||||
public void Remove(CharacterAttributes attributes)
|
||||
{
|
||||
Apply(attributes, -value);
|
||||
}
|
||||
|
||||
private void Apply(CharacterAttributes attributes, float value)
|
||||
{
|
||||
ModifyAttributes(attributes, value);
|
||||
}
|
||||
|
||||
private void ModifyAttributes(CharacterAttributes attributes, float value)
|
||||
{
|
||||
switch (stat)
|
||||
{
|
||||
case Stat.Health:
|
||||
attributes.ModifyHealth(value);
|
||||
break;
|
||||
case Stat.MaxHealth:
|
||||
attributes.ModifyMaxHealth(value);
|
||||
break;
|
||||
case Stat.MoveSpeed:
|
||||
attributes.ModifyMoveSpeed(value);
|
||||
break;
|
||||
case Stat.Luck:
|
||||
attributes.ModifyLuck(value);
|
||||
break;
|
||||
case Stat.Armor:
|
||||
attributes.ModifyArmor(value);
|
||||
break;
|
||||
case Stat.Damage:
|
||||
attributes.ModifyDamage(value);
|
||||
break;
|
||||
case Stat.RangedDamage:
|
||||
attributes.ModifyRangedDamage(value);
|
||||
break;
|
||||
case Stat.MeleeDamage:
|
||||
attributes.ModifyMeleeDamage(value);
|
||||
break;
|
||||
case Stat.AttackRange:
|
||||
attributes.ModifyAttackRange(value);
|
||||
break;
|
||||
case Stat.AttackSpeed:
|
||||
attributes.ModifyAttackSpeed(value);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
3
Assets/Scripts/Modifiers/FlatStatModifier.cs.meta
Normal file
3
Assets/Scripts/Modifiers/FlatStatModifier.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a042ddb31fa24a1d9e50e1016922b2bc
|
||||
timeCreated: 1752266791
|
57
Assets/Scripts/Modifiers/PercentStatModifier.cs
Normal file
57
Assets/Scripts/Modifiers/PercentStatModifier.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using Data;
|
||||
using Interfaces;
|
||||
|
||||
namespace Modifiers
|
||||
{
|
||||
public class PercentStatModifier : IStatModifier
|
||||
{
|
||||
private float lastAppliedAmount;
|
||||
|
||||
public Stat Stat { get; set; }
|
||||
public float Percent { get; set; }
|
||||
public string Description => $"{Stat} +{Percent * 100}%";
|
||||
|
||||
public void Apply(CharacterAttributes attributes)
|
||||
{
|
||||
var baseValue = GetBaseValue<float>(attributes);
|
||||
lastAppliedAmount = baseValue * Percent;
|
||||
|
||||
var flatModifier = new FlatStatModifier
|
||||
{
|
||||
value = lastAppliedAmount,
|
||||
stat = Stat
|
||||
};
|
||||
|
||||
flatModifier.Apply(attributes);
|
||||
}
|
||||
|
||||
public void Remove(CharacterAttributes attributes)
|
||||
{
|
||||
var flatModifier = new FlatStatModifier
|
||||
{
|
||||
value = -lastAppliedAmount,
|
||||
stat = Stat
|
||||
};
|
||||
|
||||
flatModifier.Apply(attributes);
|
||||
}
|
||||
|
||||
private T GetBaseValue<T>(CharacterAttributes attributes)
|
||||
{
|
||||
return Stat switch
|
||||
{
|
||||
Stat.Health => (T)(object)attributes.Health,
|
||||
Stat.MaxHealth => (T)(object)attributes.MaxHealth,
|
||||
Stat.MoveSpeed => (T)(object)attributes.MoveSpeed,
|
||||
Stat.Luck => (T)(object)attributes.Luck,
|
||||
Stat.Armor => (T)(object)attributes.Armor,
|
||||
Stat.Damage => (T)(object)attributes.Damage,
|
||||
Stat.RangedDamage => (T)(object)attributes.RangedDamage,
|
||||
Stat.MeleeDamage => (T)(object)attributes.MeleeDamage,
|
||||
Stat.AttackRange => (T)(object)attributes.AttackRange,
|
||||
Stat.AttackSpeed => (T)(object)attributes.AttackSpeed,
|
||||
_ => throw new System.ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Modifiers/PercentStatModifier.cs.meta
Normal file
3
Assets/Scripts/Modifiers/PercentStatModifier.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d0a1bf6263e84c8cab3312fec117b3fa
|
||||
timeCreated: 1752267326
|
29
Assets/Scripts/Systems/CharacterModifierManager.cs
Normal file
29
Assets/Scripts/Systems/CharacterModifierManager.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using Data;
|
||||
using Interfaces;
|
||||
using Sirenix.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
public class CharacterModifierManager : MonoBehaviour
|
||||
{
|
||||
[OdinSerialize] private List<IStatModifier> activeModifiers = new();
|
||||
|
||||
[SerializeField] private Character character;
|
||||
|
||||
public void EquipItem(IStatModifier modifier)
|
||||
{
|
||||
activeModifiers.Add(modifier);
|
||||
modifier.Apply(character.attributes);
|
||||
}
|
||||
|
||||
public void UnequipItem(IStatModifier modifier)
|
||||
{
|
||||
if (activeModifiers.Remove(modifier))
|
||||
{
|
||||
modifier.Remove(character.attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Systems/CharacterModifierManager.cs.meta
Normal file
3
Assets/Scripts/Systems/CharacterModifierManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c860b1e8855495097022a829e769fcc
|
||||
timeCreated: 1752267836
|
Reference in New Issue
Block a user