doodle/pkg/uix/scripting.go

151 lines
4.2 KiB
Go
Raw Normal View History

package uix
import (
2022-09-24 22:17:25 +00:00
"git.kirsle.net/SketchyMaze/doodle/pkg/doodads"
"git.kirsle.net/SketchyMaze/doodle/pkg/level"
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
"git.kirsle.net/SketchyMaze/doodle/pkg/scripting"
"git.kirsle.net/go/render"
)
// Functions relating to the Doodad JavaScript API for Canvas Actors.
// MakeScriptAPI makes several useful globals available to doodad
// scripts such as Actors.At()
func (w *Canvas) MakeScriptAPI(vm *scripting.VM) {
vm.Set("Actors", map[string]interface{}{
// Actors.At(Point)
"At": func(p render.Point) []*Actor {
var result = []*Actor{}
for _, actor := range w.actors {
var box = actor.Hitbox().AddPoint(actor.Position())
if actor != nil && p.Inside(box) {
result = append(result, actor)
}
}
return result
},
// Actors.FindPlayer: returns the nearest player character.
"FindPlayer": func() *Actor {
for _, actor := range w.actors {
if actor.IsPlayer() {
return actor
}
}
return nil
},
// Actors.New: create a new actor.
"New": func(filename string) *Actor {
doodad, err := doodads.LoadFile(filename)
if err != nil {
panic(err)
}
actor := NewActor("_new", &level.Actor{}, doodad)
w.AddActor(actor)
// Set up the player character's script in the VM.
if err := w.scripting.AddLevelScript(actor.ID(), filename); err != nil {
log.Error("Actors.New(%s): scripting.InstallActor(player) failed: %s", filename, err)
}
return actor
},
// Actors.SetPlayerCharacter: remake the player character.
"SetPlayerCharacter": func(filename string) {
if w.OnSetPlayerCharacter != nil {
w.OnSetPlayerCharacter(filename)
} else {
log.Error("Actors.SetPlayerCharacter: caller was not ready")
}
},
})
vm.Set("Level", map[string]interface{}{
"Difficulty": w.level.GameRule.Difficulty,
"ResetTimer": func() {
if w.OnResetTimer != nil {
w.OnResetTimer()
} else {
log.Error("Level.ResetTimer: caller was not ready")
}
},
})
}
// MakeSelfAPI generates the `Self` object for the scripting API in
// reference to a live Canvas actor in the level.
func (w *Canvas) MakeSelfAPI(actor *Actor) map[string]interface{} {
return map[string]interface{}{
"Filename": actor.Doodad().Filename,
"Title": actor.Doodad().Title,
// functions
"ID": actor.ID,
"Size": func() render.Rect {
var size = actor.Doodad().ChunkSize()
return render.NewRect(size, size)
},
Doodad/Actor Runtime Options * Add "Options" support for Doodads: these allow for individual Actor instances on your level to customize properties about the doodad. They're like "Tags" except the player can customize them on a per-actor basis. * Doodad Editor: you can specify the Options in the Doodad Properties window. * Level Editor: when the Actor Tool is selected, on mouse-over of an actor, clicking on the gear icon will open a new "Actor Properties" window which shows metadata (title, author, ID, position) and an Options tab to configure the actor's options. Updates to the scripting API: * Self.Options() returns a list of option names defined on the Doodad. * Self.GetOption(name) returns the value for the named option, or nil if neither the actor nor its doodad have the option defined. The return type will be correctly a string, boolean or integer type. Updates to the doodad command-line tool: * `doodad show` will print the Options on a .doodad file and, when showing a .level file with --actors, prints any customized Options with the actors. * `doodad edit-doodad` adds a --option parameter to define options. Options added to the game's built-in doodads: * Warp Doors: "locked (exit only)" will make it so the door can not be opened by the player, giving the "locked" message (as if it had no linked door), but the player may still exit from the door if sent by another warp door. * Electric Door & Electric Trapdoor: "opened" can make the door be opened by default when the level begins instead of closed. A switch or a button that removes power will close the door as normal. * Colored Doors & Small Key Door: "unlocked" will make the door unlocked at level start, not requiring a key to open it. * Colored Keys & Small Key: "has gravity" will make the key subject to gravity and set its Mobile flag so that if it falls onto a button, it will activate. * Gemstones: they had gravity by default; you can now uncheck "has gravity" to remove their Gravity and IsMobile status. * Gemstone Totems: "has gemstone" will set the totem to its unlocked status by default with the gemstone inserted. No power signal will be emitted; it is cosmetic only. * Fire Region: "name" can let you set a name for the fire region similarly to names for fire pixels: "Watch out for ${name}!" * Invisible Warp Door: "locked (exit only)" added as well.
2022-10-10 00:41:24 +00:00
"GetTag": actor.Doodad().Tag,
"Options": actor.Options,
"GetOption": func(name string) interface{} {
opt := actor.GetOption(name)
if opt == nil {
return nil
}
return opt.Value
},
"Position": actor.Position,
"MoveTo": func(p render.Point) {
actor.MoveTo(p)
actor.SetGrounded(false)
},
"Grounded": actor.Grounded,
"SetHitbox": actor.SetHitbox,
"Hitbox": actor.Hitbox,
"SetVelocity": actor.SetVelocity,
"GetVelocity": actor.Velocity,
"SetMobile": actor.SetMobile,
"SetInventory": actor.SetInventory,
"HasInventory": actor.HasInventory,
"SetGravity": actor.SetGravity,
"Invulnerable": actor.Invulnerable,
"SetInvulnerable": actor.SetInvulnerable,
"AddAnimation": actor.AddAnimation,
"IsAnimating": actor.IsAnimating,
"IsPlayer": actor.IsPlayer,
"PlayAnimation": actor.PlayAnimation,
"StopAnimation": actor.StopAnimation,
"ShowLayer": actor.ShowLayer,
"ShowLayerNamed": actor.ShowLayerNamed,
"Inventory": actor.Inventory,
"AddItem": actor.AddItem,
"RemoveItem": actor.RemoveItem,
"HasItem": actor.HasItem,
"ClearInventory": actor.ClearInventory,
"Destroy": actor.Destroy,
"Freeze": actor.Freeze,
"Unfreeze": actor.Unfreeze,
"IsWet": actor.IsWet,
"Hide": actor.Hide,
"Show": actor.Show,
"GetLinks": func() []map[string]interface{} {
var result = []map[string]interface{}{}
for _, linked := range w.GetLinkedActors(actor) {
result = append(result, w.MakeSelfAPI(linked))
}
return result
},
// Attract the camera's attention.
"CameraFollowMe": func() {
// Update the doodad that the camera should focus on.
w.FollowActor = actor.ID()
},
}
}