Class: Gamefic::Response
- Inherits:
-
Object
- Object
- Gamefic::Response
- Defined in:
- lib/gamefic/response.rb
Overview
A proc to be executed in response to a command that matches its verb and queries.
Instance Attribute Summary collapse
- #narrative ⇒ Narrative readonly
- #queries ⇒ Array<Query::Base> readonly
- #verb ⇒ Symbol readonly
Instance Method Summary collapse
-
#accept?(actor, command) ⇒ Boolean
True if the Response can be executed for the given actor and command.
-
#attempt(actor, command) ⇒ Action?
Return an Action if the Response can accept the actor’s command.
- #execute(*args) ⇒ Object
- #hidden? ⇒ Boolean
-
#initialize(verb, narrative, *queries, meta: false, &block) ⇒ Response
constructor
A new instance of Response.
-
#meta? ⇒ Boolean
The ‘meta?` flag is just a way for authors to identify responses that serve a purpose other than performing in-game actions.
- #precision ⇒ Object
- #syntax ⇒ Object
Constructor Details
#initialize(verb, narrative, *queries, meta: false, &block) ⇒ Response
Returns a new instance of Response.
22 23 24 25 26 27 28 |
# File 'lib/gamefic/response.rb', line 22 def initialize verb, narrative, *queries, meta: false, &block @verb = verb @narrative = narrative @queries = map_queryable_objects(queries) @meta = @block = block end |
Instance Attribute Details
#narrative ⇒ Narrative (readonly)
15 16 17 |
# File 'lib/gamefic/response.rb', line 15 def narrative @narrative end |
#queries ⇒ Array<Query::Base> (readonly)
12 13 14 |
# File 'lib/gamefic/response.rb', line 12 def queries @queries end |
#verb ⇒ Symbol (readonly)
9 10 11 |
# File 'lib/gamefic/response.rb', line 9 def verb @verb end |
Instance Method Details
#accept?(actor, command) ⇒ Boolean
True if the Response can be executed for the given actor and command.
62 63 64 65 66 67 68 69 70 |
# File 'lib/gamefic/response.rb', line 62 def accept? actor, command return false if command.verb != verb || command.arguments.length != queries.length queries.each_with_index do |query, idx| return false unless query.accept?(actor, command.arguments[idx]) end true end |
#attempt(actor, command) ⇒ Action?
Return an Action if the Response can accept the actor’s command.
52 53 54 55 56 |
# File 'lib/gamefic/response.rb', line 52 def attempt actor, command return nil unless accept?(actor, command) Action.new(actor, command.arguments, self) end |
#execute(*args) ⇒ Object
72 73 74 |
# File 'lib/gamefic/response.rb', line 72 def execute *args Stage.run(narrative, *args, &@block) end |
#hidden? ⇒ Boolean
39 40 41 |
# File 'lib/gamefic/response.rb', line 39 def hidden? @hidden ||= verb.to_s.start_with?('_') end |
#meta? ⇒ Boolean
The ‘meta?` flag is just a way for authors to identify responses that serve a purpose other than performing in-game actions. Out-of-game responses can include features like displaying help documentation or listing credits.
35 36 37 |
# File 'lib/gamefic/response.rb', line 35 def @meta end |
#precision ⇒ Object
76 77 78 |
# File 'lib/gamefic/response.rb', line 76 def precision @precision ||= calculate_precision end |
#syntax ⇒ Object
43 44 45 |
# File 'lib/gamefic/response.rb', line 43 def syntax @syntax ||= generate_default_syntax end |