Module: Chingu::Helpers::GameObject

Included in:
Console, GameState, Window
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 Method Summary collapse

Instance Method Details

#add_game_object(object) ⇒ Object



31
32
33
# File 'lib/chingu/helpers/game_object.rb', line 31

def add_game_object(object)
  @game_objects.add_game_object(object)
end

#game_objectsObject



39
40
41
# File 'lib/chingu/helpers/game_object.rb', line 39

def game_objects
  @game_objects
end

#game_objects_of_class(klass) ⇒ Object

Fetch game objects of a certain type/class



60
61
62
# File 'lib/chingu/helpers/game_object.rb', line 60

def game_objects_of_class(klass)
  @game_objects.select { |game_object| game_object.is_a? klass }
end

#hide_game_object(object) ⇒ Object



46
47
48
# File 'lib/chingu/helpers/game_object.rb', line 46

def hide_game_object(object)
  @game_objects.hide_game_object(object)
end

#load_game_objects(options = {}) ⇒ Object

Creates game objects from a Chingu-spezed game objects file (created with game state 'Edit')



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chingu/helpers/game_object.rb', line 67

def load_game_objects(options = {})
  file = options[:file] || self.filename + ".yml"
  debug = options[:debug]
  except = Array(options[: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.options[:created_with_editor] = true if object.options
          end
        rescue
          puts "Couldn't create class '#{klassname}'"
        end
      end
    end
  end
end

#pause_game_object(object) ⇒ Object



49
50
51
# File 'lib/chingu/helpers/game_object.rb', line 49

def pause_game_object(object)
  @game_objects.pause_game_object(object)
end

#remove_game_object(object) ⇒ Object



35
36
37
# File 'lib/chingu/helpers/game_object.rb', line 35

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

NOTE: To save a color do: :color => game_object.color.argb



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/chingu/helpers/game_object.rb', line 103

def save_game_objects(options = {})
  #
  # TODO: Move this to GameObjectList#save ?
  #
  file = options[:file] || "#{self.class.to_s.downcase}.yml"
  game_objects = options[:game_objects]
  classes = options[:classes]
  attributes = options[: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

#show_game_object(object) ⇒ Object



43
44
45
# File 'lib/chingu/helpers/game_object.rb', line 43

def show_game_object(object)
  @game_objects.show_game_object(object)
end

#unpause_game_object(object) ⇒ Object



52
53
54
# File 'lib/chingu/helpers/game_object.rb', line 52

def unpause_game_object(object)
  @game_objects.unpause_game_object(object)
end