spacetea/simulator/pod.go

70 lines
1.2 KiB
Go
Raw Normal View History

2022-05-17 23:29:59 -04:00
package simulator
//Pod is a "location" where a player can move
type Pod struct {
Tiles [8][8]Tile
}
func newPod() *Pod {
return &Pod{
Tiles: [8][8]Tile{},
}
}
//Tick one iteration
func (p *Pod) Tick() {
for i := range p.Tiles {
for _, v := range p.Tiles[i] {
2022-05-19 17:09:46 -04:00
if v.Building != nil {
v.Building.Tick()
2022-05-17 23:29:59 -04:00
}
}
}
}
//Place an item on a tile
2022-05-19 17:09:46 -04:00
func (p *Pod) Place(item Object, x, y int) bool {
if p.Tiles[x][y].Building == nil {
p.Tiles[x][y].Building = item
2022-05-17 23:29:59 -04:00
return true
}
return false
}
//MovePlayer swaps player tiles
2022-05-19 15:18:03 -04:00
func (p *Pod) MovePlayer(x, y, s, t int) *Tile {
2022-05-17 23:29:59 -04:00
if oob(x) || oob(y) || oob(s) || oob(t) {
2022-05-19 15:18:03 -04:00
return nil
2022-05-17 23:29:59 -04:00
}
if p.Tiles[x][y].User == nil || p.Tiles[s][t].User != nil {
2022-05-19 15:18:03 -04:00
return nil
2022-05-17 23:29:59 -04:00
}
p.Tiles[s][t].User = p.Tiles[x][y].User
p.Tiles[x][y].User = nil
2022-05-19 15:18:03 -04:00
return &p.Tiles[s][t]
2022-05-17 23:29:59 -04:00
}
func (p *Pod) String() string {
var res string
res += "##########\n"
for i := range p.Tiles {
res += "#"
for _, v := range p.Tiles[i] {
if v.User != nil {
res += "@"
2022-05-19 17:09:46 -04:00
} else if v.Building != nil {
res += v.Building.String()
2022-05-17 23:29:59 -04:00
} else {
res += "."
}
}
res += "#\n"
}
res += "##########"
return res
}
func oob(i int) bool {
return (i >= 8 || i < 0)
}