Module: Chingu::Helpers::GameObject
- Extended by:
- Forwardable
- Defined in:
- lib/chingu/helpers/game_object.rb
Overview
Convenience-methods for classes that hold game objects Mixed into Chingu::Window and Chingu::GameState
Instance Attribute Summary collapse
-
#game_objects ⇒ Object
readonly
Returns the value of attribute game_objects.
Instance Method Summary collapse
-
#game_objects_of_class(klass) ⇒ Object
Fetch game objects of a certain type/class.
-
#load_game_objects(options = {}) ⇒ Object
Creates game objects from a Chingu-spezed game objects file (created with game state 'Edit').
-
#save_game_objects(options = {}) ⇒ Object
Save given game_objects to a file.
Instance Attribute Details
#game_objects ⇒ Object (readonly)
Returns the value of attribute game_objects
34 35 36 |
# File 'lib/chingu/helpers/game_object.rb', line 34 def game_objects @game_objects end |
Instance Method Details
#game_objects_of_class(klass) ⇒ Object
Fetch game objects of a certain type/class
46 47 48 |
# File 'lib/chingu/helpers/game_object.rb', line 46 def game_objects_of_class(klass) @game_objects.select { |game_object| game_object.is_a? klass } end |
#load_game_objects(options = {}) ⇒ Object
Creates game objects from a Chingu-spezed game objects file (created with game state 'Edit')
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/chingu/helpers/game_object.rb', line 53 def load_game_objects( = {}) file = [:file] || self.filename + ".yml" debug = [:debug] except = Array([:except]) || [] require 'yaml' puts "* Loading game objects from #{file}" if debug if File.exists?(file) objects = YAML.load_file(file) objects.each do |object| object.each_pair do |klassname, attributes| begin klass = Object names = klassname.split('::') names.each do |name| klass = klass.const_defined?(name) ? klass.const_get(name) : klass.const_missing(name) end unless klass.class == "GameObject" && !except.include?(klass) puts "Creating #{klassname.to_s}: #{attributes.to_s}" if debug object = klass.create(attributes) object.[:created_with_editor] = true if object. end rescue => e puts "Couldn't create class '#{klassname}' because: #{e}" #raise # I suggest this re-raise the error. end end end end end |
#save_game_objects(options = {}) ⇒ Object
Save given game_objects to a file. Hashoptions
:file - a String, name of file to write to, default is current game_state class name. :game_objects - an Array, game objects to save :classes - an Array, save only game objects of theese classes
NOTE: To save a color do: :color => game_object.color.argb
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/chingu/helpers/game_object.rb', line 94 def save_game_objects( = {}) # # TODO: Move this to GameObjectList#save ? # file = [:file] || "#{self.class.to_s.downcase}.yml" game_objects = [:game_objects] classes = [:classes] attributes = [:attributes] || [:x, :y, :angle, :zorder, :factor_x, :factor_y, :alpha] require 'yaml' objects = [] game_objects.each do |game_object| # Only save specified classes, if given. next if classes and !classes.empty? and !classes.include?(game_object.class) attr_hash = {} attributes.each do |attr| begin attr_hash[attr] = game_object.send(attr) rescue NoMethodError # silently ignore attributes that doesn't exist on the particular game object end end objects << {game_object.class.to_s => attr_hash} end File.open(file, 'w') { |out| YAML.dump(objects, out) } end |