hash out state

This commit is contained in:
Barak Michener 2016-08-05 16:36:23 -07:00
parent 3b1033ff1a
commit 006a3a5b72
4 changed files with 101 additions and 71 deletions

View file

@ -3,7 +3,6 @@ package roba
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"io" "io"
"os" "os"
"strconv" "strconv"
@ -17,16 +16,10 @@ const (
) )
type Layout struct { type Layout struct {
Height int
Width int
layout [][]byte layout [][]byte
} }
type Map interface { func NewLayout(path string) (*Layout, error) {
DebugPrint()
}
func NewMap(path string) (Map, error) {
m := &Layout{} m := &Layout{}
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
@ -39,33 +32,35 @@ func NewMap(path string) (Map, error) {
return nil, err return nil, err
} }
s := strings.Split(buf, " ") s := strings.Split(buf, " ")
m.Width, err = strconv.Atoi(s[0]) width, err := strconv.Atoi(s[0])
if err != nil { if err != nil {
return nil, err return nil, err
} }
m.Height, err = strconv.Atoi(s[1]) height, err := strconv.Atoi(s[1])
if err != nil { if err != nil {
return nil, err return nil, err
} }
for j := 0; j < m.Height; j++ { for j := 0; j < height; j++ {
b, err := br.ReadBytes('\n') b, err := br.ReadBytes('\n')
if err == io.EOF { if err == io.EOF {
b = bytes.Repeat([]byte{TileWall}, m.Width) b = bytes.Repeat([]byte{TileWall}, width)
} }
if len(b) < m.Width { if len(b) < height {
bytes.TrimRight(b, "\n") bytes.TrimRight(b, "\n")
b = append(b, bytes.Repeat([]byte{TileWall}, m.Width-len(b))...) b = append(b, bytes.Repeat([]byte{TileWall}, width-len(b))...)
} }
if len(b) > m.Width { if len(b) > width {
b = b[:m.Width] b = b[:width]
} }
m.layout = append(m.layout, b) m.layout = append(m.layout, b)
} }
return m, nil return m, nil
} }
func (l *Layout) DebugPrint() { func (l *Layout) Height() int {
for _, x := range l.layout { return len(l.layout)
fmt.Println(string(x)) }
}
func (l *Layout) Width() int {
return len(l.layout[0])
} }

15
simple_map.go Normal file
View file

@ -0,0 +1,15 @@
package roba
type SimpleMap struct {
layout *Layout
}
func NewSimpleMap() (Map, error) {
var err error
m := &SimpleMap{}
m.layout, err = NewLayout("test.txt")
if err != nil {
return nil, err
}
return m, nil
}

20
state.go Normal file
View file

@ -0,0 +1,20 @@
package roba
type Loc struct {
X int
Y int
}
type Cell struct {
Tile Tile
}
type Map interface {
OnTick(n int, s *State)
Name() string
}
type State struct {
Layout *Layout
Map Map
}