Noah Petherbridge
a7fd3aa1ca
Adds the first features to Edit Mode to support creation of Doodad files! The "New Doodad" button pops up a prompt for a Doodad size (default 100px) and configures the Canvas widget and makes a Doodad struct instead of a Level to manage. * Move the custom Canvas widget from `level.Canvas` to `uix.Canvas` (the uix package is for our custom UI widgets now) * Rename the `doodads.Doodad` interface (for runtime instances of Doodads) to `doodads.Actor` and make `doodads.Doodad` describe the file format and JSON schema instead. * Rename the `EditLevel()` method to `EditDrawing()` and it inspects the file extension to know whether to launch the Edit Mode for a Level or for a Doodad drawing. * Doodads can be edited by using the `-edit` CLI flag or using the in-game file open features (including `edit` command of dev console). * Add a `Scrollable` boolean to uix.Canvas to restrict the keyboard being able to scroll the level, for editing Doodads which have a fixed size.
90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
package level
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"git.kirsle.net/apps/doodle/balance"
|
|
"git.kirsle.net/apps/doodle/render"
|
|
)
|
|
|
|
// Base provides the common struct keys that are shared between Levels and
|
|
// Doodads.
|
|
type Base struct {
|
|
Version int `json:"version"` // File format version spec.
|
|
GameVersion string `json:"gameVersion"` // Game version that created the level.
|
|
Title string `json:"title"`
|
|
Author string `json:"author"`
|
|
}
|
|
|
|
// Level is the container format for Doodle map drawings.
|
|
type Level struct {
|
|
Base
|
|
Password string `json:"passwd"`
|
|
Locked bool `json:"locked"`
|
|
|
|
// Chunked pixel data.
|
|
Chunker *Chunker `json:"chunks"`
|
|
|
|
// The Palette holds the unique "colors" used in this map file, and their
|
|
// properties (solid, fire, slippery, etc.)
|
|
Palette *Palette `json:"palette"`
|
|
}
|
|
|
|
// New creates a blank level object with all its members initialized.
|
|
func New() *Level {
|
|
return &Level{
|
|
Base: Base{
|
|
Version: 1,
|
|
},
|
|
Chunker: NewChunker(balance.ChunkSize),
|
|
Palette: &Palette{},
|
|
}
|
|
}
|
|
|
|
// Pixel associates a coordinate with a palette index.
|
|
type Pixel struct {
|
|
X int32 `json:"x"`
|
|
Y int32 `json:"y"`
|
|
PaletteIndex int32 `json:"p"`
|
|
|
|
// Private runtime values.
|
|
Swatch *Swatch `json:"-"` // pointer to its swatch, for when rendered.
|
|
}
|
|
|
|
func (p Pixel) String() string {
|
|
return fmt.Sprintf("Pixel<%s '%s' (%d,%d)>", p.Swatch.Color, p.Swatch.Name, p.X, p.Y)
|
|
}
|
|
|
|
// Point returns the pixel's point.
|
|
func (p Pixel) Point() render.Point {
|
|
return render.Point{
|
|
X: p.X,
|
|
Y: p.Y,
|
|
}
|
|
}
|
|
|
|
// MarshalJSON serializes a Pixel compactly as a simple list.
|
|
func (p Pixel) MarshalJSON() ([]byte, error) {
|
|
return []byte(fmt.Sprintf(
|
|
`[%d, %d, %d]`,
|
|
p.X, p.Y, p.PaletteIndex,
|
|
)), nil
|
|
}
|
|
|
|
// UnmarshalJSON loads a Pixel from JSON again.
|
|
func (p *Pixel) UnmarshalJSON(text []byte) error {
|
|
var triplet []int32
|
|
err := json.Unmarshal(text, &triplet)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p.X = triplet[0]
|
|
p.Y = triplet[1]
|
|
if len(triplet) > 2 {
|
|
p.PaletteIndex = triplet[2]
|
|
}
|
|
return nil
|
|
}
|