Class: Gamefic::Character

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

Instance Attribute Summary collapse

Attributes inherited from Entity

#plot, #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

#[], #[]=, #destroy, #on_update, #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(plot, args = {}) ⇒ Character

Returns a new instance of Character.



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

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

Instance Attribute Details

#last_objectEntity? (readonly)

Returns:



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

def last_object
  @last_object
end

#last_orderGamefic::Director::Order



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

def last_order
  @last_order
end

#object_of_pronounObject

Returns the value of attribute object_of_pronoun.



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

def object_of_pronoun
  @object_of_pronoun
end

#queueObject (readonly)

Returns the value of attribute queue.



5
6
7
# File 'lib/gamefic/character.rb', line 5

def queue
  @queue
end

#userObject (readonly)

Returns the value of attribute user.



5
6
7
# File 'lib/gamefic/character.rb', line 5

def user
  @user
end

Instance Method Details

#conclude(scene_name) ⇒ Object



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

def conclude scene_name
  scene = plot.scenes[scene_name]
  raise "#{scene_name} is not a conclusion" unless scene.kind_of?(Scene::Conclusion)
  cue scene_name
end

#connect(user) ⇒ Object

Connect a User.

Parameters:



24
25
26
# File 'lib/gamefic/character.rb', line 24

def connect(user)
  @user = user
end

#cue(scene_name) ⇒ Object



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

def cue scene_name
  @scene = scene_name
  @next_scene = nil
  plot.scenes[scene_name].start self
end

#disconnectObject

Disconnect the current User.



30
31
32
# File 'lib/gamefic/character.rb', line 30

def disconnect
  @user = nil
end

#next_sceneObject



162
163
164
# File 'lib/gamefic/character.rb', line 162

def next_scene
  @next_scene
end

#perform(*command, from_user: false) ⇒ 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.

If the from_user argument is true, the command is assumed to have come directly from user input. The character’s last_order and last_object will be updated with the result.

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
54
55
# File 'lib/gamefic/character.rb', line 51

def perform(*command, from_user: false)
  o = Director.dispatch(self, *command)
  last_order = o if from_user
  o
end

#prepare(scene_name) ⇒ Object



140
141
142
# File 'lib/gamefic/character.rb', line 140

def prepare scene_name
  @next_scene = scene_name
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


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

def proceed
  return if delegate_stack.last.nil?
  delegate_stack.last.proceed
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.



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

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

#sceneSymbol

Get the name of the character’s current scene

Returns:

  • (Symbol)

    The name of the scene



153
154
155
# File 'lib/gamefic/character.rb', line 153

def scene
  @scene
end

#scene=(key) ⇒ Object

Alias for Character#cue key



158
159
160
# File 'lib/gamefic/character.rb', line 158

def scene= key
  cue key.to_sym
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:



96
97
98
# File 'lib/gamefic/character.rb', line 96

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:



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

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