Class: Lotu::Game
- Inherits:
-
Gosu::Window
- Object
- Gosu::Window
- Lotu::Game
- Includes:
- SystemUser
- Defined in:
- lib/lotu/game.rb
Instance Attribute Summary collapse
-
#draw_queue ⇒ Object
Accessors for queues.
-
#dt ⇒ Object
readonly
Accessors for time delta, systems and fonts.
-
#fonts ⇒ Object
readonly
Accessors for time delta, systems and fonts.
-
#input_listeners ⇒ Object
Accessors for queues.
-
#systems ⇒ Object
readonly
Accessors for time delta, systems and fonts.
-
#update_queue ⇒ Object
Accessors for queues.
Instance Method Summary collapse
- #animation(name) ⇒ Object
-
#button_down(id) ⇒ Object
These are for managing input.
- #button_up(id) ⇒ Object
- #debug! ⇒ Object
- #debug? ⇒ Boolean
-
#draw ⇒ Object
Main draw loop.
-
#image(name) ⇒ Object
These are for managing resources.
-
#initialize(opts = {}) ⇒ Game
constructor
A new instance of Game.
- #kill_me(actor) ⇒ Object
- #load_animations(path) ⇒ Object
- #load_images(path) ⇒ Object
-
#load_resources ⇒ Object
Hook methods, these are meant to be replaced by subclasses.
- #load_songs(path) ⇒ Object
- #load_sounds(path) ⇒ Object
-
#manage_me(actor) ⇒ Object
For actor management.
- #pause! ⇒ Object
- #paused? ⇒ Boolean
- #register_for_input(controller) ⇒ Object
- #setup_actors ⇒ Object
-
#setup_containers ⇒ Object
Setup various containers.
- #setup_input ⇒ Object
- #setup_systems ⇒ Object
- #song(name) ⇒ Object
- #sound(name) ⇒ Object
-
#update ⇒ Object
Main update loop.
- #with_files(regexp, path) ⇒ Object
- #with_path_from_file(path, &blk) ⇒ Object
Methods included from SystemUser
Constructor Details
#initialize(opts = {}) ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/lotu/game.rb', line 10 def initialize(opts={}) default_opts = { :width => 1024, :height => 768, :fullscreen => false } opts = default_opts.merge!(opts) super(opts[:width], opts[:height], opts[:fullscreen]) # Handy global window variable $lotu = self @debug = opts[:debug] || false setup_containers # For timer initialization @last_time = Gosu::milliseconds # Memoize fonts by size @fonts = Hash.new{|h,k| h[k] = Gosu::Font.new(self, Gosu::default_font_name, k)} # Call hook methods load_resources setup_systems setup_actors setup_input end |
Instance Attribute Details
#draw_queue ⇒ Object
Accessors for queues
6 7 8 |
# File 'lib/lotu/game.rb', line 6 def draw_queue @draw_queue end |
#dt ⇒ Object (readonly)
Accessors for time delta, systems and fonts
4 5 6 |
# File 'lib/lotu/game.rb', line 4 def dt @dt end |
#fonts ⇒ Object (readonly)
Accessors for time delta, systems and fonts
4 5 6 |
# File 'lib/lotu/game.rb', line 4 def fonts @fonts end |
#input_listeners ⇒ Object
Accessors for queues
6 7 8 |
# File 'lib/lotu/game.rb', line 6 def input_listeners @input_listeners end |
#systems ⇒ Object (readonly)
Accessors for time delta, systems and fonts
4 5 6 |
# File 'lib/lotu/game.rb', line 4 def systems @systems end |
#update_queue ⇒ Object
Accessors for queues
6 7 8 |
# File 'lib/lotu/game.rb', line 6 def update_queue @update_queue end |
Instance Method Details
#animation(name) ⇒ Object
152 153 154 |
# File 'lib/lotu/game.rb', line 152 def animation(name) @animations[name] end |
#button_down(id) ⇒ Object
These are for managing input
120 121 122 123 124 |
# File 'lib/lotu/game.rb', line 120 def (id) @input_register[id].each do |item| item.(id) end end |
#button_up(id) ⇒ Object
126 127 128 129 130 |
# File 'lib/lotu/game.rb', line 126 def (id) @input_register[id].each do |item| item.(id) end end |
#debug! ⇒ Object
44 45 46 |
# File 'lib/lotu/game.rb', line 44 def debug! @debug = !@debug end |
#debug? ⇒ Boolean
48 49 50 |
# File 'lib/lotu/game.rb', line 48 def debug? @debug end |
#draw ⇒ Object
Main draw loop
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lotu/game.rb', line 96 def draw # Systems may report interesting stuff @systems.each_value do |system| system.draw end # Draw each actor in queue @draw_queue.each do |actor| actor.draw end end |
#image(name) ⇒ Object
These are for managing resources
140 141 142 |
# File 'lib/lotu/game.rb', line 140 def image(name) @images[name] end |
#kill_me(actor) ⇒ Object
114 115 116 117 |
# File 'lib/lotu/game.rb', line 114 def kill_me(actor) @draw_queue.delete(actor) @update_queue.delete(actor) end |
#load_animations(path) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/lotu/game.rb', line 183 def load_animations(path) count = 0 coords = Hash.new{|h,k| h[k] = []} with_files(/\.txt/, path) do |file_name, file_path| name = File.basename(file_name, '.txt') File.open(file_path) do |file| file.lines.each do |line| coords[name] << line.scan(/\d+/).map!(&:to_i) end end false end with_files(/\.png|\.jpg|\.bmp/, path) do |file_name, file_path| name, extension = file_name.split('.') count += 1 if coords[name] coords[name].each do |index, x, y, width, height| @animations[file_name] << Gosu::Image.new($lotu, file_path, true, x, y, width, height) end end puts "\n#{count} animation(s) loaded." end |
#load_images(path) ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/lotu/game.rb', line 156 def load_images(path) count = 0 with_files(/\.png|\.jpg|\.bmp/, path) do |file_name, file_path| count += 1 @images[file_name] = Gosu::Image.new($lotu, file_path) end puts "\n#{count} image(s) loaded." end |
#load_resources ⇒ Object
Hook methods, these are meant to be replaced by subclasses
53 |
# File 'lib/lotu/game.rb', line 53 def load_resources;end |
#load_songs(path) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/lotu/game.rb', line 174 def load_songs(path) count = 0 with_files(/\.ogg|\.mp3|\.wav/, path) do |file_name, file_path| count += 1 @songs[file_name] = Gosu::Song.new($lotu, file_path) end puts "\n#{count} song(s) loaded." end |
#load_sounds(path) ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/lotu/game.rb', line 165 def load_sounds(path) count = 0 with_files(/\.ogg|\.mp3|\.wav/, path) do |file_name, file_path| count += 1 @sounds[file_name] = Gosu::Sample.new($lotu, file_path) end puts "\n#{count} sounds(s) loaded." end |
#manage_me(actor) ⇒ Object
For actor management
109 110 111 112 |
# File 'lib/lotu/game.rb', line 109 def manage_me(actor) @draw_queue << actor @update_queue << actor end |
#pause! ⇒ Object
36 37 38 |
# File 'lib/lotu/game.rb', line 36 def pause! @pause = !@pause end |
#paused? ⇒ Boolean
40 41 42 |
# File 'lib/lotu/game.rb', line 40 def paused? @pause end |
#register_for_input(controller) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/lotu/game.rb', line 132 def register_for_input(controller) controller.keys.each_key do |key| @input_register[key] << controller end @update_queue << controller end |
#setup_actors ⇒ Object
54 |
# File 'lib/lotu/game.rb', line 54 def setup_actors;end |
#setup_containers ⇒ Object
Setup various containers
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/lotu/game.rb', line 62 def setup_containers # For systems @systems = {} # For queues @update_queue = [] @draw_queue = [] @input_register = Hash.new{|hash,key| hash[key] = []} # For resource management @images = {} @sounds = {} @songs = {} @animations = Hash.new{|h,k| h[k] = []} end |
#setup_input ⇒ Object
55 |
# File 'lib/lotu/game.rb', line 55 def setup_input;end |
#setup_systems ⇒ Object
57 58 59 |
# File 'lib/lotu/game.rb', line 57 def setup_systems use(InputSystem) end |
#song(name) ⇒ Object
148 149 150 |
# File 'lib/lotu/game.rb', line 148 def song(name) @songs[name] end |
#sound(name) ⇒ Object
144 145 146 |
# File 'lib/lotu/game.rb', line 144 def sound(name) @sounds[name] end |
#update ⇒ Object
Main update loop
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/lotu/game.rb', line 79 def update new_time = Gosu::milliseconds @dt = (new_time - @last_time)/1000.0 @last_time = new_time # Update each system @systems.each_value do |system| system.update end # Update each actor @update_queue.each do |actor| actor.update end unless paused? end |
#with_files(regexp, path) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/lotu/game.rb', line 212 def with_files(regexp, path) path = File.(File.join(@path, path)) puts "\nLoading from: #{path}".blue if @debug Dir.entries(path).grep(regexp).each do |entry| begin report = yield(entry, File.join(path, entry)) print '.'.green if report rescue Exception => e print '.'.red puts e, File.join(path, entry) if @debug end end end |
#with_path_from_file(path, &blk) ⇒ Object
207 208 209 210 |
# File 'lib/lotu/game.rb', line 207 def with_path_from_file(path, &blk) @path = File.(File.dirname path) yield end |