Class: Engine::GameObject
- Inherits:
-
Object
- Object
- Engine::GameObject
show all
- Includes:
- Serializable
- Defined in:
- lib/engine/game_object.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
allowed_class?, get_class, included, register_class, #uuid
Instance Attribute Details
#components ⇒ Object
Returns the value of attribute components.
15
16
17
|
# File 'lib/engine/game_object.rb', line 15
def components
@components
end
|
#created_at ⇒ Object
Returns the value of attribute created_at.
15
16
17
|
# File 'lib/engine/game_object.rb', line 15
def created_at
@created_at
end
|
#local_version ⇒ Object
Returns the value of attribute local_version.
16
17
18
|
# File 'lib/engine/game_object.rb', line 16
def local_version
@local_version
end
|
#name ⇒ Object
Returns the value of attribute name.
15
16
17
|
# File 'lib/engine/game_object.rb', line 15
def name
@name
end
|
#parent ⇒ Object
Returns the value of attribute parent.
16
17
18
|
# File 'lib/engine/game_object.rb', line 16
def parent
@parent
end
|
#pos ⇒ Object
Returns the value of attribute pos.
16
17
18
|
# File 'lib/engine/game_object.rb', line 16
def pos
@pos
end
|
#renderers ⇒ Object
Returns the value of attribute renderers.
15
16
17
|
# File 'lib/engine/game_object.rb', line 15
def renderers
@renderers
end
|
#rotation ⇒ Object
Returns the value of attribute rotation.
16
17
18
|
# File 'lib/engine/game_object.rb', line 16
def rotation
@rotation
end
|
#scale ⇒ Object
Returns the value of attribute scale.
16
17
18
|
# File 'lib/engine/game_object.rb', line 16
def scale
@scale
end
|
#ui_renderers ⇒ Object
Returns the value of attribute ui_renderers.
15
16
17
|
# File 'lib/engine/game_object.rb', line 15
def ui_renderers
@ui_renderers
end
|
Class Method Details
.destroy_all ⇒ Object
269
270
271
272
273
|
# File 'lib/engine/game_object.rb', line 269
def self.destroy_all
GameObject.objects.dup.each do |object|
object.destroy! unless object.destroyed?
end
end
|
.destroyed_objects ⇒ Object
310
311
312
|
# File 'lib/engine/game_object.rb', line 310
def self.destroyed_objects
@destroyed_objects ||= []
end
|
.erase_destroyed_objects ⇒ Object
247
248
249
250
251
252
|
# File 'lib/engine/game_object.rb', line 247
def self.erase_destroyed_objects
destroyed_objects.each do |object|
object._erase!
end
@destroyed_objects = []
end
|
.mesh_renderers ⇒ Object
284
285
286
|
# File 'lib/engine/game_object.rb', line 284
def self.mesh_renderers
@cached_mesh_renderers ||= []
end
|
.method_added(name) ⇒ Object
9
10
11
12
13
|
# File 'lib/engine/game_object.rb', line 9
def self.method_added(name)
@methods ||= Set.new
return if name == :initialize || name == :destroyed?
@methods.add(name)
end
|
.object_spawned(object) ⇒ Object
302
303
304
|
# File 'lib/engine/game_object.rb', line 302
def self.object_spawned(object)
objects << object
end
|
.objects ⇒ Object
306
307
308
|
# File 'lib/engine/game_object.rb', line 306
def self.objects
@objects ||= []
end
|
.register_renderers(game_object) ⇒ Object
292
293
294
295
|
# File 'lib/engine/game_object.rb', line 292
def self.register_renderers(game_object)
game_object.renderers.each { |r| mesh_renderers << r }
game_object.ui_renderers.each { |r| ui_renderers << r }
end
|
.ui_renderers ⇒ Object
288
289
290
|
# File 'lib/engine/game_object.rb', line 288
def self.ui_renderers
@cached_ui_renderers ||= []
end
|
.unregister_renderers(game_object) ⇒ Object
297
298
299
300
|
# File 'lib/engine/game_object.rb', line 297
def self.unregister_renderers(game_object)
game_object.renderers.each { |r| mesh_renderers.delete(r) }
game_object.ui_renderers.each { |r| ui_renderers.delete(r) }
end
|
.update_all(delta_time) ⇒ Object
275
276
277
278
279
280
281
282
|
# File 'lib/engine/game_object.rb', line 275
def self.update_all(delta_time)
GameObject.objects.each do |object|
object.components.each { |component| component.update(delta_time) }
end
Component.erase_destroyed_components
GameObject.erase_destroyed_objects
end
|
Instance Method Details
#_erase! ⇒ Object
237
238
239
240
241
242
243
244
245
|
# File 'lib/engine/game_object.rb', line 237
def _erase!
GameObject.objects.delete(self)
parent.children.delete(self) if parent
name = @name
self.class.instance_variable_get(:@methods).each do |method|
singleton_class.send(:undef_method, method)
singleton_class.send(:define_method, method) { raise "This object has been destroyed: #{name}" }
end
end
|
#add_child(child) ⇒ Object
84
85
86
87
|
# File 'lib/engine/game_object.rb', line 84
def add_child(child)
child.parent = self
children << child
end
|
#all_components ⇒ Object
80
81
82
|
# File 'lib/engine/game_object.rb', line 80
def all_components
@all_components ||= @components + @renderers + @ui_renderers
end
|
#awake ⇒ Object
18
19
20
21
22
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
|
# File 'lib/engine/game_object.rb', line 18
def awake
@name ||= "Game Object"
@pos ||= Vector[0, 0, 0]
@rotation ||= Vector[0, 0, 0]
@scale ||= Vector[1, 1, 1]
@local_version ||= 0
@cached_world_version ||= nil
@created_at ||= Time.now
@rotation = normalize_rotation(@rotation)
@rotation_quaternion = Quaternion.from_euler(@rotation)
@pos = Vector[@pos[0], @pos[1], @pos[2] || 0]
all_components = (@components || []) + (@renderers || []) + (@ui_renderers || [])
@components = all_components.select { |c| !c.renderer? && !c.ui_renderer? }
@renderers = all_components.select { |c| c.renderer? }
@ui_renderers = all_components.select { |c| c.ui_renderer? }
GameObject.object_spawned(self)
@parent&.add_child(self)
all_components.each { |component| component.set_game_object(self) }
all_components.each(&:start)
GameObject.register_renderers(self)
end
|
#children ⇒ Object
67
68
69
|
# File 'lib/engine/game_object.rb', line 67
def children
@children ||= Set.new
end
|
#component(klass) ⇒ Object
71
72
73
|
# File 'lib/engine/game_object.rb', line 71
def component(klass)
components_of_type(klass).first
end
|
#components_of_type(klass) ⇒ Object
75
76
77
78
|
# File 'lib/engine/game_object.rb', line 75
def components_of_type(klass)
@components_cache ||= {}
@components_cache[klass] ||= all_components.select { |c| c.is_a?(klass) }
end
|
#destroy! ⇒ Object
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/engine/game_object.rb', line 225
def destroy!
return unless GameObject.objects.include?(self)
children.each(&:destroy!)
components.each(&:destroy!)
ui_renderers.each(&:destroy!)
renderers.each(&:destroy!)
GameObject.unregister_renderers(self)
GameObject.destroyed_objects << self unless @destroyed
@destroyed = true
end
|
#destroyed? ⇒ Boolean
221
222
223
|
# File 'lib/engine/game_object.rb', line 221
def destroyed?
@destroyed
end
|
#euler_angles ⇒ Object
117
118
119
|
# File 'lib/engine/game_object.rb', line 117
def euler_angles
rotation.to_euler
end
|
#forward ⇒ Object
264
265
266
267
|
# File 'lib/engine/game_object.rb', line 264
def forward
model_matrix
@cached_forward ||= local_to_world_direction(Vector[0, 0, 1])
end
|
#local_to_world_coordinate(local) ⇒ Object
153
154
155
156
157
|
# File 'lib/engine/game_object.rb', line 153
def local_to_world_coordinate(local)
local_x4 = Matrix[[local[0], local[1], local[2], 1.0]]
world = local_x4 * model_matrix
Vector[world[0, 0], world[0, 1], world[0, 2]]
end
|
#local_to_world_direction(local) ⇒ Object
165
166
167
|
# File 'lib/engine/game_object.rb', line 165
def local_to_world_direction(local)
local_to_world_coordinate(local) - world_pos
end
|
#model_matrix ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/engine/game_object.rb', line 179
def model_matrix
current_version = world_transform_version
return @cached_world_matrix if @cached_world_version == current_version
@cached_world_pos = nil
@cached_right = nil
@cached_up = nil
@cached_forward = nil
@cached_world_version = current_version
@cached_world_matrix = compute_world_matrix
end
|
#right ⇒ Object
259
260
261
262
|
# File 'lib/engine/game_object.rb', line 259
def right
model_matrix
@cached_right ||= local_to_world_direction(Vector[1, 0, 0])
end
|
#rotate_around(axis, angle) ⇒ Object
169
170
171
172
173
|
# File 'lib/engine/game_object.rb', line 169
def rotate_around(axis, angle)
rotation_quaternion = Quaternion.from_angle_axis(angle, axis)
self.rotation = rotation_quaternion * rotation
end
|
#to_s ⇒ Object
63
64
65
|
# File 'lib/engine/game_object.rb', line 63
def to_s
@name
end
|
#up ⇒ Object
254
255
256
257
|
# File 'lib/engine/game_object.rb', line 254
def up
model_matrix
@cached_up ||= local_to_world_direction(Vector[0, 1, 0])
end
|
#world_pos ⇒ Object
148
149
150
151
|
# File 'lib/engine/game_object.rb', line 148
def world_pos
m = model_matrix
@cached_world_pos ||= Vector[m[3, 0], m[3, 1], m[3, 2]]
end
|
#world_to_local_coordinate(world) ⇒ Object
159
160
161
162
163
|
# File 'lib/engine/game_object.rb', line 159
def world_to_local_coordinate(world)
world_x4 = Matrix[[world[0], world[1], world[2], 1.0]]
local = world_x4 * model_matrix.inverse
Vector[local[0, 0], local[0, 1], local[0, 2]]
end
|
175
176
177
|
# File 'lib/engine/game_object.rb', line 175
def world_transform_version
@local_version + (@parent&.world_transform_version || 0)
end
|
#x ⇒ Object
121
122
123
|
# File 'lib/engine/game_object.rb', line 121
def x
@pos[0]
end
|
#x=(value) ⇒ Object
125
126
127
128
|
# File 'lib/engine/game_object.rb', line 125
def x=(value)
@pos = Vector[value, y, z]
@local_version += 1
end
|
#y ⇒ Object
130
131
132
|
# File 'lib/engine/game_object.rb', line 130
def y
@pos[1]
end
|
#y=(value) ⇒ Object
134
135
136
137
|
# File 'lib/engine/game_object.rb', line 134
def y=(value)
@pos = Vector[x, value, z]
@local_version += 1
end
|
#z ⇒ Object
139
140
141
|
# File 'lib/engine/game_object.rb', line 139
def z
@pos[2]
end
|
#z=(value) ⇒ Object
143
144
145
146
|
# File 'lib/engine/game_object.rb', line 143
def z=(value)
@pos = Vector[x, y, value]
@local_version += 1
end
|