Select/hover tetramino with Area3D signal
This commit is contained in:
@@ -5,6 +5,11 @@
|
||||
class_name Tetromino
|
||||
extends Node3D
|
||||
|
||||
signal hovered(tetromino)
|
||||
signal unhovered(tetromino)
|
||||
signal selected(tetromino, grid_pos)
|
||||
signal deselected(tetromino)
|
||||
|
||||
# Data
|
||||
@export var resource: TetrominoDefinition
|
||||
|
||||
@@ -28,15 +33,15 @@ var _grid_cells: PackedVector3Array = [] # Relative cell coordinates
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
#_initialize_material()
|
||||
#_set_mesh_representation()
|
||||
_initialize_material()
|
||||
_set_mesh_representation()
|
||||
pass
|
||||
|
||||
|
||||
## Initializes the material for visual representation.
|
||||
func _initialize_material() -> void:
|
||||
_material = StandardMaterial3D.new()
|
||||
_material.albedo_color = mesh_color
|
||||
_material.albedo_color = Color.RED
|
||||
_material.metallic = 0.3
|
||||
_material.roughness = 0.7
|
||||
|
||||
@@ -44,15 +49,9 @@ func _initialize_material() -> void:
|
||||
## Creates a simple mesh representation based on shape type.
|
||||
func _set_mesh_representation() -> void:
|
||||
_mesh_instance.mesh = resource.mesh
|
||||
#_mesh_instance.set_surface_override_material(0, _material)
|
||||
_mesh_instance.set_surface_override_material(0, _material)
|
||||
$SelectionArea/CollisionShape3D.shape = _mesh_instance.mesh.create_trimesh_shape()
|
||||
_grid_cells = resource.grid_cells
|
||||
|
||||
|
||||
## Creates a simple box mesh for tetromino visualization.
|
||||
func _create_box_mesh(width: float, height: float, depth: float) -> BoxMesh:
|
||||
var box = BoxMesh.new()
|
||||
box.size = Vector3(width, height, depth)
|
||||
return box
|
||||
|
||||
|
||||
## Gets the grid cells occupied by this tetromino at its current position.
|
||||
@@ -139,3 +138,26 @@ func _to_string() -> String:
|
||||
grid_position,
|
||||
_grid_cells.size()
|
||||
]
|
||||
|
||||
## Callback used to handle tetromino selection and deselection
|
||||
func _on_selection_area_input_event(camera: Node, event: InputEvent, event_position: Vector3, normal: Vector3, shape_idx: int) -> void:
|
||||
if event is InputEventMouseButton:
|
||||
if event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
||||
print("Tetromino selected")
|
||||
selected.emit(self, grid_position)
|
||||
elif event.pressed and event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
print("Tetromino released")
|
||||
deselected.emit(self)
|
||||
|
||||
|
||||
## Callback used to handle tetromino hovering
|
||||
func _on_selection_area_mouse_entered() -> void:
|
||||
print("Tetromino hovered")
|
||||
set_highlighted(true)
|
||||
hovered.emit(self)
|
||||
|
||||
## Callback used to handle tetromino unhovering
|
||||
func _on_selection_area_mouse_exited() -> void:
|
||||
print("Tetromino unhovered")
|
||||
set_highlighted(false)
|
||||
unhovered.emit(self)
|
||||
|
||||
@@ -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