2019-04-16 06:07:15 +00:00
|
|
|
package scripting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-06 02:04:02 +00:00
|
|
|
"reflect"
|
|
|
|
"time"
|
2019-04-16 06:07:15 +00:00
|
|
|
|
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
2019-06-27 18:57:26 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/shmem"
|
2019-04-16 06:07:15 +00:00
|
|
|
"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{}
|
|
|
|
|
2019-06-24 00:30:12 +00:00
|
|
|
// Channels for inbound and outbound PubSub messages.
|
|
|
|
// Each VM has a single Inbound channel that watches for received messages
|
|
|
|
// and invokes the Message.Subscribe() handlers for relevant ones.
|
|
|
|
// Each VM also has an array of Outbound channels which map to the Inbound
|
|
|
|
// channel of the VMs it is linked to, for pushing out Message.Publish()
|
|
|
|
// messages.
|
|
|
|
Inbound chan Message
|
|
|
|
Outbound []chan Message
|
|
|
|
subscribe map[string][]otto.Value // Subscribed message handlers by name.
|
|
|
|
|
2019-04-16 06:07:15 +00:00
|
|
|
vm *otto.Otto
|
2019-04-19 01:15:05 +00:00
|
|
|
|
|
|
|
// setTimeout and setInterval variables.
|
|
|
|
timerLastID int // becomes 1 when first timer is set
|
|
|
|
timers map[int]*Timer
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewVM creates a new JavaScript VM.
|
|
|
|
func NewVM(name string) *VM {
|
|
|
|
vm := &VM{
|
|
|
|
Name: name,
|
|
|
|
Events: NewEvents(),
|
|
|
|
vm: otto.New(),
|
2019-04-19 01:15:05 +00:00
|
|
|
timers: map[int]*Timer{},
|
2019-06-24 00:30:12 +00:00
|
|
|
|
|
|
|
// Pub/sub structs.
|
|
|
|
Inbound: make(chan Message),
|
|
|
|
Outbound: []chan Message{},
|
|
|
|
subscribe: map[string][]otto.Value{},
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
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,
|
2019-06-27 18:57:26 +00:00
|
|
|
"Flash": shmem.Flash,
|
2019-04-16 06:07:15 +00:00
|
|
|
"RGBA": render.RGBA,
|
|
|
|
"Point": render.NewPoint,
|
2019-04-19 01:15:05 +00:00
|
|
|
"Self": vm.Self, // i.e., the uix.Actor object
|
2019-04-16 06:07:15 +00:00
|
|
|
"Events": vm.Events,
|
Improve Collision Detection: More Active w/ Actors
* Improve the collision detection algorithm so that Actor OnCollide
scripts get called more often WHILE an actor is moving, to prevent a
fast-moving actor from zipping right through the "solid" hitbox and
not giving the subject actor time to protest the movement.
* It's implemented by adding a `Settled` boolean to the OnCollide event
object. When the game is testing out movement, Settled=false to give
the actor a chance to say "I'm solid!" and have the moving party be
stopped early.
* After all this is done, for any pair of actors still with overlapping
hitboxes, OnCollide is called one last time with Settled=true. This is
when the actor should run its actions (like publishing messages to
other actors, changing state as in a trapdoor, etc.)
* The new collision detection algorithm works as follows:
* Stage 1 is the same as before, all mobile actors are moved and
tested against level geometry. They record their Original and New
position during this phase.
* Stage 2 is where we re-run that movement but ping actors being
intersected each step of the way. We trace the steps between
Original and New position, test OnCollide handler, and if it returns
false we move the mobile actor to the Last Good Position along the
trace.
* Stage 3 we run the final OnCollide(Settled=true) to let actors run
actions they wanted to for their collide handler, WITHOUT spamming
those actions during Stage 2.
* This should now allow for tweaking of gravity speed and player speed
without breaking all actor collision checking.
2019-07-17 04:07:38 +00:00
|
|
|
"GetTick": func() uint64 {
|
|
|
|
return shmem.Tick
|
|
|
|
},
|
2019-04-19 01:15:05 +00:00
|
|
|
|
2019-05-06 02:04:02 +00:00
|
|
|
"TypeOf": reflect.TypeOf,
|
|
|
|
"time": map[string]interface{}{
|
|
|
|
"Now": time.Now,
|
|
|
|
"Add": func(t time.Time, ms int64) time.Time {
|
|
|
|
return t.Add(time.Duration(ms) * time.Millisecond)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// Timer functions with APIs similar to the web browsers.
|
|
|
|
"setTimeout": vm.SetTimeout,
|
|
|
|
"setInterval": vm.SetInterval,
|
|
|
|
"clearTimeout": vm.ClearTimer,
|
|
|
|
"clearInterval": vm.ClearTimer,
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
for name, v := range bindings {
|
|
|
|
err := vm.vm.Set(name, v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("RegisterLevelHooks(%s): %s",
|
|
|
|
name, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 01:15:05 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
`)
|
2019-04-16 06:07:15 +00:00
|
|
|
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() {
|
2019-04-19 01:15:05 +00:00
|
|
|
return nil
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 05:02:59 +00:00
|
|
|
// Catch panics.
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
log.Error("Panic caught in JavaScript VM: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-04-16 06:07:15 +00:00
|
|
|
_, err = function.Call(otto.Value{})
|
|
|
|
return err
|
|
|
|
}
|