lib/gamefic/plot.rb in gamefic-1.4.0 vs lib/gamefic/plot.rb in gamefic-1.4.1
- old
+ new
@@ -26,21 +26,20 @@
# Gamefic Studio has a PlotStageMetaMapper that handles it, but it doesn't run if
# the plugin isn't activated.
#include Gamefic, Tester, SceneMount, CommandMount, EntityMount, QueryMount, ArticleMount, YouMount, Snapshot
mount Gamefic, Tester, SceneMount, CommandMount, EntityMount, QueryMount,
ArticleMount, YouMount, Snapshot, Subplot::Host
- expose :script, :introduction, :assert_action, :before_player_update,
+ expose :script, :introduction, :assert_action,
:on_update, :on_player_update, :entities, :on_ready, :on_player_ready,
:players, :scenes, :metadata
# @param [Source::Base]
def initialize(source = nil)
@source = source || Source::Text.new({})
@commands = {}
@syntaxes = []
@ready_procs = []
- @before_player_update_procs = []
@update_procs = []
@player_ready = []
@player_procs = []
@working_scripts = []
@imported_scripts = []
@@ -169,52 +168,34 @@
# Introduce a player to the game.
# This method is typically called by the Engine that manages game execution.
def introduce(player)
player.extend Subplot::Feature
+ player.cue :active
@players.push player
- if @introduction != nil
- @introduction.call(player)
- end
- # TODO: There should probably be a default state specified
- # by the plot, which would be :active by default. We could
- # get it like player.cue nil.
- if player.scene.nil?
- player.cue :active
- ready
- update
- end
+ @introduction.call(player) unless @introduction.nil?
end
# Prepare the Plot for the next turn of gameplay.
# This method is typically called by the Engine that manages game execution.
def ready
- @ready_procs.each { |p|
- p.call
- }
+ @ready_procs.each { |p| p.call }
# Prepare player scenes for the update.
@players.each { |player|
+ this_scene = player.next_scene || player.scene
+ player.prepare nil
+ player.cue this_scene unless player.scene == this_scene
@player_ready.each { |block|
block.call player
}
}
end
# Update the Plot's current turn of gameplay.
# This method is typically called by the Engine that manages game execution.
def update
- @players.each { |player|
- @before_player_update_procs.each { |p| p.call player }
- this_scene = player.next_scene || player.scene
- player.prepare nil
- if this_scene != player.scene
- player.cue this_scene
- player.queue.shift
- else
- process_input player
- end
- }
+ @players.each { |p| process_input p }
@entities.each { |e| e.update }
@players.each { |player| update_player player }
@update_procs.each { |p| p.call }
end
@@ -260,40 +241,30 @@
# @yieldparam [Character]
def on_player_update &block
@player_procs.push block
end
- # Add a block to be executed for each player before the turn's update is
- # performed.
- #
- # @yieldparam [Character]
- def before_player_update &block
- @before_player_update_procs.push block
- end
-
private
+
def process_input player
line = player.queue.shift
if !line.nil?
scenes[player.scene].finish player, line
end
end
+
def update_player player
@player_procs.each { |proc|
proc.call player
}
end
+
def rem_entity(entity)
@entities.delete(entity)
@players.delete(entity)
end
- def recursive_update(entity)
- entity.update
- entity.children.each { |e|
- recursive_update e
- }
- end
+
def add_syntax syntax
if @commands[syntax.verb] == nil
raise "Action \"#{syntax.verb}\" does not exist."
end
# Delete duplicate syntaxes
@@ -308,10 +279,11 @@
else
b.token_count <=> a.token_count
end
}
end
+
def add_action(action)
@commands[action.verb] ||= []
@commands[action.verb].unshift action
@commands[action.verb].sort! { |a, b|
if a.specificity == b.specificity
@@ -322,10 +294,11 @@
b.specificity <=> a.specificity
end
}
generate_default_syntax action
end
+
def generate_default_syntax action
user_friendly = action.verb.to_s.gsub(/_/, ' ')
args = []
used_names = []
action.queries.each { |c|
@@ -339,15 +312,18 @@
user_friendly += " #{new_name}"
args.push new_name
}
add_syntax Syntax.new(user_friendly.strip, "#{action.verb} #{args.join(' ')}")
end
+
def rem_action(action)
@commands[action.verb].delete(action)
end
+
def rem_syntax(syntax)
@syntaxes.delete syntax
end
+
def add_entity(entity)
@entities.push entity
end
end