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)
98 lines
2.0 KiB
Go
98 lines
2.0 KiB
Go
package scripting
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
|
"github.com/robertkrimen/otto"
|
|
)
|
|
|
|
// VM manages a single isolated JavaScript VM.
|
|
type VM struct {
|
|
Name string
|
|
|
|
// Globals available to the scripts.
|
|
Events *Events
|
|
Self interface{}
|
|
|
|
vm *otto.Otto
|
|
|
|
// setTimeout and setInterval variables.
|
|
timerLastID int // becomes 1 when first timer is set
|
|
timers map[int]*Timer
|
|
}
|
|
|
|
// NewVM creates a new JavaScript VM.
|
|
func NewVM(name string) *VM {
|
|
vm := &VM{
|
|
Name: name,
|
|
Events: NewEvents(),
|
|
vm: otto.New(),
|
|
timers: map[int]*Timer{},
|
|
}
|
|
return vm
|
|
}
|
|
|
|
// Run code in the VM.
|
|
func (vm *VM) Run(src interface{}) (otto.Value, error) {
|
|
v, err := vm.vm.Run(src)
|
|
return v, err
|
|
}
|
|
|
|
// Set a value in the VM.
|
|
func (vm *VM) Set(name string, v interface{}) error {
|
|
return vm.vm.Set(name, v)
|
|
}
|
|
|
|
// RegisterLevelHooks registers accessors to the level hooks
|
|
// and Doodad API for Play Mode.
|
|
func (vm *VM) RegisterLevelHooks() error {
|
|
bindings := map[string]interface{}{
|
|
"log": log.Logger,
|
|
"RGBA": render.RGBA,
|
|
"Point": render.NewPoint,
|
|
"Self": vm.Self, // i.e., the uix.Actor object
|
|
"Events": vm.Events,
|
|
|
|
// Timer functions with APIs similar to the web browsers.
|
|
"setTimeout": vm.SetTimeout,
|
|
"setInterval": vm.SetInterval,
|
|
"clearTimeout": vm.ClearTimer,
|
|
"clearInterval": vm.ClearTimer,
|
|
}
|
|
for name, v := range bindings {
|
|
err := vm.vm.Set(name, v)
|
|
if err != nil {
|
|
return fmt.Errorf("RegisterLevelHooks(%s): %s",
|
|
name, err,
|
|
)
|
|
}
|
|
}
|
|
|
|
// Alias the console.log functions to the logger.
|
|
vm.vm.Run(`
|
|
console = {};
|
|
console.log = log.Info;
|
|
console.debug = log.Debug;
|
|
console.warn = log.Warn;
|
|
console.error = log.Error;
|
|
`)
|
|
return nil
|
|
}
|
|
|
|
// Main calls the main function of the script.
|
|
func (vm *VM) Main() error {
|
|
function, err := vm.vm.Get("main")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !function.IsFunction() {
|
|
return nil
|
|
}
|
|
|
|
_, err = function.Call(otto.Value{})
|
|
return err
|
|
}
|