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 (
"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])
}

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
}