2019-04-16 06:07:15 +00:00
|
|
|
package scripting
|
|
|
|
|
|
|
|
import (
|
2019-05-07 05:57:32 +00:00
|
|
|
"errors"
|
2020-01-03 01:58:22 +00:00
|
|
|
"sync"
|
2019-05-07 05:57:32 +00:00
|
|
|
|
2019-12-23 02:21:58 +00:00
|
|
|
"git.kirsle.net/go/render/event"
|
2019-04-16 06:07:15 +00:00
|
|
|
"github.com/robertkrimen/otto"
|
|
|
|
)
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
2019-05-07 05:57:32 +00:00
|
|
|
// Event return errors.
|
|
|
|
var (
|
|
|
|
ErrReturnFalse = errors.New("JS callback function returned false")
|
|
|
|
)
|
|
|
|
|
2019-04-16 06:07:15 +00:00
|
|
|
// Events API for Doodad scripts.
|
|
|
|
type Events struct {
|
|
|
|
registry map[string][]otto.Value
|
2020-01-03 01:58:22 +00:00
|
|
|
lock sync.RWMutex
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-04-19 01:15:05 +00:00
|
|
|
return e.register(CollideEvent, call.Argument(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCollide invokes the OnCollide handler function.
|
2019-05-07 05:57:32 +00:00
|
|
|
func (e *Events) RunCollide(v interface{}) error {
|
|
|
|
return e.run(CollideEvent, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnLeave fires when another actor stops colliding with yours.
|
|
|
|
func (e *Events) OnLeave(call otto.FunctionCall) otto.Value {
|
|
|
|
return e.register(LeaveEvent, call.Argument(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunLeave invokes the OnLeave handler function.
|
|
|
|
func (e *Events) RunLeave(v interface{}) error {
|
|
|
|
return e.run(LeaveEvent, v)
|
2019-04-19 01:15:05 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 21:03:20 +00:00
|
|
|
// OnKeypress fires when another actor collides with yours.
|
|
|
|
func (e *Events) OnKeypress(call otto.FunctionCall) otto.Value {
|
|
|
|
return e.register(KeypressEvent, call.Argument(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunKeypress invokes the OnCollide handler function.
|
2019-12-22 22:11:01 +00:00
|
|
|
func (e *Events) RunKeypress(ev *event.State) error {
|
2019-05-05 21:03:20 +00:00
|
|
|
return e.run(KeypressEvent, ev)
|
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// register a named event.
|
|
|
|
func (e *Events) register(name string, callback otto.Value) otto.Value {
|
2019-04-16 06:07:15 +00:00
|
|
|
if !callback.IsFunction() {
|
|
|
|
return otto.Value{} // TODO
|
|
|
|
}
|
|
|
|
|
2020-01-03 01:58:22 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
if _, ok := e.registry[name]; !ok {
|
|
|
|
e.registry[name] = []otto.Value{}
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
e.registry[name] = append(e.registry[name], callback)
|
2019-04-16 06:07:15 +00:00
|
|
|
return otto.Value{}
|
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// 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 {
|
2020-01-03 01:58:22 +00:00
|
|
|
e.lock.RLock()
|
|
|
|
defer e.lock.RUnlock()
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
if _, ok := e.registry[name]; !ok {
|
2019-04-16 06:07:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
for _, callback := range e.registry[name] {
|
2019-05-07 05:57:32 +00:00
|
|
|
value, err := callback.Call(otto.Value{}, args...)
|
2019-04-16 06:07:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-07 05:57:32 +00:00
|
|
|
|
|
|
|
// If the event handler returned a boolean false, stop all other
|
|
|
|
// callbacks and return the boolean.
|
|
|
|
if value.IsBoolean() {
|
|
|
|
if b, err := value.ToBoolean(); err == nil && b == false {
|
|
|
|
return ErrReturnFalse
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|