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)

Instance Attribute Summary

Attributes inherited from Chingu::GameState

#game_objects, #game_state_manager, #options, #previous_game_state

Attributes included from Helpers::InputDispatcher

#input_clients

Attributes included from Helpers::GameObject

#game_objects

Instance Method Summary collapse

Methods inherited from Chingu::GameState

#button_down, #button_up, #close, #close_game, #draw_trait, #filename, #setup_trait, #to_s, #to_sym, trait, #trait_options, traits, #update_trait

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

#initialize(new_game_state, options = {}) ⇒ FadeTo

Returns a new instance of FadeTo



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

def initialize(new_game_state, options = {})
  super(options)
  @options = {:speed => 3, :zorder => INFINITY}.merge(options)
  
  @new_game_state = new_game_state
  @new_game_state = new_game_state.new if new_game_state.is_a? Class        
end

Instance Method Details

#drawObject



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

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

    $window.draw_quad( 0,0,@color,
                        $window.width,0,@color,
                        $window.width,$window.height,@color,
                        0,$window.height,@color,@options[:zorder])
  end
  
  if @fading_in && @alpha == 0
    switch_game_state(@new_game_state, :transitional => false)
  end
                      
end

#setupObject



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

def setup
  @color = Gosu::Color.new(0,0,0,0)
  if previous_game_state
    p "* Setup: fading out"   if options[:debug]
    @fading_in = false
    @alpha = 0.0
  else
    p "* Setup: fading in"    if options[:debug]
    @fading_in = true 
    @alpha = 255.0
  end
  
  update                        # Since draw is called before update
end

#updateObject



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

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