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

BasicGameObject initialize

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



36
37
38
39
40
41
42
# File 'lib/chingu/basic_game_object.rb', line 36

def initialize(options = {})
  @options = options
  
  # 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.



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

def options
  @options
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
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


82
83
84
# File 'lib/chingu/basic_game_object.rb', line 82

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

.create(options = {}) ⇒ 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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chingu/basic_game_object.rb', line 52

def self.create(options = {})
  instance = self.new(options)
  
  #
  # A GameObject either belong to a GameState or our mainwindow ($window)
  #
  if $window && $window.respond_to?(:game_state_manager)
    if (instance.parent = $window.game_state_manager.inside_state || $window)
      instance.parent.add_game_object(instance)
    end
  end
  
  return instance
end

.destroy_allObject

Destroys all intances of objects class:

Bullet.destroy_all    # Removes all Bullet objects from the game


107
108
109
# File 'lib/chingu/basic_game_object.rb', line 107

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


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

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



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

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

.has_traits(*traits) ⇒ Object

See #has_trait



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

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

.sizeObject

Returns



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

def self.size
  $window.current_parent.game_objects.of_class(self).size
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).



115
116
117
118
# File 'lib/chingu/basic_game_object.rb', line 115

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

#drawObject



73
74
# File 'lib/chingu/basic_game_object.rb', line 73

def draw
end

#setup_trait(options) ⇒ Object



67
68
# File 'lib/chingu/basic_game_object.rb', line 67

def setup_trait(options)
end

#updateObject



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

def update
end