doodle/pkg/scripting/supervisor_events.go
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

35 lines
950 B
Go

package scripting
/*
RegisterEventHooks attaches the supervisor level event hooks into a JS VM.
Names registered:
- EndLevel(): for a doodad to exit the level. Panics if the OnLevelExit
handler isn't defined.
*/
func RegisterEventHooks(s *Supervisor, vm *VM) {
vm.Set("EndLevel", func() {
if s.onLevelFail == nil {
panic("JS FailLevel(): No OnLevelFail handler attached to script supervisor")
}
s.onLevelExit()
})
vm.Set("FailLevel", func(message string) {
if s.onLevelFail == nil {
panic("JS FailLevel(): No OnLevelFail handler attached to script supervisor")
}
s.onLevelFail(message)
})
}
// OnLevelExit registers an event hook for when a Level Exit doodad is reached.
func (s *Supervisor) OnLevelExit(handler func()) {
s.onLevelExit = handler
}
// OnLevelFail registers an event hook for level failures (doodads killing the player).
func (s *Supervisor) OnLevelFail(handler func(string)) {
s.onLevelFail = handler
}