Card.Then

Add a callback to the Card interface to be invoked after an option is selected. This will most frequently be used for cards that always want to shuffle themselves back into the deck regardless of the Option selected.
This commit is contained in:
Kistaro Windrider 2023-03-27 20:21:57 -07:00
parent b672dfa9ec
commit 8153a7c083
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -16,6 +16,11 @@ type Card[C StatsCollection] interface {
// Options returns the possible actions the player can take for this card.
// There must be at least one option.
Options(p *Player[C]) ([]CardOption[C], error)
// Then is invoked after an option is selected and executed. The selected
// option is provided as an argument. This allows cards to do certain
// cleanup for every action -- for example, returning to the deck.
Then(p *Player[C], option CardOption[C]) error
}
// A CardOption represents a choice a player could make for some card.
@ -35,11 +40,12 @@ type CardOption[C StatsCollection] interface {
Enact(p *Player[C]) (Message, error)
}
// A BasicCard is a Card with fixed title, text, and options.
// A BasicCard is a Card with fixed title, text, options, and optional post-option callback.
type BasicCard[C StatsCollection] struct {
CardTitle Message
CardText Message
CardOptions []CardOption[C]
AfterOption func(p *Player[C], option CardOption[C]) error
}
func (b *BasicCard[C]) Title(p *Player[C]) (Message, error) {
@ -54,6 +60,13 @@ func (b *BasicCard[C]) Options(_ *Player[C]) ([]CardOption[C], error) {
return b.CardOptions, nil
}
func (b *BasicCard[C]) Then(p *Player[C], option CardOption[C]) error {
if b.AfterOption == nil {
return nil
}
return b.AfterOption(p, option)
}
// A BasicOption is a CardOption with fixed text, effects, and output.
type BasicOption[C StatsCollection] struct {
Text Message