Actions

Actions are reusable, well, actions which can last longer than an instant. This could be anything from just a delayed action, a soundeffect to a player jump.

An action has its own update function completed?. It is called that way because it also returns the state of the action (if it is completed). There also the on_start and on_end functions which can be overriden and are called on the appropriate times.

An action is initiated via the App#act method. First, the Action#on_start method is called. After that, the Action#completed?function is called every frame until it returns true. Then Action#on_end is called and the action removed.

There are builtin actions as well. They are explained in the 'Builtin Actions' chapter of this book.

class IntervalAction < Scar::Action
    @elapsed = 0f32
    def initialize(@interval : Float32, @repeats : Int32, @f : Proc(Void))
    end
    
    def completed?(dt)
        return true if @repeats <= 0
        @elapsed += dt
        if @elapsed >= @interval
            f.call
            @repeats -= 1
            @elapsed = 0f32
        end
        return false
    end
end


# Beep every second five times
app.act IntervalAction.new(1f32, 5, -> { app.act Scar::Actions::PlaySound.new(Assets["beep.wav", Assets::Sound]) })

Last updated