Compare commits

..

No commits in common. "doodad-api-strong-typing" and "master" have entirely different histories.

6 changed files with 38 additions and 164 deletions

View File

@ -1,2 +0,0 @@
// Package api defines the JavaScript API surface area for Doodad scripts.
package api

View File

@ -1,32 +0,0 @@
package api
import "github.com/dop251/goja"
/*
Console is exported to the global scope with `console.log` and friends available.
These functions have an API similar to that found in web browsers and node.js.
*/
type Console struct {
Log func(string, ...interface{}) `json:"log"`
Debug func(string, ...interface{}) `json:"debug"`
Warn func(string, ...interface{}) `json:"warn"`
Error func(string, ...interface{}) `json:"error"`
}
// ToMap converts the struct into a generic hash map.
func (g Console) ToMap() map[string]interface{} {
return map[string]interface{}{
"log": g.Log,
"debug": g.Debug,
"warn": g.Warn,
"error": g.Error,
}
}
// Register the global functions in your JavaScript VM.
func (g Console) Register(vm *goja.Runtime) {
for k, v := range g.ToMap() {
vm.Set(k, v)
}
}

View File

@ -1,34 +0,0 @@
package api
import (
"git.kirsle.net/go/render"
"github.com/dop251/goja"
)
// GameplayLevelControl
type GameplayLevelControl struct {
// EndLevel ends the game in a "win" condition.
EndLevel func()
// FailLevel ends the game in a "fail" condition, with the message shown to the player.
FailLevel func(message string)
// SetCheckpoint updates the player's respawn position to the given point.
SetCheckpoint func(render.Point)
}
// ToMap converts the struct into a generic hash map.
func (g GameplayLevelControl) ToMap() map[string]interface{} {
return map[string]interface{}{
"EndLevel": g.EndLevel,
"FailLevel": g.FailLevel,
"SetCheckpoint": g.SetCheckpoint,
}
}
// Register the global functions in your JavaScript VM.
func (g GameplayLevelControl) Register(vm *goja.Runtime) {
for k, v := range g.ToMap() {
vm.Set(k, v)
}
}

View File

@ -1,61 +0,0 @@
// Package api defines the JavaScript API surface area for Doodad scripts.
package api
import (
"time"
"github.com/dop251/goja"
)
// Time functions exposed on the global scope like `time.Now()`.
//
// They are mainly a direct export of useful things from the Go `time` package.
type Time struct {
Now func() time.Time
Since func(time.Time) time.Duration
Add func(t time.Time, milliseconds int64) time.Time
// Multiples of time.Duration.
Hour time.Duration
Minute time.Duration
Second time.Duration
Millisecond time.Duration
Microsecond time.Duration
}
// NewTime creates an instanced global `time` object for your JavaScript VM.
func NewTime() Time {
return Time{
Now: time.Now,
Since: time.Since,
Add: func(t time.Time, ms int64) time.Time {
return t.Add(time.Duration(ms) * time.Millisecond)
},
Hour: time.Hour,
Minute: time.Minute,
Second: time.Second,
Millisecond: time.Millisecond,
Microsecond: time.Microsecond,
}
}
// ToMap converts the struct into a generic hash map.
func (g Time) ToMap() map[string]interface{} {
return map[string]interface{}{
"Now": g.Now,
"Since": g.Since,
"Add": g.Add,
"Hour": g.Hour,
"Minute": g.Minute,
"Second": g.Second,
"Millisecond": g.Millisecond,
"Microsecond": g.Microsecond,
}
}
// Register the global functions in your JavaScript VM.
func (g Time) Register(vm *goja.Runtime) {
for k, v := range g.ToMap() {
vm.Set(k, v)
}
}

View File

@ -2,10 +2,10 @@ package scripting
import (
"fmt"
"time"
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
"git.kirsle.net/SketchyMaze/doodle/pkg/physics"
"git.kirsle.net/SketchyMaze/doodle/pkg/scripting/api"
"git.kirsle.net/SketchyMaze/doodle/pkg/shmem"
"git.kirsle.net/SketchyMaze/doodle/pkg/sound"
"git.kirsle.net/go/render"
@ -29,17 +29,14 @@ func ProxyLog(vm *VM, fn func(string, ...interface{})) func(string, ...interface
// NewJSProxy initializes the API structure for JavaScript binding.
func NewJSProxy(vm *VM) JSProxy {
// jp := JSProxy{}
// return jp
return JSProxy{
// Console logging.
"console": api.Console{
Log: ProxyLog(vm, log.Info),
Debug: ProxyLog(vm, log.Debug),
Warn: ProxyLog(vm, log.Warn),
Error: ProxyLog(vm, log.Error),
}.ToMap(),
"console": map[string]interface{}{
"log": ProxyLog(vm, log.Info),
"debug": ProxyLog(vm, log.Debug),
"warn": ProxyLog(vm, log.Warn),
"error": ProxyLog(vm, log.Error),
},
// Audio API.
"Sound": map[string]interface{}{
@ -56,7 +53,18 @@ func NewJSProxy(vm *VM) JSProxy {
"GetTick": func() uint64 {
return shmem.Tick
},
"time": api.NewTime().ToMap(),
"time": map[string]interface{}{
"Now": time.Now,
"Since": time.Since,
"Add": func(t time.Time, ms int64) time.Time {
return t.Add(time.Duration(ms) * time.Millisecond)
},
"Hour": time.Hour,
"Minute": time.Minute,
"Second": time.Second,
"Millisecond": time.Millisecond,
"Microsecond": time.Microsecond,
},
// Bindings into the VM.
"Events": vm.Events,

View File

@ -1,9 +1,6 @@
package scripting
import (
"git.kirsle.net/SketchyMaze/doodle/pkg/scripting/api"
"git.kirsle.net/go/render"
)
import "git.kirsle.net/go/render"
/*
RegisterEventHooks attaches the supervisor level event hooks into a JS VM.
@ -16,26 +13,24 @@ Names registered:
- SetCheckpoint(): update the player's respawn location.
*/
func RegisterEventHooks(s *Supervisor, vm *VM) {
api.GameplayLevelControl{
EndLevel: func() {
if s.onLevelFail == nil {
panic("JS FailLevel(): No OnLevelFail handler attached to script supervisor")
}
s.onLevelExit()
},
FailLevel: func(message string) {
if s.onLevelFail == nil {
panic("JS FailLevel(): No OnLevelFail handler attached to script supervisor")
}
s.onLevelFail(message)
},
SetCheckpoint: func(p render.Point) {
if s.onSetCheckpoint == nil {
panic("JS SetCheckpoint(): No OnSetCheckpoint handler attached to script supervisor")
}
s.onSetCheckpoint(p)
},
}.Register(vm.vm)
vm.Set("EndLevel", func() {
if s.onLevelFail == nil {
panic("JS FailLevel(): No OnLevelFail handler attached to script supervisor")
}
s.onLevelExit()
})
vm.Set("FailLevel", func(message string) {
if s.onLevelFail == nil {
panic("JS FailLevel(): No OnLevelFail handler attached to script supervisor")
}
s.onLevelFail(message)
})
vm.Set("SetCheckpoint", func(p render.Point) {
if s.onSetCheckpoint == nil {
panic("JS SetCheckpoint(): No OnSetCheckpoint handler attached to script supervisor")
}
s.onSetCheckpoint(p)
})
}
// OnLevelExit registers an event hook for when a Level Exit doodad is reached.