lib/fantasy/global.rb in fantasy-0.1.1 vs lib/fantasy/global.rb in fantasy-0.1.3
- old
+ new
@@ -1,10 +1,10 @@
require "ostruct"
module Global
class << self
- attr_accessor :actors, :hud_texts, :hud_images, :clocks
+ attr_accessor :actors, :hud_texts, :hud_images, :backgrounds, :clocks
attr_accessor :debug
attr_accessor :setup_proc, :loop_proc, :button_proc
attr_accessor :presentation_proc, :game_proc, :end_proc
attr_accessor :space_bar_proc
@@ -23,23 +23,31 @@
def initialize
puts "Global.initialize"
@actors = []
@hud_texts = []
@hud_images = []
+ @backgrounds = []
@clocks = []
@last_frame_at = Time.now
@debug = false
@pixel_font = Gosu::Font.new(20, { name: "#{__dir__}/../../fonts/VT323-Regular.ttf" } )
@d_key_pressed = false
@references = OpenStruct.new
@camera = Camera.new(position: Coordinates.zero)
@game_state = Global.presentation_proc.nil? ? "game" : "presentation"
- @presentation_proc = Global.default_on_presentation if @presentation_proc.nil?
- @game_proc = Global.default_on_game if @game_proc.nil?
- @end_proc = Global.default_on_end if @end_proc.nil?
- @paused = false
+ if @presentation_proc.nil?
+ on_presentation { Global.default_on_presentation }
+ end
+
+ if @game_proc.nil?
+ on_game { Global.default_on_game }
+ end
+
+ if @end_proc.nil?
+ on_end { Global.default_on_end }
+ end
end
def update
@frame_time = Time.now - @last_frame_at
@last_frame_at = Time.now
@@ -72,11 +80,11 @@
def go_to_presentation
puts "Game stage 'presentation'"
clear_state_elements
- presentation_proc.call
+ @presentation_proc.call
end
def go_to_game
puts "Game stage 'game'"
@@ -93,10 +101,11 @@
def clear_state_elements
@actors.clear
@hud_texts.clear
@hud_images.clear
- @clocks.each(&:stop)
+ @backgrounds.clear
+ @clocks.reject(&:persistent?).each(&:stop)
@background = Color.new(r: 0, g: 0, b: 0)
end
def mouse_position
Coordinates.new(Global.game.mouse_x, Global.game.mouse_y)