Git Product home page Git Product logo

nekostats's Introduction

Overview

NekoStats is a lightweight reactive stat system for Unity. It provides the building blocks for character stats, perks, buffs & debuffs, all that stat-related logic you might see in data-heavy genres such as rogue-likes and RPGs.

Key Features

(Click to expand each section)

Flexible value modification
Each stat instance maintains its own collection of modifiers for non-destructive value modification.
Stat speed = new Stat(10f);

StatModifier speedMultiplyModifier = new StatModifier(0.5f, StatModifierEffect.Mult);
StatModifier speedAddModifier = new StatModifier(2f, StatModifierEffect.Add);

// Add multiplicative modifier.
speed.AddModifier(speedMultiplyModifier);
Assert.AreEqual(speed.Value, 15f);

// Add additive modifier.
speed.AddModifier(speedAddModifier);
Assert.AreEqual(speed.Value, 17f);

// Remove multiplicative modifier.
speed.RemoveModifier(speedMultiplyModifier);
Assert.AreEqual(speed.Value, 12f);
Listen for changes
You can easily subcribe to value change events on a stat to avoid polling for changes every frame.
  • Subscribe to ValueChanged event to observe value
  • Subscribe to StatChanged event to observe stat instance
void OnEnable() {
    _character.Health.StatChanged += HandleHealthChanged;
}

void OnDisable() {
    _character.Health.StatChanged -= HandleHealthChanged;
}

void HandleHealthChanged(Stat stat) {
    Debug.Log("Character health has changed: " + stat.Value);
}
Optimized value caching
If a stat is marked dirty, its final value will be re-calculated upon next access, then cached until marked dirty again.
// Default configuration. Every tick, if the stat is marked dirty, invokes events once to notify pending changes.
Stat stat1 = new Stat().SetObserveMode(StatObserveMode.EveryTick);

// Directly invokes events to notify changes.
// Note this might cause the stat to be re-calculated multiple times in one frame.
Stat stat2 = new Stat().SetObserveMode(StatObserveMode.EveryChange);
Upper & lower bounds
Enable upper or lower bounds for a stat.

Changes in a stat's upper bound and lower bound will propagate to the stat and cause it to be marked dirty.

// currentHealth will be bounded between 0 and maxHealth.
Stat maxHealth = new Stat(500f);
Stat currentHealth = new Stat(500f).SetLowerBound().SetUpperBound(maxHealth);

Assert.AreEqual(maxHealth.Value, 500f);
Assert.AreEqual(currentHealth.Value, 500f);

// maxHealth has become lower than currentHealth.
// currentHealth is be marked dirty and re-calculated on next access.
maxHealth.Value = 450f;
Assert.AreEqual(maxHealth.Value, 450f);
Assert.AreEqual(currentHealth.Value, 450f);
Easily create and access stats
Manage stat creation with StatContainer. Access stats efficiently through custom enum keys.
    public enum AvatarStatType
    {
        MaxHealth,
        Health,
        MaxMana,
        Mana,
        Attack,
        Defence,
        Speed,
    }

    public class AvatarData : MonoBehaviour
    {
        [field: SerializeField] public AvatarConfig Config { get; private set; }
        [field: SerializeField] public StatContainer<AvatarStatType> Stats { get; private set; }
            = new StatContainer<AvatarStatType>();

        private void Awake()
        {
            Stats.RegisterStat(AvatarStatType.MaxHealth, Config.MaxHealth);
            Stats.RegisterResourceStat(AvatarStatType.Health, Config.MaxHealth, AvatarStatType.MaxHealth);
            Stats.RegisterStat(AvatarStatType.MaxMana, Config.MaxMana);
            Stats.RegisterResourceStat(AvatarStatType.Mana, Config.MaxMana, AvatarStatType.MaxMana);

            Stats.RegisterStat(AvatarStatType.Attack, Config.Attack).SetLowerBound(0f);
            Stats.RegisterStat(AvatarStatType.Defence, Config.Defence).SetLowerBound(0f);
            Stats.RegisterStat(AvatarStatType.Speed, Config.Speed).SetLowerBound(0f);
        }

        private void LateUpdate()
        {
            Stats.Tick();
        }

        public Stat GetStat(AvatarStatType statType)
        {
            return Stats.Get(statType);
        }
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.