Class: Nimo::GameWindow

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/nimo/game_window.rb

Overview

Represents a game instance and provides access to the game screen and screen transition. It is an extension of Gosu::Window, thus implementing the update, draw and button_down hooks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, width, height) ⇒ GameWindow

Returns a new instance of GameWindow.



10
11
12
13
14
15
16
# File 'lib/nimo/game_window.rb', line 10

def initialize(name, width, height)
  super(width, height, false)
  self.caption = name
  
  @screens = {}
  @background_screens = []
end

Instance Attribute Details

#current_screenObject (readonly)

Returns the value of attribute current_screen.



8
9
10
# File 'lib/nimo/game_window.rb', line 8

def current_screen
  @current_screen
end

Instance Method Details

#add_screen(name, screen) ⇒ Object



18
19
20
21
# File 'lib/nimo/game_window.rb', line 18

def add_screen(name, screen)
  @screens[name] = screen
  go_to(name) if @screens.size == 1
end

#button_down(id) ⇒ Object



53
54
55
# File 'lib/nimo/game_window.rb', line 53

def button_down(id)
  @current_screen.button_down(id)
end

#close_menuObject



38
39
40
# File 'lib/nimo/game_window.rb', line 38

def close_menu
  @current_screen = @background_screens.pop
end

#drawObject



48
49
50
51
# File 'lib/nimo/game_window.rb', line 48

def draw
  @background_screens.each { |screen| screen.draw }
  @current_screen.draw
end

#go_to(screen_name) ⇒ Object

Switch to a Screen registered with the screen_name, notifying listeners of the :on_enter event.



25
26
27
28
29
# File 'lib/nimo/game_window.rb', line 25

def go_to(screen_name)
  raise "There is no screen named #{screen_name}" unless @screens.has_key? screen_name
  @current_screen = @screens[screen_name]
  @current_screen.notify(:on_enter)
end

#open_menu(screen_name) ⇒ Object

Open a Screen registered with the screen_name as a menu. To dismiss the menu, use close_menu.



33
34
35
36
# File 'lib/nimo/game_window.rb', line 33

def open_menu(screen_name)
  @background_screens << @current_screen
  go_to(screen_name)
end

#updateObject

:section: Gosu::Window hooks



44
45
46
# File 'lib/nimo/game_window.rb', line 44

def update
 @current_screen.update
end