Class: Chingu::GameStates::FadeTo

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

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, #to_s, #to_sym

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, #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

#fill, #fill_gradient, #fill_rect

Constructor Details

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

Returns a new instance of FadeTo.



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

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

Instance Method Details

#drawObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chingu/game_states/fade_to.rb', line 67

def draw
  # Stop endless loops
  if @drawn == false
    @drawn = true
    
    if @fading_in
      @new_game_state.draw
    else
      @manager.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)
  end
                      
end

#setupObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chingu/game_states/fade_to.rb', line 44

def setup
  @color = Gosu::Color.new(0,0,0,0)
  if @manager.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



57
58
59
60
61
62
63
64
65
# File 'lib/chingu/game_states/fade_to.rb', line 57

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