Documentation ¶
Overview ¶
Package scripting manages the JavaScript VMs for Doodad scripts.
Index ¶
- Constants
- Variables
- func ProxyLog(vm *VM, fn func(string, ...interface{})) func(string, ...interface{})
- func RegisterEventHooks(s *Supervisor, vm *VM)
- func RegisterPublishHooks(s *Supervisor, vm *VM)
- type Events
- func (e *Events) OnCollide(call goja.Callable) goja.Value
- func (e *Events) OnKeypress(call goja.Callable) goja.Value
- func (e *Events) OnLeave(call goja.Callable) goja.Value
- func (e *Events) OnUse(call goja.Callable) goja.Value
- func (e *Events) RunCollide(v interface{}) error
- func (e *Events) RunKeypress(ev keybind.State) error
- func (e *Events) RunLeave(v interface{}) error
- func (e *Events) RunUse(v interface{}) error
- type JSProxy
- type Message
- type Supervisor
- func (s *Supervisor) AddLevelScript(id string, name string) error
- func (s *Supervisor) GetVM(name string) (*VM, error)
- func (s *Supervisor) InstallScripts(level *level.Level) error
- func (s *Supervisor) Loop() error
- func (s *Supervisor) OnLevelExit(handler func())
- func (s *Supervisor) OnLevelFail(handler func(string))
- func (s *Supervisor) OnSetCheckpoint(handler func(render.Point))
- func (s *Supervisor) RemoveVM(name string) error
- func (s *Supervisor) Teardown()
- func (s *Supervisor) To(name string) *VM
- type Timer
- type VM
- func (vm *VM) AddTimer(callback goja.Value, interval int, repeat bool) int
- func (vm *VM) ClearTimer(id int)
- func (vm *VM) Get(name string) goja.Value
- func (vm *VM) Main() error
- func (vm *VM) RegisterLevelHooks() error
- func (vm *VM) Run(src string) (goja.Value, error)
- func (vm *VM) Set(name string, v interface{}) error
- func (vm *VM) SetInterval(callback goja.Value, interval int) int
- func (vm *VM) SetTimeout(callback goja.Value, interval int) int
- func (vm *VM) TickTimer(now time.Time)
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 UseEvent = "OnUse" // player pressed the Use key while touching us // Controllable (player character) doodad events KeypressEvent = "OnKeypress" // i.e. arrow keys )
Event name constants.
Variables ¶
var (
ErrReturnFalse = errors.New("JS callback function returned false")
)
Event return errors.
Functions ¶
func RegisterEventHooks ¶
func RegisterEventHooks(s *Supervisor, vm *VM)
RegisterEventHooks attaches the supervisor level event hooks into a JS VM.
Names registered:
- EndLevel(): for a doodad to exit the level. Panics if the OnLevelExit handler isn't defined.
- FailLevel(): for a doodad to cause a level failure.
- SetCheckpoint(): update the player's respawn location.
func RegisterPublishHooks ¶
func RegisterPublishHooks(s *Supervisor, vm *VM)
RegisterPublishHooks adds the pub/sub hooks to a JavaScript VM.
This adds the global methods `Message.Subscribe(name, func)` and `Message.Publish(name, args)` to the JavaScript VM's scope.
Types ¶
type Events ¶
type Events struct {
// contains filtered or unexported fields
}
Events API for Doodad scripts.
func (*Events) OnKeypress ¶
OnKeypress fires when another actor collides with yours.
func (*Events) RunCollide ¶
RunCollide invokes the OnCollide handler function.
func (*Events) RunKeypress ¶
RunKeypress invokes the OnCollide handler function.
type JSProxy ¶
type JSProxy map[string]interface{}
JSProxy offers a function API interface to expose to Doodad javascripts. These methods safely give the JS access to important attributes and functions without exposing unintended API surface area in the process.
func NewJSProxy ¶
NewJSProxy initializes the API structure for JavaScript binding.
type Message ¶
Message holds data being published from one script VM with information sent to the linked VMs.
type Supervisor ¶
type Supervisor struct {
// contains filtered or unexported fields
}
Supervisor manages the JavaScript VMs for each doodad by its unique ID.
func NewSupervisor ¶
func NewSupervisor() *Supervisor
NewSupervisor creates a new JavaScript Supervior.
func (*Supervisor) AddLevelScript ¶
func (s *Supervisor) AddLevelScript(id string, name string) error
AddLevelScript adds a script to the supervisor with level hooks. The `id` will key the VM and should be the Actor ID in the level. The `name` is used to name the VM for debug logging.
func (*Supervisor) GetVM ¶
func (s *Supervisor) GetVM(name string) (*VM, error)
GetVM returns a script VM from the supervisor.
func (*Supervisor) InstallScripts ¶
func (s *Supervisor) InstallScripts(level *level.Level) error
InstallScripts loads scripts for all actors in the level.
func (*Supervisor) Loop ¶
func (s *Supervisor) Loop() error
Loop the supervisor to invoke timer events in any running scripts.
func (*Supervisor) OnLevelExit ¶
func (s *Supervisor) OnLevelExit(handler func())
OnLevelExit registers an event hook for when a Level Exit doodad is reached.
func (*Supervisor) OnLevelFail ¶
func (s *Supervisor) OnLevelFail(handler func(string))
OnLevelFail registers an event hook for level failures (doodads killing the player).
func (*Supervisor) OnSetCheckpoint ¶
func (s *Supervisor) OnSetCheckpoint(handler func(render.Point))
OnSetCheckpoint registers an event hook for setting player checkpoints.
func (*Supervisor) RemoveVM ¶
func (s *Supervisor) RemoveVM(name string) error
RemoveVM removes a script from the supervisor, stopping it.
func (*Supervisor) Teardown ¶
func (s *Supervisor) Teardown()
Teardown the supervisor to clean up goroutines.
func (*Supervisor) To ¶
func (s *Supervisor) To(name string) *VM
To returns the VM for a named script.
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
Timer keeps track of delayed function calls for the scripting engine.
type VM ¶
type VM struct { Name string // Globals available to the scripts. Events *Events Self interface{} // 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 // contains filtered or unexported fields }
VM manages a single isolated JavaScript VM.
func (*VM) AddTimer ¶
AddTimer loads timeouts and intervals into the VM's memory and returns the ID.
func (*VM) ClearTimer ¶
ClearTimer will clear both timeouts and intervals.
In the JavaScript VM this function is bound to clearTimeout() and clearInterval() to expose an API like that seen in web browsers.
func (*VM) RegisterLevelHooks ¶
RegisterLevelHooks registers accessors to the level hooks and Doodad API for Play Mode.
func (*VM) SetInterval ¶
SetInterval registers a callback function to be run repeatedly.
Returns the ID number of the timer in case you want to clear it. The underlying Timer type is NOT exposed to JavaScript.
func (*VM) SetTimeout ¶
SetTimeout registers a callback function to be run after a while.
This is to be called by JavaScript running in the VM and has an API similar to that found in web browsers.
The callback is a JavaScript function and the interval is in milliseconds, with 1000 being 'one second.'
Returns the ID number of the timer in case you want to clear it. The underlying Timer type is NOT exposed to JavaScript.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package exceptions handles JavaScript errors nicely for the game.
|
Package exceptions handles JavaScript errors nicely for the game. |