<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
  module Helpers
  
  #
  # Convenience-methods for classes that hold game objects
  # Mixed into Chingu::Window and Chingu::GameState
  #
  module GameObject
  
    def add_game_object(object)
      # puts "#{self.to_s} add_game_object(#{object.to_s})"
      @game_objects.add_game_object(object)
    end
    
    def remove_game_object(object)
      @game_objects.remove_game_object(object)
    end
    
    def game_objects
      @game_objects
    end
    
    #
    # Fetch game objects of a certain type/class
    #
    def game_objects_of_class(klass)
      @game_objects.select { |game_object| game_object.is_a? klass }
    end
    
    #
    # Creates game objects from a Chingu-spezed game objects file (created with game state 'Edit')
    #
    def load_game_objects(options = {})
      file = options[:file] || "#{self.class.to_s.downcase}.yml"
      debug = options[:debug]
      
      require 'yaml'
      
      if File.exists?(file)
        game_objects = YAML.load_file(file)
        game_objects.each do |game_object|
          game_object.each_pair do |klassname, attributes|
            begin
              klass = Kernel::const_get(klassname)
              unless klass.class == "GameObject"
                puts "Creating #{klassname.to_s}: #{attributes.to_s}" if debug
                klass.create(attributes)
              end
            rescue
              puts "Couldn't create class '#{klassname}'"
            end
          end
        end
      end
    end
    
    def save_game_objects(options = {})
      file = options[:file] || "#{self.class.to_s.downcase}.yml"
      game_objects = options[:game_objects]
      
      require 'yaml'
      objects = []
      game_objects.each do |game_object|
        objects << {game_object.class.to_s  => 
                      {
                      :x => game_object.x, 
                      :y => game_object.y,
                      :angle => game_object.angle,
                      :zorder => game_object.zorder,
                      :factor_x => game_object.factor_x,
                      :factor_y => game_object.factor_y,
                      :center_x => game_object.center_x,
                      :center_y => game_object.center_y,
                      }
                    }
      end
        
      #Marshal.dump(previous_game_state.game_objects, File.open(@filename, "w"))
      File.open(file, 'w') do |out|
        YAML.dump(objects, out)
      end
    end
    
  
  end

  end
end<commit_msg>add :only-option to save_game_objects, save only specific classes<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
  module Helpers
  
  #
  # Convenience-methods for classes that hold game objects
  # Mixed into Chingu::Window and Chingu::GameState
  #
  module GameObject
  
    def add_game_object(object)
      # puts "#{self.to_s} add_game_object(#{object.to_s})"
      @game_objects.add_game_object(object)
    end
    
    def remove_game_object(object)
      @game_objects.remove_game_object(object)
    end
    
    def game_objects
      @game_objects
    end
    
    #
    # Fetch game objects of a certain type/class
    #
    def game_objects_of_class(klass)
      @game_objects.select { |game_object| game_object.is_a? klass }
    end
    
    #
    # Creates game objects from a Chingu-spezed game objects file (created with game state 'Edit')
    #
    def load_game_objects(options = {})
      file = options[:file] || "#{self.class.to_s.downcase}.yml"
      debug = options[:debug]
      
      require 'yaml'
      
      if File.exists?(file)
        game_objects = YAML.load_file(file)
        game_objects.each do |game_object|
          game_object.each_pair do |klassname, attributes|
            begin
              klass = Kernel::const_get(klassname)
              unless klass.class == "GameObject"
                puts "Creating #{klassname.to_s}: #{attributes.to_s}" if debug
                klass.create(attributes)
              end
            rescue
              puts "Couldn't create class '#{klassname}'"
            end
          end
        end
      end
    end
    
    def save_game_objects(options = {})
      file = options[:file] || "#{self.class.to_s.downcase}.yml"
      game_objects = options[:game_objects]
      only = options[:only]
      
      require 'yaml'
      objects = []
      game_objects.each do |game_object|
        next if only and !only.include?(game_object.class)
        
        objects << {game_object.class.to_s  => 
                      {
                      :x => game_object.x, 
                      :y => game_object.y,
                      :angle => game_object.angle,
                      :zorder => game_object.zorder,
                      :factor_x => game_object.factor_x,
                      :factor_y => game_object.factor_y,
                      :center_x => game_object.center_x,
                      :center_y => game_object.center_y,
                      }
                    }
      end
        
      #Marshal.dump(previous_game_state.game_objects, File.open(@filename, "w"))
      File.open(file, 'w') do |out|
        YAML.dump(objects, out)
      end
    end
    
  
  end

  end
end
{'fraction_non_alphanumeric': 0.08725518854136217, 'fraction_numerical': 0.006138555977784273, 'mean_word_length': 2.0631154879140556, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6180761', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1967, 'n_words': 577}