aboutsummaryrefslogtreecommitdiff
path: root/go/game/unit.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/game/unit.go')
-rw-r--r--go/game/unit.go89
1 files changed, 89 insertions, 0 deletions
diff --git a/go/game/unit.go b/go/game/unit.go
new file mode 100644
index 00000000..4c07d625
--- /dev/null
+++ b/go/game/unit.go
@@ -0,0 +1,89 @@
+package game
+
+import (
+ "golang.org/x/exp/slices"
+ "log"
+ "strconv"
+ "strings"
+)
+
+type UnitStates int
+
+const (
+ Paralysis UnitStates = iota + 1
+ Poison
+ Panic
+ Rage
+ Ward
+)
+
+type Attack struct {
+ Damage int
+ Range int
+}
+
+func parseAttack(attack string) Attack {
+ tokens := strings.Split(attack, " ")
+ a, err := strconv.Atoi(tokens[0])
+ if err != nil {
+ log.Fatalf("Attack %s has no valid Damage value\n", attack)
+ }
+
+ r := 1
+ if len(tokens) == 3 {
+ r, err = strconv.Atoi(tokens[2])
+ if err != nil {
+ log.Fatalf("Attack %s has no valid Range value\n", attack)
+ }
+ }
+ return Attack{a, r}
+}
+
+type Unit struct {
+ Permanent
+ Health int
+ Movement int
+ Attack Attack
+ Upkeep int
+ // FullActions []FullActions
+ // FreeActions []FreeActions
+ States []UnitStates
+ // Effects []Effects
+}
+
+func NewUnit(cardPath string, tile *Tile, owner *Player) *Unit {
+ card := NewCard(cardPath)
+
+ return &Unit{
+ Permanent{card, tile, 0, []*Permanent{}, owner, owner},
+ card.Values["health"].(int),
+ card.Values["movement"].(int),
+ parseAttack(card.Values["attack"].(string)),
+ card.Values["upkeep"].(int),
+ []UnitStates{},
+ }
+}
+
+func (u *Unit) move(s *State, tile *Tile) {
+ if !slices.Contains(u.MoveRangeTiles(s.Map), tile) {
+ log.Panicf("Unit %s at %v can not move to %v", u.Card.Name, u.Tile.Position, tile.Position)
+ }
+
+ u.Tile.Permanent = nil
+ u.Tile = tile
+}
+
+func (u *Unit) MoveRangeTiles(m *Map) []*Tile {
+ tiles := []*Tile{}
+ for _, pos := range PositionsInRange(u.Tile.Position, u.Movement, false) {
+ tile := m.TileAt(pos)
+ if u.IsAvailableTile(tile) {
+ tiles = append(tiles, tile)
+ }
+ }
+ return tiles
+}
+
+func (u *Unit) IsAvailableTile(tile *Tile) bool {
+ return tile.IsFree()
+}