Class: Chingu::GameObjectList

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GameObjectList

Returns a new instance of GameObjectList.



31
32
33
34
35
36
37
38
39
# File 'lib/chingu/game_object_list.rb', line 31

def initialize(options = {})
  @game_objects = options[:game_objects] || []
  @visible_game_objects = []
  @unpaused_game_objects = []
  
  #@game_objects = {}
  #@visible_game_objects = {}
  #@unpaused_game_objects = {}
end

Instance Attribute Details

#unpaused_game_objectsObject (readonly)

Returns the value of attribute unpaused_game_objects.



29
30
31
# File 'lib/chingu/game_object_list.rb', line 29

def unpaused_game_objects
  @unpaused_game_objects
end

#visible_game_objectsObject (readonly)

Returns the value of attribute visible_game_objects.



29
30
31
# File 'lib/chingu/game_object_list.rb', line 29

def visible_game_objects
  @visible_game_objects
end

Instance Method Details

#add_game_object(object) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/chingu/game_object_list.rb', line 68

def add_game_object(object)
  @game_objects.push(object)
  @visible_game_objects.push(object)  if object.respond_to?(:visible)  && object.visible
  @unpaused_game_objects.push(object) if object.respond_to?(:paused)   && !object.paused
  
  #@game_objects[object] = true
  #@visible_game_objects[object] = true  if object.respond_to?(:visible)  && object.visible
  #@unpaused_game_objects[object] = true if object.respond_to?(:paused)   && !object.paused
end

#destroy_allObject Also known as: clear, remove_all



49
50
51
# File 'lib/chingu/game_object_list.rb', line 49

def destroy_all
  @game_objects.each { |game_object| game_object.destroy }
end

#destroy_ifObject



84
85
86
# File 'lib/chingu/game_object_list.rb', line 84

def destroy_if
  @game_objects.select { |object| object.destroy if yield(object) }
end

#drawObject



103
104
105
# File 'lib/chingu/game_object_list.rb', line 103

def draw
  @visible_game_objects.each { |go| go.draw_trait; go.draw; }
end

#draw_relative(x = 0, y = 0, zorder = 0, angle = 0, center_x = 0, center_y = 0, factor_x = 0, factor_y = 0) ⇒ Object



110
111
112
113
114
115
# File 'lib/chingu/game_object_list.rb', line 110

def draw_relative(x=0, y=0, zorder=0, angle=0, center_x=0, center_y=0, factor_x=0, factor_y=0)
  @visible_game_objects.each do |object| 
    object.draw_trait
    object.draw_relative(x, y, zorder, angle, center_x, center_y, factor_x, factor_y)
  end
end

#eachObject



118
119
120
# File 'lib/chingu/game_object_list.rb', line 118

def each
  @game_objects.dup.each { |object| yield object }
end

#each_with_indexObject



122
123
124
# File 'lib/chingu/game_object_list.rb', line 122

def each_with_index
  @game_objects.dup.each_with_index { |object, index| yield object, index }
end

#empty?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/chingu/game_object_list.rb', line 92

def empty?
  @game_objects.empty?
end

#firstObject



134
135
136
# File 'lib/chingu/game_object_list.rb', line 134

def first
  @game_objects.first
end

#force_drawObject



106
107
108
# File 'lib/chingu/game_object_list.rb', line 106

def force_draw
  @game_objects.each { |go| go.draw_trait; go.draw }
end

#force_updateObject



99
100
101
# File 'lib/chingu/game_object_list.rb', line 99

def force_update
  @game_objects.each { |go| go.update_trait; go.update; }
end

#hide!Object Also known as: hide

Disable automatic calling of draw and draw_trait each game loop for all game objects



161
162
163
# File 'lib/chingu/game_object_list.rb', line 161

def hide!
  @game_objects.each { |object| object.hide! }
end

#hide_game_object(object) ⇒ Object



58
59
60
# File 'lib/chingu/game_object_list.rb', line 58

def hide_game_object(object)
  @visible_game_objects.delete(object)
end

#lastObject



138
139
140
# File 'lib/chingu/game_object_list.rb', line 138

def last
  @game_objects.last
end

#mapObject



130
131
132
# File 'lib/chingu/game_object_list.rb', line 130

def map
  @game_objects.map { |object| yield object }
end

#of_class(klass) ⇒ Object



45
46
47
# File 'lib/chingu/game_object_list.rb', line 45

def of_class(klass)
  @game_objects.select { |game_object| game_object.is_a? klass }
end

#pause!Object Also known as: pause

Disable automatic calling of update() and update_trait() each game loop for all game objects



145
146
147
# File 'lib/chingu/game_object_list.rb', line 145

def pause!
  @game_objects.each { |object| object.pause! }
end

#pause_game_object(object) ⇒ Object



61
62
63
# File 'lib/chingu/game_object_list.rb', line 61

def pause_game_object(object)
  @unpaused_game_objects.delete(object)
end

#remove_game_object(object) ⇒ Object



78
79
80
81
82
# File 'lib/chingu/game_object_list.rb', line 78

def remove_game_object(object)
  @game_objects.delete(object)
  @visible_game_objects.delete(object)
  @unpaused_game_objects.delete(object)
end

#selectObject



126
127
128
# File 'lib/chingu/game_object_list.rb', line 126

def select
  @game_objects.dup.select { |object| yield object }
end

#show!Object Also known as: show

Enable automatic calling of draw and draw_trait each game loop for all game objects



169
170
171
# File 'lib/chingu/game_object_list.rb', line 169

def show!
  @game_objects.each { |object| object.show! }
end

#show_game_object(object) ⇒ Object



55
56
57
# File 'lib/chingu/game_object_list.rb', line 55

def show_game_object(object)
  @visible_game_objects.push(object)
end

#sizeObject



88
89
90
# File 'lib/chingu/game_object_list.rb', line 88

def size
  @game_objects.size
end

#to_sObject



41
42
43
# File 'lib/chingu/game_object_list.rb', line 41

def to_s
  "#{@game_objects.size} game objects."
end

#unpause!Object Also known as: unpause

Enable automatic calling of update() and update_trait() each game loop for all game objects



153
154
155
# File 'lib/chingu/game_object_list.rb', line 153

def unpause!
  @game_objects.each { |object| object.unpause! }
end

#unpause_game_object(object) ⇒ Object



64
65
66
# File 'lib/chingu/game_object_list.rb', line 64

def unpause_game_object(object)
  @unpaused_game_objects.push(object)
end

#updateObject



96
97
98
# File 'lib/chingu/game_object_list.rb', line 96

def update
  @unpaused_game_objects.each { |go| go.update_trait; go.update; }
end