Class: Chingu::GameStates::FadeTo

Inherits:
Chingu::GameState show all
Defined in:
lib/chingu/game_states/fade_to.rb

Overview

Premade game state for chingu - Fade between two game states Fade from the current game state to a new one whenever with:

push_game_state(Chingu::GameStates::FadeTo.new(new_game_state, :speed => 3))

.. Or make your whole game look better with 1 line:

transitional_game_state(Chingu::GameStates::FadeTo, :speed => 10)

Constant Summary

Constants included from Helpers::GFX

Helpers::GFX::CIRCLE_STEP

Instance Attribute Summary

Attributes inherited from Chingu::GameState

#game_objects, #game_state_manager, #options

Attributes included from Helpers::InputDispatcher

#input_clients

Instance Method Summary collapse

Methods inherited from Chingu::GameState

#button_down, #button_up, #close, #close_game, #draw_trait, has_trait, has_traits, #setup_trait, #to_s, #to_sym, #trait_options, #update_trait

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, #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(new_game_state, options = {}) ⇒ FadeTo

Returns a new instance of FadeTo.



38
39
40
41
42
43
44
45
46
# File 'lib/chingu/game_states/fade_to.rb', line 38

def initialize(new_game_state, options = {})
  @options = {:speed => 3}.merge(options)
  
  @new_game_state = new_game_state
  @new_game_state = new_game_state.new if new_game_state.is_a? Class
  
  #@manager = options[:game_state_manager] || self
  #@manager = game_state_manager
end

Instance Method Details

#drawObject



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

def draw
  # Stop possible endless loops
  if @drawn == false
    @drawn = true
    
    if @fading_in
      @new_game_state.draw
    else
      ## @manager.previous_game_state.draw
      previous_game_state.draw
    end

    $window.draw_quad( 0,0,@color,
                        $window.width,0,@color,
                        $window.width,$window.height,@color,
                        0,$window.height,@color,999)
  end
  
  if @fading_in && @alpha == 0
    ##@manager.switch_game_state(@new_game_state, :transitional => false)
    switch_game_state(@new_game_state, :transitional => false)
  end
                      
end

#setupObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chingu/game_states/fade_to.rb', line 48

def setup
  @color = Gosu::Color.new(0,0,0,0)
  ## if @manager.previous_game_state
  if previous_game_state
    @fading_in = false
    @alpha = 0.0
  else
    @fading_in = true 
    @alpha = 255.0
  end
  # @new_game_state.update      # Make sure states game logic is run Once (for a correct draw())
  update                        # Since draw is called before update
end

#updateObject



62
63
64
65
66
67
68
69
70
# File 'lib/chingu/game_states/fade_to.rb', line 62

def update
  @alpha += (@fading_in ? -@options[:speed] : @options[:speed])
  @alpha = 0    if @alpha < 0
  @alpha = 255  if @alpha > 255
  
  @fading_in = true   if @alpha == 255
  @color.alpha = @alpha.to_i
  @drawn = false
end