Class: Chingu::GameState

Inherits:
Object
  • Object
show all
Includes:
GFXHelpers, GameObjectHelpers, GameStateHelpers, InputClient, InputDispatcher
Defined in:
lib/chingu/game_state.rb

Overview

Chingu incorporates a basic push/pop game state system (as discussed here: www.gamedev.net/community/forums/topic.asp?topic_id=477320). Game states is a way of organizing your intros, menus, levels. Game states aren’t complicated. In Chingu a GameState is a class that behaves mostly like your default Gosu::Window (or in our case Chingu::Window) game loop.

# A simple GameState-example class Intro < Chingu::GameState

def update
  # game logic here
end

def draw
  # screen manipulation here
end

# Called when we enter the game state
def setup
  @player.angle = 0   # point player upwards
end

# Called when we leave the current game state
def finalize
  push_game_state(Menu)   # switch to game state "Menu"
end

end

Instance Attribute Summary collapse

Attributes included from InputDispatcher

#input_clients

Instance Method Summary collapse

Methods included from InputClient

#input, #input=

Methods included from InputDispatcher

#add_input_client, #dispatch_action, #dispatch_button_down, #dispatch_button_up, #dispatch_input_for, #remove_input_client

Methods included from GameObjectHelpers

#add_game_object, #game_objects_of_class, #remove_game_object

Methods included from GFXHelpers

#fill, #fill_gradient, #fill_rect

Methods included from GameStateHelpers

#clear_game_states, #current_game_state, #pop_game_state, #previous_game_state, #push_game_state, #switch_game_state, #transitional_game_state

Constructor Details

#initialize(options = {}) ⇒ GameState

Returns a new instance of GameState.



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chingu/game_state.rb', line 61

def initialize(options = {})
  @options = options
  ## @game_state_manager = options[:game_state_manager] || $window.game_state_manager
  @game_objects = GameObjectList.new
  @input_clients = Set.new          # Set is like a unique Array with Hash lookupspeed
  
  # Game state mamanger can be run alone
  if defined?($window) && $window.respond_to?(:game_state_manager)
    $window.game_state_manager.inside_state = self
  end
end

Instance Attribute Details

#game_objectsObject

Returns the value of attribute game_objects.



59
60
61
# File 'lib/chingu/game_state.rb', line 59

def game_objects
  @game_objects
end

#game_state_managerObject

Returns the value of attribute game_state_manager.



59
60
61
# File 'lib/chingu/game_state.rb', line 59

def game_state_manager
  @game_state_manager
end

#optionsObject (readonly)

so jlnr can access his :level-number



58
59
60
# File 'lib/chingu/game_state.rb', line 58

def options
  @options
end

Instance Method Details

#button_down(id) ⇒ Object

Called when a button is pressed and a game state is active



92
93
94
95
# File 'lib/chingu/game_state.rb', line 92

def button_down(id)
  dispatch_button_down(id, self)
  @input_clients.each { |object| dispatch_button_down(id, object) } if @input_clients
end

#button_up(id) ⇒ Object

Called when a button is released and a game state active



100
101
102
103
# File 'lib/chingu/game_state.rb', line 100

def button_up(id)
  dispatch_button_up(id, self)
  @input_clients.each { |object| dispatch_button_up(id, object) }   if @input_clients
end

#closeObject

Closes game state by poping it off the stack (and activating the game state below)



126
127
128
# File 'lib/chingu/game_state.rb', line 126

def close
  pop_game_state
end

#close_gameObject

Closes main window and terminates the application



133
134
135
# File 'lib/chingu/game_state.rb', line 133

def close_game
  $window.close
end

#drawObject

Calls Draw on each game object that has current game state as parent (created inside that game state)



119
120
121
# File 'lib/chingu/game_state.rb', line 119

def draw
  @game_objects.draw
end

#setupObject



85
86
87
# File 'lib/chingu/game_state.rb', line 85

def setup
  # Your game state setup logic here.
end

#to_sObject



81
82
83
# File 'lib/chingu/game_state.rb', line 81

def to_s
  self.class.to_s
end

#to_symObject

An unique identifier for the GameState-class, Used in game state manager to keep track of created states.



77
78
79
# File 'lib/chingu/game_state.rb', line 77

def to_sym
  self.class.to_s.to_sym
end

#updateObject

Calls update on each game object that has current game state as parent (created inside that game state)



108
109
110
111
112
113
114
# File 'lib/chingu/game_state.rb', line 108

def update
  dispatch_input_for(self)
  
  @input_clients.each { |game_object| dispatch_input_for(game_object) }      
  
  @game_objects.update
end