aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-02-04 13:09:57 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-01-27 16:43:47 +0100
commitb0a8cf800123af3fe6e200eee364457168afee04 (patch)
treed72f347458c48e26750432e222b3a0d2c11e7351
parent77b53f7feb759c3969ec3937edddad8cf4e4a00f (diff)
downloadmuhqs-game-b0a8cf800123af3fe6e200eee364457168afee04.tar.gz
muhqs-game-b0a8cf800123af3fe6e200eee364457168afee04.zip
introduce state-based action
State-based action are executed anytime a action resolved to handle events caused by the game state. Currently state-based actions only destroy unit with more damage than health.
-rw-r--r--go/game/stack.go7
-rw-r--r--go/game/state.go20
2 files changed, 15 insertions, 12 deletions
diff --git a/go/game/stack.go b/go/game/stack.go
index 9132ae13..cf0622ce 100644
--- a/go/game/stack.go
+++ b/go/game/stack.go
@@ -17,11 +17,11 @@ func (s *Stack) IsEmpty() bool {
return len(s.Actions) == 0
}
-func (s *Stack) Push(a Action) {
+func (s *Stack) push(a Action) {
s.Actions = append(s.Actions, a)
}
-func (s *Stack) Pop() {
+func (s *Stack) pop() {
l := len(s.Actions)
if l == 0 {
log.Fatalf("Can not pop from empty stack")
@@ -41,6 +41,7 @@ func (s *Stack) Pop() {
func (s *Stack) Resolve() {
for !s.IsEmpty() {
s.gameState.AllPassing(false)
- s.Pop()
+ s.pop()
+ s.gameState.stateBasedActions()
}
}
diff --git a/go/game/state.go b/go/game/state.go
index 15b6b226..45c8bba5 100644
--- a/go/game/state.go
+++ b/go/game/state.go
@@ -96,6 +96,14 @@ func (s *State) Loop() []*Player {
return winners
}
+func (s *State) stateBasedActions() {
+ for _, u := range s.Units {
+ if u.IsDestroyed() {
+ s.DestroyPermanent(u)
+ }
+ }
+}
+
func (s *State) IsValidPlay(a *PlayAction) error {
if s.ActivePhase != Phases.ActionPhase {
log.Fatalf("Play actions can only be declared during one's actions phase")
@@ -203,12 +211,12 @@ func (s *State) ValidateAction(a Action) (err error) {
return
}
-func (s *State) PushAction(a Action) {
+func (s *State) pushAction(a Action) {
if !a.PayCosts(s) {
log.Fatalf("Cost for %s could not be paid", a)
}
- s.Stack.Push(a)
+ s.Stack.push(a)
}
func (s *State) DeclareAction(a Action) {
@@ -234,7 +242,7 @@ func (s *State) DeclareAction(a Action) {
}
if err == nil {
- s.PushAction(a)
+ s.pushAction(a)
}
s.broadcastNotification(NewDeclaredActionNotification(a, err))
@@ -337,12 +345,6 @@ func (s *State) DestroyPermanent(p Permanent) {
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) {