doodle/pkg/scripting/js_api.go
Noah Petherbridge 9201475060 Update Doodad JS API + Hostile mobs
New functions are available on the JavaScript API for doodads:

* Actors.At(Point) []*Actor: returns actors intersecting a point
* Actors.FindPlayer() *Actor: returns the nearest player character
* Actors.New(filename string): create a new actor (NOT TESTED YET!)
* Self.Grounded() bool: query the grounded status of current actor

With this the game's built-in doodads have been revised:

* Bird: will now scan 240 pixels diagonally searching for the player
  character and will dive if seen. The Bird is dangerous while
  diving. It will return to its original altitude once it touches
  the ground.
* Azulians: the Azulians are now dangerous to player characters but
  not to the Thief. Azulians will begin to follow the player when
  they are within the aggro range and will hop if the player is
  above them to try and overcome obstacles.
  * Blue Azulian: aggro is (250, 100) jump speed 12 movement 2
  * Red Azulian: aggro is (250, 200) jump speed 14 movement 4
2022-01-17 21:28:05 -08:00

70 lines
1.7 KiB
Go

package scripting
import (
"time"
"git.kirsle.net/apps/doodle/pkg/log"
"git.kirsle.net/apps/doodle/pkg/physics"
"git.kirsle.net/apps/doodle/pkg/shmem"
"git.kirsle.net/apps/doodle/pkg/sound"
"git.kirsle.net/go/render"
)
// SEE ALSO: uix/scripting.go for more global functions
// 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.
type JSProxy map[string]interface{}
// NewJSProxy initializes the API structure for JavaScript binding.
func NewJSProxy(vm *VM) JSProxy {
return JSProxy{
// Console logging.
"console": map[string]interface{}{
"log": log.Info,
"debug": log.Debug,
"warn": log.Warn,
"error": log.Error,
},
// Audio API.
"Sound": map[string]interface{}{
"Play": sound.PlaySound,
},
// Type constructors.
"RGBA": render.RGBA,
"Point": render.NewPoint,
"Vector": physics.NewVector,
// Useful types and functions.
"Flash": shmem.Flash,
"GetTick": func() uint64 {
return shmem.Tick
},
"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,
"setTimeout": vm.SetTimeout,
"setInterval": vm.SetInterval,
"clearTimeout": vm.ClearTimer,
"clearInterval": vm.ClearTimer,
// Self for an actor to inspect themselves.
"Self": vm.Self,
}
}