Class: Chingu::BasicGameObject

Inherits:
Object
  • Object
show all
Includes:
Helpers::ClassInheritableAccessor
Defined in:
lib/chingu/basic_game_object.rb

Overview

BasicGameObject. Resonating with 1.9.1, this is our most basic class that all game objects ultimate should build on.

All objects that inherits from this class will by default be automaticly be updated and drawn. It will also acts as a container for the trait-system of chingu.

Direct Known Subclasses

GameObject

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ClassInheritableAccessor

included

Constructor Details

#initialize(options = {}) ⇒ BasicGameObject

BasicGameObject initialize

  • call .setup_trait() on all traits that implements it



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

def initialize(options = {})
  @options = options
  @parent = options[:parent]
  
  #
  # A GameObject either belong to a GameState or our mainwindow ($window)
  #
  #if !@parent && $window && $window.respond_to?(:game_state_manager)
  #  @parent = $window.game_state_manager.inside_state || $window
  #end
  @parent = $window.current_scope if !@parent && $window
  
  # if true, BasicGameObject#update will be called
  @paused = options[:paused] || false
  
  # if true, BasicGameObject#draw will be called
  @visible = options[:visible] || true

  # This will call #setup_trait on the latest trait mixed in
  # which then will pass it on to the next setup_trait() with a super-call.
  setup_trait(options)      
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/chingu/basic_game_object.rb', line 12

def options
  @options
end

#parentObject Also known as: game_state

Returns the value of attribute parent.



13
14
15
# File 'lib/chingu/basic_game_object.rb', line 13

def parent
  @parent
end

#pausedObject (readonly)

Returns the value of attribute paused.



12
13
14
# File 'lib/chingu/basic_game_object.rb', line 12

def paused
  @paused
end

#visibleObject (readonly)

Returns the value of attribute visible.



12
13
14
# File 'lib/chingu/basic_game_object.rb', line 12

def visible
  @visible
end

Class Method Details

.allObject

Returns an array with all objects of current class. BasicGameObject#all is state aware so only objects belonging to the current state will be returned.

Bullet.all.each do {}  # Iterate through all bullets in current game state


160
161
162
# File 'lib/chingu/basic_game_object.rb', line 160

def self.all
  $window.current_scope.game_objects.of_class(self).dup
end

.create(*options, &block) ⇒ Object

Creates a new object from class just as new() but also:

  • adds game object to current game state

  • or $window if no game state exists

Use create() instead of new() if you want to keep track of your objects through Chingus “game_objects” which is available in all game states and the main window.

def self.create(options = {})



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chingu/basic_game_object.rb', line 102

def self.create(*options, &block)
  instance = self.new(*options, &block)
  
  #
  # Add to parents list of game objects
  #
  instance.parent.add_game_object(instance) if instance.parent
  
  
  return instance
end

.destroy_allObject

Destroys all intances of objects class:

Bullet.destroy_all    # Removes all Bullet objects from the game


185
186
187
# File 'lib/chingu/basic_game_object.rb', line 185

def self.destroy_all
  self.all.each { |object| object.destroy! }
end

.destroy_if(&block) ⇒ Object

Destroy all instances of current class that fills a certain condition

Enemy.destroy_if(&:dead?)   # Assumes Enemy.dead? returns true/false depending on aliveness :)


175
176
177
178
179
# File 'lib/chingu/basic_game_object.rb', line 175

def self.destroy_if(&block)
  all.each do |object|
    object.destroy if yield(object)
  end
end

.initialize_trait(options) ⇒ Object

Empty placeholders to be overridden



146
# File 'lib/chingu/basic_game_object.rb', line 146

def self.initialize_trait(options); end

.sizeObject

Returns



167
168
169
# File 'lib/chingu/basic_game_object.rb', line 167

def self.size
  $window.current_scope.game_objects.of_class(self).size
end

.trait(trait, options = {}) ⇒ Object Also known as: has_trait

Adds a trait or traits to a certain game class Executes a standard ruby “include” the specified module



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chingu/basic_game_object.rb', line 23

def self.trait(trait, options = {})
  
  if trait.is_a?(::Symbol) || trait.is_a?(::String)
    ## puts "trait #{trait}, #{options}"
    begin
      # Convert user-given symbol (eg. :timer) to a Module (eg. Chingu::Traits::Timer)
      mod = Chingu::Traits.const_get(Chingu::Inflector.camelize(trait))
      
      # Include the module, which will add the containing methods as instance methods
      include mod
               
      # Does sub-module "ClessMethods" exists?
      # (eg: Chingu::Traits::Timer::ClassMethods)
      if mod.const_defined?("ClassMethods")
        # Add methods in scope ClassMethods as.. class methods!
        mod2 = mod.const_get("ClassMethods")
        extend mod2
      
        # If the newly included trait has a initialize_trait method in the ClassMethods-scope:
        # ... call it with the options provided with the trait-line.
        if mod2.method_defined?(:initialize_trait)
          initialize_trait(options)
        end
      end
    rescue
      puts $!
    end
  end
end

.traits(*traits) ⇒ Object Also known as: has_traits



54
55
56
# File 'lib/chingu/basic_game_object.rb', line 54

def self.traits(*traits)
  Array(traits).each { |trait_name| trait trait_name }
end

Instance Method Details

#destroyObject Also known as: destroy!

Removes object from the update cycle and freezes the object to prevent further modifications. If the object isn’t being managed by Chingu (ie. you’re doing manual update/draw calls) the object is only frozen, not removed from any updae cycle (because you are controlling that).



193
194
195
# File 'lib/chingu/basic_game_object.rb', line 193

def destroy
  @parent.remove_game_object(self) if @parent
end

#drawObject



152
# File 'lib/chingu/basic_game_object.rb', line 152

def draw; end

#draw_traitObject



150
# File 'lib/chingu/basic_game_object.rb', line 150

def draw_trait; end

#pause!Object

Disable automatic calling of update() and update_trait() each game loop



125
126
127
# File 'lib/chingu/basic_game_object.rb', line 125

def pause!
  @paused = true
end

#paused?Boolean

Returns true if paused

Returns:

  • (Boolean)


139
140
141
# File 'lib/chingu/basic_game_object.rb', line 139

def paused?
  @paused == true
end

#setupObject



148
# File 'lib/chingu/basic_game_object.rb', line 148

def setup; end

#setup_trait(options) ⇒ Object



147
# File 'lib/chingu/basic_game_object.rb', line 147

def setup_trait(options); end

#trait_optionsObject



17
# File 'lib/chingu/basic_game_object.rb', line 17

def trait_options; self.class.trait_options; end

#unpause!Object

Enable automatic calling of update() and update_trait() each game loop



132
133
134
# File 'lib/chingu/basic_game_object.rb', line 132

def unpause!
  @paused = false
end

#updateObject



151
# File 'lib/chingu/basic_game_object.rb', line 151

def update; end

#update_traitObject



149
# File 'lib/chingu/basic_game_object.rb', line 149

def update_trait; end