summaryrefslogtreecommitdiff
path: root/Towers/Components/RangeComponent.gd
blob: a2f7ae7245506a4a5a243207ad2d0fde861b2101 (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
49
50
51
52
class_name RangeTowerComponent
extends TowerComponent


signal range_changed
@warning_ignore("shadowed_global_identifier")
var range := 0:
	set(value):
		range = value
		range_changed.emit()

var area := Area2D.new()
var collision_shape := CollisionShape2D.new()
var shape := CircleShape2D.new()

var units_in_range: Array[Unit]


func _init() -> void:
	type = ComponentType.Range
	set_sprite(preload("res://Towers/Components/Assets/range-component.png"))
	
	update_range()
	level_changed.connect(update_range)


func update_range():
	var tile_size = Client.current_stage.map.tile_set.tile_size.x
	@warning_ignore("integer_division")
	range = (tile_size * 2) + ((tile_size / 2) * level)
	shape.radius = range


func on_add(tower: Tower):
	super.on_add(tower)
	
	area.set_collision_layer_value(1, false)
	area.set_collision_mask_value(1, true)
	
	area.body_entered.connect(func(body: Node2D):
		units_in_range.append(body)
	)
	area.body_exited.connect(func(body: Node2D):
		units_in_range.erase(body)
	)
	
	area.position = tower.get_node("CenterAnchor").position
	
	collision_shape.shape = shape
	area.add_child(collision_shape)
	
	tower.add_child(area)