2021-01-03 23:19:21 +00:00
|
|
|
package uix
|
|
|
|
|
2022-01-18 05:28:05 +00:00
|
|
|
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"
|
2022-01-18 05:28:05 +00:00
|
|
|
"git.kirsle.net/go/render"
|
|
|
|
)
|
2021-08-09 03:10:42 +00:00
|
|
|
|
2021-01-03 23:19:21 +00:00
|
|
|
// Functions relating to the Doodad JavaScript API for Canvas Actors.
|
|
|
|
|
2022-01-18 05:28:05 +00:00
|
|
|
// 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
|
|
|
|
},
|
2022-01-19 05:24:36 +00:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
},
|
2022-01-18 05:28:05 +00:00
|
|
|
})
|
2022-03-07 06:16:09 +00:00
|
|
|
|
|
|
|
vm.Set("Level", map[string]interface{}{
|
2022-03-26 20:55:06 +00:00
|
|
|
"Difficulty": w.level.GameRule.Difficulty,
|
2022-03-27 18:51:14 +00:00
|
|
|
"ResetTimer": func() {
|
|
|
|
if w.OnResetTimer != nil {
|
|
|
|
w.OnResetTimer()
|
|
|
|
} else {
|
|
|
|
log.Error("Level.ResetTimer: caller was not ready")
|
|
|
|
}
|
|
|
|
},
|
2022-03-07 06:16:09 +00:00
|
|
|
})
|
2022-01-18 05:28:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 23:19:21 +00:00
|
|
|
// 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
|
2021-09-03 05:33:28 +00:00
|
|
|
"ID": actor.ID,
|
|
|
|
"Size": func() render.Rect {
|
|
|
|
var size = actor.Doodad().ChunkSize()
|
|
|
|
return render.NewRect(size, size)
|
|
|
|
},
|
2021-08-09 03:10:42 +00:00
|
|
|
"GetTag": actor.Doodad().Tag,
|
|
|
|
"Position": actor.Position,
|
|
|
|
"MoveTo": func(p render.Point) {
|
|
|
|
actor.MoveTo(p)
|
|
|
|
actor.SetGrounded(false)
|
|
|
|
},
|
2022-02-20 19:48:36 +00:00
|
|
|
"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,
|
2022-05-06 04:35:32 +00:00
|
|
|
"IsWet": actor.IsWet,
|
2022-02-20 19:48:36 +00:00
|
|
|
"Hide": actor.Hide,
|
|
|
|
"Show": actor.Show,
|
2021-01-03 23:19:21 +00:00
|
|
|
"GetLinks": func() []map[string]interface{} {
|
|
|
|
var result = []map[string]interface{}{}
|
|
|
|
for _, linked := range w.GetLinkedActors(actor) {
|
|
|
|
result = append(result, w.MakeSelfAPI(linked))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|