Right-click to delete a domino

main
Charlotte Som 2022-03-27 13:58:22 +01:00
parent 5165a2cd4f
commit 5df0507bf3
4 changed files with 36 additions and 13 deletions

View File

@ -8,6 +8,8 @@ var camera: Camera3D
var tool_active: bool = true var tool_active: bool = true
var raytrace_results: Dictionary = {}
func _ready(): func _ready():
domino = preload("res://Domino.tscn") domino = preload("res://Domino.tscn")
ghost_domino = preload("res://GhostDomino.tscn").instantiate() ghost_domino = preload("res://GhostDomino.tscn").instantiate()
@ -21,6 +23,7 @@ func _physics_process(delta):
remove_child(ghost_domino) remove_child(ghost_domino)
if not _is_active(): if not _is_active():
raytrace_results = {}
return return
var space_state = get_world_3d().direct_space_state var space_state = get_world_3d().direct_space_state
@ -35,17 +38,23 @@ func _physics_process(delta):
query.from = camera.position query.from = camera.position
query.to = mouse_3d_origin + mouse_3d_normal * 64 query.to = mouse_3d_origin + mouse_3d_normal * 64
var raytrace_results = space_state.intersect_ray(query) raytrace_results = space_state.intersect_ray(query)
if raytrace_results.has("position"): if not raytrace_results.is_empty():
ghost_domino.position = raytrace_results.position + Vector3.UP * 0.5 ghost_domino.position = raytrace_results.position + Vector3.UP * 0.5
if not ghost_domino.get_parent(): if not ghost_domino.get_parent():
add_child(ghost_domino) add_child(ghost_domino)
if raytrace_results.has("collider"):
var collider: CollisionObject3D = raytrace_results.collider
if self.is_ancestor_of(collider):
ghost_domino.global_transform = collider.global_transform
func _input(event): func _input(event):
if not _is_active(): if not _is_active():
return return
if event.is_action_pressed("use_tool") and ghost_domino.get_parent(): if event.is_action_pressed("place_domino") and ghost_domino.get_parent():
var new_domino: RigidDynamicBody3D = domino.instantiate() var new_domino: RigidDynamicBody3D = domino.instantiate()
new_domino.transform = ghost_domino.transform new_domino.transform = ghost_domino.transform
add_child(new_domino) add_child(new_domino)
@ -55,4 +64,10 @@ func _input(event):
if event.is_action_pressed("domino_rotate_anticlockwise"): if event.is_action_pressed("domino_rotate_anticlockwise"):
ghost_domino.rotate_y(PI / 12) ghost_domino.rotate_y(PI / 12)
if event.is_action_pressed("remove_domino"):
if raytrace_results.has("collider"):
var collider: Node = raytrace_results.collider
if self.is_ancestor_of(collider):
collider.get_parent().remove_child(collider)

View File

@ -12,7 +12,7 @@ backlight = Color(0.282353, 0.282353, 0.282353, 1)
[sub_resource type="BoxMesh" id="BoxMesh_n8mck"] [sub_resource type="BoxMesh" id="BoxMesh_n8mck"]
material = SubResource( "StandardMaterial3D_lep5x" ) material = SubResource( "StandardMaterial3D_lep5x" )
size = Vector3(0.1, 1, 0.5) size = Vector3(0.105, 1.005, 0.505)
[node name="GhostDomino" type="MeshInstance3D"] [node name="GhostDomino" type="MeshInstance3D"]
mesh = SubResource( "BoxMesh_n8mck" ) mesh = SubResource( "BoxMesh_n8mck" )

View File

@ -15,12 +15,8 @@ func _ready():
func _process(delta): func _process(delta):
var adjusting = Input.is_action_pressed("cam_adjust") var adjusting = Input.is_action_pressed("cam_adjust")
if Input.is_action_pressed("cam_pan_mod"): is_dragging_position = adjusting and Input.is_action_pressed("cam_pan_mod")
is_dragging_position = adjusting is_dragging_rotation = adjusting and Input.is_action_pressed("cam_rot_mod")
is_dragging_rotation = false
else:
is_dragging_position = false
is_dragging_rotation = adjusting
if is_dragging_position or is_dragging_rotation: if is_dragging_position or is_dragging_rotation:
Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED) Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)

View File

@ -24,19 +24,31 @@ node_naming/name_num_separator=2
cam_adjust={ cam_adjust={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":2,"pressed":false,"double_click":false,"script":null) "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":3,"pressed":false,"double_click":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":false,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":16777237,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
] ]
} }
cam_pan_mod={ cam_pan_mod={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"pressed":false,"keycode":16777237,"physical_keycode":0,"unicode":0,"echo":false,"script":null) "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":2,"pressed":false,"double_click":false,"script":null)
, Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":3,"pressed":false,"double_click":false,"script":null)
] ]
} }
use_tool={ cam_rot_mod={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"pressed":false,"double_click":false,"script":null) "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"pressed":false,"double_click":false,"script":null)
] ]
} }
place_domino={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"pressed":false,"double_click":false,"script":null)
]
}
remove_domino={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":2,"pressed":false,"double_click":false,"script":null)
]
}
domino_rotate_clockwise={ domino_rotate_clockwise={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":4,"pressed":false,"double_click":false,"script":null) "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":4,"pressed":false,"double_click":false,"script":null)