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
Class Attribute Summary collapse
-
.instances ⇒ Object
Returns the value of attribute instances.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
(also: #game_state)
Returns the value of attribute parent.
-
#paused ⇒ Object
readonly
Returns the value of attribute paused.
Class Method Summary collapse
-
.all ⇒ Object
Returns an array with all objects of current class.
-
.create(*options, &block) ⇒ Object
Works just as BasicGameObject#new with the addition that Chingu will keep track of the new object.
-
.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 :).
-
.each ⇒ Object
As Array.each on the instances of the current class.
-
.each_with_index ⇒ Object
As Array.each_with_index on the instances of the current class.
-
.initialize_trait(options) ⇒ Object
Empty placeholders to be overridden.
-
.select ⇒ Object
As Array.select but on the instances of current class.
-
.size ⇒ Object
Returns the total amount of game objects based on this class.
-
.trait(trait, options = {}) ⇒ Object
(also: has_trait)
Adds a trait or traits to a certain game class Executes a standard ruby “include” the specified module.
- .traits(*traits) ⇒ Object (also: has_traits)
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
-
#filename ⇒ Object
Returns a filename-friendly string from the current class-name.
-
#initialize(options = {}) ⇒ BasicGameObject
constructor
BasicGameObject initialize - call .setup_trait() on all traits that implements it.
-
#pause! ⇒ Object
(also: #pause)
Disable automatic calling of update() and update_trait() each game loop.
-
#paused? ⇒ Boolean
Returns true if paused.
- #setup ⇒ Object
- #setup_trait(options) ⇒ Object
- #trait_options ⇒ Object
-
#unpause! ⇒ Object
(also: #unpause)
Enable automatic calling of update() and update_trait() each game loop.
- #update ⇒ Object
- #update_trait ⇒ Object
Methods included from Helpers::ClassInheritableAccessor
Constructor Details
#initialize(options = {}) ⇒ BasicGameObject
BasicGameObject initialize
-
call .setup_trait() on all traits that implements it
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/chingu/basic_game_object.rb', line 68 def initialize( = {}) @options = @parent = [:parent] self.class.instances ||= Array.new self.class.instances << self # # A GameObject either belong to a GameState or our mainwindow ($window) # @parent = $window.current_scope if !@parent && $window # if true, BasicGameObject#update will be called @paused = [:paused] || [:pause] || false # 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() setup end |
Class Attribute Details
.instances ⇒ Object
Returns the value of attribute instances
11 12 13 |
# File 'lib/chingu/basic_game_object.rb', line 11 def instances @instances end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options
15 16 17 |
# File 'lib/chingu/basic_game_object.rb', line 15 def @options end |
#parent ⇒ Object Also known as: game_state
Returns the value of attribute parent
16 17 18 |
# File 'lib/chingu/basic_game_object.rb', line 16 def parent @parent end |
#paused ⇒ Object (readonly)
Returns the value of attribute paused
15 16 17 |
# File 'lib/chingu/basic_game_object.rb', line 15 def paused @paused 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
168 169 170 171 172 |
# File 'lib/chingu/basic_game_object.rb', line 168 def self.all instances ? instances.dup : [] # instances ? instances.keys : [] # for hash instance # $window.current_scope.game_objects.of_class(self).dup # old school way end |
.create(*options, &block) ⇒ Object
Works just as BasicGameObject#new with the addition that Chingu will keep track of the new object. The object will be assigned to a game_objects list. If created within a game state it will be added to that_game_state.game_objects. Otherwise it will be added to $window.game_objects list. The naming is inspired from ActiveRecord#create which will persist the object in the database right away.
Chingu will automatically call update() and draw() on stored game objects. Often in a smaller game this is exaclty what you want. If not, use the normal new().
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/chingu/basic_game_object.rb', line 99 def self.create(*, &block) instance = self.new(*, &block) # # Add to parents list of game objects # instance.parent.add_game_object(instance) if instance.parent # # Keep track of instances in class-variable # Used for quick access to all isntances of a certain class, for example Enemy.all # #instances ||= Array.new #instances << instance return instance end |
.destroy_all ⇒ Object
Destroys all intances of objects class:
Bullet.destroy_all # Removes all Bullet objects from the game
216 217 218 219 |
# File 'lib/chingu/basic_game_object.rb', line 216 def self.destroy_all all.each { |game_object| game_object.parent.remove_game_object(game_object) } instances.clear if instances 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 :)
206 207 208 209 210 |
# File 'lib/chingu/basic_game_object.rb', line 206 def self.destroy_if(&block) all.each do |object| object.destroy if yield(object) end end |
.each ⇒ Object
As Array.each on the instances of the current class
177 178 179 |
# File 'lib/chingu/basic_game_object.rb', line 177 def self.each all.each { |object| yield object } end |
.each_with_index ⇒ Object
As Array.each_with_index on the instances of the current class
184 185 186 |
# File 'lib/chingu/basic_game_object.rb', line 184 def self.each_with_index all.each_with_index { |object, index| yield object, index } end |
.initialize_trait(options) ⇒ Object
Empty placeholders to be overridden
154 |
# File 'lib/chingu/basic_game_object.rb', line 154 def self.initialize_trait(); end |
.select ⇒ Object
As Array.select but on the instances of current class
191 192 193 |
# File 'lib/chingu/basic_game_object.rb', line 191 def self.select all.select { |object| yield object } end |
.size ⇒ Object
Returns the total amount of game objects based on this class
198 199 200 |
# File 'lib/chingu/basic_game_object.rb', line 198 def self.size all.size end |
.trait(trait, options = {}) ⇒ Object Also known as: has_trait
Adds a trait or traits to a certain game class Executes a standard ruby “include” the specified module
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 52 53 |
# File 'lib/chingu/basic_game_object.rb', line 26 def self.trait(trait, = {}) if trait.is_a?(::Symbol) || trait.is_a?(::String) 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 trait-line. if mod2.method_defined?(:initialize_trait) initialize_trait() end end rescue puts "Error in 'trait #{trait}': " + $!.to_s end end end |
.traits(*traits) ⇒ Object Also known as: has_traits
56 57 58 |
# File 'lib/chingu/basic_game_object.rb', line 56 def self.traits(*traits) Array(traits).each { |trait_name| trait trait_name } 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).
225 226 227 228 229 230 231 |
# File 'lib/chingu/basic_game_object.rb', line 225 def destroy if @parent @parent.remove_game_object(self) @parent.remove_input_client(self) end self.class.instances.delete(self) end |
#draw ⇒ Object
160 |
# File 'lib/chingu/basic_game_object.rb', line 160 def draw; end |
#draw_trait ⇒ Object
158 |
# File 'lib/chingu/basic_game_object.rb', line 158 def draw_trait; end |
#filename ⇒ Object
Returns a filename-friendly string from the current class-name
“FireBall” -> “fire_ball”
147 148 149 |
# File 'lib/chingu/basic_game_object.rb', line 147 def filename Chingu::Inflector.underscore(Chingu::Inflector.demodulize(self.class.to_s)) end |
#pause! ⇒ Object Also known as: pause
Disable automatic calling of update() and update_trait() each game loop
120 121 122 123 |
# File 'lib/chingu/basic_game_object.rb', line 120 def pause! @parent.game_objects.pause_game_object(self) if @parent && !@paused @paused = true end |
#paused? ⇒ Boolean
Returns true if paused
138 139 140 |
# File 'lib/chingu/basic_game_object.rb', line 138 def paused? @paused == true end |
#setup ⇒ Object
156 |
# File 'lib/chingu/basic_game_object.rb', line 156 def setup; end |
#setup_trait(options) ⇒ Object
155 |
# File 'lib/chingu/basic_game_object.rb', line 155 def setup_trait(); end |
#trait_options ⇒ Object
20 |
# File 'lib/chingu/basic_game_object.rb', line 20 def ; self.class.; end |
#unpause! ⇒ Object Also known as: unpause
Enable automatic calling of update() and update_trait() each game loop
129 130 131 132 |
# File 'lib/chingu/basic_game_object.rb', line 129 def unpause! @parent.game_objects.unpause_game_object(self) if @parent && @paused @paused = false end |
#update ⇒ Object
159 |
# File 'lib/chingu/basic_game_object.rb', line 159 def update; end |
#update_trait ⇒ Object
157 |
# File 'lib/chingu/basic_game_object.rb', line 157 def update_trait; end |