class TungstenEngine::Core::Game
Represents the base class for a real game
Attributes
fixed_time_step[RW]
Public Class Methods
new(fixed_time_step = true, target_frame_rate = 60)
click to toggle source
# File lib/core/game.rb, line 11 def initialize(fixed_time_step = true, target_frame_rate = 60) @components = [] @fixed_time_step = fixed_time_step self.target_frame_rate = target_frame_rate end
Public Instance Methods
exit()
click to toggle source
# File lib/core/game.rb, line 68 def exit @running = false end
register_component(component)
click to toggle source
# File lib/core/game.rb, line 21 def register_component(component) raise("Cannot register objects that ain't GameComponent instances") \ unless component.is_a? GameComponent end
render(game_time)
click to toggle source
# File lib/core/game.rb, line 62 def render(game_time) @components.each do |component| component.render(game_time) end end
run()
click to toggle source
# File lib/core/game.rb, line 26 def run started = Time.now last_loop = Time.now @running = true game_loop = Thread.new do while @running now = Time.now total = TimeSpan.diff(started, now) elapsed = TimeSpan.diff(last_loop, now) spare = @target_frame_microseconds - elapsed.microseconds slow = spare < 0 if @fixed_time_step && !slow sleep(spare / 1_000_000) now = Time.now total = TimeSpan.diff(started, now) elapsed = TimeSpan.diff(last_loop, now) # do not re-calculate 'slow' here, since we want to know if we # have been to slow when the loop was entered again, not if we maybe # are too slow after waiting end game_time = GameTime.new(total, elapsed, slow) update(game_time) render(game_time) last_loop = now end end game_loop.join end
target_frame_rate=(frame_rate)
click to toggle source
# File lib/core/game.rb, line 17 def target_frame_rate=(frame_rate) @target_frame_microseconds = 1_000_000.0 / frame_rate end
update(game_time)
click to toggle source
# File lib/core/game.rb, line 56 def update(game_time) @components.each do |component| component.update(game_time) end end