doodle/level/json.go
Noah Petherbridge 27fafdc96d Save and restore maps as JSON files
First pass at a level storage format to save and restore maps.

To save a map: press F12. It takes a screenshot PNG into the
screenshots/ folder and outputs a map JSON in the working directory.

To restore a map: "go run cmd/doodle/main.go map.json"
2018-06-17 10:31:44 -07:00

31 lines
560 B
Go

package level
import (
"bytes"
"encoding/json"
"os"
)
// ToJSON serializes the level as JSON.
func (m *Level) ToJSON() ([]byte, error) {
out := bytes.NewBuffer([]byte{})
encoder := json.NewEncoder(out)
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 Level{}, err
}
defer fh.Close()
m := Level{}
decoder := json.NewDecoder(fh)
err = decoder.Decode(&m)
return m, err
}