show what's on tiles

This commit is contained in:
stryan 2022-05-19 15:18:03 -04:00
parent 160204daa4
commit ab12a9b26a
6 changed files with 42 additions and 10 deletions

View File

@ -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))
}

View File

@ -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
}

View File

@ -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 {

View File

@ -5,6 +5,7 @@ type Producer interface {
Tick()
Get() Produce
String() string
Describe() string
}
//Produce is the result of a producer

View File

@ -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()

View File

@ -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
}