Class: Chingu::Window
- Inherits:
-
Gosu::Window
- Object
- Gosu::Window
- Chingu::Window
- Includes:
- GFXHelpers, GameObjectHelpers, GameStateHelpers, InputClient, InputDispatcher
- Defined in:
- lib/chingu/window.rb
Instance Attribute Summary collapse
-
#game_objects ⇒ Object
readonly
Returns the value of attribute game_objects.
-
#game_state_manager ⇒ Object
readonly
Returns the value of attribute game_state_manager.
-
#milliseconds_since_last_tick ⇒ Object
readonly
Returns the value of attribute milliseconds_since_last_tick.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Attributes included from InputDispatcher
Instance Method Summary collapse
- #add_game_object(object) ⇒ Object
-
#button_down(id) ⇒ Object
By default button_down sends the keyevent to the GameStateManager ..
-
#button_up(id) ⇒ Object
By default button_up sends the keyevent to the GameStateManager ..
-
#draw ⇒ Object
Chingus main screen manupulation method.
-
#dt ⇒ Object
Mathematical short name for “milliseconds since last tick”.
-
#fps ⇒ Object
(also: #framerate)
Frames per second, access with $window.fps or $window.framerate.
- #initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666) ⇒ Window constructor
-
#intermediate_update ⇒ Object
“game logic” update that is safe to call even between Gosus update-calls.
- #remove_game_object(object) ⇒ Object
-
#ticks ⇒ Object
Total amount of game iterations (ticks).
-
#update ⇒ Object
Chingus core-logic / loop.
-
#update_game_objects ⇒ Object
Call update() on all game objects in main game window.
-
#update_game_state_manager ⇒ Object
Call update() on our game_state_manger -> call update on active state -> call update on all game objects in that state.
Methods included from InputClient
Methods included from InputDispatcher
#add_input_client, #dispatch_action, #dispatch_button_down, #dispatch_button_up, #dispatch_input_for, #remove_input_client
Methods included from GameObjectHelpers
Methods included from GFXHelpers
#fill, #fill_gradient, #fill_rect
Methods included from GameStateHelpers
#clear_game_states, #current_game_state, #pop_game_state, #previous_game_state, #push_game_state, #switch_game_state, #transitional_game_state
Constructor Details
#initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666) ⇒ Window
See www.libgosu.org/rdoc/classes/Gosu/Window.html
On top of that we add:
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/chingu/window.rb', line 32 def initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666) fullscreen ||= ARGV.include?("--fullscreen") $window = super(width, height, fullscreen, update_interval) @root = File.dirname(File.($0)) Gosu::Image.autoload_dirs = [".", File.join(@root, "gfx"), File.join(@root, "media")] Gosu::Sample.autoload_dirs = [".", File.join(@root, "sound"), File.join(@root, "media")] Gosu::Tile.autoload_dirs = [".", File.join(@root, "gfx"), File.join(@root, "media")] Gosu::Song.autoload_dirs = [".", File.join(@root, "sfx"), File.join(@root, "media")] @game_objects = Set.new @input_clients = Set.new # Set is like a unique Array with Hash lookupspeed @fps_counter = FPSCounter.new @game_state_manager = GameStateManager.new @milliseconds_since_last_tick = 0 end |
Instance Attribute Details
#game_objects ⇒ Object (readonly)
Returns the value of attribute game_objects.
19 20 21 |
# File 'lib/chingu/window.rb', line 19 def game_objects @game_objects end |
#game_state_manager ⇒ Object (readonly)
Returns the value of attribute game_state_manager.
19 20 21 |
# File 'lib/chingu/window.rb', line 19 def game_state_manager @game_state_manager end |
#milliseconds_since_last_tick ⇒ Object (readonly)
Returns the value of attribute milliseconds_since_last_tick.
19 20 21 |
# File 'lib/chingu/window.rb', line 19 def milliseconds_since_last_tick @milliseconds_since_last_tick end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
19 20 21 |
# File 'lib/chingu/window.rb', line 19 def root @root end |
Instance Method Details
#add_game_object(object) ⇒ Object
50 51 52 |
# File 'lib/chingu/window.rb', line 50 def add_game_object(object) @game_objects << object end |
#button_down(id) ⇒ Object
By default button_down sends the keyevent to the GameStateManager .. Which then is responsible to send it to the right GameState(s)
167 168 169 170 171 |
# File 'lib/chingu/window.rb', line 167 def (id) (id, self) @input_clients.each { |object| (id, object) } @game_state_manager.(id) end |
#button_up(id) ⇒ Object
By default button_up sends the keyevent to the GameStateManager .. Which then is responsible to send it to the right GameState(s)
157 158 159 160 161 |
# File 'lib/chingu/window.rb', line 157 def (id) (id, self) @input_clients.each { |object| (id, object) } @game_state_manager.(id) end |
#draw ⇒ Object
Chingus main screen manupulation method. If you override this in your Chingu::Window class, make sure to call super. Gosu will call this each game-iteration just after #update
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/chingu/window.rb', line 125 def draw # # Draw all game objects associated with the main window. # @game_objects.each { |object| object.draw } # # Let the game state manager call draw on the active game state (if any) # @game_state_manager.draw end |
#dt ⇒ Object
Mathematical short name for “milliseconds since last tick”
75 76 77 |
# File 'lib/chingu/window.rb', line 75 def dt @milliseconds_since_last_tick end |
#fps ⇒ Object Also known as: framerate
Frames per second, access with $window.fps or $window.framerate
60 61 62 |
# File 'lib/chingu/window.rb', line 60 def fps @fps_counter.fps end |
#intermediate_update ⇒ Object
“game logic” update that is safe to call even between Gosus update-calls
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/chingu/window.rb', line 97 def intermediate_update # # Dispatch inputmap for main window # dispatch_input_for(self) # # Dispatch input for all input-clients handled by to main window (game objects with input created in main win) # @input_clients.each { |game_object| dispatch_input_for(game_object) } # # Call update() on all game objects belonging to the main window. # update_game_objects # # Call update() on all game objects belonging to the current game state. # update_game_state_manager end |
#remove_game_object(object) ⇒ Object
53 54 55 |
# File 'lib/chingu/window.rb', line 53 def remove_game_object(object) @game_objects.delete(object) end |
#ticks ⇒ Object
Total amount of game iterations (ticks)
68 69 70 |
# File 'lib/chingu/window.rb', line 68 def ticks @fps_counter.ticks end |
#update ⇒ Object
Chingus core-logic / loop. Gosu will call this each game-iteration.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chingu/window.rb', line 82 def update # # Register a tick with our rather standard tick/framerate counter. # Returns the amount of milliseconds since last tick. This number is used in all update()-calls. # Without this self.fps would return an incorrect value. # If you override this in your Chingu::Window class, make sure to call super. # @milliseconds_since_last_tick = @fps_counter.register_tick intermediate_update end |
#update_game_objects ⇒ Object
Call update() on all game objects in main game window.
140 141 142 |
# File 'lib/chingu/window.rb', line 140 def update_game_objects @game_objects.each { |object| object.update } end |
#update_game_state_manager ⇒ Object
Call update() on our game_state_manger -> call update on active state -> call update on all game objects in that state
149 150 151 |
# File 'lib/chingu/window.rb', line 149 def update_game_state_manager @game_state_manager.update end |