2018-09-26 17:04:46 +00:00
|
|
|
package doodads
|
|
|
|
|
|
|
|
import (
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/level"
|
2018-09-26 17:04:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Doodad is a reusable component for Levels that have scripts and graphics.
|
|
|
|
type Doodad struct {
|
|
|
|
level.Base
|
2019-05-29 04:43:30 +00:00
|
|
|
Filename string `json:"-"` // used internally, not saved in json
|
|
|
|
Palette *level.Palette `json:"palette"`
|
|
|
|
Script string `json:"script"`
|
|
|
|
Layers []Layer `json:"layers"`
|
|
|
|
Tags map[string]string `json:"data"` // arbitrary key/value data storage
|
2018-09-26 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Layer holds a layer of drawing data for a Doodad.
|
|
|
|
type Layer struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Chunker *level.Chunker `json:"chunks"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Doodad.
|
|
|
|
func New(size int) *Doodad {
|
|
|
|
if size == 0 {
|
|
|
|
size = balance.DoodadSize
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Doodad{
|
|
|
|
Base: level.Base{
|
|
|
|
Version: 1,
|
|
|
|
},
|
|
|
|
Palette: level.DefaultPalette(),
|
|
|
|
Layers: []Layer{
|
|
|
|
{
|
|
|
|
Name: "main",
|
|
|
|
Chunker: level.NewChunker(size),
|
|
|
|
},
|
|
|
|
},
|
2019-05-29 04:43:30 +00:00
|
|
|
Tags: map[string]string{},
|
2018-09-26 17:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 22:42:49 +00:00
|
|
|
// ChunkSize returns the chunk size of the Doodad's first layer.
|
|
|
|
func (d *Doodad) ChunkSize() int {
|
|
|
|
return d.Layers[0].Chunker.Size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rect returns a rect of the ChunkSize for scaling a Canvas widget.
|
|
|
|
func (d *Doodad) Rect() render.Rect {
|
|
|
|
var size = d.ChunkSize()
|
|
|
|
return render.Rect{
|
|
|
|
W: int32(size),
|
|
|
|
H: int32(size),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
// Inflate attaches the pixels to their swatches after loading from disk.
|
|
|
|
func (d *Doodad) Inflate() {
|
|
|
|
d.Palette.Inflate()
|
|
|
|
for _, layer := range d.Layers {
|
|
|
|
layer.Chunker.Inflate(d.Palette)
|
|
|
|
}
|
|
|
|
}
|