diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-01-25 21:10:44 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-27 16:43:44 +0100 |
| commit | 8720ebf9c74d9e11001772b5c00c293fa3a8b9e5 (patch) | |
| tree | 70013771c425746318d290b5cadad0bbc1a48abb /go/game/unit.go | |
| parent | 73d40903aecad3efcf7c6055c133f0db86558203 (diff) | |
| download | muhqs-game-8720ebf9c74d9e11001772b5c00c293fa3a8b9e5.tar.gz muhqs-game-8720ebf9c74d9e11001772b5c00c293fa3a8b9e5.zip | |
intermediate commit
Diffstat (limited to 'go/game/unit.go')
| -rw-r--r-- | go/game/unit.go | 84 |
1 files changed, 41 insertions, 43 deletions
diff --git a/go/game/unit.go b/go/game/unit.go index 02ba9830..0d39e904 100644 --- a/go/game/unit.go +++ b/go/game/unit.go @@ -1,10 +1,7 @@ package game import ( - "fmt" "log" - "strconv" - "strings" "golang.org/x/exp/slices" ) @@ -19,44 +16,10 @@ const ( Ward ) -type Attack struct { - Damage int - Range int -} - -func (a *Attack) String() string { - if a.Range == 1 { - return fmt.Sprintf("%d", a.Damage) - } - return fmt.Sprintf("%d range %d", a.Damage, a.Range) -} - -func parseAttack(attack interface{}) Attack { - if damage, ok := attack.(int); ok { - return Attack{damage, 1} - } - - attackStr := attack.(string) - tokens := strings.Split(attackStr, " ") - a, err := strconv.Atoi(tokens[0]) - if err != nil { - log.Fatalf("Attack %s has no valid Damage value\n", attackStr) - } - - 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", attackStr) - } - } - return Attack{a, r} -} - type Unit struct { PermanentBase Health int - Movement int + Movement Movement Attack Attack Upkeep int FullActions []*FullActionPrototype @@ -68,12 +31,33 @@ type Unit struct { } func NewUnit(card *Card, tile *Tile, owner *Player) *Unit { + var movement Movement + movementDesc, found := card.Values["movement"] + if !found { + movement = INVALID_MOVEMENT() + } else { + movement = parseMovement(movementDesc) + } + + upkeep, ok := card.Values["upkeep"].(int) + if !ok { + upkeep = 0 + } + + var attack Attack + attackDesc, found := card.Values["attack"] + if !found { + attack = INVALID_ATTACK() + } else { + attack = parseAttack(attackDesc) + } + u := &Unit{ NewPermanentBase(card, tile, owner), card.Values["health"].(int), - card.Values["movement"].(int), - parseAttack(card.Values["attack"]), - card.Values["upkeep"].(int), + movement, + attack, + upkeep, []*FullActionPrototype{}, []*FreeActionPrototype{}, []UnitState{}, @@ -98,12 +82,13 @@ func (u *Unit) move(s *State, tile *Tile) { } u.Tile.Permanent = nil + tile.Permanent = u u.Tile = tile } func (u *Unit) MoveRangeTiles(m *Map) []*Tile { tiles := []*Tile{} - for _, pos := range PositionsInRange(u.Tile.Position, u.Movement, false) { + for _, pos := range PositionsInRange(u.Tile.Position, u.Movement.Range, false) { tile := m.TileAt(pos) if tile != nil && u.IsAvailableTile(tile) { tiles = append(tiles, tile) @@ -126,8 +111,21 @@ func (u *Unit) AttackablePermanents(m *Map) []Permanent { return permanents } +func (u *Unit) AttackableEnemyPermanents(m *Map) []Permanent { + controller := u.GetController() + permanents := []Permanent{} + for _, t := range u.AttackableTiles(m) { + if t.Permanent != nil && t.Permanent.Attackable() && + t.Permanent.GetController() != controller { + + permanents = append(permanents, t.Permanent) + } + } + return permanents +} + func (u *Unit) IsAvailableTile(tile *Tile) bool { - return tile.IsFree() + return tile.IsAvailableForCard(u.Card) } func (u *Unit) Fight(p Permanent) { |
