/examples/example5_gamestates_in_pure_gosu.rb

http://github.com/ippa/chingu · Ruby · 109 lines · 75 code · 23 blank · 11 comment · 9 complexity · 5691d164ff82555251343ac8b65fbc2b MD5 · raw file

  1. #!/usr/bin/env ruby
  2. require 'rubygems' rescue nil
  3. $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
  4. require 'chingu'
  5. include Gosu
  6. #
  7. # Using Chingus game state mananger in pure Gosu.
  8. #
  9. class Game < Gosu::Window
  10. attr_reader :font
  11. def initialize
  12. $window = super(800,600,false)
  13. @font = Font.new($window, default_font_name(), 20)
  14. # Create our game state manager
  15. @manager = Chingu::GameStateManager.new
  16. # Switch to first state
  17. @manager.switch_game_state(State1)
  18. # Insert FadeTo state between every push, pop and switch
  19. @manager.transitional_game_state(Chingu::GameStates::FadeTo, :speed => 10, :game_state_manager => @manager)
  20. end
  21. def button_down(id)
  22. # This makes sure button_down(id) is called on the active game state
  23. # Enables input-handling in game states, you might wanna do the same with button_up()
  24. @manager.button_down(id)
  25. if @manager.current_game_state.class != Chingu::GameStates::FadeTo
  26. @manager.push_game_state(State1) if(id==Button::Kb1)
  27. @manager.push_game_state(State2) if(id==Button::Kb2)
  28. @manager.push_game_state(State3) if(id==Button::Kb3)
  29. @manager.push_game_state(Chingu::GameStates::Pause) if(id==Button::KbP)
  30. @manager.pop_game_state if(id==Button::KbBackspace)
  31. end
  32. exit if(id==Button::KbEscape)
  33. end
  34. def update
  35. # This makes sure update() is called on the active game state
  36. @manager.update
  37. end
  38. def draw
  39. @font.draw("Game State Stack. 1-3 to push a game state. Backspace to pop.", 100, 200, 0)
  40. @manager.game_states.each_with_index do |game_state, index|
  41. @font.draw("#{index+1}) #{game_state.to_s}", 100, 220+index*20, 0)
  42. end
  43. # This makes sure draw() is called on the active game state
  44. @manager.draw
  45. end
  46. end
  47. class State1 < Chingu::GameState
  48. def setup
  49. @spinner = ["|", "/", "-", "\\", "|", "/", "-", "\\"]
  50. @spinner_index = 0.0
  51. end
  52. def update
  53. @spinner_index += 0.1
  54. @spinner_index = 0 if @spinner_index >= @spinner.size
  55. end
  56. def draw
  57. $window.font.draw("Inside State1: #{@spinner[@spinner_index.to_i]}", 100, 100, 0)
  58. end
  59. end
  60. class State2 < Chingu::GameState
  61. def setup
  62. @factor = 0.0
  63. @ticks = 0.0
  64. end
  65. def update
  66. @ticks += 0.01
  67. @factor = 1.5 + Math.sin(@ticks).to_f
  68. end
  69. def draw
  70. $window.font.draw("Inside State2 - factor_y: #{@factor.to_s}", 100, 100, 0, 1.0, @factor)
  71. end
  72. end
  73. class State3 < Chingu::GameState
  74. def setup
  75. @factor = 0.0
  76. @ticks = 0.0
  77. end
  78. def update
  79. @ticks += 0.01
  80. @factor = 1.5 + Math.sin(@ticks).to_f
  81. end
  82. def draw
  83. $window.font.draw("Inside State3 - factor_x: #{@factor.to_s}", 100, 100, 0, @factor, 1.0)
  84. end
  85. end
  86. Game.new.show