49 lines
1.6 KiB
GDScript
49 lines
1.6 KiB
GDScript
extends Node2D
|
|
|
|
var mouse_down_position = Vector2()
|
|
|
|
func _unhandled_input(event):
|
|
if event is InputEventMouseButton:
|
|
if event.button_index == MOUSE_BUTTON_LEFT:
|
|
if event.pressed:
|
|
# Record the position where the mouse was pressed down
|
|
mouse_down_position = get_global_mouse_position()
|
|
else:
|
|
# Handle mouse button release
|
|
var pos = get_global_mouse_position()
|
|
var vol = pos - mouse_down_position
|
|
spawn(pos,vol)
|
|
|
|
func spawn(pos,vol):
|
|
var new_player = load("res://player.tscn").instantiate()
|
|
new_player.get_node("Body2D").position = pos
|
|
|
|
new_player.get_node("Body2D").apply_impulse(vol)
|
|
|
|
# Prepare the shape query parameters
|
|
var query_parameters = PhysicsShapeQueryParameters2D.new()
|
|
var collision_shape = new_player.get_node("Body2D/CollisionShape2D").shape
|
|
query_parameters.set_shape(collision_shape)
|
|
query_parameters.set_transform(Transform2D(0, pos))
|
|
query_parameters.set_collision_mask(1)
|
|
|
|
# Get the Physics2DDirectSpaceState for collision checking
|
|
var space_state = get_world_2d().direct_space_state
|
|
|
|
# Check for collision
|
|
var collision_results = space_state.intersect_shape(query_parameters)
|
|
if collision_results.size() == 0:
|
|
# No collision, safe to add child
|
|
add_child(new_player)
|
|
else:
|
|
for collision in collision_results:
|
|
#print
|
|
if collision.collider.get_parent() is Pool:
|
|
collision.collider.get_parent()._on_decay_timer_timeout()
|
|
return
|
|
var marker = Sprite2D.new()
|
|
marker.position = new_player.get_node("Body2D").position
|
|
marker.scale=Vector2(0.1,0.1)
|
|
marker.texture = load("res://images/crosshair.png")
|
|
add_child(marker)
|