diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-08-03 19:41:20 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 15:57:07 +0200 |
| commit | a3e558ff022fb9c38e9edcf2ced51cfa95b8de4c (patch) | |
| tree | f907aee0ba70ca8347f6ec88458f0faea0413462 /go/game/state.go | |
| parent | 49a5eda54785976028e360ef1af311dfcc8e1560 (diff) | |
| download | muhqs-game-a3e558ff022fb9c38e9edcf2ced51cfa95b8de4c.tar.gz muhqs-game-a3e558ff022fb9c38e9edcf2ced51cfa95b8de4c.zip | |
add skeleton for triggers and triggered actions
Diffstat (limited to 'go/game/state.go')
| -rw-r--r-- | go/game/state.go | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/go/game/state.go b/go/game/state.go index 49ee0c06..6bb3dbf0 100644 --- a/go/game/state.go +++ b/go/game/state.go @@ -9,6 +9,7 @@ import ( "time" "golang.org/x/exp/slices" + "muhq.space/muhqs-game/go/utils" ) type State struct { @@ -24,6 +25,8 @@ type State struct { Rand *rand.Rand eotEffects []effect outstandingEquipment []*Card + events []Event + triggers []Trigger } func NewState() *State { @@ -96,6 +99,39 @@ func (s *State) Loop() []*Player { return winners } +func (s *State) removeTriggers(triggers []Trigger) { + for _, t := range triggers { + s.triggers = utils.RemoveFromUnorderedSlice(s.triggers, t) + } +} + +func (s *State) handleTriggers() { + var actions []Action + var triggersToRemove []Trigger + + for _, e := range s.events { + for _, t := range s.triggers { + a, remove := t.trigger(s, e) + if a != nil { + actions = append(actions, a...) + } + if remove { + triggersToRemove = append(triggersToRemove, t) + } + } + } + + // Reset occured events + s.events = nil + + s.removeTriggers(triggersToRemove) + + orderedActions := s.orderTriggeredActions(actions) + for _, a := range orderedActions { + s.declareAction(a) + } +} + func (s *State) stateBasedActions() { for _, p := range s.Permanents { p.Card().Impl.stateBasedActions(s, p) @@ -104,6 +140,8 @@ func (s *State) stateBasedActions() { s.destroyPermanent(p) } } + + s.handleTriggers() } func (s *State) IsValidPlay(a *PlayAction) error { @@ -322,6 +360,11 @@ func (s *State) declareAction(a Action) { s.broadcastNotification(newDeclaredActionNotification(a, err)) } +func (s *State) orderTriggeredActions(actions []Action) []Action { + // TODO: implement ordering + return actions +} + func (s *State) allPassing(skipFirst bool) bool { nPlayers := len(s.Players) i := 0 @@ -600,6 +643,8 @@ func (s *State) endOfTurn() { for _, e := range s.eotEffects { e.end(s) } + s.eotEffects = nil - s.eotEffects = []effect{} + s.events = append(s.events, newEotEvent()) + s.stateBasedActions() } |
