From ab12a9b26aa0c2bb8642b4c24713daa8f6a8cf41 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 19 May 2022 15:18:03 -0400 Subject: [PATCH] show what's on tiles --- simulator/plant.go | 6 ++++++ simulator/player.go | 11 +++++++++-- simulator/pod.go | 8 ++++---- simulator/producer.go | 1 + simulator/simulator.go | 12 ++++++++---- simulator/tile.go | 14 ++++++++++++++ 6 files changed, 42 insertions(+), 10 deletions(-) diff --git a/simulator/plant.go b/simulator/plant.go index 303f837..90bab50 100644 --- a/simulator/plant.go +++ b/simulator/plant.go @@ -1,6 +1,7 @@ package simulator import ( + "fmt" "strconv" ) @@ -40,3 +41,8 @@ func (p *Plant) Get() Produce { func (p *Plant) String() string { return strconv.Itoa(p.kind) } + +//Describe returns a human useful string +func (p *Plant) Describe() string { + return fmt.Sprintf("A %v plant with %v value", strconv.Itoa(p.kind), strconv.Itoa(p.value)) +} diff --git a/simulator/player.go b/simulator/player.go index 14ad3a2..5a28601 100644 --- a/simulator/player.go +++ b/simulator/player.go @@ -1,10 +1,13 @@ package simulator -import "fmt" +import ( + "fmt" +) //Player is a player controlled mob type Player struct { - Resources map[int]int + Resources map[int]int + CurrentTile *Tile } //NewPlayer initializes a player @@ -18,5 +21,9 @@ func (p *Player) String() string { for i := range p.Resources { res += fmt.Sprintf("%v: %v", i, p.Resources[i]) } + res += "\nLocation: \n" + if p.CurrentTile != nil { + res += p.CurrentTile.String() + } return res } diff --git a/simulator/pod.go b/simulator/pod.go index 21cc994..f60ca52 100644 --- a/simulator/pod.go +++ b/simulator/pod.go @@ -32,16 +32,16 @@ func (p *Pod) Place(item Producer, x, y int) bool { } //MovePlayer swaps player tiles -func (p *Pod) MovePlayer(x, y, s, t int) bool { +func (p *Pod) MovePlayer(x, y, s, t int) *Tile { if oob(x) || oob(y) || oob(s) || oob(t) { - return false + return nil } if p.Tiles[x][y].User == nil || p.Tiles[s][t].User != nil { - return false + return nil } p.Tiles[s][t].User = p.Tiles[x][y].User p.Tiles[x][y].User = nil - return true + return &p.Tiles[s][t] } func (p *Pod) String() string { diff --git a/simulator/producer.go b/simulator/producer.go index 5dd453d..2fb0a7b 100644 --- a/simulator/producer.go +++ b/simulator/producer.go @@ -5,6 +5,7 @@ type Producer interface { Tick() Get() Produce String() string + Describe() string } //Produce is the result of a producer diff --git a/simulator/simulator.go b/simulator/simulator.go index 7feaa72..2aa93c5 100644 --- a/simulator/simulator.go +++ b/simulator/simulator.go @@ -60,23 +60,27 @@ func (s *Simulator) Input(cmd string) { case "left": res := s.Place.MovePlayer(s.Px, s.Py, s.Px, s.Py-1) - if res { + if res != nil { s.Py = s.Py - 1 + s.Player.CurrentTile = res } case "right": res := s.Place.MovePlayer(s.Px, s.Py, s.Px, s.Py+1) - if res { + if res != nil { s.Py = s.Py + 1 + s.Player.CurrentTile = res } case "up": res := s.Place.MovePlayer(s.Px, s.Py, s.Px-1, s.Py) - if res { + if res != nil { s.Px = s.Px - 1 + s.Player.CurrentTile = res } case "down": res := s.Place.MovePlayer(s.Px, s.Py, s.Px+1, s.Py) - if res { + if res != nil { s.Px = s.Px + 1 + s.Player.CurrentTile = res } case "quit": s.Stop() diff --git a/simulator/tile.go b/simulator/tile.go index 9290157..21a912a 100644 --- a/simulator/tile.go +++ b/simulator/tile.go @@ -1,7 +1,21 @@ package simulator +import ( + "fmt" +) + //Tile is a tile type Tile struct { Maker Producer User *Player } + +func (t *Tile) String() string { + var res string + if t.Maker != nil { + res += fmt.Sprintf("There is a %v here\n", t.Maker.Describe()) + } else { + res += "Nothing here" + } + return res +}