diff options
Diffstat (limited to 'Towers/Components/RangeComponent.gd')
-rw-r--r-- | Towers/Components/RangeComponent.gd | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/Towers/Components/RangeComponent.gd b/Towers/Components/RangeComponent.gd new file mode 100644 index 0000000..a2f7ae7 --- /dev/null +++ b/Towers/Components/RangeComponent.gd @@ -0,0 +1,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) |