Class: Gamefic::Character

Inherits:
Entity
  • Object
show all
Defined in:
lib/gamefic/character.rb

Instance Attribute Summary collapse

Attributes inherited from Entity

#session

Attributes included from Grammar::Plural

#plural

Attributes included from Grammar::Person

#person

Attributes included from Grammar::Gender

#gender

Attributes included from Describable

#definite_article, #indefinite_article, #name, #synonyms

Instance Method Summary collapse

Methods inherited from Entity

#[], #[]=, #parent=, #post_initialize, #pre_initialize, #uid, #update

Methods included from Serialized::ClassMethods

#serialize, #serializer

Methods included from Grammar::WordAdapter

#contract, #pronoun, #verb

Methods included from Grammar::Plural

#plural?

Methods included from Serialized

included, #serialized_attributes

Methods included from Describable

default_description, default_description=, #definitely, #description, #description=, #has_description?, #indefinitely, #keywords, #proper_named=, #proper_named?, #to_s

Methods included from Node

#children, #flatten, #parent, #parent=

Constructor Details

#initialize(args = {}) ⇒ Character

Returns a new instance of Character.



18
19
20
21
22
23
# File 'lib/gamefic/character.rb', line 18

def initialize(args = {})
  @queue = Array.new
  super
  @buffer_stack = 0
  @buffer = ""
end

Instance Attribute Details

#last_objectEntity? (readonly)

Returns:



12
13
14
# File 'lib/gamefic/character.rb', line 12

def last_object
  @last_object
end

#last_orderGamefic::Director::Order



10
11
12
# File 'lib/gamefic/character.rb', line 10

def last_order
  @last_order
end

#next_sceneObject (readonly)

Returns the value of attribute next_scene.



15
16
17
# File 'lib/gamefic/character.rb', line 15

def next_scene
  @next_scene
end

#object_of_pronounObject

Returns the value of attribute object_of_pronoun.



13
14
15
# File 'lib/gamefic/character.rb', line 13

def object_of_pronoun
  @object_of_pronoun
end

#playbookObject

Returns the value of attribute playbook.



16
17
18
# File 'lib/gamefic/character.rb', line 16

def playbook
  @playbook
end

#queueObject (readonly)

Returns the value of attribute queue.



8
9
10
# File 'lib/gamefic/character.rb', line 8

def queue
  @queue
end

#sceneObject (readonly)

Returns the value of attribute scene.



14
15
16
# File 'lib/gamefic/character.rb', line 14

def scene
  @scene
end

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/gamefic/character.rb', line 8

def user
  @user
end

Instance Method Details

#conclude(scene) ⇒ Object

Raises:



133
134
135
136
# File 'lib/gamefic/character.rb', line 133

def conclude scene
  raise NotConclusionError if !scene.kind_of?(Scene::Conclusion)
  cue scene
end

#concluded?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/gamefic/character.rb', line 138

def concluded?
  !scene.nil? and scene.kind_of?(Scene::Conclusion)
end

#connect(user) ⇒ Object

Connect a User.

Parameters:



28
29
30
# File 'lib/gamefic/character.rb', line 28

def connect(user)
  @user = user
end

#cue(scene) ⇒ Object



123
124
125
126
127
# File 'lib/gamefic/character.rb', line 123

def cue scene
  @next_scene = nil
  @scene = scene
  @scene.start self unless @scene.nil?
end

#disconnectObject

Disconnect the current User.



34
35
36
# File 'lib/gamefic/character.rb', line 34

def disconnect
  @user = nil
end

#perform(*command) ⇒ Object

Perform a command. The command can be specified as a String or a set of tokens. Either form should yield the same result, but using tokens can yield better performance since it bypasses the parser.

The command will be executed immediately regardless of game state.

Examples:

Send a command as a string

character.perform "take the key"

Send a command as a set of tokens

character.perform :take, @key


51
52
53
# File 'lib/gamefic/character.rb', line 51

def perform(*command)
  Director.dispatch(self, *command)
end

#performed(order) ⇒ Object



142
143
144
# File 'lib/gamefic/character.rb', line 142

def performed order
  @last_order = order
end

#prepare(scene) ⇒ Object



129
130
131
# File 'lib/gamefic/character.rb', line 129

def prepare scene
  @next_scene = scene
end

#proceedObject

Proceed to the next Action in the current stack. This method is typically used in Action blocks to cascade through multiple implementations of the same verb.

Examples:

Proceed through two implementations of a verb

introduction do |actor|
  actor[:has_eaten] = false # Initial value
end
respond :eat do |actor|
  actor.tell "You eat something."
  actor[:has_eaten] = true
end
respond :eat do |actor|
  # This version will be executed first because it was implemented last
  if actor[:has_eaten]
    actor.tell "You already ate."
  else
    actor.proceed # Execute the previous implementation
  end
end


119
120
121
# File 'lib/gamefic/character.rb', line 119

def proceed
  Director::Delegate.proceed_for self
end

#promptObject



146
147
148
# File 'lib/gamefic/character.rb', line 146

def prompt
  scene.nil? ? '>' : scene.prompt_for(self)
end

#quietly(*command) ⇒ String

Quietly perform a command. This method executes the command exactly as #perform does, except it buffers the resulting output instead of sending it to the user.

Returns:

  • (String)

    The output that resulted from performing the command.



60
61
62
63
64
65
66
67
68
# File 'lib/gamefic/character.rb', line 60

def quietly(*command)
  if @buffer_stack == 0
    @buffer = ""
  end
  @buffer_stack += 1
  self.perform *command
  @buffer_stack -= 1
  @buffer
end

#stream(message) ⇒ Object

Send a message to the Character as raw text. Unlike #tell, this method will not wrap the message in HTML paragraphs.

Parameters:



94
95
96
# File 'lib/gamefic/character.rb', line 94

def stream(message)
  user.send message.strip unless user.nil?
end

#tell(message) ⇒ Object

Send a message to the Character. This method will automatically wrap the message in HTML paragraphs. To send a message without paragraph formatting, use #stream instead.

Parameters:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gamefic/character.rb', line 75

def tell(message)
  if user != nil and message.to_s != ''
    if @buffer_stack > 0
      @buffer += message
    else
      message = "<p>#{message.strip}</p>"
      # This method uses String#gsub instead of String#gsub! for

      # compatibility with Opal.

      message = message.gsub(/[ \t\r]*\n[ \t\r]*\n[ \t\r]*/, '</p><p>')
      message = message.gsub(/[ \t]*\n[ \t]*/, ' ')
      user.send message
    end
  end
end