Class: Chingu::GameObjectList
- Inherits:
-
Object
- Object
- Chingu::GameObjectList
- Defined in:
- lib/chingu/game_object_list.rb
Overview
Manages a list of game objects An instance of GameObjectList is automaticly created as “game_objects” if using Chingu::Window
Instance Method Summary collapse
- #add_game_object(object) ⇒ Object
- #destroy_all ⇒ Object (also: #clear, #remove_all)
- #destroy_if ⇒ Object
- #draw ⇒ Object
- #each ⇒ Object
-
#hide! ⇒ Object
Disable automatic calling of draw and draw_trait each game loop for all game objects.
-
#initialize ⇒ GameObjectList
constructor
A new instance of GameObjectList.
- #of_class(klass) ⇒ Object
-
#pause! ⇒ Object
Disable automatic calling of update() and update_trait() each game loop for all game objects.
- #remove_game_object(object) ⇒ Object
- #select ⇒ Object
-
#show! ⇒ Object
Enable automatic calling of draw and draw_trait each game loop for all game objects.
- #size ⇒ Object
- #to_s ⇒ Object
-
#unpause! ⇒ Object
Enable automatic calling of update() and update_trait() each game loop for all game objects.
- #update ⇒ Object
Constructor Details
#initialize ⇒ GameObjectList
Returns a new instance of GameObjectList.
30 31 32 33 |
# File 'lib/chingu/game_object_list.rb', line 30 def initialize @game_objects = Array.new #@game_objects_by_class = Hash.new end |
Instance Method Details
#add_game_object(object) ⇒ Object
51 52 53 54 |
# File 'lib/chingu/game_object_list.rb', line 51 def add_game_object(object) @game_objects.push(object) #(@game_objects_by_class[object.class] ||= []).push(object) end |
#destroy_all ⇒ Object Also known as: clear, remove_all
44 45 46 47 |
# File 'lib/chingu/game_object_list.rb', line 44 def destroy_all @game_objects.clear #@game_objects_of_class.clear end |
#destroy_if ⇒ Object
61 62 63 64 |
# File 'lib/chingu/game_object_list.rb', line 61 def destroy_if @game_objects.reject! { |object| yield(object) } #@game_objects_by_class.delete_if { |klass, object| yield(object) } end |
#draw ⇒ Object
70 71 72 73 74 75 |
# File 'lib/chingu/game_object_list.rb', line 70 def draw @game_objects.each{ |object| object.visible }.each do |object| object.draw_trait object.draw end end |
#each ⇒ Object
84 85 86 |
# File 'lib/chingu/game_object_list.rb', line 84 def each @game_objects.each { |object| yield object } end |
#hide! ⇒ Object
Disable automatic calling of draw and draw_trait each game loop for all game objects
109 110 111 |
# File 'lib/chingu/game_object_list.rb', line 109 def hide! @game_objects.each { |object| object.hide! } end |
#of_class(klass) ⇒ Object
39 40 41 42 |
# File 'lib/chingu/game_object_list.rb', line 39 def of_class(klass) @game_objects.select { |game_object| game_object.is_a? klass } #@game_objects_by_class[klass] || [] end |
#pause! ⇒ Object
Disable automatic calling of update() and update_trait() each game loop for all game objects
95 96 97 |
# File 'lib/chingu/game_object_list.rb', line 95 def pause! @game_objects.each { |object| object.pause! } end |
#remove_game_object(object) ⇒ Object
56 57 58 59 |
# File 'lib/chingu/game_object_list.rb', line 56 def remove_game_object(object) @game_objects.delete(object) #@game_objects_by_class[object.class].delete(object) end |
#select ⇒ Object
88 89 90 |
# File 'lib/chingu/game_object_list.rb', line 88 def select @game_objects.select { |object| yield object } end |
#show! ⇒ Object
Enable automatic calling of draw and draw_trait each game loop for all game objects
116 117 118 |
# File 'lib/chingu/game_object_list.rb', line 116 def show! @game_objects.each { |object| object.show! } end |
#size ⇒ Object
66 67 68 |
# File 'lib/chingu/game_object_list.rb', line 66 def size @game_objects.size end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/chingu/game_object_list.rb', line 35 def to_s "#{@game_objects.size} game objects." end |
#unpause! ⇒ Object
Enable automatic calling of update() and update_trait() each game loop for all game objects
102 103 104 |
# File 'lib/chingu/game_object_list.rb', line 102 def unpause! @game_objects.each { |object| object.unpause! } end |
#update ⇒ Object
77 78 79 80 81 82 |
# File 'lib/chingu/game_object_list.rb', line 77 def update @game_objects.select{ |object| not object.paused }.each do |object| object.update_trait object.update end end |