Class: Engine::GameObject

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/engine/game_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

allowed_class?, get_class, included, register_class, #uuid

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



15
16
17
# File 'lib/engine/game_object.rb', line 15

def components
  @components
end

#created_atObject

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_versionObject (readonly)

Returns the value of attribute local_version.



16
17
18
# File 'lib/engine/game_object.rb', line 16

def local_version
  @local_version
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/engine/game_object.rb', line 15

def name
  @name
end

#parentObject

Returns the value of attribute parent.



16
17
18
# File 'lib/engine/game_object.rb', line 16

def parent
  @parent
end

#posObject

Returns the value of attribute pos.



16
17
18
# File 'lib/engine/game_object.rb', line 16

def pos
  @pos
end

#renderersObject

Returns the value of attribute renderers.



15
16
17
# File 'lib/engine/game_object.rb', line 15

def renderers
  @renderers
end

#rotationObject

Returns the value of attribute rotation.



16
17
18
# File 'lib/engine/game_object.rb', line 16

def rotation
  @rotation
end

#scaleObject

Returns the value of attribute scale.



16
17
18
# File 'lib/engine/game_object.rb', line 16

def scale
  @scale
end

#ui_renderersObject

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_allObject



261
262
263
264
265
# File 'lib/engine/game_object.rb', line 261

def self.destroy_all
  GameObject.objects.dup.each do |object|
    object.destroy! unless object.destroyed?
  end
end

.destroyed_objectsObject



302
303
304
# File 'lib/engine/game_object.rb', line 302

def self.destroyed_objects
  @destroyed_objects ||= []
end

.erase_destroyed_objectsObject



236
237
238
239
240
241
# File 'lib/engine/game_object.rb', line 236

def self.erase_destroyed_objects
  destroyed_objects.each do |object|
    object._erase!
  end
  @destroyed_objects = []
end

.mesh_renderersObject



276
277
278
# File 'lib/engine/game_object.rb', line 276

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



294
295
296
# File 'lib/engine/game_object.rb', line 294

def self.object_spawned(object)
  objects << object
end

.objectsObject



298
299
300
# File 'lib/engine/game_object.rb', line 298

def self.objects
  @objects ||= []
end

.register_renderers(game_object) ⇒ Object



284
285
286
287
# File 'lib/engine/game_object.rb', line 284

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_renderersObject



280
281
282
# File 'lib/engine/game_object.rb', line 280

def self.ui_renderers
  @cached_ui_renderers ||= []
end

.unregister_renderers(game_object) ⇒ Object



289
290
291
292
# File 'lib/engine/game_object.rb', line 289

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



267
268
269
270
271
272
273
274
# File 'lib/engine/game_object.rb', line 267

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



226
227
228
229
230
231
232
233
234
# File 'lib/engine/game_object.rb', line 226

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_componentsObject



80
81
82
# File 'lib/engine/game_object.rb', line 80

def all_components
  @all_components ||= @components + @renderers + @ui_renderers
end

#awakeObject



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
  # Set defaults
  @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

  # Normalize rotation (handle Numeric/Quaternion/Vector)
  @rotation = normalize_rotation(@rotation)
  @rotation_quaternion = Quaternion.from_euler(@rotation)

  # Normalize pos to ensure 3 components
  @pos = Vector[@pos[0], @pos[1], @pos[2] || 0]

  # Split components by type (handles both .create with mixed array and deserialization with separate arrays)
  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

#childrenObject



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



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/engine/game_object.rb', line 214

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



210
211
212
# File 'lib/engine/game_object.rb', line 210

def destroyed?
  @destroyed
end

#euler_anglesObject



117
118
119
# File 'lib/engine/game_object.rb', line 117

def euler_angles
  rotation.to_euler
end

#forwardObject



255
256
257
258
259
# File 'lib/engine/game_object.rb', line 255

def forward
  return @forward if @cached_forward_rotation == rotation
  @cached_forward_rotation = rotation.dup
  @forward = local_to_world_direction(Vector[0, 0, 1])
end

#local_to_world_coordinate(local) ⇒ Object



148
149
150
151
152
# File 'lib/engine/game_object.rb', line 148

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



160
161
162
# File 'lib/engine/game_object.rb', line 160

def local_to_world_direction(local)
  local_to_world_coordinate(local) - local_to_world_coordinate(Vector[0, 0, 0])
end

#model_matrixObject



174
175
176
177
178
179
180
# File 'lib/engine/game_object.rb', line 174

def model_matrix
  current_version = world_transform_version
  return @cached_world_matrix if @cached_world_version == current_version

  @cached_world_version = current_version
  @cached_world_matrix = compute_world_matrix
end

#rightObject



249
250
251
252
253
# File 'lib/engine/game_object.rb', line 249

def right
  return @right if @cached_right_rotation == rotation
  @cached_right_rotation = rotation.dup
  @right = local_to_world_direction(Vector[1, 0, 0])
end

#rotate_around(axis, angle) ⇒ Object



164
165
166
167
168
# File 'lib/engine/game_object.rb', line 164

def rotate_around(axis, angle)
  rotation_quaternion = Quaternion.from_angle_axis(angle, axis)

  self.rotation = rotation_quaternion * rotation
end

#to_sObject



63
64
65
# File 'lib/engine/game_object.rb', line 63

def to_s
  @name
end

#upObject



243
244
245
246
247
# File 'lib/engine/game_object.rb', line 243

def up
  return @up if @cached_up_rotation == rotation
  @cached_up_rotation = rotation.dup
  @up = local_to_world_direction(Vector[0, 1, 0])
end

#world_to_local_coordinate(world) ⇒ Object



154
155
156
157
158
# File 'lib/engine/game_object.rb', line 154

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

#world_transform_versionObject



170
171
172
# File 'lib/engine/game_object.rb', line 170

def world_transform_version
  @local_version + (@parent&.world_transform_version || 0)
end

#xObject



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

#yObject



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

#zObject



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