From 0b1e62335734b5e22cb32ec75bc232658ee11e48 Mon Sep 17 00:00:00 2001 From: Lennart Buhl Date: Sun, 7 Feb 2016 17:43:56 +0100 Subject: [PATCH] Enable movement ; Fix locomotive frames --- cmd/server.go | 47 ++++++++++++++++++----------- data/level.go | 24 ++++++--------- levels/locoworld/locomotive/1.frame | 16 +++++----- levels/locoworld/locomotive/2.frame | 16 +++++----- levels/locoworld/locomotive/3.frame | 16 +++++----- levels/locoworld/locomotive/4.frame | 16 +++++----- levels/locoworld/locomotive/5.frame | 16 +++++----- levels/locoworld/locomotive/6.frame | 16 +++++----- 8 files changed, 88 insertions(+), 79 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 0514aeb..87ac7f0 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -28,6 +28,7 @@ type goslClient struct { id int w int h int + off int } /* GLOBAL SERVER STATE */ @@ -37,7 +38,11 @@ var ( ServerPort int TotalWidth int = 0 clients map[int]goslClient = make(map[int]goslClient) - clientKeys []int = make([]int, 100) + clientKeys []int = make([]int, 0) + // big server canvas + canvas [][]rune + canvasX int + frameCounter int = 0 ) func handleConn(conn *net.TCPConn) { @@ -46,22 +51,40 @@ func handleConn(conn *net.TCPConn) { dec := gob.NewDecoder(conn) dec.Decode(&hs) // decode handshake log.Println("Got client! ID:", hs.ID, "dimensions:", hs.W, hs.H) + // memorize client in server state clientKeys = append(clientKeys, hs.ID) sort.Ints(clientKeys) clients[hs.ID] = goslClient{con: conn, id: hs.ID, w: hs.W, h: hs.H} TotalWidth += hs.W - // conn.Close() + // reset server + resetServer() +} + +func resetServer() { + // adjust canvas width + canvasX = 0 + for _, k := range clientKeys { + _, client := k, clients[k] + canvasX += client.w + } + canvasX += level.Width() + // adjust client offsets + off := 0 + for _, k := range clientKeys { + _, client := k, clients[k] + client.off = off + off += client.w + } } func serveClients() { level = data.LoadLevel(LevelFile) - log.Println("canvas X:", canvasX()) - fCounter := 0 + frameCounter = 0 for { // while true for _, k := range clientKeys { id, client := k, clients[k] if id > 0 { - oFrame := level.GetFrame(0, client.w, fCounter) + oFrame := level.GetFrame(0+frameCounter, client.w, frameCounter) enc := gob.NewEncoder(client.con) err := enc.Encode(oFrame) if err != nil { @@ -79,13 +102,13 @@ func serveClients() { //log.Println("ID:", id, "Client:", client) } } - fCounter++ + frameCounter++ time.Sleep(time.Second / time.Duration(level.FPS)) } } func runServer(cmd *cobra.Command, args []string) { - log.Println("running server on port", ServerPort) + log.Println("Running server on port", ServerPort) listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: ServerPort}) if err != nil { log.Fatal(err) @@ -110,13 +133,3 @@ func init() { cmdServer.Flags().StringVarP(&LevelFile, "level", "l", "default.lvl", "Use specific levelfile") cmdServer.Flags().IntVarP(&ServerPort, "port", "p", 8090, "Run server on this port") } - -func canvasX() (x int) { - x = 0 - for _, k := range clientKeys { - _, client := k, clients[k] - x += client.w - } - x += level.Width() - return -} diff --git a/data/level.go b/data/level.go index bc8b74e..8299a7d 100644 --- a/data/level.go +++ b/data/level.go @@ -47,11 +47,11 @@ type Level struct { //} func LoadLevel(filename string) *Level { - log.Println("Loading lvl ", filename) + log.Println("Loading lvl", filename) ret := &Level{} file, err := os.Open(filename) if err != nil { - log.Println("Error reading lvl: ", err) + log.Println("Error reading lvl:", err) return nil } dec := gob.NewDecoder(file) @@ -73,13 +73,9 @@ func (lvl *Level) Height() int { } func (lvl *Level) Width() (max int) { - max = 0 for _, lay := range lvl.Layers { - // lay is of type map[int]([][]rune) for _, fra := range lay.Frames { - // fra is of type [][]rune for _, fra2 := range fra { - // fra2 is of type []rune if len(fra2) > max { max = len(fra2) } @@ -89,8 +85,7 @@ func (lvl *Level) Width() (max int) { return } -func (lvl *Level) GetFrame(off, w, frame int) (ret *Frame) { - //log.Println(ret) +func (lvl *Level) GetFrame(off, w, frameNo int) (ret *Frame) { h := lvl.Height() ret = &Frame{ @@ -98,13 +93,14 @@ func (lvl *Level) GetFrame(off, w, frame int) (ret *Frame) { H: h, } - for _, l := range lvl.Layers { - if l.Z == 0 { - for i := 0; i < h; i++ { + for _, layer := range lvl.Layers { + if layer.Z == 0 { + for row := 0; row < h; row++ { ret.Data = append(ret.Data, []rune{}) - f := (frame % len(l.Frames)) + 1 - if i <= len(l.Frames[f]) { - ret.Data[i] = append(ret.Data[i], l.Frames[f][i]...) + f := (frameNo % len(layer.Frames)) + 1 + if row <= len(layer.Frames[f]) { + //log.Println(len(layer.Frames[f][row])) + ret.Data[row] = append(ret.Data[row], (layer.Frames[f][row][off:])...) } } } diff --git a/levels/locoworld/locomotive/1.frame b/levels/locoworld/locomotive/1.frame index b54a438..d805eb8 100644 --- a/levels/locoworld/locomotive/1.frame +++ b/levels/locoworld/locomotive/1.frame @@ -1,10 +1,10 @@ - ==== ________ ___________ - _D _| |_______/ \__I_I_____===__|_________| - |(_)--- | H\________/ | | =|___ ___| _________________ - / | | H | | | | ||_| |_|| _| \_____A - | | | H |__--------------------| [___] | =| | - | ________|___H__/__|_____/[][]~\_______| | -| | + ==== ________ ___________ + _D _| |_______/ \__I_I_____===__|_________| + |(_)--- | H\________/ | | =|___ ___| _________________ + / | | H | | | | ||_| |_|| _| \_____A + | | | H |__--------------------| [___] | =| | + | ________|___H__/__|_____/[][]~\_______| | -| | |/ | |-----------I_____I [][] [] D |=======|____|________________________|_ __/ =| o |=-~~\ /~~\ /~~\ /~~\ ____Y___________|__|__________________________|_ - |/-=|___|= || || || |_____/~\___/ |_D__D__D_| |_D__D__D_| - \_/ \_O=====O=====O=====O/ \_/ \_/ \_/ \_/ \_/ + |/-=|___|= || || || |_____/~\___/ |_D__D__D_| |_D__D__D_| + \_/ \_O=====O=====O=====O/ \_/ \_/ \_/ \_/ \_/ \ No newline at end of file diff --git a/levels/locoworld/locomotive/2.frame b/levels/locoworld/locomotive/2.frame index 2e3fb0e..a9ec49b 100644 --- a/levels/locoworld/locomotive/2.frame +++ b/levels/locoworld/locomotive/2.frame @@ -1,10 +1,10 @@ - ==== ________ ___________ - _D _| |_______/ \__I_I_____===__|_________| - |(_)--- | H\________/ | | =|___ ___| _________________ - / | | H | | | | ||_| |_|| _| \_____A - | | | H |__--------------------| [___] | =| | - | ________|___H__/__|_____/[][]~\_______| | -| | + ==== ________ ___________ + _D _| |_______/ \__I_I_____===__|_________| + |(_)--- | H\________/ | | =|___ ___| _________________ + / | | H | | | | ||_| |_|| _| \_____A + | | | H |__--------------------| [___] | =| | + | ________|___H__/__|_____/[][]~\_______| | -| | |/ | |-----------I_____I [][] [] D |=======|____|________________________|_ __/ =| o |=-~~\ /~~\ /~~\ /~~\ ____Y___________|__|__________________________|_ - |/-=|___|= O=====O=====O=====O|_____/~\___/ |_D__D__D_| |_D__D__D_| - \_/ \__/ \__/ \__/ \__/ \_/ \_/ \_/ \_/ \_/ + |/-=|___|= O=====O=====O=====O|_____/~\___/ |_D__D__D_| |_D__D__D_| + \_/ \__/ \__/ \__/ \__/ \_/ \_/ \_/ \_/ \_/ \ No newline at end of file diff --git a/levels/locoworld/locomotive/3.frame b/levels/locoworld/locomotive/3.frame index 6606d5b..4d3545a 100644 --- a/levels/locoworld/locomotive/3.frame +++ b/levels/locoworld/locomotive/3.frame @@ -1,10 +1,10 @@ - ==== ________ ___________ - _D _| |_______/ \__I_I_____===__|_________| - |(_)--- | H\________/ | | =|___ ___| _________________ - / | | H | | | | ||_| |_|| _| \_____A - | | | H |__--------------------| [___] | =| | - | ________|___H__/__|_____/[][]~\_______| | -| | + ==== ________ ___________ + _D _| |_______/ \__I_I_____===__|_________| + |(_)--- | H\________/ | | =|___ ___| _________________ + / | | H | | | | ||_| |_|| _| \_____A + | | | H |__--------------------| [___] | =| | + | ________|___H__/__|_____/[][]~\_______| | -| | |/ | |-----------I_____I [][] [] D |=======|____|________________________|_ __/ =| o |=-~O=====O=====O=====O\ ____Y___________|__|__________________________|_ - |/-=|___|= || || || |_____/~\___/ |_D__D__D_| |_D__D__D_| - \_/ \__/ \__/ \__/ \__/ \_/ \_/ \_/ \_/ \_/ + |/-=|___|= || || || |_____/~\___/ |_D__D__D_| |_D__D__D_| + \_/ \__/ \__/ \__/ \__/ \_/ \_/ \_/ \_/ \_/ \ No newline at end of file diff --git a/levels/locoworld/locomotive/4.frame b/levels/locoworld/locomotive/4.frame index 45c1844..65752eb 100644 --- a/levels/locoworld/locomotive/4.frame +++ b/levels/locoworld/locomotive/4.frame @@ -1,10 +1,10 @@ - ==== ________ ___________ - _D _| |_______/ \__I_I_____===__|_________| - |(_)--- | H\________/ | | =|___ ___| _________________ - / | | H | | | | ||_| |_|| _| \_____A - | | | H |__--------------------| [___] | =| | - | ________|___H__/__|_____/[][]~\_______| | -| | + ==== ________ ___________ + _D _| |_______/ \__I_I_____===__|_________| + |(_)--- | H\________/ | | =|___ ___| _________________ + / | | H | | | | ||_| |_|| _| \_____A + | | | H |__--------------------| [___] | =| | + | ________|___H__/__|_____/[][]~\_______| | -| | |/ | |-----------I_____I [][] [] D |=======|____|________________________|_ __/ =| o |=-O=====O=====O=====O \ ____Y___________|__|__________________________|_ - |/-=|___|= || || || |_____/~\___/ |_D__D__D_| |_D__D__D_| - \_/ \__/ \__/ \__/ \__/ \_/ \_/ \_/ \_/ \_/ + |/-=|___|= || || || |_____/~\___/ |_D__D__D_| |_D__D__D_| + \_/ \__/ \__/ \__/ \__/ \_/ \_/ \_/ \_/ \_/ \ No newline at end of file diff --git a/levels/locoworld/locomotive/5.frame b/levels/locoworld/locomotive/5.frame index def7386..febaf9e 100644 --- a/levels/locoworld/locomotive/5.frame +++ b/levels/locoworld/locomotive/5.frame @@ -1,10 +1,10 @@ - ==== ________ ___________ - _D _| |_______/ \__I_I_____===__|_________| - |(_)--- | H\________/ | | =|___ ___| _________________ - / | | H | | | | ||_| |_|| _| \_____A - | | | H |__--------------------| [___] | =| | - | ________|___H__/__|_____/[][]~\_______| | -| | + ==== ________ ___________ + _D _| |_______/ \__I_I_____===__|_________| + |(_)--- | H\________/ | | =|___ ___| _________________ + / | | H | | | | ||_| |_|| _| \_____A + | | | H |__--------------------| [___] | =| | + | ________|___H__/__|_____/[][]~\_______| | -| | |/ | |-----------I_____I [][] [] D |=======|____|________________________|_ __/ =| o |=-~~\ /~~\ /~~\ /~~\ ____Y___________|__|__________________________|_ - |/-=|___|=O=====O=====O=====O |_____/~\___/ |_D__D__D_| |_D__D__D_| - \_/ \__/ \__/ \__/ \__/ \_/ \_/ \_/ \_/ \_/ + |/-=|___|=O=====O=====O=====O |_____/~\___/ |_D__D__D_| |_D__D__D_| + \_/ \__/ \__/ \__/ \__/ \_/ \_/ \_/ \_/ \_/ \ No newline at end of file diff --git a/levels/locoworld/locomotive/6.frame b/levels/locoworld/locomotive/6.frame index a2c2b06..730a087 100644 --- a/levels/locoworld/locomotive/6.frame +++ b/levels/locoworld/locomotive/6.frame @@ -1,10 +1,10 @@ - ==== ________ ___________ - _D _| |_______/ \__I_I_____===__|_________| - |(_)--- | H\________/ | | =|___ ___| _________________ - / | | H | | | | ||_| |_|| _| \_____A - | | | H |__--------------------| [___] | =| | - | ________|___H__/__|_____/[][]~\_______| | -| | + ==== ________ ___________ + _D _| |_______/ \__I_I_____===__|_________| + |(_)--- | H\________/ | | =|___ ___| _________________ + / | | H | | | | ||_| |_|| _| \_____A + | | | H |__--------------------| [___] | =| | + | ________|___H__/__|_____/[][]~\_______| | -| | |/ | |-----------I_____I [][] [] D |=======|____|________________________|_ __/ =| o |=-~~\ /~~\ /~~\ /~~\ ____Y___________|__|__________________________|_ - |/-=|___|= || || || |_____/~\___/ |_D__D__D_| |_D__D__D_| - \_/ \O=====O=====O=====O_/ \_/ \_/ \_/ \_/ \_/ + |/-=|___|= || || || |_____/~\___/ |_D__D__D_| |_D__D__D_| + \_/ \O=====O=====O=====O_/ \_/ \_/ \_/ \_/ \_/ \ No newline at end of file