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.
-
#paused ⇒ Object
readonly
Returns the value of attribute paused.
-
#visible ⇒ Object
readonly
Returns the value of attribute visible.
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
(also: #destroy!)
Removes object from the update cycle and freezes the object to prevent further modifications.
- #draw ⇒ Object
- #draw_trait ⇒ Object
-
#hide! ⇒ Object
Disable auto-drawing of object.
-
#initialize(options = {}) ⇒ BasicGameObject
constructor
BasicGameObject initialize - call .setup_trait() on all traits that implements it.
-
#pause! ⇒ Object
Disable auto-updating of traits.
- #setup_trait(options) ⇒ Object
-
#show! ⇒ Object
Enable auto-drawing of object.
-
#unpause! ⇒ Object
Enable auto-update of traits.
- #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 50 51 52 53 54 55 |
# File 'lib/chingu/basic_game_object.rb', line 36 def initialize( = {}) @options = # # 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 # if true, BasicGameObject#update will be called @paused = [:paused] || false # if true, BasicGameObject#draw will be called @visible = [: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() 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 @options 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 |
#paused ⇒ Object (readonly)
Returns the value of attribute paused.
9 10 11 |
# File 'lib/chingu/basic_game_object.rb', line 9 def paused @paused end |
#visible ⇒ Object (readonly)
Returns the value of attribute visible.
9 10 11 |
# File 'lib/chingu/basic_game_object.rb', line 9 def visible @visible 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
124 125 126 |
# File 'lib/chingu/basic_game_object.rb', line 124 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.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/chingu/basic_game_object.rb', line 65 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
149 150 151 |
# File 'lib/chingu/basic_game_object.rb', line 149 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 :)
139 140 141 142 143 |
# File 'lib/chingu/basic_game_object.rb', line 139 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
131 132 133 |
# File 'lib/chingu/basic_game_object.rb', line 131 def self.size $window.current_parent.game_objects.of_class(self).size end |
Instance Method Details
#destroy ⇒ Object 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).
157 158 159 |
# File 'lib/chingu/basic_game_object.rb', line 157 def destroy @parent.remove_game_object(self) if @parent end |
#draw ⇒ Object
114 115 |
# File 'lib/chingu/basic_game_object.rb', line 114 def draw end |
#draw_trait ⇒ Object
108 109 |
# File 'lib/chingu/basic_game_object.rb', line 108 def draw_trait end |
#hide! ⇒ Object
Disable auto-drawing of object
92 93 94 |
# File 'lib/chingu/basic_game_object.rb', line 92 def hide! @visible = false end |
#pause! ⇒ Object
Disable auto-updating of traits
80 81 82 |
# File 'lib/chingu/basic_game_object.rb', line 80 def pause! @paused = true end |
#setup_trait(options) ⇒ Object
102 103 |
# File 'lib/chingu/basic_game_object.rb', line 102 def setup_trait() end |
#show! ⇒ Object
Enable auto-drawing of object
98 99 100 |
# File 'lib/chingu/basic_game_object.rb', line 98 def show! @visible = true end |
#unpause! ⇒ Object
Enable auto-update of traits
86 87 88 |
# File 'lib/chingu/basic_game_object.rb', line 86 def unpause! @paused = false end |
#update ⇒ Object
111 112 |
# File 'lib/chingu/basic_game_object.rb', line 111 def update end |
#update_trait ⇒ Object
105 106 |
# File 'lib/chingu/basic_game_object.rb', line 105 def update_trait end |