Class: Chingu::BasicGameObject
- Inherits:
-
Object
- Object
- Chingu::BasicGameObject
- 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
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
-
.all ⇒ Object
Fetch all objects of a current class.
-
.clear ⇒ Object
Clear all intances of objects class: Bullet.clear # Removes all Bullet objects from the game.
-
.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 :).
-
.has_trait(*traits) ⇒ Object
adds a trait or traits to a certain game class.
-
.has_traits(*traits) ⇒ Object
See #has_trait.
Instance Method Summary collapse
-
#destroy! ⇒ Object
Removes object from the update cycle and freezes the object to prevent further modifications.
- #draw ⇒ Object
-
#initialize(options = {}) ⇒ BasicGameObject
constructor
BasicGameUnit initialize.
- #setup_trait(options) ⇒ Object
- #update ⇒ Object
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( = {}) = # # 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() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/chingu/basic_game_object.rb', line 9 def end |
#parent ⇒ Object (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
.all ⇒ Object
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 |
.clear ⇒ Object
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 |
#draw ⇒ Object
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() end |
#update ⇒ Object
58 59 |
# File 'lib/chingu/basic_game_object.rb', line 58 def update end |