summaryrefslogtreecommitdiff
path: root/Scenes/Entities/Objects/Box.gd
blob: 8d1d40b23546fa2dab25ae55e4f615b2a167f204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
extends StaticBody2D


func _ready():
	add_to_group("breakables")
	
	var collision_area = Utilities.Collision.Area.new(self, $CollisionShape2D, false)
	collision_area.connect("collided", Callable(self, "_collide"))
	add_child(collision_area)


func hit_by_explosion():
	$AnimationPlayer.play("breaking")
	await $AnimationPlayer.animation_finished
	
	if randi_range(1, 3) == 1:
		call_deferred("spawn_coin")
	
	queue_free()


func spawn_coin():
	var coin = preload("res://Scenes/Entities/Objects/Coin.tscn").instantiate()
	coin.position = self.position
	get_tree().get_current_scene().add_child(coin)


func _collide(area: Area2D):
	if area.is_in_group("explosions"):
		self.hit_by_explosion()