2018-06-17 17:29:57 +00:00
|
|
|
package level
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-08-11 00:19:47 +00:00
|
|
|
"fmt"
|
2018-10-16 16:20:25 +00:00
|
|
|
"io/ioutil"
|
2018-06-17 17:29:57 +00:00
|
|
|
"os"
|
2019-05-05 21:03:20 +00:00
|
|
|
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/balance"
|
2018-06-17 17:29:57 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 22:07:34 +00:00
|
|
|
// FromJSON loads a level from JSON string.
|
|
|
|
func FromJSON(filename string, data []byte) (*Level, error) {
|
2019-07-03 23:22:30 +00:00
|
|
|
var m = New()
|
2019-06-27 22:07:34 +00:00
|
|
|
err := json.Unmarshal(data, m)
|
|
|
|
|
|
|
|
// Fill in defaults.
|
|
|
|
if m.Wallpaper == "" {
|
|
|
|
m.Wallpaper = DefaultWallpaper
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inflate the chunk metadata to map the pixels to their palette indexes.
|
|
|
|
m.Chunker.Inflate(m.Palette)
|
|
|
|
m.Actors.Inflate()
|
|
|
|
|
|
|
|
// Inflate the private instance values.
|
|
|
|
m.Palette.Inflate()
|
|
|
|
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:29:57 +00:00
|
|
|
// ToJSON serializes the level as JSON.
|
|
|
|
func (m *Level) ToJSON() ([]byte, error) {
|
|
|
|
out := bytes.NewBuffer([]byte{})
|
|
|
|
encoder := json.NewEncoder(out)
|
2019-05-05 21:03:20 +00:00
|
|
|
if balance.JSONIndent {
|
|
|
|
encoder.SetIndent("", "\t")
|
|
|
|
}
|
2018-06-17 17:29:57 +00:00
|
|
|
err := encoder.Encode(m)
|
|
|
|
return out.Bytes(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadJSON loads a map from JSON file.
|
2018-08-11 00:19:47 +00:00
|
|
|
func LoadJSON(filename string) (*Level, error) {
|
2018-06-17 17:29:57 +00:00
|
|
|
fh, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2018-08-11 00:19:47 +00:00
|
|
|
return nil, err
|
2018-06-17 17:29:57 +00:00
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// Decode the JSON file from disk.
|
2018-08-11 00:19:47 +00:00
|
|
|
m := New()
|
2018-06-17 17:29:57 +00:00
|
|
|
decoder := json.NewDecoder(fh)
|
|
|
|
err = decoder.Decode(&m)
|
2018-08-11 00:19:47 +00:00
|
|
|
if err != nil {
|
2018-09-23 22:20:45 +00:00
|
|
|
return m, fmt.Errorf("level.LoadJSON: JSON decode error: %s", err)
|
2018-08-11 00:19:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 05:22:13 +00:00
|
|
|
// Fill in defaults.
|
|
|
|
if m.Wallpaper == "" {
|
|
|
|
m.Wallpaper = DefaultWallpaper
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// Inflate the chunk metadata to map the pixels to their palette indexes.
|
|
|
|
m.Chunker.Inflate(m.Palette)
|
2018-10-19 20:31:58 +00:00
|
|
|
m.Actors.Inflate()
|
2018-09-23 22:20:45 +00:00
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// Inflate the private instance values.
|
Menu Toolbar for Editor + Shell Prompts + Theme
* Added a "menu toolbar" to the top of the Edit Mode with useful buttons
that work: New Level, New Doodad (same thing), Save, Save as, Open.
* Added ability for the dev console to prompt the user for a question,
which opens the console automatically. "Save", "Save as" and "Load"
ask for their filenames this way.
* Started groundwork for theming the app. The palette window is a light
brown with an orange title bar, the Menu Toolbar has a black
background, etc.
* Added support for multiple fonts instead of just monospace. DejaVu
Sans (normal and bold) are used now for most labels and window titles,
respectively. The dev console uses DejaVu Sans Mono as before.
* Update ui.Label to accept PadX and PadY separately instead of only
having the Padding option which did both.
* Improvements to Frame packing algorithm.
* Set the SDL draw mode to BLEND so we can use alpha colors properly,
so now the dev console is semi-translucent.
2018-08-12 00:30:00 +00:00
|
|
|
m.Palette.Inflate()
|
2018-06-17 17:29:57 +00:00
|
|
|
return m, err
|
|
|
|
}
|
2019-05-05 21:03:20 +00:00
|
|
|
|
|
|
|
// WriteJSON writes a level to JSON on disk.
|
|
|
|
func (m *Level) WriteJSON(filename string) error {
|
|
|
|
json, err := m.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Level.WriteJSON: JSON encode error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filename, json, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Level.WriteJSON: WriteFile error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|