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