Created
March 6, 2022 22:04
-
-
Save bendangelo/2ba2b75efa0d7f7479b0fe2a370987b7 to your computer and use it in GitHub Desktop.
Set any number of custom values to items, abilities etc for an RPG.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (c) 2022 Ben D'Angelo. www.throwtheproject.com | |
// This code is licensed under MIT license (see LICENSE.txt for details) | |
using System.Collections; | |
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Assertions; | |
public class StatList { | |
public Dictionary<Stat, int> values; | |
public StatList() { | |
values = new Dictionary<Stat, int>(); | |
} | |
public int this[Stat stat] { | |
get { | |
return GetVal(stat); | |
} | |
set { | |
values[stat] = value; | |
} | |
} | |
public void Add(StatList list) { | |
foreach (var v in list.values) { | |
Add(v.Key, v.Value); | |
} | |
} | |
public void Add(StatVal[] list) { | |
foreach (var v in list) { | |
Add(v.stat, v.value); | |
} | |
} | |
public void Add(List<StatVal> list) { | |
foreach (var v in list) { | |
Add(v.stat, v.value); | |
} | |
} | |
public void Remove(StatList list) { | |
foreach (var v in list.values) { | |
Remove(v.Key, v.Value); | |
} | |
} | |
public void Add(StatVal statVal) { | |
Add(statVal.stat, statVal.value); | |
} | |
public void Add(Stat stat, int value) { | |
this[stat] += value; | |
} | |
public void SetVal(Stat stat, int value) { | |
this[stat] = value; | |
} | |
public void Remove(Stat stat, int value) { | |
Add(stat, -value); | |
} | |
public void Reset() { | |
values.Clear(); | |
} | |
public int GetVal(Stat stat) { | |
int val = -1; | |
if (values.TryGetValue(stat, out val)) { | |
return val; | |
} | |
return 0; | |
} | |
public int GetVal(Stat stat, int def = 1) { | |
int val = -1; | |
if (values.TryGetValue(stat, out val)) { | |
return val; | |
} | |
return def; | |
} | |
public float GetMod(Stat stat) { | |
int val = GetVal(stat); | |
return (float)(val / 100.0f); | |
} | |
public bool HasVal(Stat stat) { | |
return GetVal(stat) != 0; | |
} | |
public string Output() { | |
string str = ""; | |
foreach (var pair in values) { | |
str += "["+pair.Key + "] = " + pair.Value + "\n"; | |
} | |
return str; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (c) 2022 Ben D'Angelo. www.throwtheproject.com | |
// This code is licensed under MIT license (see LICENSE.txt for details) | |
using System.Collections; | |
using System.Collections.Generic; | |
using System; | |
using UnityEngine; | |
using UnityEngine.Assertions; | |
using UnityEngine.Serialization; | |
public enum Stat { | |
None, | |
ChargeSpeed, | |
SonicBoom, | |
Cover, | |
BlastDef | |
} | |
[System.Serializable] | |
public class StatVal { | |
public static Stat ToStat(string stat) { | |
Enum.TryParse(stat, out Stat statEnum); | |
return statEnum; | |
} | |
public Stat stat; | |
public int value; | |
public StatVal(Stat stat, int value) { | |
this.stat = stat; | |
Assert.IsFalse(stat == Stat.None, "None stat was set! with val " + value); | |
this.value = value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment