Class: Gamefic::Action

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 meta=(value)
  @meta = value
end

#order_keyObject (readonly)

Returns the value of attribute order_key.



11
12
13
# File 'lib/gamefic/action.rb', line 11

def order_key
  @order_key
end

#queriesObject (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 meta?
  @meta ||= false
end

#signatureObject



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

#specificityFixnum

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

#verbSymbol

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