Read Frame data
This commit is contained in:
parent
431e735870
commit
602949f1eb
|
@ -1,11 +1,52 @@
|
||||||
package cmd // code.bitsetter.de/fun/gosl/cmd
|
package cmd // code.bitsetter.de/fun/gosl/cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"path"
|
"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 (
|
var (
|
||||||
levelDir string
|
levelDir string
|
||||||
levelFile string
|
levelFile string
|
||||||
|
@ -20,7 +61,6 @@ It's contents contain at least:
|
||||||
/ leveldir
|
/ leveldir
|
||||||
+-- Manifest.json Basic level Info
|
+-- Manifest.json Basic level Info
|
||||||
+-- Layer1/ At least one layer dir containing …
|
+-- Layer1/ At least one layer dir containing …
|
||||||
+-- Manifest.json Basic layer Info
|
|
||||||
+-- 1.Frame At least one frame
|
+-- 1.Frame At least one frame
|
||||||
|
|
||||||
See the example level for further details (which does not exist, yet :p)`,
|
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)
|
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() {
|
func init() {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package cmd // code.bitsetter.de/fun/gosl/cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
|
@ -1,4 +1,18 @@
|
||||||
{
|
{
|
||||||
"Name": "The locomotive!",
|
"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": "•"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user