aboutsummaryrefslogtreecommitdiff
path: root/go/game/state.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-04-03 16:47:42 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2025-01-27 16:43:53 +0100
commit011b95e8db31f1306eafb575ed12bed4e2fc1ca3 (patch)
tree44b007c1324e99e26014bab45faefa755c6ab38d /go/game/state.go
parent4d33d8b637211aaa3e0adfda68e363e64f90623f (diff)
downloadmuhqs-game-011b95e8db31f1306eafb575ed12bed4e2fc1ca3.tar.gz
muhqs-game-011b95e8db31f1306eafb575ed12bed4e2fc1ca3.zip
implement equipment with next unit play option
Also make more state methods private.
Diffstat (limited to 'go/game/state.go')
-rw-r--r--go/game/state.go84
1 files changed, 50 insertions, 34 deletions
diff --git a/go/game/state.go b/go/game/state.go
index 604e4530..87e1ee3d 100644
--- a/go/game/state.go
+++ b/go/game/state.go
@@ -12,20 +12,21 @@ import (
)
type State struct {
- Decks []*Deck
- Stores []*Store
- Exile *PileOfCardsBase
- Map *Map
- Stack *Stack
- Hands []*Hand
- DiscardPiles []*DiscardPile
- Players []*Player
- ActivePlayer *Player
- ActivePhase PhaseType
- Permanents []Permanent
- Units []*Unit
- Rand *rand.Rand
- EotEffects []effect
+ Decks []*Deck
+ Stores []*Store
+ Exile *PileOfCardsBase
+ Map *Map
+ Stack *Stack
+ Hands []*Hand
+ DiscardPiles []*DiscardPile
+ Players []*Player
+ ActivePlayer *Player
+ ActivePhase PhaseType
+ Permanents []Permanent
+ Units []*Unit
+ Rand *rand.Rand
+ EotEffects []effect
+ outstandingEquipment []*Card
}
func NewState() *State {
@@ -114,6 +115,11 @@ func (s *State) IsValidPlay(a *PlayAction) error {
}
if a.Card.IsPermanent() {
+ // Equipments may be played not on a spawn tile but equiped to the next played unit
+ if a.Card.Type == CardTypes.Equipment && len(a.Target().sel) == 0 {
+ return nil
+ }
+
spawn := a.Target().sel[0].(*Tile)
if !slices.Contains(s.AvailableSpawnTiles(a.Source().(*Player), a.Card), spawn) {
return fmt.Errorf("Spawn %v is not a valid spawn tile for %s",
@@ -354,7 +360,7 @@ func (s *State) broadcastNotification(n PlayerNotification) {
}
}
-func (s *State) AddPermanent(perm Permanent) {
+func (s *State) addPermanent(perm Permanent) {
s.Permanents = append(s.Permanents, perm)
switch p := perm.(type) {
case *Unit:
@@ -366,26 +372,22 @@ func (s *State) AddPermanent(perm Permanent) {
}
}
-func (s *State) AddNewUnit(card *Card, pos Position, owner *Player) {
- s.AddPermanent(NewUnit(card, s.Map.TileAt(pos), owner))
-}
-
-func (s *State) AddNewArtifact(card *Card, pos Position, owner *Player) {
- s.AddPermanent(NewArtifact(card, s.Map.TileAt(pos), owner))
+func (s *State) addNewUnit(card *Card, pos Position, owner *Player) *Unit {
+ u := NewUnit(card, s.Map.TileAt(pos), owner)
+ s.addPermanent(u)
+ return u
}
-func (s *State) addNewEquipment(card *Card, pos Position, owner *Player) {
- s.AddPermanent(newEquipment(card, s.Map.TileAt(pos), owner))
+func (s *State) addNewArtifact(card *Card, pos Position, owner *Player) *Artifact {
+ a := NewArtifact(card, s.Map.TileAt(pos), owner)
+ s.addPermanent(a)
+ return a
}
-func (s *State) AddNewPermFromPath(cardPath string, pos Position, owner *Player) {
- card := NewCard(cardPath)
- switch card.Type {
- case CardTypes.Unit:
- s.AddNewUnit(card, pos, owner)
- case CardTypes.Artifact:
- s.AddNewArtifact(card, pos, owner)
- }
+func (s *State) addNewEquipment(card *Card, pos Position, owner *Player) *Equipment {
+ e := newEquipment(card, s.Map.TileAt(pos), owner)
+ s.addPermanent(e)
+ return e
}
func (s *State) removePermanent(p Permanent) {
@@ -470,12 +472,21 @@ func (s *State) resolvePlay(p *Player, c *Card, targets *Targets) {
switch c.Type {
case CardTypes.Unit:
tile := targets.ts[0].sel[0].(*Tile)
- s.AddNewUnit(c, tile.Position, p)
+ u := s.addNewUnit(c, tile.Position, p)
+ if len(s.outstandingEquipment) > 0 {
+ for _, e := range s.outstandingEquipment {
+ u.equip(newEquipment(e, nil, p))
+ }
+ s.outstandingEquipment = nil
+ }
case CardTypes.Artifact:
tile := targets.ts[0].sel[0].(*Tile)
- s.AddNewArtifact(c, tile.Position, p)
+ s.addNewArtifact(c, tile.Position, p)
case CardTypes.Equipment:
- if tile, ok := targets.ts[0].sel[0].(*Tile); ok {
+ if len(targets.ts[0].sel) == 0 {
+ s.outstandingEquipment = append(s.outstandingEquipment, c)
+ } else {
+ tile, _ := targets.ts[0].sel[0].(*Tile)
s.addNewEquipment(c, tile.Position, p)
}
case CardTypes.Spell:
@@ -584,6 +595,11 @@ func (s *State) addEotEffect(e effect) {
}
func (s *State) endOfTurn() {
+ // Move all outstanding equipment cards to the discard pile
+ for _, e := range s.outstandingEquipment {
+ s.ActivePlayer.DiscardPile.AddCard(e)
+ }
+
for _, e := range s.EotEffects {
e.end(s)
}