scripting

package
v0.0.0-...-79996cc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2023 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package scripting manages the JavaScript VMs for Doodad scripts.

Index

Constants

View Source
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

View Source
var (
	ErrReturnFalse = errors.New("JS callback function returned false")
)

Event return errors.

Functions

func ProxyLog

func ProxyLog(vm *VM, fn func(string, ...interface{})) func(string, ...interface{})

ProxyLog wraps a console.log function to inject the script's name.

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.

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 NewEvents

func NewEvents(vm *VM) *Events

NewEvents initializes the Events API.

func (*Events) OnCollide

func (e *Events) OnCollide(call goja.Callable) goja.Value

OnCollide fires when another actor collides with yours.

func (*Events) OnKeypress

func (e *Events) OnKeypress(call goja.Callable) goja.Value

OnKeypress fires when another actor collides with yours.

func (*Events) OnLeave

func (e *Events) OnLeave(call goja.Callable) goja.Value

OnLeave fires when another actor stops colliding with yours.

func (*Events) OnUse

func (e *Events) OnUse(call goja.Callable) goja.Value

OnUse fires when another actor collides with yours.

func (*Events) RunCollide

func (e *Events) RunCollide(v interface{}) error

RunCollide invokes the OnCollide handler function.

func (*Events) RunKeypress

func (e *Events) RunKeypress(ev keybind.State) error

RunKeypress invokes the OnCollide handler function.

func (*Events) RunLeave

func (e *Events) RunLeave(v interface{}) error

RunLeave invokes the OnLeave handler function.

func (*Events) RunUse

func (e *Events) RunUse(v interface{}) error

RunUse invokes the OnUse 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

func NewJSProxy(vm *VM) JSProxy

NewJSProxy initializes the API structure for JavaScript binding.

type Message

type Message struct {
	Name     string
	SenderID string
	Args     []goja.Value
}

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.

func (*Timer) Schedule

func (t *Timer) Schedule()

Schedule the callback to be run in the future.

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 NewVM

func NewVM(name string) *VM

NewVM creates a new JavaScript VM.

func (*VM) AddTimer

func (vm *VM) AddTimer(callback goja.Value, interval int, repeat bool) int

AddTimer loads timeouts and intervals into the VM's memory and returns the ID.

func (*VM) ClearTimer

func (vm *VM) ClearTimer(id int)

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) Get

func (vm *VM) Get(name string) goja.Value

Get a value from the VM.

func (*VM) Main

func (vm *VM) Main() error

Main calls the main function of the script.

func (*VM) RegisterLevelHooks

func (vm *VM) RegisterLevelHooks() error

RegisterLevelHooks registers accessors to the level hooks and Doodad API for Play Mode.

func (*VM) Run

func (vm *VM) Run(src string) (goja.Value, error)

Run code in the VM.

func (*VM) Set

func (vm *VM) Set(name string, v interface{}) error

Set a value in the VM.

func (*VM) SetInterval

func (vm *VM) SetInterval(callback goja.Value, interval int) int

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

func (vm *VM) SetTimeout(callback goja.Value, interval int) int

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.

func (*VM) TickTimer

func (vm *VM) TickTimer(now time.Time)

TickTimer checks if any timers are ready and calls their functions.

Directories

Path Synopsis
Package exceptions handles JavaScript errors nicely for the game.
Package exceptions handles JavaScript errors nicely for the game.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL