mischief-ecs
Safe HaskellNone
LanguageGHC2024

Mischief.ECS.Tutorial.Guides.Time

Description

Synopsis

    Learn You an ECS for Great Mischief! - 2.8. Keeping Track of Time

    This chapter shows you how to get the Time and use Timers.

    Time

    To read time you can use the functions from:

    import Mischief.ECS.Time qualified as Time
    

    The most important one is delta, which gets you the time that has passed since the last frame, in seconds.

    d <- Time.delta
    

    You can also use Time.time to get the actual Time:

    t <- Time.time
    

    Time has two fields: .elapsed and .delta. Both contain a TimeSpec.

    You can convert a TimeSpec to seconds using specToSecs:

    let elapsed = Time.specToSecs t.elapsed
    let delta = Time.specToSecs t.delta
    

    Timer

    A Timer is a data type that can keep track of time and be ticked down each frame.

    import Mischief.ECS.Timer qualified as Timer
    

    There are two modes for a timer. A Repeat timer will infinitely loop around once it finished, while an Once timer stops after finishing once.

    To create a timer you must give it a duration and a mode:

    let timer = Timer.new 0.5 Timer.Repeat
    

    The tick function takes an amount of time (typically obtained from Time.delta) and returns a new timer, along with a bool specifying whether it just finished or not:

    delta <- Time.delta
    let (timer', justFinished) <- Timer.tick delta timer
    

    Next Chapter: Parallelism and Asynchronicity