feature-attributes #7

Merged
zephyr merged 10 commits from feature-attributes into main 2023-07-30 10:58:19 -05:00
4 changed files with 62 additions and 17 deletions
Showing only changes of commit 8170727c59 - Show all commits

View File

@ -5,25 +5,16 @@ using UnityEngine;
public class Attribute : ScriptableObject public class Attribute : ScriptableObject
zephyr marked this conversation as resolved Outdated

Missing SO suffix

Missing SO suffix

I think this should go back to a struct rather than an SO. But it also seems like that makes it so the editor can't see them. Might need to implement a custom editor?

I think this should go back to a struct rather than an SO. But it also seems like that makes it so the editor can't see them. Might need to implement a custom editor?
{ {
public long Base { get; set; } public long Base { get; set; }
public long BaseBonus { get; set; }
public long PermyriadBonus { get; set; }
public long FlatBonus { get; set; }
public long Value => Calc(Base, BaseBonus, PermyriadBonus, FlatBonus);
/// <summary> /// <summary>
/// Calculates the stat value with accumulated buff and debuff modifiers. /// Calculates the stat value with accumulated buff and debuff modifiers.
/// </summary> /// </summary>
/// <param name="baseValue">Base value.</param>
/// <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> /// <returns>(base + baseBonus) * (100% + permyriadBonus) + flatBonus, bounded below by 0.</returns>
public static long Calc(long baseValue, long baseBonus, long permyriadBonus, long flatBonus) public long Calc(EffectSO effect)
{ {
var r = baseValue + baseBonus; var r = Base + effect.Base;
var m = permyriadBonus + 10000; var m = effect.Permyriad + 10000;
r = r * (m / 10000) + (r / 10000) * (m % 10000) + flatBonus; r = r * (m / 10000) + (r / 10000) * (m % 10000) + effect.Flat;
if (r <= 0) if (r <= 0)
{ {
return 0; return 0;

View File

@ -0,0 +1,45 @@
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; }
zephyr marked this conversation as resolved Outdated

If we private set here is the editor able to set these values?

If we private set here is the editor able to set these values?

Tried some things. It looks like the editor can't set properties at all, only fields.

Tried some things. It looks like the editor can't set properties at all, only fields.
/// <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));
}
}

11
Assets/Scripts/EffectSO.cs.meta generated Normal file
View File

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

View File

@ -19,9 +19,7 @@ public class AttributeTest
{ {
var attr = ScriptableObject.CreateInstance<Attribute>(); var attr = ScriptableObject.CreateInstance<Attribute>();
attr.Base = startStat; attr.Base = startStat;
attr.BaseBonus = baseBonus; var effect = EffectSO.New(baseBonus, permyriadBonus, flatBonus);
attr.PermyriadBonus = permyriadBonus; return attr.Calc(effect);
attr.FlatBonus = flatBonus;
return attr.Value;
} }
} }