Class: Chingu::BasicGameObject

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(options = {}) ⇒ BasicGameObject

BasicGameUnit initialize

  • caches all trait methods for fast calls later on

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

  • adds game object to correct game state or $window if no game state exists



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chingu/basic_game_object.rb', line 38

def initialize(options = {})
  @options = options
  
  #
  # A GameObject can either belong to a GameState or our mainwindow ($window)
  # .. or live in limbo with manual updates
  #
  if $window && $window.respond_to?(:game_state_manager)
    @parent = $window.game_state_manager.inside_state || $window
    @parent.add_game_object(self) if @parent
  end
  
  # This will call #setup on the latest trait mixed in, which then will pass it on with super.
  setup_trait(options) 
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/chingu/basic_game_object.rb', line 9

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/chingu/basic_game_object.rb', line 9

def parent
  @parent
end

Class Method Details

.allObject

Fetch all objects of a current class.

Bullet.all   # => Enumerator of all objects of class Bullet

NOTE: ObjectSpace doesn’t play nice with jruby.



70
71
72
# File 'lib/chingu/basic_game_object.rb', line 70

def self.all
  ObjectSpace.each_object(self)
end

.clearObject

Clear all intances of objects class:

Bullet.clear    # Removes all Bullet objects from the game


89
90
91
# File 'lib/chingu/basic_game_object.rb', line 89

def self.clear
  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 :)


79
80
81
82
83
# File 'lib/chingu/basic_game_object.rb', line 79

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

.has_trait(*traits) ⇒ Object

adds a trait or traits to a certain game class

Executes a ruby “include” the specified module



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

def self.has_trait(*traits)
  has_traits(*traits)
end

.has_traits(*traits) ⇒ Object

See #has_trait



23
24
25
26
27
28
29
# File 'lib/chingu/basic_game_object.rb', line 23

def self.has_traits(*traits)
  Array(traits).each do |trait|
    if trait.is_a?(::Symbol) || trait.is_a?(::String)
      include Chingu::Traits.const_get(Chingu::Inflector.camelize(trait))
    end
  end
end

Instance Method Details

#destroy!Object

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).



97
98
99
100
# File 'lib/chingu/basic_game_object.rb', line 97

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

#drawObject



61
62
# File 'lib/chingu/basic_game_object.rb', line 61

def draw
end

#setup_trait(options) ⇒ Object



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

def setup_trait(options)
end

#updateObject



58
59
# File 'lib/chingu/basic_game_object.rb', line 58

def update
end