Blipples

Fishnet SynvVar<Stat> - Relevant Code

Dec 28th, 2024
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.48 KB | None | 0 0
  1. public class NPC : NetworkBehaviour, IInteractable, IDamageable
  2. {
  3.  
  4. /// <summary>
  5. /// The stats of the NPC.
  6. /// </summary>
  7. private readonly SyncVar<Stats> npcStats;
  8.  
  9. }
  10.  
  11. public class Stats
  12. {
  13. /// <summary>
  14. /// Dictionary of stats
  15. /// Key: StatType
  16. /// Value: Dictionary of stats
  17. /// Key: Enum
  18. /// Value: Observer<Stat>
  19. /// Example: statsObservers[StatType.Primary][PrimaryStat.Level].Value.StatValue
  20. /// Example: statsObservers[StatType.Secondary][SecondaryStat.WeaponDamage].Value.StatValue
  21. /// Example: statsObservers[StatType.Resistance][ResistanceStat.FireResistance].Value.StatValue
  22. /// </summary>
  23. public Dictionary<StatType, Dictionary<Enum, Observer<Stat>>> statsDictionary = new Dictionary<StatType, Dictionary<Enum, Observer<Stat>>>();
  24. }
  25.  
  26. /// <summary>
  27. /// Enumeration for different types of stats.
  28. /// </summary>
  29. public enum StatType
  30. {
  31. /// <summary>
  32. /// Represents primary stats, such as strength or intelligence.
  33. /// </summary>
  34. Primary,
  35.  
  36. /// <summary>
  37. /// Represents secondary stats, such as agility or stamina.
  38. /// </summary>
  39. Secondary,
  40.  
  41. /// <summary>
  42. /// Represents resistance stats, such as fire or ice resistance.
  43. /// </summary>
  44. Resistance
  45. }
  46.  
  47.  
  48.  
  49. /// <summary>
  50. /// Represents an observer that monitors changes to a value and invokes events when the value changes.
  51. /// </summary>
  52. /// <typeparam name="T">The type of the value being observed.</typeparam>
  53. [Serializable]
  54. public class Observer<T>
  55. {
  56. /// <summary>
  57. /// The value being observed.
  58. /// </summary>
  59. [SerializeField]
  60. T value;
  61.  
  62. /// <summary>
  63. /// Event invoked when the value changes.
  64. /// </summary>
  65. [SerializeField]
  66. UnityEvent<T> OnValueChanged;
  67.  
  68. /// <summary>
  69. /// Event invoked when the value changes, without passing the new value.
  70. /// </summary>
  71. [SerializeField]
  72. UnityEvent OnValueChangedVoid;
  73.  
  74. /// <summary>
  75. /// Gets or sets the observed value.
  76. /// </summary>
  77. public T Value
  78. {
  79. get => value;
  80. set => Set(value);
  81. }
  82.  
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="Observer{T}"/> class with the specified value and optional callbacks.
  85. /// </summary>
  86. /// <param name="value">The initial value.</param>
  87. /// <param name="callback">The callback to invoke when the value changes.</param>
  88. /// <param name="voidCallback">The callback to invoke when the value changes, without passing the new value.</param>
  89. public Observer(T value, UnityAction<T> callback = null, UnityAction voidCallback = null)
  90. {
  91. this.value = value;
  92.  
  93. OnValueChanged = new UnityEvent<T>();
  94. OnValueChangedVoid = new UnityEvent();
  95.  
  96. if (callback != null) OnValueChanged.AddListener(callback);
  97. if (voidCallback != null) OnValueChangedVoid.AddListener(voidCallback);
  98. }
  99.  
  100. /// <summary>
  101. /// Sets the observed value and invokes the events if the value has changed.
  102. /// </summary>
  103. /// <param name="value">The new value.</param>
  104. public void Set(T value)
  105. {
  106. //Debug.Log("Observer: We're in Set()");
  107. if (Equals(this.value, value)) return;
  108.  
  109. this.value = value;
  110.  
  111. Invoke();
  112. }
  113.  
  114. /// <summary>
  115. /// Invokes the events associated with the value change.
  116. /// </summary>
  117. public void Invoke()
  118. {
  119. //Debug.Log($"Invoking {OnValueChanged.GetPersistentEventCount()} listeners");
  120.  
  121. OnValueChanged.Invoke(value);
  122. OnValueChangedVoid.Invoke();
  123. }
  124.  
  125. /// <summary>
  126. /// Adds a listener to the value change event.
  127. /// </summary>
  128. /// <param name="callback">The callback to invoke when the value changes.</param>
  129. public void AddListener(UnityAction<T> callback)
  130. {
  131. if (callback == null) return;
  132.  
  133. if (OnValueChanged == null) OnValueChanged = new UnityEvent<T>();
  134.  
  135. OnValueChanged.AddListener(callback);
  136. }
  137.  
  138. /// <summary>
  139. /// Adds a listener to the void value change event.
  140. /// </summary>
  141. /// <param name="callback">The callback to invoke when the value changes, without passing the new value.</param>
  142. public void AddListener(UnityAction callback)
  143. {
  144. if (callback == null) return;
  145.  
  146. if (OnValueChangedVoid == null) OnValueChangedVoid = new UnityEvent();
  147.  
  148. OnValueChangedVoid.AddListener(callback);
  149. }
  150.  
  151. /// <summary>
  152. /// Removes a listener from the value change event.
  153. /// </summary>
  154. /// <param name="callback">The callback to remove.</param>
  155. public void RemoveListener(UnityAction<T> callback)
  156. {
  157. if (callback == null || OnValueChanged == null) return;
  158.  
  159. OnValueChanged.RemoveListener(callback);
  160. }
  161.  
  162. /// <summary>
  163. /// Removes a listener from the void value change event.
  164. /// </summary>
  165. /// <param name="callback">The callback to remove.</param>
  166. public void RemoveListener(UnityAction callback)
  167. {
  168. if (callback == null || OnValueChangedVoid == null) return;
  169.  
  170. OnValueChangedVoid.RemoveListener(callback);
  171. }
  172.  
  173. /// <summary>
  174. /// Removes all listeners from both value change events.
  175. /// </summary>
  176. public void RemoveAllListeners()
  177. {
  178. if (OnValueChanged != null) OnValueChanged.RemoveAllListeners();
  179. if (OnValueChangedVoid != null) OnValueChangedVoid.RemoveAllListeners();
  180. }
  181.  
  182. /// <summary>
  183. /// Disposes the observer by removing all listeners and resetting the value.
  184. /// </summary>
  185. public void Dispose()
  186. {
  187. RemoveAllListeners();
  188.  
  189. OnValueChanged = null;
  190. OnValueChangedVoid = null;
  191.  
  192. value = default;
  193. }
  194. }
  195.  
  196. using Sirenix.OdinInspector;
  197. using System;
  198. using System.Collections;
  199. using System.Collections.Generic;
  200. using UnityEngine;
  201.  
  202. /// <summary>
  203. /// Represents a stat with its type, name, and value.
  204. /// </summary>
  205. [Serializable]
  206. public class Stat
  207. {
  208. /// <summary>
  209. /// The type of the stat.
  210. /// </summary>
  211. [SerializeField]
  212. protected StatType statType;
  213.  
  214. /// <summary>
  215. /// The primary stat, enabled only if the stat type is primary.
  216. /// </summary>
  217. [SerializeField]
  218. [ShowIf("IsPrimaryStat")]
  219. protected PrimaryStat primaryStat;
  220.  
  221. /// <summary>
  222. /// The secondary stat, enabled only if the stat type is secondary.
  223. /// </summary>
  224. [SerializeField]
  225. [ShowIf("IsSecondaryStat")]
  226. protected SecondaryStat secondaryStat;
  227.  
  228. /// <summary>
  229. /// The resistance stat, enabled only if the stat type is resistance.
  230. /// </summary>
  231. [SerializeField]
  232. [ShowIf("IsResistanceStat")]
  233. protected ResistanceStat resistanceStat;
  234.  
  235. /// <summary>
  236. /// The name of the stat.
  237. /// </summary>
  238. [SerializeField]
  239. protected string statName;
  240.  
  241. /// <summary>
  242. /// The value of the stat.
  243. /// </summary>
  244. [SerializeField]
  245. protected int statValue;
  246.  
  247. /// <summary>
  248. /// Gets or sets the type of the stat.
  249. /// </summary>
  250. public StatType StatType { get { return statType; } set { statType = value; } }
  251.  
  252. /// <summary>
  253. /// Gets or sets the primary stat.
  254. /// </summary>
  255. public PrimaryStat PrimaryStat { get { return primaryStat; } set { primaryStat = value; } }
  256.  
  257. /// <summary>
  258. /// Gets or sets the secondary stat.
  259. /// </summary>
  260. public SecondaryStat SecondaryStat { get { return secondaryStat; } set { secondaryStat = value; } }
  261.  
  262. /// <summary>
  263. /// Gets or sets the resistance stat.
  264. /// </summary>
  265. public ResistanceStat ResistanceStat { get { return resistanceStat; } set { resistanceStat = value; } }
  266.  
  267. /// <summary>
  268. /// Gets or sets the name of the stat.
  269. /// </summary>
  270. public string StatName { get { return statName; } set { statName = value; } }
  271.  
  272. /// <summary>
  273. /// Gets or sets the value of the stat.
  274. /// </summary>
  275. public int StatValue { get { return statValue; } set { statValue = value; } }
  276.  
  277. /// <summary>
  278. /// Initializes a new instance of the <see cref="Stat"/> class.
  279. /// </summary>
  280. /// <param name="statType">The type of the stat.</param>
  281. /// <param name="statName">The name of the stat.</param>
  282. /// <param name="statValue">The value of the stat.</param>
  283. public Stat(StatType statType, string statName, int statValue)
  284. {
  285. this.statType = statType;
  286. this.statName = statName;
  287. this.statValue = statValue;
  288. }
  289.  
  290. /// <summary>
  291. /// Initializes a new instance of the <see cref="Stat"/> class with a primary stat.
  292. /// </summary>
  293. /// <param name="statType">The type of the stat.</param>
  294. /// <param name="primaryStat">The primary stat.</param>
  295. /// <param name="statName">The name of the stat.</param>
  296. /// <param name="statValue">The value of the stat.</param>
  297. public Stat(StatType statType, PrimaryStat primaryStat, string statName, int statValue)
  298. : this(statType, statName, statValue)
  299. {
  300. this.primaryStat = primaryStat;
  301. }
  302.  
  303. /// <summary>
  304. /// Initializes a new instance of the <see cref="Stat"/> class with a secondary stat.
  305. /// </summary>
  306. /// <param name="statType">The type of the stat.</param>
  307. /// <param name="secondaryStat">The secondary stat.</param>
  308. /// <param name="statName">The name of the stat.</param>
  309. /// <param name="statValue">The value of the stat.</param>
  310. public Stat(StatType statType, SecondaryStat secondaryStat, string statName, int statValue)
  311. : this(statType, statName, statValue)
  312. {
  313. this.secondaryStat = secondaryStat;
  314. }
  315.  
  316. /// <summary>
  317. /// Initializes a new instance of the <see cref="Stat"/> class with a resistance stat.
  318. /// </summary>
  319. /// <param name="statType">The type of the stat.</param>
  320. /// <param name="resistanceStat">The resistance stat.</param>
  321. /// <param name="statName">The name of the stat.</param>
  322. /// <param name="statValue">The value of the stat.</param>
  323. public Stat(StatType statType, ResistanceStat resistanceStat, string statName, int statValue)
  324. : this(statType, statName, statValue)
  325. {
  326. this.resistanceStat = resistanceStat;
  327. }
  328.  
  329. /// <summary>
  330. /// Initializes a new instance of the <see cref="Stat"/> class by copying another stat.
  331. /// </summary>
  332. /// <param name="statToCopy">The stat to copy.</param>
  333. public Stat(Stat statToCopy)
  334. {
  335. statType = statToCopy.statType;
  336. primaryStat = statToCopy.primaryStat;
  337. secondaryStat = statToCopy.secondaryStat;
  338. resistanceStat = statToCopy.resistanceStat;
  339. statName = statToCopy.statName;
  340. statValue = statToCopy.statValue;
  341. }
  342.  
  343. /// <summary>
  344. /// Determines if the stat is a primary stat.
  345. /// Used by the inspector to conditionally show the primary stat field.
  346. /// </summary>
  347. private bool IsPrimaryStat()
  348. {
  349. return statType == StatType.Primary;
  350. }
  351.  
  352. /// <summary>
  353. /// Determines if the stat is a secondary stat.
  354. /// Used by the inspector to conditionally show the secondary stat field.
  355. /// </summary>
  356. private bool IsSecondaryStat()
  357. {
  358. return statType == StatType.Secondary;
  359. }
  360.  
  361. /// <summary>
  362. /// Determines if the stat is a resistance stat.
  363. /// Used by the inspector to conditionally show the resistance stat field.
  364. /// </summary>
  365. private bool IsResistanceStat()
  366. {
  367. return statType == StatType.Resistance;
  368. }
  369. }
  370.  
  371.  
Advertisement
Add Comment
Please, Sign In to add comment