class RubyMan::GameMain
Game window and the game itself
Attributes
lives[RW]
moving_objects[R]
objects[R]
player[RW]
score[RW]
scores[R]
x_res[R]
y_res[R]
Public Class Methods
new()
click to toggle source
Calls superclass method
# File lib/ruby_man/game_main.rb, line 18 def initialize @x_res, @y_res = 800, 640 super(@x_res, @y_res, false) @resmgr = ResourceManager.new(self) @objects, @moving_objects = [], [] @last_milliseconds = Gosu.milliseconds @food_left = 0 @pause_time = -0.5 self.caption = 'RubyMan' load_scores load_level_list main_menu end
Public Instance Methods
add_object(obj)
click to toggle source
# File lib/ruby_man/game_main.rb, line 130 def add_object(obj) @objects.push(obj) obj.game = self @moving_objects.push(obj) if obj.is_a?(MovingObject) obj.created end
add_score(name)
click to toggle source
# File lib/ruby_man/game_main.rb, line 55 def add_score(name) entry = [name, Time.new.strftime('%Y-%m-%d %H:%M'), @score] @scores.push(entry) sort_scores save_scores end
check_collisions()
click to toggle source
# File lib/ruby_man/game_main.rb, line 183 def check_collisions @moving_objects.each do |obj1| bbox = obj1.bbox @moving_objects.each do |obj2| obj2.collision(obj1) if bbox.intersects?(obj2.bbox) end end end
destroy_object(obj)
click to toggle source
# File lib/ruby_man/game_main.rb, line 137 def destroy_object(obj) obj.destroyed @objects.delete(obj) @moving_objects.delete(obj) if obj.is_a?(MovingObject) end
draw()
click to toggle source
Calls superclass method
# File lib/ruby_man/game_main.rb, line 178 def draw super @objects.each(&:draw) end
end_level()
click to toggle source
# File lib/ruby_man/game_main.rb, line 105 def end_level @objects.clear @moving_objects.clear end
food_created()
click to toggle source
# File lib/ruby_man/game_main.rb, line 204 def food_created @food_left += 1 end
food_destroyed()
click to toggle source
# File lib/ruby_man/game_main.rb, line 208 def food_destroyed @food_left -= 1 next_level if @food_left == 0 end
game_over()
click to toggle source
# File lib/ruby_man/game_main.rb, line 120 def game_over end_level add_object(GameOver.new) end
level_loaded()
click to toggle source
# File lib/ruby_man/game_main.rb, line 115 def level_loaded # reevaluate ghost directions after level is fully loaded moving_objects.each { |obj| obj.select_direction! if obj.is_a?(Ghost) } end
load_level(num, restart)
click to toggle source
# File lib/ruby_man/game_main.rb, line 110 def load_level(num, restart) end_level load_level_file(@levels[num], restart) end
load_level_list()
click to toggle source
# File lib/ruby_man/game_main.rb, line 70 def load_level_list @levels = [] @level = 0 File.open(ResourceManager.media('levellist.txt')).each do |line| @levels.push(line.strip) end end
load_scores()
click to toggle source
# File lib/ruby_man/game_main.rb, line 32 def load_scores @config_file_name = "#{Dir.home}/.rubyman.cfg" @scores = [] @score = 0 return unless File.exist?(@config_file_name) File.open(@config_file_name).each do |line| entry = line.strip.split(';') entry[2] = entry[2].to_i @scores.push(entry) end end
next_level()
click to toggle source
# File lib/ruby_man/game_main.rb, line 94 def next_level if @level < @levels.size - 1 @level += 1 load_level(@level, false) ResourceManager['sfx_begin'].play pause(3000) else game_over end end
objects_at(x, y)
click to toggle source
# File lib/ruby_man/game_main.rb, line 197 def objects_at(x, y) @objects.select do |obj| next unless obj.is_a?(GridObject) obj.grid_x == x && obj.grid_y == y end end
pause(ms = -1000.0)
click to toggle source
# File lib/ruby_man/game_main.rb, line 149 def pause(ms = -1000.0) if paused @pause_time = -0.5 else @pause_time = ms / 1000.0 end end
paused()
click to toggle source
# File lib/ruby_man/game_main.rb, line 157 def paused return true if @pause_time > 0 || @pause_time == -1 false end
place_free(x, y)
click to toggle source
# File lib/ruby_man/game_main.rb, line 192 def place_free(x, y) objects_at(x, y).each { |obj| return false if obj.solid } true end
restart()
click to toggle source
# File lib/ruby_man/game_main.rb, line 85 def restart persistent = @objects.select { |obj| obj.class.persistent } persistent_moving = @moving_objects.select { |obj| obj.class.persistent } load_level(@level, true) @objects += persistent @moving_objects += persistent_moving pause(1000) end
save_scores()
click to toggle source
# File lib/ruby_man/game_main.rb, line 44 def save_scores file = File.open("#{Dir.home}/.rubyman.cfg", 'w') count = 0 @scores.each do |entry| count += 1 break if count > 10 line = "#{entry[0]};#{entry[1]};#{entry[2]}" file.puts(line) end end
sort_scores()
click to toggle source
# File lib/ruby_man/game_main.rb, line 62 def sort_scores @scores.sort! do |a, b| a[2] <=> b[2] end @scores.reverse! @scores = @scores.first(10) end
start()
click to toggle source
# File lib/ruby_man/game_main.rb, line 78 def start @lives = 3 @score = 0 @level = -1 next_level end
update()
click to toggle source
# File lib/ruby_man/game_main.rb, line 162 def update current_time = Gosu.milliseconds / 1000.0 delta = [current_time - @last_milliseconds, 0.25].min @last_milliseconds = current_time update_delta(delta) end
update_delta(delta)
click to toggle source
# File lib/ruby_man/game_main.rb, line 170 def update_delta(delta) @pause_time -= delta if @pause_time > 0 return if paused @objects.each { |obj| obj.update(delta) } check_collisions end