freego/tile.go

104 lines
1.7 KiB
Go
Raw Permalink Normal View History

2022-02-21 19:07:28 -05:00
package freego
2022-02-18 18:36:18 -05:00
import (
"errors"
"fmt"
)
//Tile represents a spot on the board
type Tile struct {
x int
y int
passable bool
entity *Piece
colour Colour
}
func (t *Tile) String() string {
icon := "?"
if !t.passable {
icon = "X"
} else if t.entity != nil {
if !t.entity.Hidden {
2022-02-20 17:11:50 -05:00
icon = "P"
2022-02-18 18:36:18 -05:00
} else {
icon = "O"
}
} else {
icon = " "
}
return fmt.Sprintf("|%v|", icon)
}
//Place a piece on the tile
func (t *Tile) Place(p *Piece) error {
if p == nil {
return errors.New("tried to place nil piece")
}
if !t.passable {
return errors.New("tile not passable")
}
if t.entity != nil {
return errors.New("entity already present")
}
t.entity = p
return nil
}
//Remove the piece from a tile
func (t *Tile) Remove() {
t.entity = nil
}
//Empty returns true if tile is empty
func (t *Tile) Empty() bool {
return t.entity == nil
}
//Team returns the team of the piece currently occupying it, or 0
func (t *Tile) Team() Colour {
if t.entity != nil {
return t.entity.Owner
}
return NoColour
}
//Occupied returns true if the tile is currently occupied
func (t *Tile) Occupied() bool {
return t.entity != nil
}
//Piece returns the current Piece if any
func (t *Tile) Piece() *Piece {
return t.entity
}
//Passable returns if the tile is passable
func (t *Tile) Passable() bool {
return t.passable
}
//Colour returns the tile colour
func (t *Tile) Colour() Colour {
return t.colour
}
//X returns tile X position
func (t *Tile) X() int {
return t.x
}
//Y returns tile y position
func (t *Tile) Y() int {
return t.y
}
//AddTerrain adds specified terrain to position
2022-02-20 16:07:20 -05:00
func (t *Tile) AddTerrain(ter int) bool {
2022-02-18 18:36:18 -05:00
if t.entity != nil {
return false
}
t.passable = false
return true
}