2019-04-10 02:17:56 +00:00
|
|
|
package uix
|
|
|
|
|
|
|
|
import (
|
2019-04-16 06:07:15 +00:00
|
|
|
"errors"
|
2021-06-17 05:35:01 +00:00
|
|
|
"fmt"
|
2022-01-17 04:20:48 +00:00
|
|
|
"sort"
|
2021-06-13 21:53:21 +00:00
|
|
|
"strings"
|
2019-04-10 02:17:56 +00:00
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/doodads"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/level"
|
2023-02-19 01:37:54 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/license/levelsigning"
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/scripting"
|
2022-09-25 04:58:01 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/scripting/exceptions"
|
2019-12-31 02:13:28 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2019-04-10 02:17:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InstallActors adds external Actors to the canvas to be superimposed on top
|
|
|
|
// of the drawing.
|
|
|
|
func (w *Canvas) InstallActors(actors level.ActorMap) error {
|
2021-06-13 21:53:21 +00:00
|
|
|
var errs []string
|
|
|
|
|
2022-01-17 04:20:48 +00:00
|
|
|
// Order the actors deterministically, by their ID string. Actors get
|
|
|
|
// a time-ordered UUID ID by default so the most recently added actor
|
|
|
|
// should render on top of the others.
|
|
|
|
var actorIDs []string
|
|
|
|
for id := range actors {
|
|
|
|
actorIDs = append(actorIDs, id)
|
|
|
|
}
|
|
|
|
sort.Strings(actorIDs)
|
|
|
|
|
2022-04-09 21:41:24 +00:00
|
|
|
// In case we are replacing the actors, free up all their textures first!
|
|
|
|
for _, actor := range w.actors {
|
|
|
|
actor.Canvas.Destroy()
|
|
|
|
}
|
|
|
|
|
2023-02-19 01:37:54 +00:00
|
|
|
// Signed Levels: the free version normally won't load embedded assets from
|
|
|
|
// a level and the call to LoadFromEmbeddable below returns the error. If the
|
|
|
|
// level is signed it is allowed to use its embedded assets.
|
|
|
|
isSigned := w.IsSignedLevelPack != nil || levelsigning.IsLevelSigned(w.level)
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
w.actors = make([]*Actor, 0)
|
2022-01-17 04:20:48 +00:00
|
|
|
for _, id := range actorIDs {
|
|
|
|
var actor = actors[id]
|
2023-02-19 01:37:54 +00:00
|
|
|
|
|
|
|
// Try loading the doodad from the level's own attached files.
|
|
|
|
doodad, err := doodads.LoadFromEmbeddable(actor.Filename, w.level, isSigned)
|
2019-04-10 02:17:56 +00:00
|
|
|
if err != nil {
|
2023-02-19 01:37:54 +00:00
|
|
|
// If we have a signed levelpack, try loading from the levelpack.
|
|
|
|
if w.IsSignedLevelPack != nil {
|
|
|
|
if found, err := doodads.LoadFromEmbeddable(actor.Filename, w.IsSignedLevelPack, true); err == nil {
|
|
|
|
doodad = found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not found, append the error and continue.
|
|
|
|
if doodad == nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("%s: %s", actor.Filename, err.Error()))
|
|
|
|
continue
|
|
|
|
}
|
2019-04-10 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-14 22:25:03 +00:00
|
|
|
// Create the "live" Actor to exist in the world, and set its world
|
|
|
|
// position to the Point defined in the level data.
|
|
|
|
liveActor := NewActor(id, actor, doodad)
|
2022-01-09 21:16:29 +00:00
|
|
|
liveActor.Canvas.parent = w
|
2023-04-09 04:26:08 +00:00
|
|
|
liveActor.LevelCanvas = w
|
2019-04-14 22:25:03 +00:00
|
|
|
liveActor.MoveTo(actor.Point)
|
|
|
|
|
|
|
|
w.actors = append(w.actors, liveActor)
|
2019-04-10 02:17:56 +00:00
|
|
|
}
|
2021-06-13 21:53:21 +00:00
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errors.New(strings.Join(errs, "\n"))
|
|
|
|
}
|
2019-04-10 02:17:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:41:53 +00:00
|
|
|
// Actors returns the list of actors currently in the Canvas.
|
|
|
|
func (w *Canvas) Actors() []*Actor {
|
|
|
|
return w.actors
|
|
|
|
}
|
|
|
|
|
2019-07-07 06:28:11 +00:00
|
|
|
// ClearActors removes all the actors from the Canvas.
|
|
|
|
func (w *Canvas) ClearActors() {
|
|
|
|
w.actors = []*Actor{}
|
|
|
|
}
|
|
|
|
|
2019-04-16 06:07:15 +00:00
|
|
|
// SetScriptSupervisor assigns the Canvas scripting supervisor to enable
|
|
|
|
// interaction with actor scripts.
|
|
|
|
func (w *Canvas) SetScriptSupervisor(s *scripting.Supervisor) {
|
|
|
|
w.scripting = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstallScripts loads all the current actors' scripts into the scripting
|
|
|
|
// engine supervisor.
|
|
|
|
func (w *Canvas) InstallScripts() error {
|
|
|
|
if w.scripting == nil {
|
|
|
|
return errors.New("no script supervisor is configured for this canvas")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(w.actors) == 0 {
|
|
|
|
return errors.New("no actors exist in this canvas to install scripts for")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, actor := range w.actors {
|
|
|
|
vm := w.scripting.To(actor.ID())
|
2020-04-22 06:50:45 +00:00
|
|
|
|
2022-01-19 05:24:36 +00:00
|
|
|
if vm.Self != nil {
|
|
|
|
// Already initialized!
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-22 06:50:45 +00:00
|
|
|
// Security: expose a selective API to the actor to the JS engine.
|
2021-01-03 23:19:21 +00:00
|
|
|
vm.Self = w.MakeSelfAPI(actor)
|
2022-01-18 05:28:05 +00:00
|
|
|
w.MakeScriptAPI(vm)
|
2019-04-16 06:07:15 +00:00
|
|
|
vm.Set("Self", vm.Self)
|
2019-04-19 05:02:59 +00:00
|
|
|
|
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
|
|
|
// If there is no script attached, do not try and load or call the main() function.
|
|
|
|
if actor.Doodad().Script == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-05 04:00:32 +00:00
|
|
|
if _, err := vm.Run(actor.Doodad().Script); err != nil {
|
2019-04-19 05:02:59 +00:00
|
|
|
log.Error("Run script for actor %s failed: %s", actor.ID(), err)
|
|
|
|
}
|
2019-04-16 06:07:15 +00:00
|
|
|
|
|
|
|
// Call the main() function.
|
|
|
|
if err := vm.Main(); err != nil {
|
2024-02-08 06:14:48 +00:00
|
|
|
exceptions.FormatAndCatch(
|
|
|
|
nil,
|
2022-09-25 04:58:01 +00:00
|
|
|
"Error in main() for actor %s:\n\n%s\n\nActor ID: %s\nFilename: %s\nPosition: %s",
|
|
|
|
actor.Actor.Filename,
|
|
|
|
err,
|
|
|
|
actor.ID(),
|
|
|
|
actor.Actor.Filename,
|
|
|
|
actor.Position(),
|
|
|
|
)
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 03:52:16 +00:00
|
|
|
// Broadcast the "ready" signal to any actors that want to publish
|
|
|
|
// messages ASAP on level start.
|
|
|
|
for _, actor := range w.actors {
|
|
|
|
w.scripting.To(actor.ID()).Inbound <- scripting.Message{
|
|
|
|
Name: "broadcast:ready",
|
|
|
|
Args: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 06:07:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
// AddActor injects additional actors into the canvas, such as a Player doodad.
|
|
|
|
func (w *Canvas) AddActor(actor *Actor) error {
|
2023-04-09 04:26:08 +00:00
|
|
|
actor.LevelCanvas = w
|
2019-04-10 02:17:56 +00:00
|
|
|
w.actors = append(w.actors, actor)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-19 05:24:36 +00:00
|
|
|
// RemoveActor removes the actor from the canvas.
|
|
|
|
func (w *Canvas) RemoveActor(actor *Actor) {
|
|
|
|
var actors = []*Actor{}
|
|
|
|
for _, exist := range w.actors {
|
|
|
|
if actor == exist {
|
|
|
|
w.scripting.RemoveVM(actor.ID())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
actors = append(actors, exist)
|
|
|
|
}
|
|
|
|
w.actors = actors
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
// drawActors is a subroutine of Present() that superimposes the actors on top
|
|
|
|
// of the level drawing.
|
|
|
|
func (w *Canvas) drawActors(e render.Engine, p render.Point) {
|
|
|
|
var (
|
|
|
|
Viewport = w.ViewportRelative()
|
|
|
|
S = w.Size()
|
|
|
|
)
|
|
|
|
|
|
|
|
// See if each Actor is in range of the Viewport.
|
|
|
|
for i, a := range w.actors {
|
|
|
|
if a == nil {
|
|
|
|
log.Error("Canvas.drawActors: null actor at index %d (of %d actors)", i, len(w.actors))
|
|
|
|
continue
|
|
|
|
}
|
2020-12-30 04:31:35 +00:00
|
|
|
|
|
|
|
// Skip hidden actors.
|
|
|
|
if a.hidden {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
var (
|
2019-04-14 22:25:03 +00:00
|
|
|
can = a.Canvas // Canvas widget that draws the actor
|
|
|
|
actorPoint = a.Position()
|
|
|
|
actorSize = a.Size()
|
2021-09-12 04:18:22 +00:00
|
|
|
resizeTo = actorSize
|
2019-04-10 02:17:56 +00:00
|
|
|
)
|
|
|
|
|
2021-09-12 04:18:22 +00:00
|
|
|
// Adjust actor position and size by the zoom level.
|
|
|
|
actorPoint.X = w.ZoomMultiply(actorPoint.X)
|
|
|
|
actorPoint.Y = w.ZoomMultiply(actorPoint.Y)
|
|
|
|
resizeTo.W = w.ZoomMultiply(resizeTo.W)
|
|
|
|
resizeTo.H = w.ZoomMultiply(resizeTo.H)
|
|
|
|
|
|
|
|
// Tell the actor's canvas to copy our zoom level so it resizes its image too.
|
|
|
|
can.Zoom = w.Zoom
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
// Create a box of World Coordinates that this actor occupies. The
|
|
|
|
// Actor X,Y from level data is already a World Coordinate;
|
|
|
|
// accomodate for the size of the Actor.
|
|
|
|
actorBox := render.Rect{
|
|
|
|
X: actorPoint.X,
|
|
|
|
Y: actorPoint.Y,
|
|
|
|
W: actorSize.W,
|
|
|
|
H: actorSize.H,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is any part of the actor visible?
|
|
|
|
if !Viewport.Intersects(actorBox) {
|
|
|
|
continue // not visible on screen
|
|
|
|
}
|
|
|
|
|
|
|
|
drawAt := render.Point{
|
|
|
|
X: p.X + w.Scroll.X + actorPoint.X + w.BoxThickness(1),
|
|
|
|
Y: p.Y + w.Scroll.Y + actorPoint.Y + w.BoxThickness(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX TODO: when an Actor hits the left or top edge and shrinks,
|
|
|
|
// scrolling to offset that shrink is currently hard to solve.
|
|
|
|
scrollTo := render.Origin
|
|
|
|
|
|
|
|
// Handle cropping and scaling if this Actor's canvas can't be
|
|
|
|
// completely visible within the parent.
|
|
|
|
if drawAt.X+resizeTo.W > p.X+S.W {
|
|
|
|
// Hitting the right edge, shrunk the width now.
|
|
|
|
delta := (drawAt.X + resizeTo.W) - (p.X + S.W)
|
|
|
|
resizeTo.W -= delta
|
|
|
|
} else if drawAt.X < p.X {
|
|
|
|
// Hitting the left edge. Cap the X coord and shrink the width.
|
|
|
|
delta := p.X - drawAt.X // positive number
|
|
|
|
drawAt.X = p.X
|
2023-02-18 05:09:11 +00:00
|
|
|
scrollTo.X -= delta
|
2019-04-10 02:17:56 +00:00
|
|
|
resizeTo.W -= delta
|
|
|
|
}
|
|
|
|
|
|
|
|
if drawAt.Y+resizeTo.H > p.Y+S.H {
|
|
|
|
// Hitting the bottom edge, shrink the height.
|
|
|
|
delta := (drawAt.Y + resizeTo.H) - (p.Y + S.H)
|
|
|
|
resizeTo.H -= delta
|
|
|
|
} else if drawAt.Y < p.Y {
|
|
|
|
// Hitting the top edge. Cap the Y coord and shrink the height.
|
|
|
|
delta := p.Y - drawAt.Y
|
|
|
|
drawAt.Y = p.Y
|
2023-02-18 05:09:11 +00:00
|
|
|
scrollTo.Y -= delta
|
2019-04-10 02:17:56 +00:00
|
|
|
resizeTo.H -= delta
|
|
|
|
}
|
|
|
|
|
|
|
|
if resizeTo != actorSize {
|
|
|
|
can.Resize(resizeTo)
|
|
|
|
can.ScrollTo(scrollTo)
|
|
|
|
}
|
|
|
|
can.Present(e, drawAt)
|
|
|
|
|
|
|
|
// Clean up the canvas size and offset.
|
|
|
|
can.Resize(actorSize) // restore original size in case cropped
|
|
|
|
can.ScrollTo(render.Origin)
|
|
|
|
}
|
|
|
|
}
|