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
Returns the value of attribute parent.
Class Method Summary collapse
-
.all ⇒ Object
Returns an array with all objects of current class.
-
.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.
-
.destroy_all ⇒ Object
Destroys all intances of objects class: Bullet.destroy_all # 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.
-
.size ⇒ Object
Returns.
Instance Method Summary collapse
-
#destroy! ⇒ Object
Removes object from the update cycle and freezes the object to prevent further modifications.
- #draw ⇒ Object
- #draw_trait ⇒ Object
-
#initialize(options = {}) ⇒ BasicGameObject
constructor
BasicGameObject initialize - call .setup_trait() on all traits that implements it.
- #setup_trait(options) ⇒ Object
- #update ⇒ Object
- #update_trait ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ BasicGameObject
BasicGameObject initialize
-
call .setup_trait() on all traits that implements it
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/chingu/basic_game_object.rb', line 36 def initialize( = {}) = # # A GameObject either belong to a GameState or our mainwindow ($window) # if $window && $window.respond_to?(:game_state_manager) @parent = $window.game_state_manager.inside_state || $window end # 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() 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
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
.all ⇒ Object
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
92 93 94 |
# File 'lib/chingu/basic_game_object.rb', line 92 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.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/chingu/basic_game_object.rb', line 59 def self.create( = {}) instance = self.new() # # Add to parents list of game objects # instance.parent.add_game_object(instance) if instance.parent return instance end |
.destroy_all ⇒ Object
Destroys all intances of objects class:
Bullet.destroy_all # Removes all Bullet objects from the game
117 118 119 |
# File 'lib/chingu/basic_game_object.rb', line 117 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 :)
107 108 109 110 111 |
# File 'lib/chingu/basic_game_object.rb', line 107 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 |
.size ⇒ Object
Returns
99 100 101 |
# File 'lib/chingu/basic_game_object.rb', line 99 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).
125 126 127 128 |
# File 'lib/chingu/basic_game_object.rb', line 125 def destroy! @parent.remove_game_object(self) if @parent self.freeze end |
#draw ⇒ Object
82 83 |
# File 'lib/chingu/basic_game_object.rb', line 82 def draw end |
#draw_trait ⇒ Object
76 77 |
# File 'lib/chingu/basic_game_object.rb', line 76 def draw_trait end |
#setup_trait(options) ⇒ Object
70 71 |
# File 'lib/chingu/basic_game_object.rb', line 70 def setup_trait() end |
#update ⇒ Object
79 80 |
# File 'lib/chingu/basic_game_object.rb', line 79 def update end |
#update_trait ⇒ Object
73 74 |
# File 'lib/chingu/basic_game_object.rb', line 73 def update_trait end |