GithubHelp home page GithubHelp logo

unity3d-timers's Introduction

License MIT

Unity3d-Timers

Timer class with various behaviors

About: Have a system allowing to easily create and extend complex timer behaviors.

Usage:

//Start simple repeater 
Timer.Repeater(5f, () => Debug.Log("Repeater test"));
//Start countdown and call Draw in 2.5 seconds
Timer.Countdown(2.5f, Draw);

Concept: A manager class pools and constructs new timers, timer store up to 4 handlers, and arbitrarely raise events in concrete implementation.

private class CountdownBehavior : TimerBehaviorBase, ITimerBehavior
{
    float exitTime;
    public void Initialize()
    {
        exitTime = f1;
    }

    public void Update(float deltaTime)
    {
        TimePassed += deltaTime;
        TotalTimeActive += deltaTime;

        if (TimePassed > exitTime)
        {
            Completed = true;
            c1();
        }
    }
}

Countdown inherits TimerBehaviorBase and implements ITimerBehavior. On top of that only concrete behavior in implemented that uses data and callbacks provided in constructor:

public static Timer Countdown(float exitTime, Action callback)
{
    Timer timer = TimerManager.getTimer();
    timer.SetBehavior<CountdownBehavior>();
    timer.behaviorBase
        .SetFloats(exitTime, 0, 0, 0)
        .SetCallbacks(callback, null, null, null);
    timer.behavior.Initialize();
    return timer;
}

as you can see you can set 4 floats, and 4 callbacks and use them in your implementation of behavior as you like. Examples

For concrete real example look at Ability class, it implements typical game ability, with cooldown.

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.