Giter Club home page Giter Club logo

dots-stats's Introduction

DOTS-Stats

DOTS-Stats is a high performance and scalable Stats-System.

Features

  • Maximum flexibility. Thanks to the pure component approach, it allows you to implement abilities, items, buffs, potions and make them work together as one single thing.
  • High performance. Zero GC allocations.
  • 100% ECS-ish. No more complex abstact OOP classes that work here but don't work there.
  • Ready-made base Authoring components for customization in the editor
  • Supports multiplying and additive stats
  • The minimum amount of boilerplate code.
  • Compatible with burst.
  • Compatible with il2cpp.

HowTo

Requirements

Min. Requirements: Unity >= 2019.4.18 and entities package >= 0.11.2-preview.1

Tested on: Unity 2020.2.3 and entities package 0.17.0-preview.42

Installation

You can install the repository using UPM: Just add this line in Packages/manifest.json:

"com.nanory.unity.entities.stats": "https://github.com/SinyavtsevIlya/DOTS-Stats.git",

Usage

Let's say you want to create an RPG'like stats system. When the player takes a weapon, his Attack stat grows. When he drops it, the stat returns to default.

1) Create a new Stat component you need

public struct Attack : IComponentData
{
    // The only constraint is you must have a float field. 
    public float Value; 
}

2) Create an Authoring for this Stat

public class AttackAuthoring : StatAuthoringBase<Attack>
{
    [SerializeField] float _value;
    // Simply return a new instance of "Attack" and set it's value from the serialized field. 
    protected override Attack GetStat() => new Attack() { Value = _value };
}

You'll get this:

Authoring

3) Create two gameobject in your scene: "Sword" and "Player". Add an AttackAuthoring on both of them.

And also add a StatReceiverTagAuthoring on the Player gameobject, to make him a "Stat-Reciever". (means that he able to accumulate stats from other contexts, e.g. items, buffs etc.)

Don't forget to add a ConvertToEntity on them to enable automatic conversion in a scene.

4) And add the test code that triggers sword equipement

// anywhere you want. Say on spacebar hit:
EntityManager.SetStatsChanged(swordEntity, playerEntity);

// and call this to deequip:
EntityManager.SetStatsRemoved(swordEntity);

5) And finally to make things work, add this system declaration somewhere

    [UpdateInGroup(typeof(StatSystemGroup))]
    public class CalculateAttackSystem : CalculateStatSystem<Attack> { }

You also able to add a system in a shorter way:

DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(_world, typeof(CalculateStatSystem<Attack>))

This step is recomended to be automated (code-gen, code-snippets, build processors or whatever you like)

Thats it!

FAQ

Ok. Does it work with Jobs/Burst?

Sure. Just use EnityCommandBuffer versions of extension methods:

entityCommandBuffer.SetStatsChanged(contextEntity, statReceiverEntity);
entityCommandBuffer.SetStatsRemoved(contextEntity);

How to react on Stat change on a stat-receiver-entity?

Just add a StatRecievedElementEvent component in your querry. Here is an example:

 Entities
    .ForEach((Entity ownerEntity, DynamicBuffer <StatRecievedElementEvent> statRecievedEvents, StatsWidget widget) =>
    {
        for (var idx = 0; idx < statRecievedEvents.Length; idx++)
        {
            var statRecievedEvent = statRecievedEvents[idx];

            if (statRecievedEvent.Is<Attack>())
                widget.SetAttack(EntityManager.GetComponentData<Attack>(ownerEntity).Value);

            if (statRecievedEvent.Is<AttackSpeed>())
                widget.SetAttackSpeed(EntityManager.GetComponentData<AttackSpeed>(ownerEntity).Value);
                
            // more stats here...
        }
    });

Since several stats can change at once during one frame, the event is presented as a DynamicBuffer of structs that clears automatically in the end of frame.

How it works under the hood

Self-editing Diagram

dots-stats's People

Contributors

sinyavtsevilya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

r2d2m gannicusy

dots-stats's Issues

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.