Compare commits

..

No commits in common. "3b0fc4481e828a4dd8ab431c96c1368c62c40d3f" and "4245513c7c0c077a02682ed4229246f44441601e" have entirely different histories.

6 changed files with 37 additions and 104 deletions

View File

@ -2,19 +2,37 @@
using System.Collections.Generic;
using UnityEngine;
public class Attribute : ScriptableObject
public struct Attribute
{
public long Base { get; set; }
[SerializeField]
private long _base;
public Attribute(long stat)
{
_base = stat;
}
/// <summary>
/// Permanently increase the stat value.
/// </summary>
/// <param name="by">Increase amount.</param>
public void LevelUp(long by)
{
_base += by;
}
/// <summary>
/// Calculates the stat value with accumulated buff and debuff modifiers.
/// </summary>
/// <param name="baseBonus">Additive bonus applied before multiplicative scaling.</param>
/// <param name="permyriadBonus">Multiplicative scaling in hundredths of a percent. E.g., 625 permyriad corresponds to 6.25%.</param>
/// <param name="flatBonus">Additive bonus applied after multiplicative scaling.</param>
/// <returns>(base + baseBonus) * (100% + permyriadBonus) + flatBonus, bounded below by 0.</returns>
public long Calc(EffectSO effect)
public long Calc(long baseBonus, long permyriadBonus, long flatBonus)
{
var r = Base + effect.Base;
var m = effect.Permyriad + 10000;
r = r * (m / 10000) + (r / 10000) * (m % 10000) + effect.Flat;
var r = _base + baseBonus;
var m = permyriadBonus + 10000;
r = r * (m / 10000) + (r / 10000) * (m % 10000) + flatBonus;
if (r <= 0)
{
return 0;

View File

@ -1,45 +0,0 @@
using UnityEngine;
[CreateAssetMenu(fileName = "StatEffect", menuName = "Stat Effect")]
public class EffectSO : ScriptableObject
{
/// <summary>
/// Additive change applied before multiplicative scaling.
/// </summary>
public long Base { get; private set; }
/// <summary>
/// Multiplicative scaling in units of hundredth of a percent.
/// E.g., 625 permyriad is 6.25%.
/// </summary>
public long Permyriad { get; private set; }
/// <summary>
/// Additive change applied after multiplicative scaling.
/// </summary>
public long Flat { get; private set; }
/// <summary>
/// Create a new attribute effect instance.
/// </summary>
/// <param name="baseBonus"></param>
/// <param name="permyriad"></param>
/// <param name="flat"></param>
/// <returns></returns>
public static EffectSO New(long baseBonus = 0, long permyriad = 0, long flat = 0)
{
var effect = ScriptableObject.CreateInstance<EffectSO>();
effect.Base = baseBonus;
effect.Permyriad = permyriad;
effect.Flat = flat;
return effect;
}
/// <summary>
/// Create a new EffectSO from a percentage.
/// </summary>
/// <param name="p">Percentage. E.g., 6.25f results in a Permyriad of 625.</param>
/// <returns>Effect with the given percentage.</returns>
public static EffectSO FromPercent(float p)
{
return New(permyriad: (long)(p * 100));
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 494a21c06960ec442a7a6806f8aefedb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,18 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroAttributesSO : ScriptableObject
{
[SerializeField] private Attribute STR;
[SerializeField] private Attribute MAG;
[SerializeField] private Attribute DEX;
[SerializeField] private Attribute CON;
[SerializeField] private Attribute CHA;
[SerializeField] private Attribute FTH;
public static HeroAttributesSO New()
{
return ScriptableObject.CreateInstance<HeroAttributesSO>();
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 538672e6491e46748a1cc27fe3a52455
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -7,19 +7,19 @@
public class AttributeTest
{
[Test]
[TestCase(1000000, 0, 0, 0, ExpectedResult = 1000000)]
[TestCase(1000000, 1, 0, 0, ExpectedResult = 1000001)]
[TestCase(1000000, 0, 2, 0, ExpectedResult = 1000200)]
[TestCase(1000000, 0, 0, 10000, ExpectedResult = 1010000)]
[TestCase(0, 1000000, 5000, 1000000, ExpectedResult = 2500000)]
[TestCase(1000000, 0, -10000, 0, ExpectedResult = 0)]
[TestCase(1000000, 0, -20000, 0, ExpectedResult = 0)]
[TestCase(1000000, 0, -20000, 0, ExpectedResult = 0)]
public long Value(long startStat, long baseBonus, long permyriadBonus, long flatBonus)
[TestCase(1000000, 0, 0, 0, 0, ExpectedResult = 1000000)]
[TestCase(1000000, 1, 0, 0, 0, ExpectedResult = 1000001)]
[TestCase(1000000, 0, 2, 0, 0, ExpectedResult = 1000002)]
[TestCase(1000000, 0, 0, 10000, 0, ExpectedResult = 2000000)]
[TestCase(1000000, 0, 0, 0, 3, ExpectedResult = 1000003)]
[TestCase(0, 1000000, 1000000, 5000, 1000, ExpectedResult = 3001000)]
[TestCase(1000000, 0, 0, -10000, 0, ExpectedResult = 0)]
[TestCase(1000000, 0, 0, -20000, 0, ExpectedResult = 0)]
[TestCase(1000000, 0, 0, -20000, 50, ExpectedResult = 0)]
public long Calc(long startStat, long levelUp, long baseBonus, long permyriadBonus, long flatBonus)
{
var attr = ScriptableObject.CreateInstance<Attribute>();
attr.Base = startStat;
var effect = EffectSO.New(baseBonus, permyriadBonus, flatBonus);
return attr.Calc(effect);
var attr = new Attribute(startStat);
attr.LevelUp(levelUp);
return attr.Calc(baseBonus, permyriadBonus, flatBonus);
}
}