Class: SDC::Entity

Inherits:
Object
  • Object
show all
Extended by:
SDCMeta::ClassProperty
Includes:
SDCMeta::AIBackend
Defined in:
lib/core/Entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SDCMeta::ClassProperty

define_class_property

Methods included from SDCMeta::AIBackend

#add_ai_page, #ai_running?, #ai_script, #current_ai, #master_ai_running?, #master_ai_script, #setup_ai, #tick_ai

Constructor Details

#initialize(param: nil) ⇒ Entity

Actual instance methods which should not be changed



211
212
213
214
215
# File 'lib/core/Entity.rb', line 211

def initialize(param: nil)
	@param = param

	initialization_procedure
end

Instance Attribute Details

#active_spritesObject

Accessor methods for content arrays (except for textures)



25
26
27
# File 'lib/core/Entity.rb', line 25

def active_sprites
  @active_sprites
end

#boxesObject

Accessor methods for content arrays (except for textures)



25
26
27
# File 'lib/core/Entity.rb', line 25

def boxes
  @boxes
end

#hitshapesObject

Accessor methods for content arrays (except for textures)



25
26
27
# File 'lib/core/Entity.rb', line 25

def hitshapes
  @hitshapes
end

#hpObject

Accessor methods for living entities



33
34
35
# File 'lib/core/Entity.rb', line 33

def hp
  @hp
end

#hurtshapesObject

Accessor methods for content arrays (except for textures)



25
26
27
# File 'lib/core/Entity.rb', line 25

def hurtshapes
  @hurtshapes
end

#magic_numberObject (readonly)

Other accessor methods



37
38
39
# File 'lib/core/Entity.rb', line 37

def magic_number
  @magic_number
end

#positionObject

Physics-based accessor methods



29
30
31
# File 'lib/core/Entity.rb', line 29

def position
  @position
end

#shapesObject

Accessor methods for content arrays (except for textures)



25
26
27
# File 'lib/core/Entity.rb', line 25

def shapes
  @shapes
end

#spritesObject

Accessor methods for content arrays (except for textures)



25
26
27
# File 'lib/core/Entity.rb', line 25

def sprites
  @sprites
end

#velocityObject

Physics-based accessor methods



29
30
31
# File 'lib/core/Entity.rb', line 29

def velocity
  @velocity
end

Class Method Details

.add_box(index: nil, offset: SDC::Coordinates.new, origin: SDC::Coordinates.new, size: nil) ⇒ Object

Class methods for adding different objects to any entity



41
42
43
44
45
46
47
48
49
50
# File 'lib/core/Entity.rb', line 41

def self.add_box(index: nil, offset: SDC::Coordinates.new, origin: SDC::Coordinates.new, size: nil)
	if !size then
		raise("No size given for box with index #{index}")
	end

	@boxes = SDC::SpecialContainer.new if !@boxes
	new_box = SDC::CollisionShapeBox.new(offset, size)
	new_box.origin = origin
	@boxes.add(new_box, index)
end

.add_shape(index: nil, type: nil, offset: SDC::Coordinates.new, origin: SDC::Coordinates.new, radius: nil, size: nil, semiaxes: nil, direction: nil, side_a: nil, side_b: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/core/Entity.rb', line 52

def self.add_shape(index: nil, type: nil, offset: SDC::Coordinates.new, origin: SDC::Coordinates.new, radius: nil, size: nil, semiaxes: nil, direction: nil, side_a: nil, side_b: nil)
	@shapes = SDC::SpecialContainer.new if !@shapes
	shape = nil

	if type == SDC::CollisionShapePoint then
		shape = type.new(offset)
	elsif type == SDC::CollisionShapeLine then
		raise("Direction not defined for line shape with index #{index}") if !direction
		shape = type.new(offset, direction)
	elsif type == SDC::CollisionShapeCircle then
		raise("Radius not defined for circle shape with index #{index}") if !radius
		shape = type.new(offset, radius)
	elsif type == SDC::CollisionShapeBox then
		raise("Size not defined for box shape with index #{index}") if !size
		shape = type.new(offset, size)
	elsif type == SDC::CollisionShapeTriangle then
		raise("Undefined sides for triangle shape with index #{index}") if !side_a || !side_b
		shape = type.new(offset, side_a, side_b)
	elsif type == SDC::CollisionShapeQuadrangle then
		raise("Quadrangle shape not supported yet")	# TODO
		shape = type.new
	elsif type == SDC::CollisionShapeEllipse then
		raise("Ellipse shape not supported yet")	# TODO
		raise("Semiaxes not defined for ellipse shape with index #{index}") if !semiaxes
		shape = type.new(offset, axes)
	else
		raise("Unknown shape type #{type} for shape index #{index}")
	end

	shape.origin = origin

	@shapes.add(shape, index)
end

.add_sprite(index: nil, active: true, texture_index: nil, offset: SDC::Coordinates.new, origin: Coordinates.new, rect: nil) ⇒ Object



86
87
88
89
# File 'lib/core/Entity.rb', line 86

def self.add_sprite(index: nil, active: true, texture_index: nil, offset: SDC::Coordinates.new, origin: Coordinates.new, rect: nil)
	@sprites = SDC::SpecialContainer.new if !@sprites
	@sprites.add([texture_index, offset, origin, active, rect], index)
end

.all_boxesObject

Class getter methods for utility and code readability



111
112
113
114
# File 'lib/core/Entity.rb', line 111

def self.all_boxes
	@boxes = SDC::SpecialContainer.new if !@boxes
	return @boxes
end

.all_hitshapesObject



126
127
128
129
# File 'lib/core/Entity.rb', line 126

def self.all_hitshapes
	@hitshapes = SDC::SpecialContainer.new if !@hitshapes
	return @hitshapes
end

.all_hurtshapesObject



131
132
133
134
# File 'lib/core/Entity.rb', line 131

def self.all_hurtshapes
	@hurtshapes = SDC::SpecialContainer.new if !@hurtshapes
	return @hurtshapes
end

.all_shapesObject



116
117
118
119
# File 'lib/core/Entity.rb', line 116

def self.all_shapes
	@shapes = SDC::SpecialContainer.new if !@shapes
	return @shapes
end

.all_spritesObject



121
122
123
124
# File 'lib/core/Entity.rb', line 121

def self.all_sprites
	@sprites = SDC::SpecialContainer.new if !@sprites
	return @sprites
end

.entity_idObject



105
106
107
# File 'lib/core/Entity.rb', line 105

def self.entity_id
	return @entity_id
end

.register_id(index) ⇒ Object



101
102
103
# File 'lib/core/Entity.rb', line 101

def self.register_id(index)
	@entity_id = SDC::Data.add_entity(self, index: index)
end

.set_hitshape(index: nil, active: true, shape_index: nil, damage: 0, attributes: {}) ⇒ Object



91
92
93
94
# File 'lib/core/Entity.rb', line 91

def self.set_hitshape(index: nil, active: true, shape_index: nil, damage: 0, attributes: {})
	@hitshapes = SDC::SpecialContainer.new if !@hitshapes
	@hitshapes.add(SDC::Hitshape.new(damage: damage, shape_index: shape_index, active: active, attributes: attributes), index)
end

.set_hurtshape(index: nil, active: true, shape_index: nil, attributes: {}) ⇒ Object



96
97
98
99
# File 'lib/core/Entity.rb', line 96

def self.set_hurtshape(index: nil, active: true, shape_index: nil, attributes: {})
	@hurtshapes = SDC::SpecialContainer.new if !@hurtshapes
	@hurtshapes.add(SDC::Hurtshape.new(active: active, shape_index: shape_index, attributes: attributes), index)
end

Instance Method Details

#absolute_positionObject



299
300
301
# File 'lib/core/Entity.rb', line 299

def absolute_position
	return (@parent ? @position + @parent.absolute_position : @position)
end

#accelerate(vector) ⇒ Object



295
296
297
# File 'lib/core/Entity.rb', line 295

def accelerate(vector)
	@acceleration += vector
end

#at_entity_collision(other_entity, hurtshape, hitshape) ⇒ Object



437
438
439
# File 'lib/core/Entity.rb', line 437

def at_entity_collision(other_entity, hurtshape, hitshape)

end

#at_initObject

Custom routines which can be redefined for inherited objects



425
426
427
# File 'lib/core/Entity.rb', line 425

def at_init

end

#at_tile_collision(tile) ⇒ Object



441
442
443
# File 'lib/core/Entity.rb', line 441

def at_tile_collision(tile)

end

#basic_hit(hurtshape, hitshape) ⇒ Object



270
271
272
273
274
275
# File 'lib/core/Entity.rb', line 270

def basic_hit(hurtshape, hitshape)
	if @invincibility_frame_counter == 0 then
		total_damage = hitshape.damage
		inflict_damage(total_damage)
	end
end

#broadcast_magic_number(number) ⇒ Object



313
314
315
316
# File 'lib/core/Entity.rb', line 313

def broadcast_magic_number(number)
	@magic_number = number
	@children.each {|child| child.cascade_magic_number(number)}
end

#collided_with(&condition) ⇒ Object



411
412
413
# File 'lib/core/Entity.rb', line 411

def collided_with(&condition)
	return @last_collisions.index(&condition)
end

#collided_with_class(klass) ⇒ Object



415
416
417
# File 'lib/core/Entity.rb', line 415

def collided_with_class(klass)
	return @last_collisions.index {|e| e.is_a?(klass)}
end

#collided_with_property(property_symbol, value: true) ⇒ Object



419
420
421
# File 'lib/core/Entity.rb', line 419

def collided_with_property(property_symbol, value: true)
	return @last_collisions.index {|e| e.send(property_symbol) == value}
end

#collision_with_entity(other_entity, hurtshape, hitshape) ⇒ Object



401
402
403
404
405
406
407
408
409
# File 'lib/core/Entity.rb', line 401

def collision_with_entity(other_entity, hurtshape, hitshape)
	@last_collisions.push(other_entity)

	if self.living then
		basic_hit(hurtshape, hitshape)
	end

	at_entity_collision(other_entity, hurtshape, hitshape)
end

#custom_draw(window) ⇒ Object



433
434
435
# File 'lib/core/Entity.rb', line 433

def custom_draw(window)

end

#custom_physicsObject



266
267
268
# File 'lib/core/Entity.rb', line 266

def custom_physics

end

#custom_pre_physicsObject



262
263
264
# File 'lib/core/Entity.rb', line 262

def custom_pre_physics
	
end

#custom_sprite_draw(window, sprite) ⇒ Object



383
384
385
# File 'lib/core/Entity.rb', line 383

def custom_sprite_draw(window, sprite)
	
end

#custom_updateObject



429
430
431
# File 'lib/core/Entity.rb', line 429

def custom_update

end

#draw(window) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/core/Entity.rb', line 369

def draw(window)
	# This function draws each sprite at its designated position
	# The offset of the sprite position relative to the entity position is included

	0.upto(@sprites.size - 1) do |i|
		sprite = @sprites[i]
		next if !sprite || !@active_sprites[i]
		window.draw_translated(sprite, self.z, absolute_position)
		custom_sprite_draw(window, sprite)
	end

	custom_draw(window)
end

#each_colliding_action_shape(other_entity) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/core/Entity.rb', line 345

def each_colliding_action_shape(other_entity)
	return nil if magic_number == other_entity.magic_number

	other_position = other_entity.absolute_position

	0.upto(@hurtshapes.size - 1) do |hurtshape_index|
		hurtshape = @hurtshapes[hurtshape_index]
		next if !hurtshape || !hurtshape.active

		shape = @shapes[hurtshape.shape_index]

		0.upto(other_entity.hitshapes.size - 1) do |hitshape_index|
			hitshape = other_entity.hitshapes[hitshape_index]
			next if !hitshape || !hitshape.active

			other_shape = other_entity.shapes[hitshape.shape_index]

			if SDC::Collider.test(shape, absolute_position, other_shape, other_position) then
				yield hurtshape, hitshape
			end
		end
	end
end

#full_healObject



283
284
285
# File 'lib/core/Entity.rb', line 283

def full_heal
	@hp = self.max_hp
end

#inflict_damage(value) ⇒ Object



277
278
279
280
281
# File 'lib/core/Entity.rb', line 277

def inflict_damage(value)
	@hp -= value
	@hp = 0 if @hp <= 0
	@invincibility_next_frame = true
end

#initialization_procedureObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/core/Entity.rb', line 217

def initialization_procedure
	@parent = nil
	@children = []
	@position = SDC::Coordinates.new
	@velocity = SDC::Coordinates.new
	@acceleration = SDC::Coordinates.new

	setup_ai
	
	@last_collisions = []

	# Set a magic number to identify parent-child-structures
	@magic_number = self.object_id

	load_boxes
	load_shapes
	load_sprites
	load_hitshapes
	load_hurtshapes

	if self.living then
		full_heal
		@invincibility_frame_counter = 0
		@invincibility_next_frame = false
	end

	at_init
end

#living_procedureObject



287
288
289
290
291
292
293
# File 'lib/core/Entity.rb', line 287

def living_procedure
	@invincibility_frame_counter -= 1 if @invincibility_frame_counter > 0
	if @invincibility_next_frame then
		@invincibility_next_frame = false
		@invincibility_frame_counter = 60
	end
end

#load_boxesObject

Create local copies of all boxes/shapes/…



138
139
140
141
142
143
144
145
146
147
# File 'lib/core/Entity.rb', line 138

def load_boxes
	@boxes = []
	all_boxes = self.class.all_boxes

	0.upto(all_boxes.size - 1) do |i|
		element = all_boxes.get(i)

		@boxes[i] = element.dup if element
	end
end

#load_hitshapesObject



185
186
187
188
189
190
191
192
193
194
# File 'lib/core/Entity.rb', line 185

def load_hitshapes
	@hitshapes = []
	all_hitshapes = self.class.all_hitshapes

	0.upto(all_hitshapes.size - 1) do |i|
		element = all_hitshapes.get(i)

		@hitshapes[i] = element.dup if element
	end
end

#load_hurtshapesObject



196
197
198
199
200
201
202
203
204
205
# File 'lib/core/Entity.rb', line 196

def load_hurtshapes
	@hurtshapes = []
	all_hurtshapes = self.class.all_hurtshapes

	0.upto(all_hurtshapes.size - 1) do |i|
		element = all_hurtshapes.get(i)

		@hurtshapes[i] = element.dup if element
	end
end

#load_shapesObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/core/Entity.rb', line 149

def load_shapes
	@shapes = []
	all_shapes = self.class.all_shapes

	0.upto(all_shapes.size - 1) do |i|
		element = all_shapes.get(i)

		@shapes[i] = element.dup if element
	end
end

#load_spritesObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/core/Entity.rb', line 160

def load_sprites
	@sprites = []
	@active_sprites = []
	all_sprites = self.class.all_sprites

	0.upto(all_sprites.size - 1) do |i|
		element = all_sprites.get(i)

		# Again this time, the objects should not be copied to avoid useless memory consumption

		if element then
			texture_index = element[0]
			@sprites[i] = SDC::Sprite.new
			@sprites[i].position = element[1]
			@sprites[i].origin = element[2]
			@active_sprites[i] = element[3]

			if texture_index then
				@sprites[i].link_texture(SDC::Data.textures[texture_index])
			end

			@sprites[i].texture_rect = element[4] if element[4]
		end
	end

	def load_hitshapes
		@hitshapes = []
		all_hitshapes = self.class.all_hitshapes

		0.upto(all_hitshapes.size - 1) do |i|
			element = all_hitshapes.get(i)

			@hitshapes[i] = element.dup if element
		end
	end

	def load_hurtshapes
		@hurtshapes = []
		all_hurtshapes = self.class.all_hurtshapes

		0.upto(all_hurtshapes.size - 1) do |i|
			element = all_hurtshapes.get(i)

			@hurtshapes[i] = element.dup if element
		end
	end

end

#physicsObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/core/Entity.rb', line 246

def physics
	accelerate(SDC.game.gravity * self.gravity_multiplier)

	self.physics_split.times do
		custom_pre_physics

		@velocity += @acceleration * SDC.game.dt * self.physics_split_step
		@position += @velocity * SDC.game.dt * self.physics_split_step

		custom_physics
	end

	@acceleration.x = 0.0
	@acceleration.y = 0.0
end

#set_child(entity) ⇒ Object



308
309
310
311
# File 'lib/core/Entity.rb', line 308

def set_child(entity)
	@children.push(entity)
	entity.set_parent(self)
end

#set_parent(entity) ⇒ Object



303
304
305
306
# File 'lib/core/Entity.rb', line 303

def set_parent(entity)
	@parent = entity
	broadcast_magic_number(entity.magic_number)
end

#test_box_collision_with(other_entity) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/core/Entity.rb', line 318

def test_box_collision_with(other_entity)
	# Avoid collisions between identical entities and its children
	return nil if magic_number == other_entity.magic_number

	@boxes.each do |box|
		other_entity.boxes.each do |other_box|
			result = SDC::Collider.test(box, absolute_position, other_box, other_entity.absolute_position)
			return other_box if result
		end
	end

	return nil
end

#test_shape_collision_with(other_entity) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/core/Entity.rb', line 332

def test_shape_collision_with(other_entity)
	return nil if magic_number == other_entity.magic_number

	@shapes.each do |shape|
		other_entity.shapes.each do |other_shape|
			result = SDC::Collider.test(shape, absolute_position, other_shape, other_entity.absolute_position)
			return other_shape if result
		end
	end

	return nil
end

#updateObject



387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/core/Entity.rb', line 387

def update
	living_procedure if self.living

	custom_update

	if self.ai_active then
		tick_ai
	end

	physics if !@parent

	@last_collisions.clear
end