Class: Chingu::BasicGameObject
- Inherits:
-
Object
- Object
- Chingu::BasicGameObject
- Includes:
- Helpers::ClassInheritableAccessor
- 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(trait, options = {}) ⇒ Object
Adds a trait or traits to a certain game class Executes a standard ruby “include” the specified module.
- .has_traits(*traits) ⇒ Object
-
.initialize_trait(options) ⇒ Object
Empty placeholders to be overridden.
-
.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 automatic calling of draw and draw_trait each game loop.
-
#initialize(options = {}) ⇒ BasicGameObject
constructor
BasicGameObject initialize - call .setup_trait() on all traits that implements it.
-
#pause! ⇒ Object
Disable automatic calling of update() and update_trait() each game loop.
-
#paused? ⇒ Boolean
Returns true if paused.
- #setup_trait(options) ⇒ Object
-
#show! ⇒ Object
Enable automatic calling of draw and draw_trait each game loop.
- #trait_options ⇒ Object
-
#unpause! ⇒ Object
Enable automatic calling of update() and update_trait() each game loop.
- #update ⇒ Object
- #update_trait ⇒ Object
-
#visible? ⇒ Boolean
Returns true if visible (not hidden).
Methods included from Helpers::ClassInheritableAccessor
Constructor Details
#initialize(options = {}) ⇒ BasicGameObject
BasicGameObject initialize
-
call .setup_trait() on all traits that implements it
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/chingu/basic_game_object.rb', line 61 def initialize( = {}) @options = @parent = [:parent] # # A GameObject either belong to a GameState or our mainwindow ($window) # #if !@parent && $window && $window.respond_to?(:game_state_manager) # @parent = $window.game_state_manager.inside_state || $window #end @parent = $window.current_scope if !@parent && $window # 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.
12 13 14 |
# File 'lib/chingu/basic_game_object.rb', line 12 def @options end |
#parent ⇒ Object
Returns the value of attribute parent.
13 14 15 |
# File 'lib/chingu/basic_game_object.rb', line 13 def parent @parent end |
#paused ⇒ Object (readonly)
Returns the value of attribute paused.
12 13 14 |
# File 'lib/chingu/basic_game_object.rb', line 12 def paused @paused end |
#visible ⇒ Object (readonly)
Returns the value of attribute visible.
12 13 14 |
# File 'lib/chingu/basic_game_object.rb', line 12 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
172 173 174 |
# File 'lib/chingu/basic_game_object.rb', line 172 def self.all $window.current_scope.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.
def self.create(options = {})
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/chingu/basic_game_object.rb', line 93 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
197 198 199 |
# File 'lib/chingu/basic_game_object.rb', line 197 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 :)
187 188 189 190 191 |
# File 'lib/chingu/basic_game_object.rb', line 187 def self.destroy_if(&block) all.each do |object| object.destroy if yield(object) end end |
.has_trait(trait, options = {}) ⇒ Object
Adds a trait or traits to a certain game class Executes a standard ruby “include” the specified module
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/chingu/basic_game_object.rb', line 23 def self.has_trait(trait, = {}) if trait.is_a?(::Symbol) || trait.is_a?(::String) ## puts "has_trait #{trait}, #{options}" begin # Convert user-given symbol (eg. :timer) to a Module (eg. Chingu::Traits::Timer) mod = Chingu::Traits.const_get(Chingu::Inflector.camelize(trait)) # Include the module, which will add the containing methods as instance methods include mod # Does sub-module "ClessMethods" exists? # (eg: Chingu::Traits::Timer::ClassMethods) if mod.const_defined?("ClassMethods") # Add methods in scope ClassMethods as.. class methods! mod2 = mod.const_get("ClassMethods") extend mod2 # If the newly included trait has a initialize_trait method in the ClassMethods-scope: # ... call it with the options provided with the has_trait-line. if mod2.method_defined?(:initialize_trait) initialize_trait() end end rescue puts $! end end end |
.has_traits(*traits) ⇒ Object
53 54 55 |
# File 'lib/chingu/basic_game_object.rb', line 53 def self.has_traits(*traits) Array(traits).each { |trait| has_trait trait } end |
.initialize_trait(options) ⇒ Object
Empty placeholders to be overridden
158 |
# File 'lib/chingu/basic_game_object.rb', line 158 def self.initialize_trait(); end |
.size ⇒ Object
Returns
179 180 181 |
# File 'lib/chingu/basic_game_object.rb', line 179 def self.size $window.current_scope.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).
205 206 207 |
# File 'lib/chingu/basic_game_object.rb', line 205 def destroy @parent.remove_game_object(self) if @parent end |
#draw ⇒ Object
163 |
# File 'lib/chingu/basic_game_object.rb', line 163 def draw; end |
#draw_trait ⇒ Object
161 |
# File 'lib/chingu/basic_game_object.rb', line 161 def draw_trait; end |
#hide! ⇒ Object
Disable automatic calling of draw and draw_trait each game loop
130 131 132 |
# File 'lib/chingu/basic_game_object.rb', line 130 def hide! @visible = false end |
#pause! ⇒ Object
Disable automatic calling of update() and update_trait() each game loop
116 117 118 |
# File 'lib/chingu/basic_game_object.rb', line 116 def pause! @paused = true end |
#paused? ⇒ Boolean
Returns true if paused
144 145 146 |
# File 'lib/chingu/basic_game_object.rb', line 144 def paused? @paused == true end |
#setup_trait(options) ⇒ Object
159 |
# File 'lib/chingu/basic_game_object.rb', line 159 def setup_trait(); end |
#show! ⇒ Object
Enable automatic calling of draw and draw_trait each game loop
137 138 139 |
# File 'lib/chingu/basic_game_object.rb', line 137 def show! @visible = true end |
#trait_options ⇒ Object
17 |
# File 'lib/chingu/basic_game_object.rb', line 17 def ; self.class.; end |
#unpause! ⇒ Object
Enable automatic calling of update() and update_trait() each game loop
123 124 125 |
# File 'lib/chingu/basic_game_object.rb', line 123 def unpause! @paused = false end |
#update ⇒ Object
162 |
# File 'lib/chingu/basic_game_object.rb', line 162 def update; end |
#update_trait ⇒ Object
160 |
# File 'lib/chingu/basic_game_object.rb', line 160 def update_trait; end |
#visible? ⇒ Boolean
Returns true if visible (not hidden)
151 152 153 |
# File 'lib/chingu/basic_game_object.rb', line 151 def visible? @visible == true end |