From 602949f1eb11416a5cbc79bdac9c7234ebd72dd9 Mon Sep 17 00:00:00 2001 From: tkarrass Date: Sat, 6 Feb 2016 22:08:04 +0100 Subject: [PATCH] Read Frame data --- cmd/compile.go | 85 +++++++++++++++++++++++++++++++++- cmd/server.go | 1 - levels/locoworld/Manifest.json | 16 ++++++- 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/cmd/compile.go b/cmd/compile.go index d60e991..bbc79a5 100644 --- a/cmd/compile.go +++ b/cmd/compile.go @@ -1,11 +1,52 @@ package cmd // code.bitsetter.de/fun/gosl/cmd import ( + "encoding/json" + "fmt" "github.com/spf13/cobra" + "io/ioutil" "log" + "path" + "strconv" + "strings" ) +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"` + Frames map[int]([][]rune) +} + +type LevelManifest struct { + Name string + FPS int + Layers map[string]*LayerManifest +} + var ( levelDir string levelFile string @@ -20,7 +61,6 @@ It's contents contain at least: / leveldir +-- Manifest.json Basic level Info +-- Layer1/ At least one layer dir containing … - +-- Manifest.json Basic layer Info +-- 1.Frame At least one frame See the example level for further details (which does not exist, yet :p)`, @@ -36,6 +76,49 @@ func compile(cmd *cobra.Command, args []string) { } log.Println("will compile to", levelFile) + lvlMan := &LevelManifest{} + buf, err := ioutil.ReadFile(levelDir + "/Manifest.json") + if err != nil { + log.Fatal("Error reading file: ", err) + } + + err = json.Unmarshal(buf, lvlMan) + if err != nil { + log.Fatal("Error reading level manifest: ", err) + } + + fmt.Println(lvlMan) + + for k, _ := range lvlMan.Layers { + log.Println("Processing layer: ", k) + dirlist, derr := ioutil.ReadDir(levelDir + "/" + k) + if derr != nil { + log.Println("Layer directory error: ", derr) + } else { + lvlMan.Layers[k].Frames = make(map[int]([][]rune)) + for _, fi := range dirlist { + if path.Ext(fi.Name()) == ".frame" { + log.Println(fi.Name()) + fdata, derr := ioutil.ReadFile(levelDir + "/" + k + "/" + fi.Name()) + if derr != nil { + log.Println("Error reading frame: ", derr) + } else { + rdata := make([][]rune, 0, 25) + for _, line := range strings.Split(string(fdata), "\n") { + rdata = append(rdata, []rune(line)) + } + idx, ierr := strconv.Atoi(strings.TrimSuffix(fi.Name(), path.Ext(fi.Name()))) + if ierr != nil { + log.Printf("Invalid Index in Filename: ", ierr) + } else { + lvlMan.Layers[k].Frames[idx] = rdata + } + } + } + } + } + } + } func init() { diff --git a/cmd/server.go b/cmd/server.go index 0e297b5..ef3d1ad 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -2,7 +2,6 @@ package cmd // code.bitsetter.de/fun/gosl/cmd import ( "encoding/gob" - "fmt" "log" "net" "sort" diff --git a/levels/locoworld/Manifest.json b/levels/locoworld/Manifest.json index 1241bee..d4a778e 100644 --- a/levels/locoworld/Manifest.json +++ b/levels/locoworld/Manifest.json @@ -1,4 +1,18 @@ { "Name": "The locomotive!", - "FPS": 10 + "FPS": 10, + "Layers" : { + "locomotive": { + "Z-Index": 0, + "Direction": 6, + "Speed": 2, + "Transparent": "•" + }, + "background": { + "Z-Index": 1, + "Direction": 6, + "Speed": 1, + "Transparent": "•" + } + } }