Noah Petherbridge
1f00af5741
Fix collision detection when an actor's hitbox is offset from 0,0: * Actors having a hitbox that didn't begin at X,Y 0,0 used to experience clipping issues with level geometry: because the game tracks their Position (top left corner of their graphical sprite) and their target position wasn't being correctly offset by their hitbox offset. * To resolve the issue, an "ActorOffset" struct is added: you give it the original game's Actor (with its offset hitbox) and it will record the offset and give a mocked Actor for collision detection purposes: where the Position and Target can be offset and where its Hitbox claims to begin at 0,0 matching its offsetted Position. * The translation between your original Actor and Offset Actor is handled at the boundary of the CollidesWithGrid function, so the main algorithm didn't need to be messed with and the game itself doesn't need to care about the offset. Make some fixes to the doodad CLI tool: * Fix palette colors being duplicated/doubled when converting from an image. * The --palette flag in `doodad convert` now actually functions: so you can supply an initial palette.json with colors and attributes to e.g. mark which colors should be solid or fire and give them names. The palette.json doesn't need to be comprehensive: it will be extended with new distinct colors as needed during the conversion.
103 lines
2.3 KiB
Go
103 lines
2.3 KiB
Go
package level
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.kirsle.net/go/render"
|
|
)
|
|
|
|
// Swatch holds details about a single value in the palette.
|
|
type Swatch struct {
|
|
Name string `json:"name"`
|
|
Color render.Color `json:"color"`
|
|
Pattern string `json:"pattern"` // like "noise.png"
|
|
|
|
// Optional attributes.
|
|
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"`
|
|
|
|
// 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 {
|
|
var parts = []string{
|
|
fmt.Sprintf("#%d", s.Index()),
|
|
}
|
|
|
|
if s.Name != "" {
|
|
parts = append(parts, fmt.Sprintf("'%s'", s.Name))
|
|
}
|
|
|
|
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, " "))
|
|
}
|
|
|
|
// 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,"
|
|
}
|
|
if s.SemiSolid {
|
|
result += "semi-solid,"
|
|
}
|
|
if s.Fire {
|
|
result += "fire,"
|
|
}
|
|
if s.Water {
|
|
result += "water,"
|
|
}
|
|
if s.isSparse {
|
|
result += "sparse,"
|
|
}
|
|
if s.Slippery {
|
|
result += "slippery,"
|
|
}
|
|
|
|
if result == "" {
|
|
result = "none,"
|
|
}
|
|
|
|
return strings.TrimSuffix(result, ",")
|
|
}
|
|
|
|
// 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
|
|
}
|