Skip to content
Browse files

refactored out event system to encapsulate clock, queue and event hoo…

…k stuff
  • Loading branch information...
1 parent 6f172d4 commit 5b9e3b04ea4b468b2a3d44146541bc1a22a0de39 @bgoodspeed committed Nov 5, 2010
View
1 lib/ai/condition_matcher.rb
@@ -4,6 +4,7 @@ def initialize(cond)
@condition = cond
end
def matches?(src, target)
+ #TODO implement this
puts "TODO: condition #{@condition} matches #{target} ?"
true
end
View
1 lib/ai/target_matcher.rb
@@ -15,7 +15,6 @@ def matches?(src,target)
if target_is_enemy?
return is_enemy_of?(src,target)
end
- puts "target #{@target} matches #{target}?"
true
end
end
View
2 lib/battle/battle_hud.rb
@@ -16,13 +16,11 @@ def map_to_colors(rate)
def draw(menu_layer_config, game, battle)
heroes = battle.heroes
hpr = heroes.collect {|h| h.hp_ratio}
- puts "hprs: #{hpr.join(',')}"
health_rates = heroes.collect {|h| h.hp_ratio * 10}
ready_rates = heroes.collect {|h| h.ready_ratio * 10}
s = @surface_factory.make_surface([500, 50])
s.fill(:green)
- puts "health rates: #{health_rates.join(',')}"
health_rates.each_with_index do |hr, hi|
sub = @surface_factory.make_surface([10, 10])
colors = map_to_colors(hr)
View
1 lib/battle/battle_participant_cursor_text_rendering_config.rb
@@ -14,7 +14,6 @@ def cursor_offsets_at(position, game, menu_action)
if matches_menu_action?(menu_action)
offset = game.current_battle_participant_offset(position)
else
- puts "trouble brewin" if position.nil?
offset = [@@BATTLE_INVENTORY_XC + @@BATTLE_INVENTORY_XF * position, @@BATTLE_INVENTORY_YC + @@BATTLE_INVENTORY_YF * position]
end
offset
View
2 lib/battle/damage_calculation_helper.rb
@@ -1,7 +1,7 @@
class DamageCalculationHelper
def calculate_damage(src,dest)
- puts "uh oh, #{src} does 0 damage " if src.damage == 0
+ puts "uh oh, #{src} does 0 damage " if src.damage == 0
src.damage #TODO take dest defense into account etc
end
end
View
37 lib/domain/game.rb
@@ -11,13 +11,12 @@ class Game
def initialize()
@factory = GameInternalsFactory.new
@screen = @factory.make_screen
- @clock = @factory.make_clock
- @queue = @factory.make_queue
+
world1 = @factory.make_world1
world2 = @factory.make_world2
world3 = @factory.make_world3
@universe = @factory.make_universe([world1, world2, world3], @factory.make_game_layers(@screen, self), @factory.make_sound_effects, self) #XXX might be bad to pass self and make loops in the obj graph
-# @universe.toggle_bg_music #TODO turned this off because it was annoying me
+# @universe.toggle_bg_music #TODO turned this off because it was annoying me during testing
@player = @factory.make_player(@screen, @universe, self)
world1.add_npc(@factory.make_npc(@player, @universe))
@@ -38,8 +37,11 @@ def initialize()
:left => :battle_left, :right => :battle_right, :up => :battle_up, :down => :battle_down,
:i => :battle_confirm, :b => :battle_cancel
}
+ @event_system = @factory.make_event_system(self, always_on_hooks, menu_killed_hooks, menu_active_hooks, battle_hooks)
- @event_helper = @factory.make_event_hooks(self, always_on_hooks, menu_killed_hooks, menu_active_hooks, battle_hooks)
+# @event_helper = @factory.make_event_hooks(self, always_on_hooks, menu_killed_hooks, menu_active_hooks, battle_hooks)
+# @clock = @factory.make_clock
+# @queue = @factory.make_queue
end
@@ -63,8 +65,8 @@ def step_until(ticks)
end
def step_until_time(millisecs)
- time_of_death = @clock.lifetime + millisecs
- while (@clock.lifetime < time_of_death) do
+ time_of_death = @event_system.lifetime + millisecs
+ while (@event_system.lifetime < time_of_death) do
step
end
end
@@ -91,11 +93,9 @@ def all_hooks
end
def remove_all_hooks
- puts "pre hook count: #{all_hooks.size}"
all_hooks.each {|hook|
remove_hook(hook)
}
- puts "post hook count: #{all_hooks.size}"
end
extend Forwardable
@@ -115,10 +115,10 @@ def remove_all_hooks
:current_battle_participant_offset, :world_number, :notifications_layer,
:notifications, :current_selected_menu_entry_name, :current_menu_entries
- def_delegators :@event_helper, :non_menu_hooks, :rebuild_event_hooks
- def_delegator :@event_helper, :menu_active_hooks, :menu_hooks
- def_delegator :@event_helper, :battle_active_hooks, :battle_hooks
- def_delegator :@event_helper, :non_menu_hooks, :non_battle_hooks
+ def_delegators :@event_system, :non_menu_hooks, :rebuild_event_hooks
+ def_delegator :@event_system, :menu_active_hooks, :menu_hooks
+ def_delegator :@event_system, :battle_active_hooks, :battle_hooks
+ def_delegator :@event_system, :non_menu_hooks, :non_battle_hooks
def_delegator :@player, :update_tile_coords, :update_player_tile_coords
def_delegator :@player, :set_position, :set_player_position
@@ -136,7 +136,6 @@ def toggle_battle_hooks(in_battle=false)
EventManager.new.swap_event_sets(self, in_battle, non_battle_hooks, battle_hooks)
end
def toggle_menu
- #puts "tm: #{@event_helper.menu_active_hooks}"
EventManager.new.swap_event_sets(self, menu_layer.active?, non_menu_hooks, menu_hooks)
menu_layer.toggle_activity
unless menu_layer.active?
@@ -149,7 +148,7 @@ def interact_with_facing(event)
end
def simulate_event_with_key(k)
- @queue << KeyPressed.new(k)
+ @event_system.queue << KeyPressed.new(k)
end
private
@@ -197,18 +196,20 @@ def blit_universe
@universe.blit_world(@screen, @player)
end
def fetch_events
- @queue.fetch_sdl_events
+ @event_system.queue.fetch_sdl_events
end
+
+ #TODO move this method into the event system
def tick_clock
- tick = @clock.tick
- @queue << tick
+ tick = @event_system.clock.tick
+ @event_system.queue << tick
tick
end
def update_hud(tick)
@hud.update :time => "Framerate: #{1.0/tick.seconds}"
end
def process_events
- @queue.each do |event|
+ @event_system.queue.each do |event|
handle( event ) #if !@paused
end
end
View
1 lib/domain/inventory.rb
@@ -69,7 +69,6 @@ def inventory_item_at(idx)
def gain_inventory(inventory)
inventory.keys.each do |key|
- puts "key: #{key}"
add_item(inventory.quantity_of(key), key)
end
end
View
14 lib/event_system/event_system.rb
@@ -0,0 +1,14 @@
+# To change this template, choose Tools | Templates
+# and open the template in the editor.
+
+class EventSystem
+ extend Forwardable
+ attr_reader :queue, :clock
+ def_delegators :@clock, :lifetime
+ def_delegators :@helper, :non_menu_hooks, :menu_active_hooks, :menu_hooks
+ def initialize(clock, queue, helper)
+ @clock = clock
+ @queue = queue
+ @helper = helper
+ end
+end
View
4 lib/factories/game_internals_factory.rb
@@ -5,6 +5,10 @@ class GameInternalsFactory
include Rubygame::EventActions
include Rubygame::EventTriggers
+ def make_event_system(game, always_on_hooks, menu_killed_hooks, menu_active_hooks, battle_hooks)
+ EventSystem.new(make_clock, make_queue, make_event_hooks(game, always_on_hooks, menu_killed_hooks, menu_active_hooks, battle_hooks))
+ end
+
def make_screen
#@screen = Screen.open( [640, 480] )
View
1 lib/game_requirements.rb
@@ -1,6 +1,7 @@
# To change this template, choose Tools | Templates
# and open the template in the editor.
+require 'lib/event_system/event_system'
require 'lib/tile_system/sdl_coordinate'
require 'lib/tile_system/positioned_tile_coordinate'
View
1 lib/helpers/resource_loader.rb
@@ -10,7 +10,6 @@ def font_path
end
def load_font(name)
filename = File.join(font_path, name)
- puts "font"
raise MissingResourceError.new unless File.exists?(filename)
FontFacade.new(filename, @@DEPTH)
View
1 lib/input/key_holder.rb
@@ -43,7 +43,6 @@ def update_timed_keys(dt)
end
}
@ms_ttl_map.each {|k,v|
- puts "dt is #{dt}"
newv = v - dt * @@MS_PER_SEC
if newv <= 0
delete_key(k)
View
2 lib/interactables/open_treasure.rb
@@ -1,5 +1,5 @@
class OpenTreasure < Treasure
def activate(game, player, worldstate, tilex, tiley)
- puts "Nothing to do, already opened"
+ #NOOP nothing to do here.
end
end
View
2 lib/interactables/warp_point.rb
@@ -17,9 +17,7 @@ def activate(game, player, worldstate, tilex, tiley)
uni = player.universe
player.universe.fade_out_bg_music
player.universe.play_sound_effect(SoundEffect::WARP)
- puts "player was at #{player.px},#{player.py}"
player.set_position(@destination_x, @destination_y)
- puts "warp from #{worldstate} to #{uni.world_by_index(@destination)}"
uni.set_current_world_by_index(@destination)
player.universe.fade_in_bg_music
end
View
4 lib/menu/cursor_helper.rb
@@ -51,7 +51,6 @@ def activate(menu)
@path.push(@position)
@position = 0
e = menu.navigate_path_to_select(path, @currently_selected)
- puts "selecting #{e}"
add_currently_selected e
if menu.any_satisfiable_and_selected?(@currently_selected)
@@ -92,9 +91,7 @@ def reduce_only_to_zero(v)
end
def cancel
- puts "before cancel path was size: #{@path.size}"
@path.shift
- puts "after cancel path was size: #{@path.size}"
@depth = reduce_only_to_zero(@depth)
end
@@ -126,7 +123,6 @@ def path_element_at_index(idx)
def should_display_submenu_for?(index, depth)
- #puts "in should display submenu we have path size #{@path.join(',')}=>#{@path.size} vs depth: #{depth}"
(depth < @path.size) && @path.first == index
end
View
1 lib/menu/selectors/filtered_inventory_menu_selector.rb
@@ -28,7 +28,6 @@ def elements(selected=[])
def select_element_at(idx, selections)
rv = elements[idx]
- puts "rv is a #{rv}"
rv
end
View
1 lib/npcs/talking_n_p_c.rb
@@ -10,7 +10,6 @@ def initialize(player, universe, text, filename, posn, inv=nil, attrib=nil, ai=n
end
def interact(game, universe, player)
- puts "display dialog '#{@text}' from #{self}"
universe.dialog_layer.active = true
universe.dialog_layer.text = @text
end
View
2 lib/tile_system/coordinate_helper.rb
@@ -196,8 +196,6 @@ def update_pos( dt, who=nil )
@px -= dx if clamp_to_tile_restrictions_on_x(topo, new_bg_tile_coords) or clamp_to_tile_restrictions_on_x(interact, new_interaction_tile_coords) or !x_collisions.empty?
@py -= dy if clamp_to_tile_restrictions_on_y(topo, new_bg_tile_coords) or clamp_to_tile_restrictions_on_y(interact, new_interaction_tile_coords) or !y_collisions.empty?
-# puts "tile topo x: #{clamp_to_tile_restrictions_on_x(topo, new_bg_tile_coords)} tile interact x: #{clamp_to_tile_restrictions_on_x(interact, new_interaction_tile_coords)} x cols: #{!x_collisions.empty?}"
-# puts "tile topo y: #{clamp_to_tile_restrictions_on_y(topo, new_bg_tile_coords)} tile interact y: #{clamp_to_tile_restrictions_on_y(interact, new_interaction_tile_coords)} y cols: #{!y_collisions.empty?}"
cols = y_hits(x_hits(candidate_npcs(who)))
handle_collision(cols) unless cols.empty?
update_tile_coords
View
4 lib/tile_system/interaction_helper.rb
@@ -34,14 +34,12 @@ def interact_with_facing(game, px,py)
return if @policy.return_after_dialog
end
- puts "you are facing #{@facing}"
tilex = game.universe.current_world.x_offset_for_interaction(px)
tiley = game.universe.current_world.y_offset_for_interaction(py)
this_tile_interacts = game.universe.current_world.interaction_interpreter.interpret(tilex, tiley)
facing_tile_interacts = false
if this_tile_interacts
- puts "you can interact with the current tile"
interact_with_current_tile(game, tilex, tiley, this_tile_interacts)
return if @policy.return_after_current
end
@@ -71,15 +69,13 @@ def interact_with_facing(game, px,py)
facing_tile_close_enough = facing_tile_dist < @@INTERACTION_DISTANCE_THRESHOLD
if facing_tile_close_enough and facing_tile_interacts
- puts "you can interact with the facing tile in the #{@facing} direction, it is at #{facing_tilex} #{facing_tiley}"
interact_with_facing_tile(game, facing_tilex, facing_tiley, facing_tile_interacts)
return if @policy.return_after_facing
end
interactable_npcs = game.universe.current_world.npcs.select {|npc| npc.nearby?(px,py, @@INTERACTION_DISTANCE_THRESHOLD, @@INTERACTION_DISTANCE_THRESHOLD) }
unless interactable_npcs.empty?
- puts "you can interact with the npc: #{interactable_npcs[0]}"
interact_with_npc(game, interactable_npcs)
end
View
4 lib/world_weapons/world_weapon_helper.rb
@@ -13,9 +13,7 @@ def initialize(game)
end
def use_weapon
- if using_weapon?
- puts "world weapon already in use!"
- else
+ unless using_weapon?
@weapon = @player.world_weapon
@weapon.fired_from(@player.px, @player.py, @player.facing)
end
View
2 lib/world_weapons/world_weapon_interaction_helper.rb
@@ -1,7 +1,7 @@
class WorldWeaponInteractionHelper < InteractionHelper
def interact_with_current_tile(game, tilex, tiley)
- puts "noop current"
+ #TODO revisit this class
end
def interact_with_dialog
View
2 spec/menu/cursor_helper_spec.rb
@@ -39,9 +39,7 @@ def menu_layer_config
@cursor_helper.activate(@menu)
@cursor_helper.currently_selected.size.should == 1
@cursor_helper.currently_selected.search_selected_for(StatLineInfoMenuAction).should_not be_nil
- puts "this one"
@cursor_helper.activate(@menu)
- puts "done"
@cursor_helper.currently_selected.size.should == 2
@cursor_helper.currently_selected.search_selected_for(Hero).should_not be_nil

0 comments on commit 5b9e3b0

Please sign in to comment.
Something went wrong with that request. Please try again.