2021-08-20 23:53:09 +02:00
|
|
|
tool
|
|
|
|
extends Spatial
|
|
|
|
|
|
|
|
export(bool) var generate_on_ready : bool = true
|
|
|
|
export(PropData) var start_room : PropData
|
|
|
|
export(Array, PropData) var rooms : Array
|
|
|
|
export(bool) var generate : bool setget set_generate, get_generate
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
#todo calc aabbs and store in PropData during prop conversion
|
2021-08-27 01:05:03 +02:00
|
|
|
var room_hulls : Dictionary
|
2021-08-20 23:53:09 +02:00
|
|
|
var portal_map : Dictionary
|
2021-08-24 21:08:34 +02:00
|
|
|
var portals : Array
|
|
|
|
|
|
|
|
var current_aabbs : Array
|
2021-08-20 23:53:09 +02:00
|
|
|
|
|
|
|
var debug : bool = true
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
func _enter_tree() -> void:
|
2021-08-20 23:53:09 +02:00
|
|
|
if not Engine.editor_hint && generate_on_ready:
|
|
|
|
call_deferred("generate")
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
func set_up_room_data() -> void:
|
2021-08-20 23:53:09 +02:00
|
|
|
clear_room_data()
|
2021-08-24 21:08:34 +02:00
|
|
|
|
|
|
|
process_prop(start_room)
|
|
|
|
|
|
|
|
for r in rooms:
|
|
|
|
process_prop(r)
|
|
|
|
|
|
|
|
process_portals()
|
|
|
|
|
|
|
|
func process_prop(room : PropData) -> void:
|
|
|
|
if !room:
|
|
|
|
return
|
|
|
|
|
|
|
|
if !room.is_room:
|
|
|
|
return
|
|
|
|
|
2021-08-27 01:05:03 +02:00
|
|
|
if room in room_hulls:
|
2021-08-24 21:08:34 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
var ps : PoolVector3Array = room.room_bounds
|
2021-08-27 01:05:03 +02:00
|
|
|
|
|
|
|
#Convert the bounds to 2d
|
|
|
|
#This should probably be done in 3d later
|
|
|
|
#I found no simple way to do it for now
|
|
|
|
#Will look later
|
2021-08-24 21:08:34 +02:00
|
|
|
|
2021-08-27 01:05:03 +02:00
|
|
|
var points : PoolVector2Array = PoolVector2Array()
|
2021-08-24 21:08:34 +02:00
|
|
|
|
|
|
|
for p in ps:
|
2021-08-27 01:05:03 +02:00
|
|
|
points.push_back(Vector2(p.x, p.z))
|
|
|
|
|
|
|
|
points = Geometry.convex_hull_2d(points)
|
|
|
|
|
|
|
|
room_hulls[room] = points
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
for i in range(room.props.size()):
|
|
|
|
var pe : PropDataEntry = room.props[i]
|
|
|
|
|
|
|
|
if pe is PropDataPortal:
|
|
|
|
portals.append([pe, room, i])
|
|
|
|
|
|
|
|
|
|
|
|
func process_portals() -> void:
|
|
|
|
for i in range(portals.size()):
|
|
|
|
var pe = portals[i]
|
|
|
|
|
|
|
|
var portal : PropDataPortal = pe[0]
|
|
|
|
var room : PropData = pe[1]
|
|
|
|
|
|
|
|
var portal_points : PoolVector2Array = portal.points
|
|
|
|
|
|
|
|
if portal in portal_map:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var map_data : Array = Array()
|
|
|
|
|
|
|
|
for j in range(portals.size()):
|
|
|
|
if i == j:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var pp : Array = portals[j]
|
|
|
|
var cportal : PropDataPortal = pp[0]
|
|
|
|
|
|
|
|
var cportal_points : PoolVector2Array = cportal.points
|
|
|
|
|
|
|
|
if (cportal_points.size() != portal_points.size()):
|
|
|
|
continue
|
|
|
|
|
|
|
|
var eq : bool = true
|
|
|
|
|
|
|
|
for k in range(portal_points.size()):
|
|
|
|
var p1 : Vector2 = portal_points[k]
|
|
|
|
var p2 : Vector2 = cportal_points[k]
|
|
|
|
|
|
|
|
if !p1.is_equal_approx(p2):
|
|
|
|
eq = false
|
|
|
|
break
|
|
|
|
|
|
|
|
if !eq:
|
|
|
|
continue
|
|
|
|
|
|
|
|
var croom : PropData = pp[1]
|
|
|
|
var cj : int = pp[2]
|
|
|
|
|
|
|
|
map_data.append([ croom, cportal, cj ])
|
|
|
|
|
|
|
|
portal_map[portal] = map_data
|
|
|
|
|
2021-08-20 23:53:09 +02:00
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
func clear_room_data() -> void:
|
2021-08-20 23:53:09 +02:00
|
|
|
portal_map.clear()
|
2021-08-24 21:08:34 +02:00
|
|
|
portals.clear()
|
2021-08-27 01:05:03 +02:00
|
|
|
room_hulls.clear()
|
2021-08-24 21:08:34 +02:00
|
|
|
current_aabbs.clear()
|
2021-08-20 23:53:09 +02:00
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
func generate() -> void:
|
2021-08-20 23:53:09 +02:00
|
|
|
clear()
|
|
|
|
set_up_room_data()
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
spawn_room(Transform(), start_room)
|
|
|
|
|
2021-08-20 23:53:09 +02:00
|
|
|
|
2021-08-25 01:03:27 +02:00
|
|
|
func spawn_room(room_lworld_transform : Transform, room : PropData, level : int = 0, current_portal : PropDataPortal = null) -> void:
|
|
|
|
if level > 4:
|
2021-08-24 21:08:34 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
var sr : PropInstanceMerger = PropInstanceMerger.new()
|
2021-08-25 01:03:27 +02:00
|
|
|
sr.prop_data = room
|
2021-08-24 21:08:34 +02:00
|
|
|
add_child(sr)
|
2021-08-25 01:03:27 +02:00
|
|
|
|
2021-08-27 01:05:03 +02:00
|
|
|
var orig_room_lworld_transform : Transform = room_lworld_transform
|
|
|
|
|
2021-08-25 01:03:27 +02:00
|
|
|
if current_portal:
|
|
|
|
var lworld_curr_portal : Transform = current_portal.transform
|
|
|
|
#portal center should be precalculated
|
|
|
|
#this will only work with the current portals
|
|
|
|
lworld_curr_portal = lworld_curr_portal.translated(Vector3(-0.5, 0, 0))
|
|
|
|
lworld_curr_portal.basis = lworld_curr_portal.basis.rotated(Vector3(0, 1, 0), PI)
|
|
|
|
room_lworld_transform = room_lworld_transform * lworld_curr_portal.inverse()
|
|
|
|
|
|
|
|
sr.transform = room_lworld_transform
|
2021-08-24 21:08:34 +02:00
|
|
|
|
2021-08-27 01:05:03 +02:00
|
|
|
var cab : PoolVector2Array = room_hulls[room]
|
|
|
|
var ctfab : PoolVector2Array = PoolVector2Array()
|
|
|
|
|
|
|
|
for a in cab:
|
|
|
|
var v : Vector3 = Vector3(a.x, 0, a.y)
|
|
|
|
v = room_lworld_transform.xform(v)
|
|
|
|
ctfab.push_back(Vector2(v.x, v.z))
|
|
|
|
#v.y = 0
|
|
|
|
#test_spawn_pos(Transform(Basis(), v))
|
|
|
|
|
|
|
|
current_aabbs.push_back(ctfab)
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
if Engine.editor_hint and debug:
|
|
|
|
sr.owner = get_tree().edited_scene_root
|
|
|
|
|
|
|
|
for pe in room.props:
|
|
|
|
if pe is PropDataPortal:
|
|
|
|
if pe == current_portal:
|
|
|
|
continue
|
|
|
|
|
2021-08-25 01:03:27 +02:00
|
|
|
var current_portal_lworld_position : Transform = room_lworld_transform * pe.transform
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
var d : Array = portal_map[pe]
|
|
|
|
|
|
|
|
if d.size() == 0:
|
2021-08-20 23:53:09 +02:00
|
|
|
continue
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
randomize()
|
|
|
|
|
|
|
|
var new_room_data = d[randi() % d.size()]
|
|
|
|
|
|
|
|
#[ croom, cportal, cj ]
|
|
|
|
var new_room : PropData = new_room_data[0]
|
|
|
|
var new_room_portal : PropDataPortal = new_room_data[1]
|
|
|
|
|
|
|
|
#todo figure out the transforms
|
|
|
|
var poffset : Vector3 = new_room_portal.transform.xform(Vector3())
|
2021-08-25 01:03:27 +02:00
|
|
|
|
|
|
|
#middle of the current portal
|
|
|
|
var offset_current_portal_lworld_position : Transform = current_portal_lworld_position
|
|
|
|
#portal center should be precalculated
|
|
|
|
#this will only work with the current portals
|
|
|
|
offset_current_portal_lworld_position = offset_current_portal_lworld_position.translated(Vector3(-0.5, 0, 0))
|
2021-08-24 21:08:34 +02:00
|
|
|
|
2021-08-27 01:05:03 +02:00
|
|
|
var ab : PoolVector2Array = room_hulls[new_room]
|
|
|
|
var tfab : PoolVector2Array = PoolVector2Array()
|
2021-08-24 21:08:34 +02:00
|
|
|
|
2021-08-27 01:05:03 +02:00
|
|
|
for a in ab:
|
|
|
|
var v : Vector3 = Vector3(a.x, 0, a.y)
|
|
|
|
v = offset_current_portal_lworld_position.xform(v)
|
|
|
|
tfab.push_back(Vector2(v.x, v.z))
|
|
|
|
# v.y = 0
|
|
|
|
# test_spawn_pos(Transform(Basis(), v))
|
2021-08-24 21:08:34 +02:00
|
|
|
|
2021-08-27 01:05:03 +02:00
|
|
|
#test_spawn_pos(offset_current_portal_lworld_position)
|
2021-08-24 21:08:34 +02:00
|
|
|
|
2021-08-27 01:05:03 +02:00
|
|
|
var can_spawn : bool = true
|
|
|
|
for saab in current_aabbs:
|
|
|
|
var ohull : PoolVector2Array = saab
|
|
|
|
|
|
|
|
var poly_int_res : Array = Geometry.intersect_polygons_2d(ohull, tfab)
|
|
|
|
|
|
|
|
if poly_int_res.size() > 0:
|
|
|
|
for poly in poly_int_res:
|
|
|
|
var indices : PoolIntArray = Geometry.triangulate_polygon(poly)
|
|
|
|
|
|
|
|
for i in range(0, indices.size(), 3):
|
|
|
|
var p1 : Vector2 = poly[indices[i]]
|
|
|
|
var p2 : Vector2 = poly[indices[i + 1]]
|
|
|
|
var p3 : Vector2 = poly[indices[i + 2]]
|
|
|
|
|
|
|
|
var pp1 : float = (p1.x * p2.y + p2.x * p3.y + p3.x * p1.y)
|
|
|
|
var pp2 : float = (p2.x * p1.y + p3.x * p2.y + p1.x * p3.y)
|
|
|
|
var area : float = 0.5 * (pp1 - pp2)
|
|
|
|
|
|
|
|
if area > 0.2:
|
|
|
|
#print(area)
|
|
|
|
#print(poly)
|
|
|
|
#print(indices)
|
|
|
|
|
|
|
|
#for p in poly:
|
|
|
|
# test_spawn_pos(Transform(Basis(), Vector3(p.x, 0, p.y)))
|
|
|
|
|
|
|
|
can_spawn = false
|
|
|
|
break
|
|
|
|
|
|
|
|
if !can_spawn:
|
|
|
|
break
|
|
|
|
|
|
|
|
if !can_spawn:
|
|
|
|
break
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
if can_spawn:
|
2021-08-25 01:03:27 +02:00
|
|
|
spawn_room(offset_current_portal_lworld_position, new_room, level + 1, new_room_portal)
|
2021-08-24 21:08:34 +02:00
|
|
|
|
|
|
|
func clear() -> void:
|
|
|
|
if Engine.editor_hint and debug:
|
|
|
|
#don't destroy the user's nodes
|
|
|
|
for c in get_children():
|
|
|
|
if c.owner == get_tree().edited_scene_root:
|
|
|
|
c.queue_free()
|
2021-08-20 23:53:09 +02:00
|
|
|
else:
|
|
|
|
for c in get_children():
|
2021-08-24 21:08:34 +02:00
|
|
|
c.queue_free()
|
2021-08-20 23:53:09 +02:00
|
|
|
|
2021-08-27 01:05:03 +02:00
|
|
|
func test_spawn_pos(lworld_position : Transform, color : Color = Color(1, 1, 1)):
|
2021-08-25 01:03:27 +02:00
|
|
|
var testspat : ImmediateGeometry = ImmediateGeometry.new()
|
|
|
|
add_child(testspat)
|
|
|
|
testspat.transform = lworld_position
|
|
|
|
testspat.begin(Mesh.PRIMITIVE_LINES)
|
2021-08-27 01:05:03 +02:00
|
|
|
testspat.set_color(color)
|
2021-08-25 01:03:27 +02:00
|
|
|
testspat.add_vertex(Vector3(0, -0.5, 0))
|
2021-08-27 01:05:03 +02:00
|
|
|
testspat.set_color(color)
|
2021-08-25 01:03:27 +02:00
|
|
|
testspat.add_vertex(Vector3(0, 0.5, 0))
|
2021-08-27 01:05:03 +02:00
|
|
|
testspat.set_color(color)
|
2021-08-25 01:03:27 +02:00
|
|
|
testspat.add_vertex(Vector3(-0.5, 0, 0))
|
2021-08-27 01:05:03 +02:00
|
|
|
testspat.set_color(color)
|
2021-08-25 01:03:27 +02:00
|
|
|
testspat.add_vertex(Vector3(0.5, 0, 0))
|
2021-08-27 01:05:03 +02:00
|
|
|
testspat.set_color(color)
|
2021-08-25 01:03:27 +02:00
|
|
|
testspat.add_vertex(Vector3(0, 0, -0.5))
|
2021-08-27 01:05:03 +02:00
|
|
|
testspat.set_color(color)
|
2021-08-25 01:03:27 +02:00
|
|
|
testspat.add_vertex(Vector3(0, 0, 0.5))
|
|
|
|
testspat.end()
|
|
|
|
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
func set_generate(on) -> void:
|
2021-08-20 23:53:09 +02:00
|
|
|
if on:
|
|
|
|
generate()
|
|
|
|
|
2021-08-24 21:08:34 +02:00
|
|
|
func get_generate() -> bool:
|
2021-08-20 23:53:09 +02:00
|
|
|
return false
|