package game import ( "image/color" "math/rand" ) type State struct { Decks []*Deck Stores []*Store Map *Map Stack *Stack Hands []*Hand DiscardPiles []*DiscardPile Players []*Player Permanents []Permanent Units []*Unit Artifacts []*Artifact Rand rand.Rand } func NewState() *State { s := &State{} s.Stack = NewStack(s) return 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) 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 := p.(type) { case *Unit: s.Units = removePtr(s.Units, p) case *Artifact: s.Artifacts = removePtr(s.Artifacts, p) } } 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 }