2016-02-06 01:48:29 +01:00
|
|
|
package data // code.bitsetter.de/fun/gosl/data
|
|
|
|
|
2016-02-07 00:16:30 +01:00
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"log"
|
|
|
|
"os"
|
2016-02-08 00:13:49 +01:00
|
|
|
//"sort"
|
2016-02-07 00:16:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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 }
|
|
|
|
|
2016-02-06 01:48:29 +01:00
|
|
|
type Layer struct {
|
2016-02-07 00:16:30 +01:00
|
|
|
Z int `json:"Z-Index"`
|
|
|
|
D directionType `json:"Direction"`
|
|
|
|
S int `json:"Speed"`
|
|
|
|
T string `json:"Transparent"`
|
|
|
|
Repeat bool
|
|
|
|
Frames map[int]([][]rune)
|
2016-02-06 01:48:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Level struct {
|
2016-02-07 00:16:30 +01:00
|
|
|
Name string
|
|
|
|
FPS int
|
|
|
|
Layers map[string]*Layer
|
2016-02-06 01:48:29 +01:00
|
|
|
}
|
|
|
|
|
2016-02-07 00:16:30 +01:00
|
|
|
//func (lvl *Level) AddLayer(z int, l *Layer) {
|
|
|
|
// lvl.Layers[z] = l
|
|
|
|
//}
|
2016-02-06 13:26:01 +01:00
|
|
|
|
2016-02-06 14:42:38 +01:00
|
|
|
func LoadLevel(filename string) *Level {
|
2016-02-07 17:43:56 +01:00
|
|
|
log.Println("Loading lvl", filename)
|
2016-02-07 00:16:30 +01:00
|
|
|
ret := &Level{}
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2016-02-07 17:43:56 +01:00
|
|
|
log.Println("Error reading lvl:", err)
|
2016-02-07 00:16:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dec := gob.NewDecoder(file)
|
|
|
|
dec.Decode(ret)
|
|
|
|
//log.Println(ret.Layers["locomotive"])
|
|
|
|
return ret
|
2016-02-06 14:42:38 +01:00
|
|
|
}
|
2016-02-07 12:16:58 +01:00
|
|
|
|
2016-02-08 00:13:49 +01:00
|
|
|
// that's a func
|
2016-02-07 12:16:58 +01:00
|
|
|
func (lvl *Level) Height() int {
|
|
|
|
max := 0
|
|
|
|
for _, l := range lvl.Layers {
|
|
|
|
for _, f := range l.Frames {
|
|
|
|
if len(f) > max {
|
|
|
|
max = len(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:13:49 +01:00
|
|
|
// this is a func also ;)
|
2016-02-07 17:02:32 +01:00
|
|
|
func (lvl *Level) Width() (max int) {
|
|
|
|
for _, lay := range lvl.Layers {
|
|
|
|
for _, fra := range lay.Frames {
|
|
|
|
for _, fra2 := range fra {
|
|
|
|
if len(fra2) > max {
|
|
|
|
max = len(fra2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:13:49 +01:00
|
|
|
//testcomment
|
2016-02-07 17:43:56 +01:00
|
|
|
func (lvl *Level) GetFrame(off, w, frameNo int) (ret *Frame) {
|
2016-02-07 12:16:58 +01:00
|
|
|
h := lvl.Height()
|
|
|
|
|
|
|
|
ret = &Frame{
|
|
|
|
W: w,
|
|
|
|
H: h,
|
|
|
|
}
|
2016-02-08 00:13:49 +01:00
|
|
|
var mdata = make([]rune, w*h)
|
|
|
|
for y := 0; y < h; y++ {
|
|
|
|
ret.Data = append(ret.Data, mdata[y*w:(y+1)*w])
|
|
|
|
}
|
2016-02-07 12:16:58 +01:00
|
|
|
|
2016-02-07 17:43:56 +01:00
|
|
|
for _, layer := range lvl.Layers {
|
|
|
|
if layer.Z == 0 {
|
|
|
|
for row := 0; row < h; row++ {
|
2016-02-08 00:13:49 +01:00
|
|
|
//ret.Data = append(ret.Data, []rune{})
|
2016-02-07 17:43:56 +01:00
|
|
|
f := (frameNo % len(layer.Frames)) + 1
|
|
|
|
if row <= len(layer.Frames[f]) {
|
2016-02-08 00:13:49 +01:00
|
|
|
//for col := 0
|
2016-02-07 17:43:56 +01:00
|
|
|
//log.Println(len(layer.Frames[f][row]))
|
2016-02-08 00:13:49 +01:00
|
|
|
//ret.Data[row] = append(ret.Data[row], (layer.Frames[f][row][off%w:])...)
|
2016-02-07 12:16:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|