From 006a3a5b727418eb474ce0b140a686c2b3b92700 Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Fri, 5 Aug 2016 16:36:23 -0700 Subject: [PATCH] hash out state --- layout.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ map.go | 71 ----------------------------------------------------------- simple_map.go | 15 +++++++++++++ state.go | 20 +++++++++++++++++ 4 files changed, 101 insertions(+), 71 deletions(-) create mode 100644 layout.go delete mode 100644 map.go create mode 100644 simple_map.go create mode 100644 state.go diff --git a/layout.go b/layout.go new file mode 100644 index 0000000..1efe51c --- /dev/null +++ b/layout.go @@ -0,0 +1,66 @@ +package roba + +import ( + "bufio" + "bytes" + "io" + "os" + "strconv" + "strings" +) + +type Tile byte + +const ( + TileWall = '#' +) + +type Layout struct { + layout [][]byte +} + +func NewLayout(path string) (*Layout, error) { + m := &Layout{} + f, err := os.Open(path) + if err != nil { + return nil, err + } + br := bufio.NewReader(f) + buf, err := br.ReadString('\n') + buf = strings.TrimRight(buf, "\n") + if err != nil { + return nil, err + } + s := strings.Split(buf, " ") + width, err := strconv.Atoi(s[0]) + if err != nil { + return nil, err + } + height, err := strconv.Atoi(s[1]) + if err != nil { + return nil, err + } + for j := 0; j < height; j++ { + b, err := br.ReadBytes('\n') + if err == io.EOF { + b = bytes.Repeat([]byte{TileWall}, width) + } + if len(b) < height { + bytes.TrimRight(b, "\n") + b = append(b, bytes.Repeat([]byte{TileWall}, width-len(b))...) + } + if len(b) > width { + b = b[:width] + } + m.layout = append(m.layout, b) + } + return m, nil +} + +func (l *Layout) Height() int { + return len(l.layout) +} + +func (l *Layout) Width() int { + return len(l.layout[0]) +} diff --git a/map.go b/map.go deleted file mode 100644 index 26de9ed..0000000 --- a/map.go +++ /dev/null @@ -1,71 +0,0 @@ -package roba - -import ( - "bufio" - "bytes" - "fmt" - "io" - "os" - "strconv" - "strings" -) - -type Tile byte - -const ( - TileWall = '#' -) - -type Layout struct { - Height int - Width int - layout [][]byte -} - -type Map interface { - DebugPrint() -} - -func NewMap(path string) (Map, error) { - m := &Layout{} - f, err := os.Open(path) - if err != nil { - return nil, err - } - br := bufio.NewReader(f) - buf, err := br.ReadString('\n') - buf = strings.TrimRight(buf, "\n") - if err != nil { - return nil, err - } - s := strings.Split(buf, " ") - m.Width, err = strconv.Atoi(s[0]) - if err != nil { - return nil, err - } - m.Height, err = strconv.Atoi(s[1]) - if err != nil { - return nil, err - } - for j := 0; j < m.Height; j++ { - b, err := br.ReadBytes('\n') - if err == io.EOF { - b = bytes.Repeat([]byte{TileWall}, m.Width) - } - if len(b) < m.Width { - bytes.TrimRight(b, "\n") - b = append(b, bytes.Repeat([]byte{TileWall}, m.Width-len(b))...) - } - if len(b) > m.Width { - b = b[:m.Width] - } - m.layout = append(m.layout, b) - } - return m, nil -} - -func (l *Layout) DebugPrint() { - for _, x := range l.layout { - fmt.Println(string(x)) - } -} 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 +}