2018-06-17 17:29:57 +00:00
|
|
|
package level
|
|
|
|
|
2018-06-17 17:40:41 +00:00
|
|
|
import (
|
2022-04-30 03:34:59 +00:00
|
|
|
"archive/zip"
|
2018-06-17 17:40:41 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-09-23 22:20:45 +00:00
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/drawtool"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/enum"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
2023-02-18 05:09:11 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/native"
|
2019-12-28 03:16:34 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2023-12-09 03:48:02 +00:00
|
|
|
"git.kirsle.net/go/ui"
|
2018-06-17 17:40:41 +00:00
|
|
|
)
|
|
|
|
|
2018-10-28 05:22:13 +00:00
|
|
|
// Useful variables.
|
|
|
|
var (
|
|
|
|
DefaultWallpaper = "notebook.png"
|
|
|
|
)
|
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
// Base provides the common struct keys that are shared between Levels and
|
|
|
|
// Doodads.
|
|
|
|
type Base struct {
|
2022-01-09 21:16:29 +00:00
|
|
|
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"`
|
|
|
|
Locked bool `json:"locked"`
|
2018-10-19 20:31:58 +00:00
|
|
|
|
2022-04-30 03:34:59 +00:00
|
|
|
// v2 level format: zip files with external chunks.
|
|
|
|
// (v0 was json text, v1 was gzip compressed json text).
|
|
|
|
// The game must load levels created using the previous
|
|
|
|
// formats, they will not have a Zipfile and will have
|
|
|
|
// Chunkers in memory from their (gz) json.
|
|
|
|
Zipfile *zip.Reader `json:"-"`
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// Every drawing type is able to embed other files inside of itself.
|
2022-04-30 19:47:35 +00:00
|
|
|
Files *FileSystem `json:"files,omitempty"`
|
2018-09-26 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Level is the container format for Doodle map drawings.
|
|
|
|
type Level struct {
|
|
|
|
Base
|
2022-03-26 20:55:06 +00:00
|
|
|
Password string `json:"passwd"`
|
Update savegame format, Allow out-of-bounds camera
Updates the savegame.json file format:
* Levels now have a UUID value assigned at first save.
* The savegame.json will now track level completion/score based on UUID,
making it robust to filename changes in either levels or levelpacks.
* The savegame file is auto-migrated on startup - for any levels not
found or have no UUID, no change is made, it's backwards compatible.
* Level Properties window adds an "Advanced" tab to show/re-roll UUID.
New JavaScript API for doodad scripts:
* `Actors.CameraFollowPlayer()` tells the camera to return focus to the
player character. Useful for "cutscene" doodads that freeze the player,
call `Self.CameraFollowMe()` and do a thing before unfreezing and sending the
camera back to the player. (Or it will follow them at their next directional
input control).
* `Self.MoveBy(Point(x, y int))` to move the current actor a bit.
New option for the `doodad` command-line tool:
* `doodad resave <.level or .doodad>` will load and re-save a drawing, to
migrate it to the newest file format versions.
Small tweaks:
* On bounded levels, allow the camera to still follow the player if the player
finds themselves WELL far out of bounds (40 pixels margin). So on bounded
levels you can create "interior rooms" out-of-bounds to Warp Door into.
* New wallpaper: "Atmosphere" has a black starscape pattern that fades into a
solid blue atmosphere.
* Camera strictly follows the player the first 20 ticks, not 60 of level start
* If player is frozen, directional inputs do not take the camera focus back.
2023-03-08 05:55:10 +00:00
|
|
|
UUID string `json:"uuid"` // unique level IDs, especially for the savegame.json
|
2022-03-26 20:55:06 +00:00
|
|
|
GameRule GameRule `json:"rules"`
|
2018-06-17 17:29:57 +00:00
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// Chunked pixel data.
|
2022-01-09 21:16:29 +00:00
|
|
|
Chunker *Chunker `json:"chunks"`
|
2018-09-23 22:20:45 +00:00
|
|
|
|
2018-06-17 17:29:57 +00:00
|
|
|
// The Palette holds the unique "colors" used in this map file, and their
|
|
|
|
// properties (solid, fire, slippery, etc.)
|
2022-01-09 21:16:29 +00:00
|
|
|
Palette *Palette `json:"palette"`
|
2018-10-19 20:31:58 +00:00
|
|
|
|
2018-10-28 05:22:13 +00:00
|
|
|
// Page boundaries and wallpaper settings.
|
2022-01-09 21:16:29 +00:00
|
|
|
PageType PageType `json:"pageType"`
|
|
|
|
MaxWidth int64 `json:"boundedWidth"` // only if bounded or bordered
|
|
|
|
MaxHeight int64 `json:"boundedHeight"`
|
|
|
|
Wallpaper string `json:"wallpaper"`
|
2018-10-28 05:22:13 +00:00
|
|
|
|
2022-05-06 04:35:32 +00:00
|
|
|
// The last scrolled position in the editor.
|
|
|
|
ScrollPosition render.Point `json:"scroll"`
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// Actors keep a list of the doodad instances in this map.
|
2022-01-09 21:16:29 +00:00
|
|
|
Actors ActorMap `json:"actors"`
|
2019-07-03 23:22:30 +00:00
|
|
|
|
2022-01-18 02:51:11 +00:00
|
|
|
// Publishing: attach any custom doodads the map uses on save.
|
|
|
|
SaveDoodads bool `json:"saveDoodads"`
|
|
|
|
SaveBuiltins bool `json:"saveBuiltins"`
|
|
|
|
|
2023-02-19 01:37:54 +00:00
|
|
|
// Signature for a level with embedded doodads to still play in free mode.
|
|
|
|
Signature []byte `json:"signature,omitempty"`
|
|
|
|
|
2019-07-03 23:22:30 +00:00
|
|
|
// Undo history, temporary live data not persisted to the level file.
|
2022-01-09 21:16:29 +00:00
|
|
|
UndoHistory *drawtool.History `json:"-"`
|
2023-12-09 03:48:02 +00:00
|
|
|
|
|
|
|
// Cache of loaded images (e.g. screenshots).
|
|
|
|
cacheImages map[string]*ui.Image
|
2018-08-11 00:19:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 20:55:06 +00:00
|
|
|
// GameRule
|
|
|
|
type GameRule struct {
|
|
|
|
Difficulty enum.Difficulty `json:"difficulty"`
|
2022-03-27 21:23:25 +00:00
|
|
|
Survival bool `json:"survival,omitempty"`
|
2022-03-26 20:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// New creates a blank level object with all its members initialized.
|
|
|
|
func New() *Level {
|
|
|
|
return &Level{
|
2018-09-26 17:04:46 +00:00
|
|
|
Base: Base{
|
|
|
|
Version: 1,
|
2022-03-26 20:55:06 +00:00
|
|
|
Title: "Untitled",
|
2023-02-18 05:09:11 +00:00
|
|
|
Author: native.DefaultAuthor(),
|
2022-04-30 19:47:35 +00:00
|
|
|
Files: NewFileSystem(),
|
2018-09-26 17:04:46 +00:00
|
|
|
},
|
2018-09-23 22:20:45 +00:00
|
|
|
Chunker: NewChunker(balance.ChunkSize),
|
2018-08-11 00:19:47 +00:00
|
|
|
Palette: &Palette{},
|
2018-10-20 22:42:49 +00:00
|
|
|
Actors: ActorMap{},
|
2018-10-28 05:22:13 +00:00
|
|
|
|
|
|
|
PageType: NoNegativeSpace,
|
|
|
|
Wallpaper: DefaultWallpaper,
|
|
|
|
MaxWidth: 2550,
|
|
|
|
MaxHeight: 3300,
|
2019-07-03 23:22:30 +00:00
|
|
|
|
|
|
|
UndoHistory: drawtool.NewHistory(balance.UndoHistory),
|
2018-08-11 00:19:47 +00:00
|
|
|
}
|
2018-06-17 17:29:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 21:41:24 +00:00
|
|
|
// Teardown the level when the game is done with it. This frees up SDL2 cached
|
|
|
|
// texture chunks and reclaims memory in ways the Go garbage collector can not.
|
|
|
|
func (m *Level) Teardown() {
|
|
|
|
var (
|
|
|
|
chunks int
|
|
|
|
textures int
|
|
|
|
)
|
|
|
|
|
2022-04-30 03:34:59 +00:00
|
|
|
// Free any CACHED chunks' memory.
|
|
|
|
for chunk := range m.Chunker.IterCachedChunks() {
|
|
|
|
freed := chunk.Teardown()
|
|
|
|
chunks++
|
|
|
|
textures += freed
|
2022-04-09 21:41:24 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 03:48:02 +00:00
|
|
|
// Free any cached images (screenshots)
|
|
|
|
if m.cacheImages != nil {
|
|
|
|
for _, img := range m.cacheImages {
|
|
|
|
img.Destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 21:41:24 +00:00
|
|
|
log.Debug("Teardown level (%s): Freed %d textures across %d level chunks", m.Title, textures, chunks)
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:29:57 +00:00
|
|
|
// Pixel associates a coordinate with a palette index.
|
|
|
|
type Pixel struct {
|
2019-12-28 03:16:34 +00:00
|
|
|
X int `json:"x"`
|
|
|
|
Y int `json:"y"`
|
|
|
|
PaletteIndex int `json:"p"`
|
2018-08-11 00:19:47 +00:00
|
|
|
|
|
|
|
// Private runtime values.
|
2018-09-25 16:40:34 +00:00
|
|
|
Swatch *Swatch `json:"-"` // pointer to its swatch, for when rendered.
|
2018-08-11 00:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p Pixel) String() string {
|
|
|
|
return fmt.Sprintf("Pixel<%s '%s' (%d,%d)>", p.Swatch.Color, p.Swatch.Name, p.X, p.Y)
|
2018-06-17 17:29:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// Point returns the pixel's point.
|
|
|
|
func (p Pixel) Point() render.Point {
|
|
|
|
return render.Point{
|
|
|
|
X: p.X,
|
|
|
|
Y: p.Y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:40:41 +00:00
|
|
|
// MarshalJSON serializes a Pixel compactly as a simple list.
|
|
|
|
func (p Pixel) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(
|
|
|
|
`[%d, %d, %d]`,
|
2018-08-11 00:19:47 +00:00
|
|
|
p.X, p.Y, p.PaletteIndex,
|
2018-06-17 17:40:41 +00:00
|
|
|
)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON loads a Pixel from JSON again.
|
|
|
|
func (p *Pixel) UnmarshalJSON(text []byte) error {
|
2019-12-28 03:16:34 +00:00
|
|
|
var triplet []int
|
2018-06-17 17:40:41 +00:00
|
|
|
err := json.Unmarshal(text, &triplet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.X = triplet[0]
|
|
|
|
p.Y = triplet[1]
|
2018-08-11 00:19:47 +00:00
|
|
|
if len(triplet) > 2 {
|
|
|
|
p.PaletteIndex = triplet[2]
|
|
|
|
}
|
2018-06-17 17:40:41 +00:00
|
|
|
return nil
|
|
|
|
}
|