Module: Chingu::Helpers::GameObject
Overview
Convenience-methods for classes that hold game objects Mixed into Chingu::Window and Chingu::GameState
Instance Method Summary collapse
- #add_game_object(object) ⇒ Object
- #game_objects ⇒ Object
-
#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’).
- #remove_game_object(object) ⇒ Object
-
#save_game_objects(options = {}) ⇒ Object
Save given game_objects to a file.
Instance Method Details
#add_game_object(object) ⇒ Object
31 32 33 34 |
# File 'lib/chingu/helpers/game_object.rb', line 31 def add_game_object(object) # puts "#{self.to_s} add_game_object(#{object.to_s})" @game_objects.add_game_object(object) end |
#game_objects ⇒ Object
40 41 42 |
# File 'lib/chingu/helpers/game_object.rb', line 40 def game_objects @game_objects end |
#game_objects_of_class(klass) ⇒ Object
Fetch game objects of a certain type/class
47 48 49 |
# File 'lib/chingu/helpers/game_object.rb', line 47 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’)
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 |
# File 'lib/chingu/helpers/game_object.rb', line 54 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 = Kernel::const_get(klassname) 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 puts "Couldn't create class '#{klassname}'" end end end end self.game_objects.sync end |
#remove_game_object(object) ⇒ Object
36 37 38 |
# File 'lib/chingu/helpers/game_object.rb', line 36 def remove_game_object(object) @game_objects.remove_game_object(object) 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
89 90 91 92 93 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 89 def save_game_objects( = {}) file = [:file] || "#{self.class.to_s.downcase}.yml" game_objects = [:game_objects] classes = [:classes] 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) 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, :color => game_object.color.argb, #:color => sprintf("0x%x",game_object.color.argb) #: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 |