diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-01-20 03:18:06 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-27 16:43:44 +0100 |
| commit | c4cb9637f5c85d03aa162968f0dfec3c193fc9dc (patch) | |
| tree | 3ad32e5dfd7c4ae72f66b86e77edef7b700a8b7a /go/game/state.go | |
| parent | 9df5cc7b55d8ac9ecd775a14a14f80a6c36c4d74 (diff) | |
| download | muhqs-game-c4cb9637f5c85d03aa162968f0dfec3c193fc9dc.tar.gz muhqs-game-c4cb9637f5c85d03aa162968f0dfec3c193fc9dc.zip | |
intermediate commit
Implement actions and multiple ui widgets
Diffstat (limited to 'go/game/state.go')
| -rw-r--r-- | go/game/state.go | 77 |
1 files changed, 72 insertions, 5 deletions
diff --git a/go/game/state.go b/go/game/state.go index caae8106..3d600093 100644 --- a/go/game/state.go +++ b/go/game/state.go @@ -1,5 +1,10 @@ package game +import ( + "image/color" + "math/rand" +) + type State struct { Decks []*Deck Stores []*Store @@ -8,25 +13,87 @@ type State struct { Hands []*Hand DiscardPiles []*DiscardPile Players []*Player - Permanents []*Permanent + Permanents []Permanent Units []*Unit + Artifacts []*Artifact + Rand rand.Rand } func NewState() *State { - return &State{} + s := &State{} + s.Stack = NewStack(s) + return s } -func (s *State) AddNewPlayer(name string, deck *Deck) { - p := NewPlayer(len(s.Players), name, deck, s) +func (s *State) AddNewPlayer(name string, deck *Deck, color color.Color) { + p := NewPlayer(len(s.Players), name, deck, s, color) s.Players = append(s.Players, p) } func (s *State) AddNewUnit(card string, pos Position, owner *Player) { unit := NewUnit(card, s.Map.TileAt(pos), owner) - s.Permanents = append(s.Permanents, &unit.Permanent) + s.Permanents = append(s.Permanents, unit) s.Units = append(s.Units, unit) } +func (s *State) removePermanent(p Permanent) { + for i, perm := range s.Permanents { + if perm == p { + s.Permanents[i] = s.Permanents[len(s.Permanents)-1] + s.Permanents = s.Permanents[:len(s.Permanents)-1] + } + } +} + +func removePtr[P *Unit | *Artifact](collection []P, ptr P) []P { + for i, p := range collection { + if p == ptr { + collection[i] = collection[len(collection)-1] + return collection[:len(collection)-1] + } + } + return collection +} + +func (s *State) DestroyPermanent(p Permanent) { + s.removePermanent(p) + + switch p.(type) { + case *Unit: + s.Units = removePtr(s.Units, p.(*Unit)) + case *Artifact: + s.Artifacts = removePtr(s.Artifacts, p.(*Artifact)) + } +} + +func (s *State) AddNewArtifact(card string, pos Position, owner *Player) { + artifact := NewArtifact(card, s.Map.TileAt(pos), owner) + s.Permanents = append(s.Permanents, artifact) + s.Artifacts = append(s.Artifacts, artifact) +} + +func (s *State) Fight(p1, p2 Permanent) { + p1.Fight(p2) + p2.Fight(p1) + if p1.IsDestroyed() { + s.DestroyPermanent(p1) + } + if p2.IsDestroyed() { + s.DestroyPermanent(p2) + } +} + func (s *State) MoveUnit(u *Unit, t *Tile) { u.move(s, t) } + +func (s *State) EnemyUnits(player *Player) []*Unit { + // TODO: support coop / teams + enemyUnits := []*Unit{} + for _, u := range s.Units { + if u.GetController() != player { + enemyUnits = append(enemyUnits, u) + } + } + return enemyUnits +} |
