Select/hover tetramino with Area3D signal
This commit is contained in:
@@ -33,34 +33,34 @@ func _ready() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton:
|
||||
if event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
||||
_handle_left_click()
|
||||
elif event.pressed and event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
_handle_right_click()
|
||||
|
||||
if _selected_tetromino and event is InputEventMouseMotion:
|
||||
_update_ghost_position()
|
||||
|
||||
if _selected_tetromino:
|
||||
if event.is_action_pressed("ui_select"): # Spacebar
|
||||
_rotate_ghost()
|
||||
elif event.is_action_pressed("ui_cancel"): # ESC
|
||||
_cancel_selection()
|
||||
#func _input(event: InputEvent) -> void:
|
||||
#if event is InputEventMouseButton:
|
||||
#if event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
||||
#_handle_left_click()
|
||||
#elif event.pressed and event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
#_handle_right_click()
|
||||
#
|
||||
#if _selected_tetromino and event is InputEventMouseMotion:
|
||||
#_update_ghost_position()
|
||||
#
|
||||
#if _selected_tetromino:
|
||||
#if event.is_action_pressed("ui_select"): # Spacebar
|
||||
#_rotate_ghost()
|
||||
#elif event.is_action_pressed("ui_cancel"): # ESC
|
||||
#_cancel_selection()
|
||||
|
||||
|
||||
## Handles left mouse click for selection or placement.
|
||||
func _handle_left_click() -> void:
|
||||
if _selected_tetromino:
|
||||
# Try to place the ghost tetromino
|
||||
_place_selection()
|
||||
return
|
||||
|
||||
# Try to select a tetromino
|
||||
var tetromino = _raycast_tetromino()
|
||||
if tetromino:
|
||||
_select_tetromino(tetromino)
|
||||
### Handles left mouse click for selection or placement.
|
||||
#func _handle_left_click() -> void:
|
||||
#if _selected_tetromino:
|
||||
## Try to place the ghost tetromino
|
||||
#_place_selection()
|
||||
#return
|
||||
#
|
||||
## Try to select a tetromino
|
||||
#var tetromino = _raycast_tetromino()
|
||||
#if tetromino:
|
||||
#_select_tetromino(tetromino)
|
||||
|
||||
|
||||
## Handles right mouse click to deselect.
|
||||
@@ -69,34 +69,34 @@ func _handle_right_click() -> void:
|
||||
_cancel_selection()
|
||||
|
||||
|
||||
## Raycasts from mouse position to find a tetromino.
|
||||
func _raycast_tetromino() -> Tetromino:
|
||||
if not camera:
|
||||
return null
|
||||
|
||||
var mouse_pos = get_viewport().get_mouse_position()
|
||||
var from = camera.project_ray_origin(mouse_pos)
|
||||
var to = from + camera.project_ray_normal(mouse_pos) * 10000.0
|
||||
|
||||
var space_state = get_world_3d().direct_space_state
|
||||
var collision_mask = 0xFFFFFFFF # Hit all layers
|
||||
var query = PhysicsRayQueryParameters3D.create(from, to, collision_mask)
|
||||
query.collide_with_areas = true
|
||||
|
||||
var result = space_state.intersect_ray(query)
|
||||
|
||||
if result and result.get("collider"):
|
||||
var collider = result["collider"]
|
||||
# Walk up the node tree to find a Tetromino
|
||||
var node = collider
|
||||
while node:
|
||||
if node is Tetromino:
|
||||
return node
|
||||
node = node.get_parent()
|
||||
else:
|
||||
print_debug("Raycast hit nothing")
|
||||
|
||||
return null
|
||||
### Raycasts from mouse position to find a tetromino.
|
||||
#func _raycast_tetromino() -> Tetromino:
|
||||
#if not camera:
|
||||
#return null
|
||||
#
|
||||
#var mouse_pos = get_viewport().get_mouse_position()
|
||||
#var from = camera.project_ray_origin(mouse_pos)
|
||||
#var to = from + camera.project_ray_normal(mouse_pos) * 10000.0
|
||||
#
|
||||
#var space_state = get_world_3d().direct_space_state
|
||||
#var collision_mask = 0xFFFFFFFF # Hit all layers
|
||||
#var query = PhysicsRayQueryParameters3D.create(from, to, collision_mask)
|
||||
#query.collide_with_areas = true;
|
||||
#
|
||||
#var result = space_state.intersect_ray(query)
|
||||
#
|
||||
#if result and result.get("collider"):
|
||||
#var collider = result["collider"]
|
||||
## Walk up the node tree to find a Tetromino
|
||||
#var node = collider
|
||||
#while node:
|
||||
#if node is Tetromino:
|
||||
#return node
|
||||
#node = node.get_parent()
|
||||
#else:
|
||||
#print_debug("Raycast hit nothing")
|
||||
#
|
||||
#return null
|
||||
|
||||
|
||||
## Selects a tetromino and converts it to ghost mode.
|
||||
|
||||
Reference in New Issue
Block a user