Add JavaScript panic catcher to doodads

* Add example mischievous doodad script that alters the logger date
  format and animates its canvas's mask color.
This commit is contained in:
Noah 2019-04-18 22:02:59 -07:00
parent 258b2eb285
commit 35d96b714d
6 changed files with 68 additions and 7 deletions

View File

@ -0,0 +1,43 @@
function main() {
console.log("%s initialized!", Self.Doodad.Title);
console.log(Object.keys(console));
console.log(Object.keys(log));
console.log(Object.keys(log.Config));
console.log(Object.keys(Self.Canvas.Palette));
console.log(Object.keys(Self.Canvas.Palette.Swatches[0]));
Self.Canvas.Palette.Swatches[0].Color = RGBA(255, 0, 255, 255);
Self.Canvas.Palette.Swatches[1].Color = RGBA(0, 255, 255, 255);
console.log(Self.Canvas.Palette.Swatches);
log.Config.TimeFormat = "haha";
var colors = [
RGBA(255, 0, 0, 255),
RGBA(255, 153, 0, 255),
RGBA(255, 255, 0, 255),
RGBA(0, 255, 0, 255),
RGBA(0, 153, 255, 255),
RGBA(0, 0, 255, 255),
RGBA(255, 0, 255, 255)
];
var colorIndex = 0;
setInterval(function() {
console.log("sticky tick");
Self.Canvas.MaskColor = colors[colorIndex];
colorIndex++;
if (colorIndex == colors.length) {
colorIndex = 0;
}
}, 100);
// log.Config.Colors = 0; // panics, can't set a golog.Color
Events.OnCollide( function() {
Self.ShowLayer(1);
setTimeout(function() {
Self.ShowLayer(0);
}, 200);
})
}

View File

@ -10,7 +10,7 @@ var (
CanvasScrollSpeed int32 = 8
// Window scrolling behavior in Play Mode.
ScrollboxHoz = 64 // horizontal px from window border to start scrol
ScrollboxHoz = 256 // horizontal px from window border to start scrol
ScrollboxVert = 128
ScrollMaxVelocity = 8 // 24

View File

@ -34,6 +34,7 @@ type Chunk struct {
uuid uuid.UUID
texture render.Texturer
textureMasked render.Texturer
textureMaskedColor render.Color
dirty bool
}
@ -87,7 +88,7 @@ func (c *Chunk) Texture(e render.Engine) render.Texturer {
// TextureMasked returns a cached texture with the ColorMask applied.
func (c *Chunk) TextureMasked(e render.Engine, mask render.Color) render.Texturer {
if c.textureMasked == nil {
if c.textureMasked == nil || c.textureMaskedColor != mask {
// Generate the normal bitmap and one with a color mask if applicable.
bitmap := c.toBitmap(mask)
defer os.Remove(bitmap)
@ -97,6 +98,7 @@ func (c *Chunk) TextureMasked(e render.Engine, mask render.Color) render.Texture
}
c.textureMasked = tex
c.textureMaskedColor = mask
}
return c.textureMasked
}

View File

@ -92,6 +92,13 @@ func (vm *VM) Main() error {
return nil
}
// Catch panics.
defer func() {
if err := recover(); err != nil {
log.Error("Panic caught in JavaScript VM: %s", err)
}
}()
_, err = function.Call(otto.Value{})
return err
}

View File

@ -53,7 +53,10 @@ func (w *Canvas) InstallScripts() error {
vm := w.scripting.To(actor.ID())
vm.Self = actor
vm.Set("Self", vm.Self)
vm.Run(actor.Drawing.Doodad.Script)
if _, err := vm.Run(actor.Doodad.Script); err != nil {
log.Error("Run script for actor %s failed: %s", actor.ID(), err)
}
// Call the main() function.
log.Debug("Calling Main() for %s", actor.ID())

View File

@ -159,6 +159,9 @@ func (w *Canvas) loopFollowActor(ev *events.State) error {
delta = int32(balance.ScrollMaxVelocity)
}
// TODO: add gravity to counteract jitters on scrolling vertically
scrollBy.Y -= int32(balance.Gravity)
if delta < 0 {
delta = -delta
}
@ -172,6 +175,9 @@ func (w *Canvas) loopFollowActor(ev *events.State) error {
delta = int32(balance.ScrollMaxVelocity)
}
scrollBy.Y = -delta
// TODO: add gravity to counteract jitters on scrolling vertically
scrollBy.Y += int32(balance.Gravity * 3)
}
if scrollBy != render.Origin {