2018-09-23 22:20:45 +00:00
|
|
|
package level
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-07 06:28:11 +00:00
|
|
|
"strings"
|
2018-09-23 22:20:45 +00:00
|
|
|
|
2019-12-23 02:21:58 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2018-09-23 22:20:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Swatch holds details about a single value in the palette.
|
|
|
|
type Swatch struct {
|
2021-06-10 05:36:32 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Color render.Color `json:"color"`
|
|
|
|
Pattern string `json:"pattern"` // like "noise.png"
|
2018-09-23 22:20:45 +00:00
|
|
|
|
|
|
|
// Optional attributes.
|
2022-10-10 04:39:43 +00:00
|
|
|
Solid bool `json:"solid,omitempty"`
|
|
|
|
SemiSolid bool `json:"semisolid,omitempty"`
|
|
|
|
Fire bool `json:"fire,omitempty"`
|
|
|
|
Water bool `json:"water,omitempty"`
|
|
|
|
Slippery bool `json:"slippery,omitempty"`
|
2018-09-23 22:20:45 +00:00
|
|
|
|
|
|
|
// Private runtime attributes.
|
|
|
|
index int // position in the Palette, for reverse of `Palette.byName`
|
|
|
|
|
|
|
|
// When the swatch is loaded from JSON we only get the index number, and
|
|
|
|
// need to expand out the swatch later when the palette is loaded.
|
|
|
|
paletteIndex int
|
|
|
|
isSparse bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSparseSwatch creates a sparse Swatch from a palette index that will need
|
|
|
|
// later expansion, when loading drawings from disk.
|
|
|
|
func NewSparseSwatch(paletteIndex int) *Swatch {
|
|
|
|
return &Swatch{
|
|
|
|
isSparse: true,
|
|
|
|
paletteIndex: paletteIndex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Swatch) String() string {
|
2024-05-27 22:14:00 +00:00
|
|
|
var parts = []string{
|
|
|
|
fmt.Sprintf("#%d", s.Index()),
|
2018-09-23 22:20:45 +00:00
|
|
|
}
|
2024-05-27 22:14:00 +00:00
|
|
|
|
|
|
|
if s.Name != "" {
|
|
|
|
parts = append(parts, fmt.Sprintf("'%s'", s.Name))
|
2018-09-23 22:20:45 +00:00
|
|
|
}
|
2024-05-27 22:14:00 +00:00
|
|
|
|
|
|
|
if s.isSparse {
|
|
|
|
parts = append(parts, "sparse")
|
|
|
|
} else {
|
|
|
|
parts = append(parts, s.Color.ToHex())
|
|
|
|
}
|
|
|
|
|
|
|
|
parts = append(parts, s.Attributes())
|
|
|
|
|
|
|
|
return fmt.Sprintf("Swatch<%s>", strings.Join(parts, " "))
|
2018-09-23 22:20:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-07 06:28:11 +00:00
|
|
|
// Attributes returns a comma-separated list of attributes as a string on
|
|
|
|
// this swatch. This is for debugging and the `doodad show` CLI command to
|
|
|
|
// summarize the swatch and shouldn't be used for game logic.
|
|
|
|
func (s *Swatch) Attributes() string {
|
|
|
|
var result string
|
|
|
|
|
|
|
|
if s.Solid {
|
|
|
|
result += "solid,"
|
|
|
|
}
|
2022-10-10 17:52:28 +00:00
|
|
|
if s.SemiSolid {
|
|
|
|
result += "semi-solid,"
|
|
|
|
}
|
2019-07-07 06:28:11 +00:00
|
|
|
if s.Fire {
|
|
|
|
result += "fire,"
|
|
|
|
}
|
|
|
|
if s.Water {
|
|
|
|
result += "water,"
|
|
|
|
}
|
|
|
|
if s.isSparse {
|
|
|
|
result += "sparse,"
|
|
|
|
}
|
2022-10-10 17:52:28 +00:00
|
|
|
if s.Slippery {
|
|
|
|
result += "slippery,"
|
|
|
|
}
|
2019-07-07 06:28:11 +00:00
|
|
|
|
|
|
|
if result == "" {
|
|
|
|
result = "none,"
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSuffix(result, ",")
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// IsSparse returns whether this Swatch is sparse (has only a palette index) and
|
|
|
|
// requires inflation.
|
|
|
|
func (s *Swatch) IsSparse() bool {
|
|
|
|
return s.isSparse
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index returns the Swatch's position in the palette.
|
|
|
|
func (s *Swatch) Index() int {
|
|
|
|
return s.index
|
|
|
|
}
|