CardOption.Enable

Make it possible for a card to display an option but not actually allow it to be selected. It's up to the UI layer to decide how to display options that are not enabled. The option text should probably contiain a note on why the option cannot be selected...
This commit is contained in:
2023-04-01 22:09:05 -07:00
parent 45e1eeebf7
commit 09fdf19948
2 changed files with 47 additions and 12 deletions

View File

@ -52,6 +52,9 @@ type CardOption[C StatsCollection] interface {
// After an option is enacted, the card is deleted. If a card should be
// repeatable, Enact must return it to the deck (on every option).
Enact(p *Player[C]) (Message, error)
// Enabled returns whether this option can curently be enacted.
Enabled(p *Player[C]) bool
}
// A BasicCard is a Card with fixed title, text, options, and optional
@ -64,22 +67,27 @@ type BasicCard[C StatsCollection] struct {
AfterOption func(p *Player[C], option CardOption[C]) error
}
func (b *BasicCard[C]) Title(p *Player[C]) (Message, error) {
// Title implements Card.
func (b *BasicCard[C]) Title(_ *Player[C]) (Message, error) {
return b.CardTitle, nil
}
// Urgent implements Card.
func (b *BasicCard[C]) Urgent(_ *Player[C]) bool {
return b.IsUrgent
}
func (b *BasicCard[C]) EventText(p *Player[C]) (Message, error) {
// EventText implements Card.
func (b *BasicCard[C]) EventText(_ *Player[C]) (Message, error) {
return b.CardText, nil
}
// Options implements Card.
func (b *BasicCard[C]) Options(_ *Player[C]) ([]CardOption[C], error) {
return b.CardOptions, nil
}
// Then implements Card.
func (b *BasicCard[C]) Then(p *Player[C], option CardOption[C]) error {
if b.AfterOption == nil {
return nil
@ -87,11 +95,13 @@ func (b *BasicCard[C]) Then(p *Player[C], option CardOption[C]) error {
return b.AfterOption(p, option)
}
func (b *BasicCard[C]) Drawn(p *Player[C]) bool {
// Drawn implements Card.
func (b *BasicCard[C]) Drawn(_ *Player[C]) bool {
return true
}
// A BasicOption is a CardOption with fixed text, effects, and output.
// It's always enabled.
type BasicOption[C StatsCollection] struct {
Text Message
Effect func(*Player[C]) error
@ -108,6 +118,11 @@ func (b *BasicOption[C]) Enact(p *Player[C]) (Message, error) {
return b.Output, b.Effect(p)
}
// Enabled implements CardOption.
func (b *BasicOption[C]) Enabled(p *Player[C]) bool {
return true
}
// OptionFunc attaches a fixed prompt to an Enact-like function. Unlike
// BasicOption, the enactment function provided to OptionFunc returns
// the text that should be output as a result of the action, so it is
@ -121,10 +136,17 @@ type optionFunc[C StatsCollection] struct {
f func(*Player[C]) (Message, error)
}
// OptionText implements CardOption.
func (o *optionFunc[C]) OptionText(p *Player[C]) (Message, error) {
return o.text, nil
}
// Enact implements CardOption.
func (o *optionFunc[C]) Enact(p *Player[C]) (Message, error) {
return o.f(p)
}
// Enabled implements CardOption.
func (o *optionFunc[C]) Enabled(p *Player[C]) bool {
return true
}