diff --git a/.gitignore b/.gitignore index 4568079..41e384b 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,6 @@ _testmain.go *.pem *.crt +*.lvl + /gosl diff --git a/cmd/compile.go b/cmd/compile.go index e4f287c..a74d878 100644 --- a/cmd/compile.go +++ b/cmd/compile.go @@ -12,44 +12,10 @@ import ( "path" "strconv" "strings" + + "code.bitsetter.de/fun/gosl/data" ) -type directionType int - -const ( - DIR_NULL directionType = iota // Undefined direction: NO Motion - DIR_SW // - DIR_S // NW N NE - DIR_SE // 7 8 9 - DIR_W // W 4 5 6 E - DIR_NONE // 1 2 3 - DIR_E // SW S SE - DIR_NW - DIR_N - DIR_NE -) - -type Direction interface { - Base() directionType -} - -func (i directionType) Base() directionType { return i } - -type LayerManifest struct { - Z int `json:"Z-Index"` - D directionType `json:"Direction"` - S int `json:"Speed"` - T string `json:"Transparent"` - Repeat bool - Frames map[int]([][]rune) -} - -type LevelManifest struct { - Name string - FPS int - Layers map[string]*LayerManifest -} - var ( levelDir string levelFile string @@ -79,7 +45,7 @@ func compile(cmd *cobra.Command, args []string) { } log.Println("will compile to", levelFile) - lvlMan := &LevelManifest{} + lvlMan := &data.Level{} buf, err := ioutil.ReadFile(levelDir + "/Manifest.json") if err != nil { log.Fatal("Error reading file: ", err) @@ -125,6 +91,7 @@ func compile(cmd *cobra.Command, args []string) { enc := gob.NewEncoder(obuf) enc.Encode(&lvlMan) ioutil.WriteFile(levelFile, obuf.Bytes(), 0644) + log.Println("done.") } func init() { diff --git a/data/level.go b/data/level.go index 87c4bb6..6c1a8e4 100644 --- a/data/level.go +++ b/data/level.go @@ -1,18 +1,61 @@ package data // code.bitsetter.de/fun/gosl/data +import ( + "encoding/gob" + "log" + "os" +) + +type directionType int + +const ( + DIR_NULL directionType = iota // Undefined direction: NO Motion + DIR_SW // + DIR_S // NW N NE + DIR_SE // 7 8 9 + DIR_W // W 4 5 6 E + DIR_NONE // 1 2 3 + DIR_E // SW S SE + DIR_NW + DIR_N + DIR_NE +) + +type Direction interface { + Base() directionType +} + +func (i directionType) Base() directionType { return i } + type Layer struct { - data []byte - w, h int + Z int `json:"Z-Index"` + D directionType `json:"Direction"` + S int `json:"Speed"` + T string `json:"Transparent"` + Repeat bool + Frames map[int]([][]rune) } type Level struct { - Layers map[int]*Layer + Name string + FPS int + Layers map[string]*Layer } -func (lvl *Level) AddLayer(z int, l *Layer) { - lvl.Layers[z] = l -} +//func (lvl *Level) AddLayer(z int, l *Layer) { +// lvl.Layers[z] = l +//} func LoadLevel(filename string) *Level { - return nil + log.Println("Loading lvl ", filename) + ret := &Level{} + file, err := os.Open(filename) + if err != nil { + log.Println("Error reading lvl: ", err) + return nil + } + dec := gob.NewDecoder(file) + dec.Decode(ret) + //log.Println(ret.Layers["locomotive"]) + return ret } diff --git a/levelinfo.txt b/levelinfo.txt index 2144e53..6470891 100644 --- a/levelinfo.txt +++ b/levelinfo.txt @@ -23,7 +23,6 @@ LevelDir +-- Manifest :) +-- Layer 1 - | +-- Manifest << layer dimension, orientation and speed, transparency char | +-- 1.frame << text, unix style lf | +-- 2.frame | | …