Class: Chingu::BasicGameObject

Inherits:
Object
  • Object
show all
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

GameObject

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  @options = 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
  
  # 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(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/chingu/basic_game_object.rb', line 9

def options
  @options
end

#parentObject

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

.allObject

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(options = {})
  instance = self.new(options)
  
  #
  # Add to parents list of game objects
  #
  instance.parent.add_game_object(instance) if instance.parent
  
  return instance
end

.destroy_allObject

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

.sizeObject

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

#drawObject



82
83
# File 'lib/chingu/basic_game_object.rb', line 82

def draw
end

#draw_traitObject



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(options)
end

#updateObject



79
80
# File 'lib/chingu/basic_game_object.rb', line 79

def update
end

#update_traitObject



73
74
# File 'lib/chingu/basic_game_object.rb', line 73

def update_trait
end