class_name BuilderElement extends Node2D @export var element: Resource var instance: Tower var collision_shape: Shape2D var previous_position: Vector2 var collision_areas: Array[Area2D] = [] func _ready(): instance = element.instantiate() var element_sprite: Sprite2D = instance.get_node("Sprite2D") $Sprite2D.texture = element_sprite.texture $Sprite2D.region_enabled = element_sprite.region_enabled $Sprite2D.region_rect = element_sprite.region_rect $Sprite2D.scale = element_sprite.scale $Sprite2D.position = element_sprite.position var element_collision_shape = instance.get_node("CollisionShape2D").duplicate() as CollisionShape2D collision_shape = element_collision_shape.shape var tile_size = Client.current_stage.map.tile_set.tile_size var snapped_size = collision_shape.size.snapped(tile_size) for x in (snapped_size.x / tile_size.x): for y in (snapped_size.y / tile_size.y): var collision_visibility_area_scene = preload("res://UI/CollisionVisibilityArea.tscn") var collision_visibility_area = collision_visibility_area_scene.instantiate() collision_visibility_area.position = Vector2( x * tile_size.x, y * tile_size.y ) collision_visibility_area.set_collision_layer_value(3, true) collision_visibility_area.set_collision_mask_value(1, true) collision_visibility_area.set_collision_mask_value(2, true) collision_visibility_area.set_collision_mask_value(3, true) collision_visibility_area.set_collision_mask_value(4, true) collision_visibility_area.set_collision_mask_value(8, true) collision_visibility_area.colliding_color = Color(1, 0, 0, 0.5) collision_visibility_area.not_colliding_color = Color(0, 1, 0, 0.25) if Client.current_stage.has_method("get_builder_collision_masks"): for idx in Client.current_stage.get_builder_collision_masks(): collision_visibility_area.set_collision_mask_value(idx, true) var shape = RectangleShape2D.new() shape.size = Vector2(tile_size - Vector2i(1, 1)) var cshape = CollisionShape2D.new() cshape.shape = shape cshape.position = tile_size / 2 collision_visibility_area.add_child(cshape) add_child(collision_visibility_area) collision_areas.append(collision_visibility_area) func _draw(): draw_circle( Client.current_stage.map.tile_set.tile_size, 8 + instance.attack_range, Color(1, 1, 1, 0.75), false, 1.0 ) func _process(_delta): previous_position = global_position global_position = get_global_mouse_position().snapped(Client.current_stage.map.tile_set.tile_size) - Vector2(16,16) if global_position != previous_position: queue_redraw() # TODO: rpc to other peers # TODO: only as preview not as blocking to build func can_build(): for area in collision_areas: if area.get_overlapping_areas().size() > 0: return false if area.get_overlapping_bodies().size() > 0: return false #if Client.current_stage.has_method("can_build"): #return Client.current_stage.can_build() return true func _on_area_entered(_node) -> void: queue_redraw() func _on_area_exited(_node) -> void: queue_redraw()