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:
parent
258b2eb285
commit
35d96b714d
43
dev-assets/doodads/mischievous.js
Normal file
43
dev-assets/doodads/mischievous.js
Normal 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);
|
||||||
|
})
|
||||||
|
}
|
|
@ -10,7 +10,7 @@ var (
|
||||||
CanvasScrollSpeed int32 = 8
|
CanvasScrollSpeed int32 = 8
|
||||||
|
|
||||||
// Window scrolling behavior in Play Mode.
|
// 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
|
ScrollboxVert = 128
|
||||||
ScrollMaxVelocity = 8 // 24
|
ScrollMaxVelocity = 8 // 24
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ type Chunk struct {
|
||||||
uuid uuid.UUID
|
uuid uuid.UUID
|
||||||
texture render.Texturer
|
texture render.Texturer
|
||||||
textureMasked render.Texturer
|
textureMasked render.Texturer
|
||||||
|
textureMaskedColor render.Color
|
||||||
dirty bool
|
dirty bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +88,7 @@ func (c *Chunk) Texture(e render.Engine) render.Texturer {
|
||||||
|
|
||||||
// TextureMasked returns a cached texture with the ColorMask applied.
|
// TextureMasked returns a cached texture with the ColorMask applied.
|
||||||
func (c *Chunk) TextureMasked(e render.Engine, mask render.Color) render.Texturer {
|
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.
|
// Generate the normal bitmap and one with a color mask if applicable.
|
||||||
bitmap := c.toBitmap(mask)
|
bitmap := c.toBitmap(mask)
|
||||||
defer os.Remove(bitmap)
|
defer os.Remove(bitmap)
|
||||||
|
@ -97,6 +98,7 @@ func (c *Chunk) TextureMasked(e render.Engine, mask render.Color) render.Texture
|
||||||
}
|
}
|
||||||
|
|
||||||
c.textureMasked = tex
|
c.textureMasked = tex
|
||||||
|
c.textureMaskedColor = mask
|
||||||
}
|
}
|
||||||
return c.textureMasked
|
return c.textureMasked
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,6 +92,13 @@ func (vm *VM) Main() error {
|
||||||
return nil
|
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{})
|
_, err = function.Call(otto.Value{})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,10 @@ func (w *Canvas) InstallScripts() error {
|
||||||
vm := w.scripting.To(actor.ID())
|
vm := w.scripting.To(actor.ID())
|
||||||
vm.Self = actor
|
vm.Self = actor
|
||||||
vm.Set("Self", vm.Self)
|
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.
|
// Call the main() function.
|
||||||
log.Debug("Calling Main() for %s", actor.ID())
|
log.Debug("Calling Main() for %s", actor.ID())
|
||||||
|
|
|
@ -159,6 +159,9 @@ func (w *Canvas) loopFollowActor(ev *events.State) error {
|
||||||
delta = int32(balance.ScrollMaxVelocity)
|
delta = int32(balance.ScrollMaxVelocity)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: add gravity to counteract jitters on scrolling vertically
|
||||||
|
scrollBy.Y -= int32(balance.Gravity)
|
||||||
|
|
||||||
if delta < 0 {
|
if delta < 0 {
|
||||||
delta = -delta
|
delta = -delta
|
||||||
}
|
}
|
||||||
|
@ -172,6 +175,9 @@ func (w *Canvas) loopFollowActor(ev *events.State) error {
|
||||||
delta = int32(balance.ScrollMaxVelocity)
|
delta = int32(balance.ScrollMaxVelocity)
|
||||||
}
|
}
|
||||||
scrollBy.Y = -delta
|
scrollBy.Y = -delta
|
||||||
|
|
||||||
|
// TODO: add gravity to counteract jitters on scrolling vertically
|
||||||
|
scrollBy.Y += int32(balance.Gravity * 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
if scrollBy != render.Origin {
|
if scrollBy != render.Origin {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user