aboutsummaryrefslogtreecommitdiff
path: root/go/game/state.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/game/state.go')
-rw-r--r--go/game/state.go77
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
+}