doodle/docs/Doodad Scripts.md
Noah Petherbridge 0518df226c New Doodad: Anvil
* The Anvil doodad is affected by gravity and becomes dangerous when
  falling. If it lands on the player character, you die! If it lands on
  any other mobile doodad, it destroys it! It can land on solid doodads
  such as the Electric Trapdoor and the Crumbly Floor. It will activate
  a Crumbly Floor if it lands on one, and can activate buttons and
  switches that it passes.
* JavaScript API: FailLevel(message) can be called from a doodad to kill
  the player character. The Anvil does this if it collides with the
  player while it's been falling.
2021-08-08 21:57:41 -07:00

1.8 KiB

Doodad Scripting Engine

Some ideas for the scripting engine for Doodads inside your level.

Architecture

The script will be an "attached file" in the Doodad format as a special file named "index.js" as the entry point.

Each Doodad will have its index.js script loaded into an isolated JS environment where it can't access any data about other Doodads or anything user specific. The main() function is called so the Doodad script can set itself up.

The main() function should:

  • Initialize any state variables the Doodad wants to use in its script.
  • Subscribe to callback events that the Doodad is interested in catching.

The script interacts with the Doodle application through an API broker object (a Go surface area of functions).

API Broker Interface

type API interface {
    // "Self" functions.
    SetFrame(frame int)  // Set the currently visible frame in this Doodad.
    MoveTo(render.Point)

    // Game functions.k
    EndLevel()  // Exit the current level with a victory
    FailLevel(message)

    /************************************
     * Event Handler Callback Functions *
     ************************************/

    // When we become visible on screen or disappear off the screen.
    OnVisible()
    OnHidden()

    // OnEnter: the other Doodad has ENTIRELY entered our box. Or if the other
    // doodad is bigger, they have ENTIRELY enveloped ours.
    OnEnter(func(other Doodad))

    // OnCollide: when we bump into another Doodad.
    OnCollide(func(other Doodad))
}

Mockup Script

function main() {
  console.log("hello world");

  // Register event callbacks.
  Doodle.OnEnter(onEnter);
}

// onEnter: handle when another Doodad (like the player) completely enters
// the bounding box of our Doodad. Example: a level exit.
function onEnter(other) {

}