129 lines
3.9 KiB
GDScript
129 lines
3.9 KiB
GDScript
extends Node3D
|
|
|
|
const WallCornerScene = preload ("../room/wall_corner.tscn")
|
|
const WallEdgeScene = preload ("../room/wall_edge.tscn")
|
|
|
|
var editing_door = null
|
|
|
|
var room1 = null
|
|
var room2 = null
|
|
var room1_corner1 = null
|
|
var room1_corner2 = null
|
|
var room2_corner1 = null
|
|
var room2_corner2 = null
|
|
|
|
@onready var ray_cast = $RayCast3D
|
|
|
|
func _ready():
|
|
pass
|
|
|
|
func is_editing():
|
|
return editing_door != null
|
|
|
|
func is_valid():
|
|
return room1 != null&&room2 != null&&room1_corner1 != null&&room1_corner2 != null&&room2_corner1 != null&&room2_corner2 != null
|
|
|
|
func edit(door):
|
|
editing_door = door
|
|
|
|
for room in House.body.get_rooms(0):
|
|
if door != null:
|
|
room.get_node("WallCollision/Clickable").on_click.connect(_add_corner.bind(room))
|
|
else:
|
|
room.get_node("WallCollision/Clickable").on_click.disconnect(_add_corner.bind(room))
|
|
|
|
func discard():
|
|
pass
|
|
|
|
func save():
|
|
pass
|
|
|
|
func _add_corner(event: EventPointer, room):
|
|
_match_connected_room(event.ray.get_collision_point(), event.ray.get_collision_normal() * - 1)
|
|
|
|
if room1_corner1 == null:
|
|
if ray_cast.get_collider() == null:
|
|
EventSystem.notify("Could not find connected room", EventNotify.Type.WARNING)
|
|
return
|
|
|
|
var connected_room = ray_cast.get_collider().get_parent()
|
|
|
|
room1 = room
|
|
room2 = connected_room
|
|
|
|
room1_corner1 = WallCornerScene.instantiate()
|
|
room1_corner1.global_position = event.ray.get_collision_point()
|
|
room1_corner1.get_node("Clickable").on_grab_move.connect(_move_corner.bind(room, room1_corner1))
|
|
add_child(room1_corner1)
|
|
|
|
room2_corner1 = WallCornerScene.instantiate()
|
|
room2_corner1.global_position = ray_cast.get_collision_point()
|
|
room2_corner1.get_node("Clickable").on_grab_move.connect(_move_corner.bind(connected_room, room2_corner1))
|
|
add_child(room2_corner1)
|
|
|
|
elif room1_corner2 == null:
|
|
if ray_cast.get_collider() == null:
|
|
EventSystem.notify("Could not find connected room", EventNotify.Type.WARNING)
|
|
return
|
|
|
|
if room1 != room:
|
|
EventSystem.notify("2nd corner must be in the same room", EventNotify.Type.WARNING)
|
|
return
|
|
|
|
var connected_room = ray_cast.get_collider().get_parent()
|
|
|
|
if room2 != connected_room:
|
|
EventSystem.notify("Connected rooms do not match", EventNotify.Type.WARNING)
|
|
return
|
|
|
|
room1_corner2 = WallCornerScene.instantiate()
|
|
room1_corner2.global_position = event.ray.get_collision_point()
|
|
room1_corner2.get_node("Clickable").on_grab_move.connect(_move_corner.bind(room, room1_corner2))
|
|
add_child(room1_corner2)
|
|
|
|
room2_corner2 = WallCornerScene.instantiate()
|
|
room2_corner2.global_position = ray_cast.get_collision_point()
|
|
room2_corner2.get_node("Clickable").on_grab_move.connect(_move_corner.bind(connected_room, room2_corner2))
|
|
add_child(room2_corner2)
|
|
|
|
func _move_corner(event: EventPointer, room, corner):
|
|
_match_room(event.ray.global_position, event.ray.to_global(event.ray.target_position) - event.ray.global_position)
|
|
|
|
if ray_cast.get_collider() == null||ray_cast.get_collider().get_parent() != room:
|
|
return
|
|
|
|
var collision_point = ray_cast.get_collision_point()
|
|
|
|
_match_connected_room(collision_point, ray_cast.get_collision_normal() * - 1)
|
|
|
|
var connected_collision_point = ray_cast.get_collision_point()
|
|
|
|
if ray_cast.get_collider() == null||ray_cast.get_collider().get_parent() != _get_connected_room(room):
|
|
return
|
|
|
|
corner.global_position = collision_point
|
|
_get_connected_corner(corner).global_position = connected_collision_point
|
|
|
|
func _get_connected_room(room):
|
|
return room1 if room == room2 else room2
|
|
|
|
func _get_connected_corner(corner):
|
|
match corner:
|
|
room1_corner1:
|
|
return room2_corner1
|
|
room1_corner2:
|
|
return room2_corner2
|
|
room2_corner1:
|
|
return room1_corner1
|
|
room2_corner2:
|
|
return room1_corner2
|
|
|
|
func _match_room(pos: Vector3, dir: Vector3):
|
|
ray_cast.global_position = pos
|
|
ray_cast.target_position = dir.normalized() * 1000
|
|
ray_cast.force_raycast_update()
|
|
|
|
func _match_connected_room(pos: Vector3, dir: Vector3):
|
|
ray_cast.global_position = pos
|
|
ray_cast.target_position = dir.normalized() * 1000
|
|
ray_cast.force_raycast_update() |