Class: Gamefic::Action
- Inherits:
-
Object
- Object
- Gamefic::Action
- Defined in:
- lib/gamefic/action.rb
Overview
Actions manage the execution of commands that Characters can perform.
Constant Summary collapse
- @@order_key_seed =
0
Instance Attribute Summary collapse
-
#meta ⇒ Object
writeonly
Sets the attribute meta.
-
#order_key ⇒ Object
readonly
Returns the value of attribute order_key.
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
Instance Method Summary collapse
-
#execute(*args) ⇒ Object
Execute this Action.
-
#initialize(verb, *queries, &proc) ⇒ Action
constructor
A new instance of Action.
-
#meta? ⇒ Boolean
Is this a meta Action? If an Action is flagged meta, it usually means that it provides information about the game or manages some aspect of the user interface.
- #signature ⇒ Object
-
#specificity ⇒ Fixnum
Get the specificity of the Action.
-
#verb ⇒ Symbol
Get the verb associated with this Action.
Constructor Details
#initialize(verb, *queries, &proc) ⇒ Action
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gamefic/action.rb', line 15 def initialize(verb, *queries, &proc) if !verb.kind_of?(Symbol) verb = verb.to_s verb = nil if verb == '' end @order_key = @@order_key_seed @@order_key_seed += 1 @proc = proc if (verb.kind_of?(Symbol) == false and !verb.nil?) raise "Action verbs must be symbols #{verb}" end if !@proc.nil? if (queries.length + 1 != @proc.arity) and (@proc.arity > 0) raise ActionArgumentError.new("Number of queries is not compatible with proc arguments") end end @verb = verb @queries = queries end |
Instance Attribute Details
#meta=(value) ⇒ Object (writeonly)
Sets the attribute meta
12 13 14 |
# File 'lib/gamefic/action.rb', line 12 def (value) = value end |
#order_key ⇒ Object (readonly)
Returns the value of attribute order_key.
11 12 13 |
# File 'lib/gamefic/action.rb', line 11 def order_key @order_key end |
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
11 12 13 |
# File 'lib/gamefic/action.rb', line 11 def queries @queries end |
Instance Method Details
#execute(*args) ⇒ Object
Execute this Action. This method is typically called by the Plot when a Character performs a command.
69 70 71 |
# File 'lib/gamefic/action.rb', line 69 def execute *args @proc.call(*args) end |
#meta? ⇒ Boolean
Is this a meta Action? If an Action is flagged meta, it usually means that it provides information about the game or manages some aspect of the user interface. It shouldn’t represent an Action that the player’s character performs in the game world. Examples include Actions to display credits or instructions.
89 90 91 |
# File 'lib/gamefic/action.rb', line 89 def ||= false end |
#signature ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/gamefic/action.rb', line 73 def signature sig = ["#{@verb}"] @queries.each { |q| sig.push q.signature } "#{sig.join(', ').gsub(/Gamefic::(Query::)?/, '')}(#{specificity})" end |
#specificity ⇒ Fixnum
Get the specificity of the Action. Specificity indicates how narrowly the Action’s queries filter matches. Actions with higher specificity are given higher priority when searching for the Action that matches a character command. For example, an Action with a Query that filters for a specific class of Entity has a higher specificity than an Action with a Query that accepts arbitrary text.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gamefic/action.rb', line 43 def specificity spec = 0 if verb.nil? spec = -100 end @queries.each { |q| if q.kind_of?(Query::Base) spec += q.specificity else spec += 1 end } return spec end |
#verb ⇒ Symbol
Get the verb associated with this Action. The verb is represented by a Symbol in the imperative form, such as :take or :look_under.
63 64 65 |
# File 'lib/gamefic/action.rb', line 63 def verb @verb end |