Noah Petherbridge
258b2eb285
* CLI: fix the `doodad convert` command to share the same Palette when converting each frame (layer) of a doodad so subsequent layers find the correct color swatches for serialization. * Scripting: add timers and intervals to Doodad scripts to allow them to animate themselves or add delayed callbacks. The timers have the same API as a web browser: setTimeout(), setInterval(), clearTimeout(), clearInterval(). * Add support for uix.Actor to change its currently rendered layer in the level. For example a Button Doodad can set its image to Layer 1 (pressed) when touched by the player, and Trapdoors can cycle through their layers to animate opening and closing. * Usage from a Doodad script: Self.ShowLayer(1) * Default Doodads: added scripts for all Buttons, Doors, Keys and the Trapdoor to run their various animations when touched (in the case of Keys, destroy themselves when touched, because there is no player inventory yet)
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package scripting
|
|
|
|
import (
|
|
"github.com/robertkrimen/otto"
|
|
)
|
|
|
|
// Event name constants.
|
|
const (
|
|
CollideEvent = "OnCollide" // another doodad collides with us
|
|
EnterEvent = "OnEnter" // a doodad is fully inside us
|
|
LeaveEvent = "OnLeave" // a doodad no longer collides with us
|
|
|
|
// Controllable (player character) doodad events
|
|
KeypressEvent = "OnKeypress" // i.e. arrow keys
|
|
)
|
|
|
|
// Events API for Doodad scripts.
|
|
type Events struct {
|
|
registry map[string][]otto.Value
|
|
}
|
|
|
|
// NewEvents initializes the Events API.
|
|
func NewEvents() *Events {
|
|
return &Events{
|
|
registry: map[string][]otto.Value{},
|
|
}
|
|
}
|
|
|
|
// OnCollide fires when another actor collides with yours.
|
|
func (e *Events) OnCollide(call otto.FunctionCall) otto.Value {
|
|
return e.register(CollideEvent, call.Argument(0))
|
|
}
|
|
|
|
// RunCollide invokes the OnCollide handler function.
|
|
func (e *Events) RunCollide() error {
|
|
return e.run(CollideEvent)
|
|
}
|
|
|
|
// register a named event.
|
|
func (e *Events) register(name string, callback otto.Value) otto.Value {
|
|
if !callback.IsFunction() {
|
|
return otto.Value{} // TODO
|
|
}
|
|
|
|
if _, ok := e.registry[name]; !ok {
|
|
e.registry[name] = []otto.Value{}
|
|
}
|
|
|
|
e.registry[name] = append(e.registry[name], callback)
|
|
return otto.Value{}
|
|
}
|
|
|
|
// Run an event handler. Returns an error only if there was a JavaScript error
|
|
// inside the function. If there are no event handlers, just returns nil.
|
|
func (e *Events) run(name string, args ...interface{}) error {
|
|
if _, ok := e.registry[name]; !ok {
|
|
return nil
|
|
}
|
|
|
|
for _, callback := range e.registry[name] {
|
|
_, err := callback.Call(otto.Value{}, args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|