Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NPC : NetworkBehaviour, IInteractable, IDamageable
- {
- /// <summary>
- /// The stats of the NPC.
- /// </summary>
- private readonly SyncVar<Stats> npcStats;
- }
- public class Stats
- {
- /// <summary>
- /// Dictionary of stats
- /// Key: StatType
- /// Value: Dictionary of stats
- /// Key: Enum
- /// Value: Observer<Stat>
- /// Example: statsObservers[StatType.Primary][PrimaryStat.Level].Value.StatValue
- /// Example: statsObservers[StatType.Secondary][SecondaryStat.WeaponDamage].Value.StatValue
- /// Example: statsObservers[StatType.Resistance][ResistanceStat.FireResistance].Value.StatValue
- /// </summary>
- public Dictionary<StatType, Dictionary<Enum, Observer<Stat>>> statsDictionary = new Dictionary<StatType, Dictionary<Enum, Observer<Stat>>>();
- }
- /// <summary>
- /// Enumeration for different types of stats.
- /// </summary>
- public enum StatType
- {
- /// <summary>
- /// Represents primary stats, such as strength or intelligence.
- /// </summary>
- Primary,
- /// <summary>
- /// Represents secondary stats, such as agility or stamina.
- /// </summary>
- Secondary,
- /// <summary>
- /// Represents resistance stats, such as fire or ice resistance.
- /// </summary>
- Resistance
- }
- /// <summary>
- /// Represents an observer that monitors changes to a value and invokes events when the value changes.
- /// </summary>
- /// <typeparam name="T">The type of the value being observed.</typeparam>
- [Serializable]
- public class Observer<T>
- {
- /// <summary>
- /// The value being observed.
- /// </summary>
- [SerializeField]
- T value;
- /// <summary>
- /// Event invoked when the value changes.
- /// </summary>
- [SerializeField]
- UnityEvent<T> OnValueChanged;
- /// <summary>
- /// Event invoked when the value changes, without passing the new value.
- /// </summary>
- [SerializeField]
- UnityEvent OnValueChangedVoid;
- /// <summary>
- /// Gets or sets the observed value.
- /// </summary>
- public T Value
- {
- get => value;
- set => Set(value);
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Observer{T}"/> class with the specified value and optional callbacks.
- /// </summary>
- /// <param name="value">The initial value.</param>
- /// <param name="callback">The callback to invoke when the value changes.</param>
- /// <param name="voidCallback">The callback to invoke when the value changes, without passing the new value.</param>
- public Observer(T value, UnityAction<T> callback = null, UnityAction voidCallback = null)
- {
- this.value = value;
- OnValueChanged = new UnityEvent<T>();
- OnValueChangedVoid = new UnityEvent();
- if (callback != null) OnValueChanged.AddListener(callback);
- if (voidCallback != null) OnValueChangedVoid.AddListener(voidCallback);
- }
- /// <summary>
- /// Sets the observed value and invokes the events if the value has changed.
- /// </summary>
- /// <param name="value">The new value.</param>
- public void Set(T value)
- {
- //Debug.Log("Observer: We're in Set()");
- if (Equals(this.value, value)) return;
- this.value = value;
- Invoke();
- }
- /// <summary>
- /// Invokes the events associated with the value change.
- /// </summary>
- public void Invoke()
- {
- //Debug.Log($"Invoking {OnValueChanged.GetPersistentEventCount()} listeners");
- OnValueChanged.Invoke(value);
- OnValueChangedVoid.Invoke();
- }
- /// <summary>
- /// Adds a listener to the value change event.
- /// </summary>
- /// <param name="callback">The callback to invoke when the value changes.</param>
- public void AddListener(UnityAction<T> callback)
- {
- if (callback == null) return;
- if (OnValueChanged == null) OnValueChanged = new UnityEvent<T>();
- OnValueChanged.AddListener(callback);
- }
- /// <summary>
- /// Adds a listener to the void value change event.
- /// </summary>
- /// <param name="callback">The callback to invoke when the value changes, without passing the new value.</param>
- public void AddListener(UnityAction callback)
- {
- if (callback == null) return;
- if (OnValueChangedVoid == null) OnValueChangedVoid = new UnityEvent();
- OnValueChangedVoid.AddListener(callback);
- }
- /// <summary>
- /// Removes a listener from the value change event.
- /// </summary>
- /// <param name="callback">The callback to remove.</param>
- public void RemoveListener(UnityAction<T> callback)
- {
- if (callback == null || OnValueChanged == null) return;
- OnValueChanged.RemoveListener(callback);
- }
- /// <summary>
- /// Removes a listener from the void value change event.
- /// </summary>
- /// <param name="callback">The callback to remove.</param>
- public void RemoveListener(UnityAction callback)
- {
- if (callback == null || OnValueChangedVoid == null) return;
- OnValueChangedVoid.RemoveListener(callback);
- }
- /// <summary>
- /// Removes all listeners from both value change events.
- /// </summary>
- public void RemoveAllListeners()
- {
- if (OnValueChanged != null) OnValueChanged.RemoveAllListeners();
- if (OnValueChangedVoid != null) OnValueChangedVoid.RemoveAllListeners();
- }
- /// <summary>
- /// Disposes the observer by removing all listeners and resetting the value.
- /// </summary>
- public void Dispose()
- {
- RemoveAllListeners();
- OnValueChanged = null;
- OnValueChangedVoid = null;
- value = default;
- }
- }
- using Sirenix.OdinInspector;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// Represents a stat with its type, name, and value.
- /// </summary>
- [Serializable]
- public class Stat
- {
- /// <summary>
- /// The type of the stat.
- /// </summary>
- [SerializeField]
- protected StatType statType;
- /// <summary>
- /// The primary stat, enabled only if the stat type is primary.
- /// </summary>
- [SerializeField]
- [ShowIf("IsPrimaryStat")]
- protected PrimaryStat primaryStat;
- /// <summary>
- /// The secondary stat, enabled only if the stat type is secondary.
- /// </summary>
- [SerializeField]
- [ShowIf("IsSecondaryStat")]
- protected SecondaryStat secondaryStat;
- /// <summary>
- /// The resistance stat, enabled only if the stat type is resistance.
- /// </summary>
- [SerializeField]
- [ShowIf("IsResistanceStat")]
- protected ResistanceStat resistanceStat;
- /// <summary>
- /// The name of the stat.
- /// </summary>
- [SerializeField]
- protected string statName;
- /// <summary>
- /// The value of the stat.
- /// </summary>
- [SerializeField]
- protected int statValue;
- /// <summary>
- /// Gets or sets the type of the stat.
- /// </summary>
- public StatType StatType { get { return statType; } set { statType = value; } }
- /// <summary>
- /// Gets or sets the primary stat.
- /// </summary>
- public PrimaryStat PrimaryStat { get { return primaryStat; } set { primaryStat = value; } }
- /// <summary>
- /// Gets or sets the secondary stat.
- /// </summary>
- public SecondaryStat SecondaryStat { get { return secondaryStat; } set { secondaryStat = value; } }
- /// <summary>
- /// Gets or sets the resistance stat.
- /// </summary>
- public ResistanceStat ResistanceStat { get { return resistanceStat; } set { resistanceStat = value; } }
- /// <summary>
- /// Gets or sets the name of the stat.
- /// </summary>
- public string StatName { get { return statName; } set { statName = value; } }
- /// <summary>
- /// Gets or sets the value of the stat.
- /// </summary>
- public int StatValue { get { return statValue; } set { statValue = value; } }
- /// <summary>
- /// Initializes a new instance of the <see cref="Stat"/> class.
- /// </summary>
- /// <param name="statType">The type of the stat.</param>
- /// <param name="statName">The name of the stat.</param>
- /// <param name="statValue">The value of the stat.</param>
- public Stat(StatType statType, string statName, int statValue)
- {
- this.statType = statType;
- this.statName = statName;
- this.statValue = statValue;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Stat"/> class with a primary stat.
- /// </summary>
- /// <param name="statType">The type of the stat.</param>
- /// <param name="primaryStat">The primary stat.</param>
- /// <param name="statName">The name of the stat.</param>
- /// <param name="statValue">The value of the stat.</param>
- public Stat(StatType statType, PrimaryStat primaryStat, string statName, int statValue)
- : this(statType, statName, statValue)
- {
- this.primaryStat = primaryStat;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Stat"/> class with a secondary stat.
- /// </summary>
- /// <param name="statType">The type of the stat.</param>
- /// <param name="secondaryStat">The secondary stat.</param>
- /// <param name="statName">The name of the stat.</param>
- /// <param name="statValue">The value of the stat.</param>
- public Stat(StatType statType, SecondaryStat secondaryStat, string statName, int statValue)
- : this(statType, statName, statValue)
- {
- this.secondaryStat = secondaryStat;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Stat"/> class with a resistance stat.
- /// </summary>
- /// <param name="statType">The type of the stat.</param>
- /// <param name="resistanceStat">The resistance stat.</param>
- /// <param name="statName">The name of the stat.</param>
- /// <param name="statValue">The value of the stat.</param>
- public Stat(StatType statType, ResistanceStat resistanceStat, string statName, int statValue)
- : this(statType, statName, statValue)
- {
- this.resistanceStat = resistanceStat;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Stat"/> class by copying another stat.
- /// </summary>
- /// <param name="statToCopy">The stat to copy.</param>
- public Stat(Stat statToCopy)
- {
- statType = statToCopy.statType;
- primaryStat = statToCopy.primaryStat;
- secondaryStat = statToCopy.secondaryStat;
- resistanceStat = statToCopy.resistanceStat;
- statName = statToCopy.statName;
- statValue = statToCopy.statValue;
- }
- /// <summary>
- /// Determines if the stat is a primary stat.
- /// Used by the inspector to conditionally show the primary stat field.
- /// </summary>
- private bool IsPrimaryStat()
- {
- return statType == StatType.Primary;
- }
- /// <summary>
- /// Determines if the stat is a secondary stat.
- /// Used by the inspector to conditionally show the secondary stat field.
- /// </summary>
- private bool IsSecondaryStat()
- {
- return statType == StatType.Secondary;
- }
- /// <summary>
- /// Determines if the stat is a resistance stat.
- /// Used by the inspector to conditionally show the resistance stat field.
- /// </summary>
- private bool IsResistanceStat()
- {
- return statType == StatType.Resistance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment