Class: Chingu::GameState

Inherits:
Object
  • Object
show all
Includes:
Helpers::ClassInheritableAccessor, Helpers::GFX, Helpers::GameObject, Helpers::GameState, Helpers::InputClient, Helpers::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 Helpers::InputDispatcher

#input_clients

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Helpers::ClassInheritableAccessor

included

Methods included from Helpers::InputClient

#add_inputs, #holding?, #holding_all?, #holding_any?, #input, #input=, #on_input

Methods included from Helpers::InputDispatcher

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

Methods included from Helpers::GameObject

#game_objects_of_class, #load_game_objects, #save_game_objects

Methods included from Helpers::GFX

#draw_arc, #draw_circle, #draw_rect, #fill, #fill_arc, #fill_circle, #fill_gradient, #fill_rect

Constructor Details

- (GameState) initialize(options = {})

Returns a new instance of GameState



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/chingu/game_state.rb', line 106

def initialize(options = {})
  @options = options
  @game_objects = GameObjectList.new
  @input_clients = Array.new
  
  # Game state manager can be run alone
  if defined?($window) && $window.respond_to?(:game_state_manager)
    
    # Since we place the init of previous_game_state here, game states can use it even 
    # in initialize() if they call super first.
    @previous_game_state = $window.game_state_manager.current_game_state
    
    $window.game_state_manager.inside_state = self
  end
  
  setup_trait(options)
end

Instance Attribute Details

- (Object) game_objects

Returns the value of attribute game_objects



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

def game_objects
  @game_objects
end

- (Object) game_state_manager

Returns the value of attribute game_state_manager



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

def game_state_manager
  @game_state_manager
end

- (Object) options (readonly)

Returns the value of attribute options



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

def options
  @options
end

- (Object) previous_game_state

Returns the value of attribute previous_game_state



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

def previous_game_state
  @previous_game_state
end

Class Method Details

+ (Object) trait(trait, options = {}) Also known as: has_trait

Adds a trait or traits to a certain game class Executes a standard ruby “include” the specified module



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chingu/game_state.rb', line 70

def self.trait(trait, options = {})
  
  if trait.is_a?(::Symbol) || trait.is_a?(::String)
    ## puts "trait #{trait}, #{options}"
    begin
      # Convert user-given symbol (eg. :timer) to a Module (eg. Chingu::Traits::Timer)
      mod = Chingu::Traits.const_get(Chingu::Inflector.camelize(trait))
      
      # Include the module, which will add the containing methods as instance methods
      include mod
               
      # Does sub-module "ClassMethods" exists?
      # (eg: Chingu::Traits::Timer::ClassMethods)
      if mod.const_defined?("ClassMethods")
        # Add methods in scope ClassMethods as.. class methods!
        mod2 = mod.const_get("ClassMethods")
        extend mod2
      
        # If the newly included trait has a initialize_trait method in the ClassMethods-scope:
        # ... call it with the options provided with the trait-line.
        if mod2.method_defined?(:initialize_trait)
          initialize_trait(options)
        end
      end
    rescue
      puts $!
    end
  end
end

+ (Object) traits(*traits) Also known as: has_traits



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

def self.traits(*traits)
  Array(traits).each { |trait| trait trait }
end

Instance Method Details

- (Object) button_down(id)

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



154
155
156
157
# File 'lib/chingu/game_state.rb', line 154

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

- (Object) button_up(id)

Called when a button is released and a game state active



162
163
164
165
# File 'lib/chingu/game_state.rb', line 162

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

- (Object) close

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



193
194
195
# File 'lib/chingu/game_state.rb', line 193

def close
  pop_game_state
end

- (Object) close_game

Closes main window and terminates the application



200
201
202
# File 'lib/chingu/game_state.rb', line 200

def close_game
  $window.close
end

- (Object) draw

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



181
182
183
# File 'lib/chingu/game_state.rb', line 181

def draw
  @game_objects.draw
end

- (Object) draw_trait



188
# File 'lib/chingu/game_state.rb', line 188

def draw_trait;end

- (Object) filename

Returns a filename-friendly string from the current class-name

“Level19” -> “level19” “BigBossLevel” -> “big_boss_level”



142
143
144
# File 'lib/chingu/game_state.rb', line 142

def filename
  Chingu::Inflector.underscore(self.class.to_s)
end

- (Object) setup



147
148
149
# File 'lib/chingu/game_state.rb', line 147

def setup
  # Your game state setup logic here.
end

- (Object) setup_trait(options)

Placeholder for trait-system to override



186
# File 'lib/chingu/game_state.rb', line 186

def setup_trait(options);end

- (Object) to_s



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

def to_s
  self.class.to_s
end

- (Object) to_sym

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



128
129
130
# File 'lib/chingu/game_state.rb', line 128

def to_sym
  self.class.to_s.to_sym
end

- (Object) trait_options



64
# File 'lib/chingu/game_state.rb', line 64

def trait_options; self.class.trait_options; end

- (Object) update

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



170
171
172
173
174
175
176
# File 'lib/chingu/game_state.rb', line 170

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

- (Object) update_trait



187
# File 'lib/chingu/game_state.rb', line 187

def update_trait;end