Hello, i need help with my destroing tool.I work on a 2D game, where u are an small astronaut and automate process,,,
I coded a destroing tool, with that u can destroy tiles in the tilemap.But 2 things dont work.
1.I only can destroy tiles that are under or at the right of the player
2. The laser beam(line2D+WorldEnviroment) glitch sometiems trought walls.
Here is the code i use for the tool and the beam:
extends Node2D
var process = 0
var pos = Vector2.ZERO
var collision_point = Vector2.ZERO
onready var tile_map = get_tree().get_root().get_node("World/World/cave")
func _process(delta):
if Input.is_action_pressed("place"):
pos = tile_map.world_to_map(get_global_mouse_position())
tile_map.set_cellv(pos,0)
tile_map.update_bitmask_area(pos)
elif Input.is_action_just_pressed("destroy"):
process = 0.1
else:
if Input.is_action_pressed("destroy"):
collision_point = to_local($RayCast2D.get_collision_point())
beam_to(get_local_mouse_position())
if $RayCast2D.is_colliding():
process -= 1 * delta
if process <= 0:
pos = tile_map.world_to_map($RayCast2D.get_collision_point() + $RayCast2D.get_collision_point().normalized() * 10)
tile_map.set_cellv(pos,-1)
tile_map.update_bitmask_area(pos)
process = 0.1
print(pos)
else:
$RayCast2D/Line2D.points[1] = Vector2(0,0)
func beam_to(position):
$RayCast2D.enabled = true
$RayCast2D.force_raycast_update()
$RayCast2D.set_cast_to(position)
if $RayCast2D.is_colliding():
$RayCast2D/Line2D.points[1] = self.tile_map.to_global(collision_point * 10 + collision_point.normalized() * 50)
else:
$RayCast2D/Line2D.points[1] = Vector2(0,0)
(sorry, the code format don't work ): )
Here is what the player tree looks like(ignor the particles):
