extends Node3D class_name EditTool @export var hero : Hero @export var inventory : Inventory @export var ray : RayCast3D @export var wall_preview : TowerBase @export var build_preview_material : StandardMaterial3D @export var progress_bar : TextureProgressBar var enabled := true var point_id := -1 var obstacle_last_point := -1 var valid_point := false var is_looking_at_tower_base := false var ray_collider var ray_point var interact_key_held := false var interacted_once := false var interact_held_time := 0.0 var interact_hold_time := 0.5 func _ready() -> void: wall_preview.set_material(build_preview_material) wall_preview.toggle_collision() func _process(delta: float) -> void: if !enabled: ray_collider = null ray_point = null wall_preview.set_visible(false) return if interact_key_held and !interacted_once and valid_point and hero.currency >= Data.wall_cost and ray.is_colliding() and Game.level.a_star_graph_3d.point_is_build_location(point_id): interact_held_time += delta set_progress_percent(interact_held_time / interact_hold_time) if interact_held_time >= interact_hold_time: set_progress_percent(0) interacted_once = true build_wall() if !interact_key_held: interact_held_time = 0.0 interacted_once = false set_progress_percent(0) point_id = -1 if ray.is_colliding(): if !interact_key_held: wall_preview.set_visible(true) ray_collider = ray.get_collider() ray_point = ray.get_collision_point() is_looking_at_tower_base = ray_collider is TowerBase if Game.level: point_id = Game.level.a_star_graph_3d.astar.get_closest_point(ray_point) if !Game.level.a_star_graph_3d.point_is_build_location(point_id) or hero.currency < Data.wall_cost: wall_preview.set_visible(false) else: var point_position = Game.level.a_star_graph_3d.astar.get_point_position(point_id) wall_preview.global_position = point_position wall_preview.global_rotation = Vector3.ZERO if obstacle_last_point != point_id: obstacle_last_point = point_id if Game.level.a_star_graph_3d.test_path_if_point_toggled(point_id): build_preview_material.albedo_color = Color.GREEN build_preview_material.albedo_color.a = 0.8 valid_point = true else: build_preview_material.albedo_color = Color.RED build_preview_material.albedo_color.a = 0.8 valid_point = false else: ray_collider = null ray_point = null is_looking_at_tower_base = false wall_preview.set_visible(false) func interact(): if ray_collider is TowerBase: var tower_base = ray_collider as TowerBase put_card_in_tower_base(tower_base) func build_wall(): if point_id >= 0 and valid_point and hero.currency >= Data.wall_cost: hero.currency -= Data.wall_cost Game.level.a_star_graph_3d.toggle_point(point_id) wall_preview.set_visible(false) func put_card_in_tower_base(tower_base: TowerBase): if tower_base.has_card: inventory.add(tower_base.remove_card()) else: tower_base.add_card(inventory.remove()) func set_progress_percent(value: float): progress_bar.value = progress_bar.max_value * value