Noah Petherbridge
864156da53
* Added a Settings window for game options, such as enabling the horizontal toolbars in Edit Mode. The Settings window also has a Controls tab showing the gameplay buttons and keyboard shortcuts. * The Settings window is available as a button on the home screen OR from the Edit->Settings menu in the EditScene. * Bugfix: using WASD to move the player character now works better and is considered by the game to be identical to the arrow key inputs. Boy now updates his animation based on these keys, and they register as boolean on/off keys instead of affected by key-repeat. * Refactor the boolProps: they are all part of usercfg now, and if you run e.g. "boolProp show-all-doodads true" and then cause the user settings to save to disk, that boolProp will be permanently enabled until turned off again.
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package level
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/usercfg"
|
|
)
|
|
|
|
// FromJSON loads a level from JSON string.
|
|
func FromJSON(filename string, data []byte) (*Level, error) {
|
|
var m = New()
|
|
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
|
|
}
|
|
|
|
// ToJSON serializes the level as JSON.
|
|
func (m *Level) ToJSON() ([]byte, error) {
|
|
out := bytes.NewBuffer([]byte{})
|
|
encoder := json.NewEncoder(out)
|
|
if usercfg.Current.JSONIndent {
|
|
encoder.SetIndent("", "\t")
|
|
}
|
|
err := encoder.Encode(m)
|
|
return out.Bytes(), err
|
|
}
|
|
|
|
// LoadJSON loads a map from JSON file.
|
|
func LoadJSON(filename string) (*Level, error) {
|
|
fh, err := os.Open(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer fh.Close()
|
|
|
|
// Decode the JSON file from disk.
|
|
m := New()
|
|
decoder := json.NewDecoder(fh)
|
|
err = decoder.Decode(&m)
|
|
if err != nil {
|
|
return m, fmt.Errorf("level.LoadJSON: JSON decode error: %s", err)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
}
|