From 35d96b714d411f5ed671f84faaf47d7212d542ea Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Thu, 18 Apr 2019 22:02:59 -0700 Subject: [PATCH] Add JavaScript panic catcher to doodads * Add example mischievous doodad script that alters the logger date format and animates its canvas's mask color. --- dev-assets/doodads/mischievous.js | 43 +++++++++++++++++++++++++++++++ pkg/balance/numbers.go | 2 +- pkg/level/chunk.go | 12 +++++---- pkg/scripting/vm.go | 7 +++++ pkg/uix/canvas_actors.go | 5 +++- pkg/uix/canvas_scrolling.go | 6 +++++ 6 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 dev-assets/doodads/mischievous.js diff --git a/dev-assets/doodads/mischievous.js b/dev-assets/doodads/mischievous.js new file mode 100644 index 0000000..b694f08 --- /dev/null +++ b/dev-assets/doodads/mischievous.js @@ -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); + }) +} diff --git a/pkg/balance/numbers.go b/pkg/balance/numbers.go index b5534ed..ec9e2d2 100644 --- a/pkg/balance/numbers.go +++ b/pkg/balance/numbers.go @@ -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 diff --git a/pkg/level/chunk.go b/pkg/level/chunk.go index 0f70b9d..1d539ee 100644 --- a/pkg/level/chunk.go +++ b/pkg/level/chunk.go @@ -31,10 +31,11 @@ type Chunk struct { Size int // Texture cache properties so we don't redraw pixel-by-pixel every frame. - uuid uuid.UUID - texture render.Texturer - textureMasked render.Texturer - dirty bool + uuid uuid.UUID + texture render.Texturer + textureMasked render.Texturer + textureMaskedColor render.Color + dirty bool } // JSONChunk holds a lightweight (interface-free) copy of the Chunk for @@ -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 } diff --git a/pkg/scripting/vm.go b/pkg/scripting/vm.go index 39bf00e..b1b6543 100644 --- a/pkg/scripting/vm.go +++ b/pkg/scripting/vm.go @@ -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 } diff --git a/pkg/uix/canvas_actors.go b/pkg/uix/canvas_actors.go index 698386b..3a94b23 100644 --- a/pkg/uix/canvas_actors.go +++ b/pkg/uix/canvas_actors.go @@ -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()) diff --git a/pkg/uix/canvas_scrolling.go b/pkg/uix/canvas_scrolling.go index ec69591..1a2990a 100644 --- a/pkg/uix/canvas_scrolling.go +++ b/pkg/uix/canvas_scrolling.go @@ -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 {