Doodad Tool: Add Tag Support for edit-doodad

* The `doodad edit-doodad` command now allows setting custom key/value
  tags in doodad files, for extra data storage useful to their scripts.
* Colored keys and doors now store a `color` tag with the appropriate
  color so that their scripts don't have to parse their Title to find
  that information.
* Trapdoors now store a `direction` tag to hold the direction the door
  is facing.
This commit is contained in:
Noah 2020-01-02 22:12:20 -08:00
parent 0e3a30e633
commit b4922edf5d
6 changed files with 62 additions and 20 deletions

View File

@ -2,6 +2,8 @@ package commands
import (
"fmt"
"os"
"strings"
"git.kirsle.net/apps/doodle/pkg/doodads"
"git.kirsle.net/apps/doodle/pkg/log"
@ -29,6 +31,10 @@ func init() {
Name: "author",
Usage: "set the doodad author",
},
cli.StringSliceFlag{
Name: "tag",
Usage: "set a key/value tag on the doodad, in key=value format. Empty value deletes the tag.",
},
cli.BoolFlag{
Name: "hide",
Usage: "Hide the doodad from the palette",
@ -92,6 +98,32 @@ func editDoodad(c *cli.Context, filename string) error {
modified = true
}
// Tags.
tags := c.StringSlice("tag")
if len(tags) > 0 {
for _, tag := range tags {
parts := strings.SplitN(tag, "=", 2)
if len(parts) != 2 {
log.Error("--tag: must be in format `key=value`. Value may be blank to delete a tag. len=%d", len(parts))
os.Exit(1)
}
var (
key = parts[0]
value = parts[1]
)
if value == "" {
log.Debug("Delete tag '%s'", key)
delete(dd.Tags, key)
} else {
log.Debug("Set tag '%s' to '%s'", key, value)
dd.Tags[key] = value
}
modified = true
}
}
if c.Bool("hide") {
dd.Hidden = true
log.Info("Marked doodad Hidden")
@ -117,7 +149,7 @@ func editDoodad(c *cli.Context, filename string) error {
******************************/
if modified {
if err := dd.WriteFile(filename); err != nil {
if err := dd.WriteJSON(filename); err != nil {
return cli.NewExitError(fmt.Sprintf("Write error: %s", err), 1)
}
} else {

View File

@ -45,27 +45,35 @@ doors() {
cd doors/
doodad convert -t "Red Door" red1.png red2.png door-red.doodad
doodad edit-doodad -q --tag color=red door-red.doodad
doodad install-script locked-door.js door-red.doodad
doodad convert -t "Blue Door" blue1.png blue2.png door-blue.doodad
doodad edit-doodad -q --tag color=blue door-blue.doodad
doodad install-script locked-door.js door-blue.doodad
doodad convert -t "Green Door" green1.png green2.png door-green.doodad
doodad edit-doodad -q --tag color=green door-green.doodad
doodad install-script locked-door.js door-green.doodad
doodad convert -t "Yellow Door" yellow1.png yellow2.png door-yellow.doodad
doodad edit-doodad -q --tag color=yellow door-yellow.doodad
doodad install-script locked-door.js door-yellow.doodad
doodad convert -t "Red Key" red-key.png key-red.doodad
doodad edit-doodad -q --tag color=red key-red.doodad
doodad install-script keys.js key-red.doodad
doodad convert -t "Blue Key" blue-key.png key-blue.doodad
doodad edit-doodad -q --tag color=blue key-blue.doodad
doodad install-script keys.js key-blue.doodad
doodad convert -t "Green Key" green-key.png key-green.doodad
doodad edit-doodad -q --tag color=green key-green.doodad
doodad install-script keys.js key-green.doodad
doodad convert -t "Yellow Key" yellow-key.png key-yellow.doodad
doodad edit-doodad -q --tag color=yellow key-yellow.doodad
doodad install-script keys.js key-yellow.doodad
doodad convert -t "Electric Door" electric{1,2,3,4}.png door-electric.doodad
@ -88,6 +96,11 @@ trapdoors() {
doodad install-script trapdoor.js trapdoor-right.doodad
doodad install-script trapdoor.js trapdoor-up.doodad
doodad edit-doodad -q --tag direction=down trapdoor-down.doodad
doodad edit-doodad -q --tag direction=left trapdoor-left.doodad
doodad edit-doodad -q --tag direction=right trapdoor-right.doodad
doodad edit-doodad -q --tag direction=up trapdoor-up.doodad
cp trapdoor-*.doodad ../../../assets/doodads/
cd ..

View File

@ -1,7 +1,8 @@
function main() {
var color = Self.Doodad.Tag("color");
Events.OnCollide(function(e) {
console.log("%s picked up by %s", Self.Doodad.Title, e.Actor.Title);
e.Actor.SetData("key:" + Self.Doodad.Title, "true");
e.Actor.SetData("key:" + color, "true");
Self.Destroy();
})
}

View File

@ -1,14 +1,7 @@
function main() {
Self.AddAnimation("open", 0, [1]);
var unlocked = false;
// Map our door names to key names.
var KeyMap = {
"Blue Door": "Blue Key",
"Red Door": "Red Key",
"Green Door": "Green Key",
"Yellow Door": "Yellow Key"
}
var color = Self.Doodad.Tag("color");
Self.SetHitbox(16, 0, 32, 64);
@ -18,7 +11,7 @@ function main() {
}
if (e.InHitbox) {
var data = e.Actor.GetData("key:" + KeyMap[Self.Doodad.Title]);
var data = e.Actor.GetData("key:" + color);
if (data === "") {
// Door is locked.
return false;

View File

@ -1,13 +1,6 @@
function main() {
// What direction is the trapdoor facing?
// - Titles are like "Trapdoor Left" or "Trapdoor Right"
// - The default (down) is called just "Trapdoor"
var direction = Self.Doodad.Title.split(" ")[1];
if (!direction) {
direction = "down";
}
direction = direction.toLowerCase();
var direction = Self.Doodad.Tag("direction");
console.log("Trapdoor(%s) initialized", direction);
var timer = 0;

View File

@ -3,6 +3,7 @@ package doodads
import (
"git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/apps/doodle/pkg/level"
"git.kirsle.net/apps/doodle/pkg/log"
"git.kirsle.net/go/render"
)
@ -44,6 +45,15 @@ func New(size int) *Doodad {
}
}
// Tag gets a value from the doodad's tags.
func (d *Doodad) Tag(name string) string {
if v, ok := d.Tags[name]; ok {
return v
}
log.Warn("Doodad(%s).Tag(%s): tag not defined", d.Title, name)
return ""
}
// ChunkSize returns the chunk size of the Doodad's first layer.
func (d *Doodad) ChunkSize() int {
return d.Layers[0].Chunker.Size