Skip to content

Commit

Permalink
Subtract, Reduce 연산 추가 및 메뉴얼 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
khchaCA committed Sep 12, 2024
1 parent a0129b3 commit 7f5322c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 37 deletions.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,45 @@ RPG 게임에서 사용하는 케릭터 스탯 관리 코드 입니다. 케릭
3. https://github.com/DarkNaku/CharacterStats.git 입력하고 Add를 클릭합니다.

### 사용방법

샘플제작중...
연산 순서는 Add > Percent > Multiply > Reduce > Subtract 이며, 추가 순서와 상관 없이 고정 입니다.
만약 순서를 변경하거나 연산 내용을 변경하고 싶다면 상속을 받아 Stat<T>의 CalculateValue 함수를 오버라이드 하면 됩니다.
CharacterStats<T> 또한 CreateStat 함수를 변경을 위해 오버라이드 할수 있도록 제공하고 있습니다.

#### 스텟
```c#
// 단일 스탯 사용 방법
public enum STAT_TYPE { POWER, HEALTH, INTELIGENCE, WISDOM, AGILITY, LUCKY }

var stat = new Stat<STAT_TYPE>(STAT_TYPE.POWER, 100);

stat.Add(new Modifier(ModifierType.Add, 5)); // 100 + 5
stat.Add(new Modifier(ModifierType.Percent, 0.15f)); // 105 + (105 * 0.15)
stat.Add(new Modifier(ModifierType.Multiply, 0.1f)); // (105 + (105 * 0.15)) * 1.1
stat.Add(new Modifier(ModifierType.Reduce, 0.1f) { Source = key }); // ((105 + (105 * 0.15)) * 1.1) * 0.9
stat.Add(new Modifier(ModifierType.Subtract, 50f).SetID("A")); // (((105 + (105 * 0.15)) * 1.1) * 0.9) - 50
stat.RemoveBySource(key); // key 오브젝트 키로 등록된 모든 수정사항 스탯에서 제거
stat.RemoveByID("A"); // "A" 아이디로 등록된 모든 수정사항 스탯에서 제거
```

#### 케릭터
```c#
// 스탯 집합인 케릭터 클래스 사용 방법
var characterStats = new CharacterStats<STAT_TYPE>("Sample");

characterStats.AddStat(STAT_TYPE.POWER, 50);
characterStats.AddStat(STAT_TYPE.HEALTH, 100);
characterStats.AddStat(STAT_TYPE.INTELIGENCE, 30);

characterStats.AddModifier(STAT_TYPE.POWER, new Modifier(ModifierType.Add, 20));
characterStats.AddModifier(STAT_TYPE.POWER, new Modifier(ModifierType.Add, 10) { Source = key }); // key 오브젝트 키 설정
characterStats.AddModifier(STAT_TYPE.INTELIGENCE, new Modifier(ModifierType.Multiply, 0.1f) { Source = key }); // key 오브젝트 키 설정
characterStats.AddModifier(STAT_TYPE.HEALTH, new Modifier(ModifierType.Percent, 0.1f).SetID("A")); // "A" 아이디 설정
characterStats.AddModifier(STAT_TYPE.INTELIGENCE, new Modifier(ModifierType.Multiply, 0.1f).SetID("A")); // "A" 아이디 설정
characterStats.RemoveModifierBySource(key); // key 오브젝트 키로 등록된 수정사항 모든 스탯에서 제거
characterStats.RemoveModifierByID("A"); // "A" 아이디로 등록된 수정사항 모든 스탯에서 제거
```

### 클래스

Expand Down
6 changes: 4 additions & 2 deletions Runtime/CharacterStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ public CharacterStats(string name, CharacterStats<T> parent)

foreach (var item in Parent.All)
{
var stat = new Stat<T>(item.Value);
var stat = CreateStat(item.Value);

stat.OnChangeValue.AddListener(OnChangeValue);

_stats.Add(item.Key, stat);
}

CharacterStatsManager.Add(this);
}
}

public virtual Stat<T> CreateStat(Stat<T> parent) => new Stat<T>(parent);

public void Dispose()
{
Expand Down
2 changes: 2 additions & 0 deletions Runtime/ModifierType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class ModifierType : IComparable<ModifierType>, IEquatable<ModifierType>
public static ModifierType Add = new(1, nameof(Add));
public static ModifierType Percent = new(2, nameof(Percent));
public static ModifierType Multiply = new(3, nameof(Multiply));
public static ModifierType Reduce = new(4, nameof(Reduce));
public static ModifierType Subtract = new(5, nameof(Subtract));

[SerializeField] private string _name;
[SerializeField] private int _id;
Expand Down
107 changes: 77 additions & 30 deletions Runtime/Stat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,16 @@ private float ValueWithoutPost
private float _value;
private float _valueWithoutPost;

private readonly Dictionary<ModifierType, HashSet<Modifier>> _modifiers;
private readonly Dictionary<ModifierType, HashSet<Modifier>> _modifiers = new Dictionary<ModifierType, HashSet<Modifier>>();

private Stat()
{
_modifiers = new Dictionary<ModifierType, HashSet<Modifier>>
{
{ ModifierType.Add, new HashSet<Modifier>() },
{ ModifierType.Percent, new HashSet<Modifier>() },
{ ModifierType.Multiply, new HashSet<Modifier>() }
};
}

public Stat(T key, float initialValue) : this()
public Stat(T key, float initialValue)
{
_initialValue = initialValue;
_baseValue = _initialValue;
_key = key;
}

public Stat(Stat<T> parent) : this()
public Stat(Stat<T> parent)
{
_initialValue = 0f;
_baseValue = _initialValue;
Expand All @@ -95,7 +85,7 @@ public Stat(Stat<T> parent) : this()
});
}

public Stat(Stat<T> parent, float initialValue) : this()
public Stat(Stat<T> parent, float initialValue)
{
_initialValue = initialValue;
_baseValue = _initialValue;
Expand Down Expand Up @@ -191,19 +181,22 @@ protected virtual float CalculateValue(bool withPostModifier)
value = CalculateAdd(value, withPostModifier);
value = CalculatePercent(value, withPostModifier);
value = CalculateMultiply(value, withPostModifier);
value = CalculateReduce(value, withPostModifier);
value = CalculateSubtract(value, withPostModifier);

return value;
}

protected float CalculateAdd(float baseValue, bool withPostModifier)
{
var modifiers = _modifiers[ModifierType.Add];

foreach (var modifier in modifiers)
if (_modifiers.TryGetValue(ModifierType.Add, out var modifiers))
{
if (modifier.IsPost) continue;
foreach (var modifier in modifiers)
{
if (modifier.IsPost) continue;

baseValue += modifier.Value;
baseValue += modifier.Value;
}
}

if (withPostModifier)
Expand All @@ -221,15 +214,16 @@ protected float CalculateAdd(float baseValue, bool withPostModifier)

protected float CalculatePercent(float baseValue, bool withPostModifier)
{
var modifiers = _modifiers[ModifierType.Percent];

var percentAddSum = 0f;

foreach (var modifier in modifiers)
if (_modifiers.TryGetValue(ModifierType.Percent, out var modifiers))
{
if (modifier.IsPost) continue;
foreach (var modifier in modifiers)
{
if (modifier.IsPost) continue;

percentAddSum += modifier.Value;
percentAddSum += modifier.Value;
}
}

if (withPostModifier)
Expand All @@ -247,13 +241,14 @@ protected float CalculatePercent(float baseValue, bool withPostModifier)

protected float CalculateMultiply(float baseValue, bool withPostModifier)
{
var modifiers = _modifiers[ModifierType.Multiply];

foreach (var modifier in modifiers)
if (_modifiers.TryGetValue(ModifierType.Multiply, out var modifiers))
{
if (modifier.IsPost) continue;
foreach (var modifier in modifiers)
{
if (modifier.IsPost) continue;

baseValue *= (1f + modifier.Value);
baseValue *= (1f + modifier.Value);
}
}

if (withPostModifier)
Expand All @@ -268,7 +263,59 @@ protected float CalculateMultiply(float baseValue, bool withPostModifier)

return baseValue;
}

protected float CalculateReduce(float baseValue, bool withPostModifier)
{
var reduceSum = 0f;

if (_modifiers.TryGetValue(ModifierType.Reduce, out var modifiers))
{
foreach (var modifier in modifiers)
{
if (modifier.IsPost) continue;

reduceSum += modifier.Value;
}
}

if (withPostModifier)
{
var postModifiers = GetPostModifiers(ModifierType.Reduce);

for (int i = 0; i < postModifiers.Count; i++)
{
reduceSum += postModifiers[i].Value;
}
}

return baseValue * Mathf.Max(0f, 1f - reduceSum);
}

protected float CalculateSubtract(float baseValue, bool withPostModifier)
{
if (_modifiers.TryGetValue(ModifierType.Subtract, out var modifiers))
{
foreach (var modifier in modifiers)
{
if (modifier.IsPost) continue;

baseValue -= modifier.Value;
}
}

if (withPostModifier)
{
var postModifiers = GetPostModifiers(ModifierType.Subtract);

foreach (var modifier in postModifiers)
{
baseValue -= modifier.Value;
}
}

return baseValue;
}

private void UpdateValues()
{
_lastBaseValue = BaseValue;
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"name": "com.darknaku.characterstat",
"displayName": "Character Stat",
"version": "1.0.0",
"version": "1.0.1",
"unity": "2018.3",
"author": "DarkNaku",
"description": "DarkNaku's Character Stat Package",
"dependencies": {
},
"repository": {
"type": "git",
"url": "git+https://github.com/DarkNaku/CharacterStat.git"
Expand Down

0 comments on commit 7f5322c

Please sign in to comment.