Advertisement
jamesonBradfield

DunMesh

Mar 1st, 2024 (edited)
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 6.28 KB | Software | 0 0
  1. @tool
  2. extends Node3D
  3. var room_script = load("res://dungeon generation/room_script.gd")
  4. @export var start : bool = false : set = set_start
  5. # @export var sort : bool = false : set = sortTiles
  6. @export var grid_map_path : NodePath
  7. @onready var grid_map : GridMap = get_node(grid_map_path)
  8. var total_distance_from_start : int = 0
  9. var room_parent_index : int = 0
  10. var parent_base : PackedScene = preload("res://dungeon generation/Scenes/parent_base.tscn")
  11. var dun_cell_scene : PackedScene = preload("res://dungeon generation/Scenes/DunCellBasicNoMat.tscn")
  12. var directions : Dictionary = {
  13.     "up" :Vector3i.FORWARD, "down" : Vector3i.BACK,
  14.     "left" : Vector3i.LEFT, "right" : Vector3i.RIGHT
  15. }
  16.  
  17. func set_start(_val:bool)->void:   
  18.     if Engine.is_editor_hint():
  19.         create_dungeon()
  20.     elif not Engine.is_editor_hint():
  21.         create_dungeon()
  22.  
  23. func handle_none(cell:Node3D,dir:String):
  24.     cell.call("remove_door_"+dir)
  25. func handle_00(cell:Node3D,dir:String):
  26.     cell.call("remove_wall_"+dir)
  27.     cell.call("remove_door_"+dir)
  28. func handle_01(cell:Node3D,dir:String):
  29.     cell.call("remove_door_"+dir)
  30. func handle_02(cell:Node3D,dir:String):
  31.     cell.call("remove_wall_"+dir)
  32.     cell.call("remove_door_"+dir)
  33. func handle_10(cell:Node3D,dir:String):
  34.     cell.call("remove_door_"+dir)
  35. func handle_11(cell:Node3D,dir:String):
  36.     cell.call("remove_wall_"+dir)
  37.     cell.call("remove_door_"+dir)
  38. func handle_12(cell:Node3D,dir:String):
  39.     cell.call("remove_wall_"+dir)
  40.     cell.call("remove_door_"+dir)
  41. func handle_20(cell:Node3D,dir:String):
  42.     cell.call("remove_wall_"+dir)
  43.     cell.call("remove_door_"+dir)
  44. func handle_21(cell:Node3D,dir:String):
  45.     cell.call("remove_wall_"+dir)
  46. func handle_22(cell:Node3D,dir:String):
  47.     cell.call("remove_wall_"+dir)
  48.     cell.call("remove_door_"+dir)
  49.  
  50. func create_dungeon():
  51.     set_self_scale(1)
  52.     room_parent_index = 0
  53.     print("create_dungeone has started.")
  54.     dungeon_data.room_nodes.clear()
  55.     for c in get_children():
  56.         remove_child(c)
  57.         c.queue_free()
  58.     var t : int = 0
  59.     var parent = add_area_to_dun_mesh()
  60.     var last_dun_cell
  61.     var room_index : int = 0
  62.     var hall_index : int = 0
  63.     var door_index : int = 0
  64.     for cell in grid_map.get_used_cells():
  65.         var cell_index : int = grid_map.get_cell_item(cell)
  66.         # cell id Table of contents
  67.         # ______________________________
  68.         #0.rooms
  69.         #1.hallway corridors.
  70.         #2.doors
  71.         #3.visual-border(red indicator for border size)
  72.         if cell_index <=2\
  73.         && cell_index >=0:
  74.             var dun_cell : Node3D = dun_cell_scene.instantiate()
  75.             dun_cell.position = Vector3(cell) + Vector3(0.5,0,0.5)
  76.             if cell_index == 1:
  77.                 dun_cell.name = "hall_" + str(hall_index)
  78.                 hall_index += 1
  79.                 add_child(dun_cell)
  80.                 dun_cell.set_owner(owner)
  81.             if cell_index == 0 || cell_index == 2:
  82.                 if cell_index == 2:
  83.                     dun_cell.name = "door_" + str(door_index)
  84.                     door_index += 1
  85.                 else:
  86.                     dun_cell.name = "room_tile_" + str(room_index)
  87.                     room_index += 1
  88.                 if last_dun_cell != null:
  89.                     var last_dun_cell_position = last_dun_cell.position
  90.                     var dun_cell_position = dun_cell.position
  91.                     if add_cells_to_new_room_node(dun_cell_position,last_dun_cell_position):
  92.                         parent = add_area_to_dun_mesh()
  93.                 parent.add_child(dun_cell)
  94.                 dun_cell.set_owner(owner)
  95.                 last_dun_cell = dun_cell
  96.             t+=1
  97.             for i in 4:
  98.                 var cell_n : Vector3i = cell + directions.values()[i]
  99.                 var cell_n_index : int = grid_map.get_cell_item(cell_n)
  100.                 if cell_n_index ==-1\
  101.                 || cell_n_index == 3:
  102.                     handle_none(dun_cell,directions.keys()[i])
  103.                 else:
  104.                     var key : String = str(cell_index) + str(cell_n_index)
  105.                     call("handle_"+key,dun_cell,directions.keys()[i])
  106.         if t%20 == 9 : await get_tree().create_timer(0).timeout
  107.     print("mesh generation is done. Moving on to room cleanup")
  108.     grid_map.visible = false
  109.     loop_room_nodes_and_find_bounds()
  110.     offset_room_children_by_new_parent_position()
  111.     remove_weird_rooms()
  112.     # set_self_scale(3)
  113.     dungeon_data.set_used_rooms_to_false()
  114.     dungeon_data.spawn_player_in_random_room()
  115.     # hide_each_room_for_debugging()
  116.  
  117.  
  118.  
  119. func _on_gun_gen_hallways_done():
  120.     print("mesh should generate")
  121.     set_start(true)
  122.  
  123.  
  124. func add_cells_to_new_room_node(dun_cell_position,last_dun_cell_position):
  125.     if dun_cell_position.distance_to(last_dun_cell_position) == 1
  126.         total_distance_from_start += dun_cell_position.distance_to(last_dun_cell_position)
  127.         return false
  128.     if floor(dun_cell_position.distance_to(last_dun_cell_position)) == total_distance_from_start:
  129.         print("total_distance_from_start : " + str(total_distance_from_start))
  130.         print("distance from last jump : " + str(floor(dun_cell_position.distance_to(last_dun_cell_position))))
  131.         total_distance_from_start = 0
  132.         return false
  133.     print(floor(dun_cell_position.distance_to(last_dun_cell_position)))
  134.     total_distance_from_start = 0
  135.     return true
  136.  
  137.  
  138. func add_area_to_dun_mesh():
  139.     var area = parent_base.instantiate()
  140.     add_child(area)
  141.     area.set_owner(owner)
  142.     area.name = "room_" + str(room_parent_index)
  143.     dungeon_data.room_nodes.append(area)
  144.     room_parent_index += 1
  145.     return area
  146.  
  147.  
  148. func hide_each_room_for_debugging():
  149.     for i in dungeon_data.room_nodes:
  150.         i.visible = false  
  151.     for i in dungeon_data.room_nodes:
  152.         await get_tree().create_timer(.25).timeout
  153.         i.visible = true
  154.  
  155.  
  156. func offset_room_children_by_new_parent_position():
  157.     for i in dungeon_data.room_nodes:
  158.         var new_parent_position = dungeon_data.find_room_nodes_center(i,1)
  159.         for c in i.get_children():
  160.             c.position = c.position - new_parent_position  
  161.         i.position = new_parent_position
  162.  
  163.  
  164. func remove_weird_rooms():
  165.         if dungeon_data.room_tiles.size() < dungeon_data.room_nodes.size():
  166.             var offending_room = dungeon_data.room_nodes[0]
  167.             var children = offending_room.get_children()
  168.             for c in children:
  169.                 c.reparent(dungeon_data.room_nodes[1])
  170.             dungeon_data.room_nodes.remove_at(0)
  171.             offending_room.free()
  172.  
  173.  
  174. func set_self_scale(self_scale):
  175.     self.scale.x = self_scale
  176.     self.scale.y = self_scale
  177.     self.scale.z = self_scale
  178.  
  179. #NOTE: THIS IS THE PROBLEM FUNCTION!!!!!!!!!!!!!!!!!
  180. func loop_room_nodes_and_find_bounds():
  181.     for c in dungeon_data.room_nodes.size():
  182.         dungeon_data.room_nodes[c].set_script(room_script)
  183.         var current_script = dungeon_data.room_nodes[c].get_script()
  184.         current_script.clear_room_purpose()
  185.         current_script.set_children_as_tiles()
  186.         current_script.create_trigger()
  187.         current_script.set_bounds_cast()
  188.        
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement