diff --git a/map.go b/layout.go similarity index 56% rename from map.go rename to layout.go index 26de9ed..1efe51c 100644 --- a/map.go +++ b/layout.go @@ -3,7 +3,6 @@ package roba import ( "bufio" "bytes" - "fmt" "io" "os" "strconv" @@ -17,16 +16,10 @@ const ( ) type Layout struct { - Height int - Width int layout [][]byte } -type Map interface { - DebugPrint() -} - -func NewMap(path string) (Map, error) { +func NewLayout(path string) (*Layout, error) { m := &Layout{} f, err := os.Open(path) if err != nil { @@ -39,33 +32,35 @@ func NewMap(path string) (Map, error) { return nil, err } s := strings.Split(buf, " ") - m.Width, err = strconv.Atoi(s[0]) + width, err := strconv.Atoi(s[0]) if err != nil { return nil, err } - m.Height, err = strconv.Atoi(s[1]) + height, err := strconv.Atoi(s[1]) if err != nil { return nil, err } - for j := 0; j < m.Height; j++ { + for j := 0; j < height; j++ { b, err := br.ReadBytes('\n') 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") - 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 { - b = b[:m.Width] + if len(b) > width { + b = b[:width] } m.layout = append(m.layout, b) } return m, nil } -func (l *Layout) DebugPrint() { - for _, x := range l.layout { - fmt.Println(string(x)) - } +func (l *Layout) Height() int { + return len(l.layout) +} + +func (l *Layout) Width() int { + return len(l.layout[0]) } diff --git a/simple_map.go b/simple_map.go new file mode 100644 index 0000000..8e6f9cc --- /dev/null +++ b/simple_map.go @@ -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 +} diff --git a/state.go b/state.go new file mode 100644 index 0000000..1a432b4 --- /dev/null +++ b/state.go @@ -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 +}