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

Constant Summary

Constants included from Helpers::GFX

Helpers::GFX::CIRCLE_STEP

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

#input, #input=

Methods included from Helpers::InputDispatcher

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

Methods included from Helpers::GameObject

#add_game_object, #game_objects_of_class, #load_game_objects, #remove_game_object

Methods included from Helpers::GameState

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

Methods included from Helpers::GFX

#draw_circle, #draw_rect, #fill, #fill_gradient, #fill_rect

Constructor Details

#initialize(options = {}) ⇒ GameState

Returns a new instance of GameState.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chingu/game_state.rb', line 105

def initialize(options = {})
  @options = options
  @game_objects = GameObjectList.new
  @input_clients = Array.new
  
  # Game state mamanger can be run alone
  if defined?($window) && $window.respond_to?(:game_state_manager)
    $window.game_state_manager.inside_state = self
  end
  
  setup_trait(options)
end

Instance Attribute Details

#game_objectsObject

Returns the value of attribute game_objects.



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

def game_objects
  @game_objects
end

#game_state_managerObject

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

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.has_trait(trait, options = {}) ⇒ Object

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.has_trait(trait, options = {})
  
  if trait.is_a?(::Symbol) || trait.is_a?(::String)
    ## puts "has_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 "ClessMethods" 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 has_trait-line.
        if mod2.method_defined?(:initialize_trait)
          initialize_trait(options)
        end
      end
    rescue
      puts $!
    end
  end
end

.has_traits(*traits) ⇒ Object



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

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

Instance Method Details

#button_down(id) ⇒ Object

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



137
138
139
140
# File 'lib/chingu/game_state.rb', line 137

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



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

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)



176
177
178
# File 'lib/chingu/game_state.rb', line 176

def close
  pop_game_state
end

#close_gameObject

Closes main window and terminates the application



183
184
185
# File 'lib/chingu/game_state.rb', line 183

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)



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

def draw
  @game_objects.draw
end

#draw_traitObject



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

def draw_trait;end

#setupObject



130
131
132
# File 'lib/chingu/game_state.rb', line 130

def setup
  # Your game state setup logic here.
end

#setup_trait(options) ⇒ Object

Placeholder for trait-system to override



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

def setup_trait(options);end

#to_sObject



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

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.



122
123
124
# File 'lib/chingu/game_state.rb', line 122

def to_sym
  self.class.to_s.to_sym
end

#trait_optionsObject



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

def trait_options; self.class.trait_options; end

#updateObject

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



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

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

#update_traitObject



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

def update_trait;end