summaryrefslogtreecommitdiff
path: root/Scenes/Entities/Objects/Tree.gd
blob: 3d8d669b165742d50d70b8efcb5828afb627285f (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
extends StaticBody2D


@export_enum("up", "down", "left", "right") var fall_direction: String
@onready var map: TileMap = get_parent().get_parent().get_node("TileMap")


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():
	var direction = Vector2(0, 0)
	
	if fall_direction == "up":
		position.y -= 5
		direction = Vector2(0, -1)
	elif fall_direction == "down":
		rotate(deg_to_rad(180))
		position.y += 5
		direction = Vector2(0, 1)
	elif fall_direction == "left":
		rotate(deg_to_rad(-90))
		position.x -= 5
		direction = Vector2(-1, 0)
	elif fall_direction == "right":
		rotate(deg_to_rad(90))
		position.x += 5
		direction = Vector2(1, 0)
		
	for r in range(3):
		var vec = map.local_to_map(self.position + direction*16*r)
		map.set_cell(
			0,
			vec,
			6,
			map.get_cell_atlas_coords(0, vec),
			map.get_cell_alternative_tile(0, vec)
		)
	
	$Edges.process_mode = Node.PROCESS_MODE_INHERIT
	$CollisionShape2D.disabled = true
	$SpriteStanding.visible = false
	$SpriteFallen.visible = true