<commit_before>#--
#
# Chingu -- OpenGL accelerated 2D game framework for Ruby
# Copyright (C) 2009 ippa / <EMAIL>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#++


module Chingu
  #
  # GameStateManger is responsible for keeping track of game states with a simple pop/push stack.
  #
  # More about the concept of states in games:
  # http://gamedevgeek.com/tutorials/managing-game-states-in-c/
  # http://www.gamedev.net/community/forums/topic.asp?topic_id=477320
  #
  # Chingu::Window automatically creates a @game_state_manager and makes it accessible in our game loop.
  # By default the game loop calls update, draw, button_up(id) and button_down(id) on the active state.
  #
  # ==== Chingu Examples
  #
  # Enter a new game state, Level, don't call finalize() on the game state we're leaving.
  #   push_game_state(Level, :finalize => false)
  #
  # Return to the previous game state, don't call setup() on it when it becomes active.
  #   pop_game_state(:setup => false)
  #
  # If you want to use Chingus GameStateManager _without_ Chingu::Window, see example5.rb
  #
  class GameStateManager
    attr_accessor :inside_state
    
    def initialize
      @inside_state = nil
      @game_states = []
      @transitional_game_state = nil
      @transitional_game_state_options = {}
      @previous_game_state = nil
    end
    
    #
    # Gets the currently active gamestate (top of stack)
    #
    def current_game_state
      @game_states.last
    end
    alias :current current_game_state

    #
    # Returns all gamestates with currenlty active game state on top.
    #
    def game_states
      @game_states.reverse
    end
    
    #
    # Sets a game state to be called between the old and the new game state 
    # whenever a game state is switched,pushed or popped.
    #
    # The transitional game state is responsible for switching to the "new game state".
    # It should do so with ":transitional => false" not to create an infinite loop.
    # 
    # The new game state is the first argument to the transitional game states initialize().
    #
    # Example:
    #   transitional_game_state(FadeIn)
    #   push_game_state(Level2)
    #
    # would in practice become:
    # 
    #   push_game_state(FadeIn.new(Level2))
    #
    # This would be the case for every game state change until the transitional game state is removed:
    #   transitional_game_state(nil)  # or false
    #
    # Very useful for fading effect between scenes.
    #
    def transitional_game_state(game_state, options = {})
      @transitional_game_state = game_state
      @transitional_game_state_options = options
    end
    
    #
    # Switch to a given game state, _replacing_ the current active one.
    # By default setup() is called on the game state  we're switching _to_.
    # .. and finalize() is called on the game state we're switching _from_.
    #
    def switch_game_state(state, options = {})
      puts state.class
      options = {:setup => true, :finalize => true, :transitional => true}.merge(options)

      @previous_game_state = current_game_state
      
      new_state = game_state_instance(state)
      
      if new_state
        # Make sure the game state knows about the manager
        new_state.game_state_manager = self
        
        # Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
        current_game_state.finalize   if current_game_state.respond_to?(:finalize) && options[:finalize]
        <commit_msg>remove debugprint<commit_after>#--
#
# Chingu -- OpenGL accelerated 2D game framework for Ruby
# Copyright (C) 2009 ippa / <EMAIL>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#++


module Chingu
  #
  # GameStateManger is responsible for keeping track of game states with a simple pop/push stack.
  #
  # More about the concept of states in games:
  # http://gamedevgeek.com/tutorials/managing-game-states-in-c/
  # http://www.gamedev.net/community/forums/topic.asp?topic_id=477320
  #
  # Chingu::Window automatically creates a @game_state_manager and makes it accessible in our game loop.
  # By default the game loop calls update, draw, button_up(id) and button_down(id) on the active state.
  #
  # ==== Chingu Examples
  #
  # Enter a new game state, Level, don't call finalize() on the game state we're leaving.
  #   push_game_state(Level, :finalize => false)
  #
  # Return to the previous game state, don't call setup() on it when it becomes active.
  #   pop_game_state(:setup => false)
  #
  # If you want to use Chingus GameStateManager _without_ Chingu::Window, see example5.rb
  #
  class GameStateManager
    attr_accessor :inside_state
    
    def initialize
      @inside_state = nil
      @game_states = []
      @transitional_game_state = nil
      @transitional_game_state_options = {}
      @previous_game_state = nil
    end
    
    #
    # Gets the currently active gamestate (top of stack)
    #
    def current_game_state
      @game_states.last
    end
    alias :current current_game_state

    #
    # Returns all gamestates with currenlty active game state on top.
    #
    def game_states
      @game_states.reverse
    end
    
    #
    # Sets a game state to be called between the old and the new game state 
    # whenever a game state is switched,pushed or popped.
    #
    # The transitional game state is responsible for switching to the "new game state".
    # It should do so with ":transitional => false" not to create an infinite loop.
    # 
    # The new game state is the first argument to the transitional game states initialize().
    #
    # Example:
    #   transitional_game_state(FadeIn)
    #   push_game_state(Level2)
    #
    # would in practice become:
    # 
    #   push_game_state(FadeIn.new(Level2))
    #
    # This would be the case for every game state change until the transitional game state is removed:
    #   transitional_game_state(nil)  # or false
    #
    # Very useful for fading effect between scenes.
    #
    def transitional_game_state(game_state, options = {})
      @transitional_game_state = game_state
      @transitional_game_state_options = options
    end
    
    #
    # Switch to a given game state, _replacing_ the current active one.
    # By default setup() is called on the game state  we're switching _to_.
    # .. and finalize() is called on the game state we're switching _from_.
    #
    def switch_game_state(state, options = {})
      options = {:setup => true, :finalize => true, :transitional => true}.merge(options)

      @previous_game_state = current_game_state
      
      new_state = game_state_instance(state)
      
      if new_state
        # Make sure the game state knows about the manager
        new_state.game_state_manager = self
        
        # Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
        current_game_state.finalize   if current_game_state.respond_to?(:finalize) && options[:finalize]
        
{'fraction_non_alphanumeric': 0.07202116147649393, 'fraction_numerical': 0.007214139713839125, 'mean_word_length': 3.0359049005337213, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '905974', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2367, 'n_words': 925}