TurboTut

Untitled

Sep 21st, 2024
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D
  2. @onready var tile_map_layer = $TileMapLayer
  3.  
  4. var floor_tile := Vector2i(2,3)
  5. var wall_tile_bottom := Vector2i(0,1)
  6. var wall_tile_top := Vector2i(8,0)
  7.  
  8. # Constants defining the grid size, cell size, and room parameters
  9. const WIDTH = 80
  10. const HEIGHT = 60
  11. const CELL_SIZE = 10
  12. const MIN_ROOM_SIZE = 5
  13. const MAX_ROOM_SIZE = 10
  14. const MAX_ROOMS = 10
  15.  
  16. # Arrays to hold the grid data and the list of rooms
  17. var grid = []
  18. var rooms = []
  19.  
  20. # _ready is called when the node is added to the scene
  21. func _ready():
  22.     # Initialize the random number generator
  23.     randomize()
  24.     # Create the grid filled with walls
  25.     initialize_grid()
  26.     # Generate the dungeon by placing rooms and connecting them
  27.     generate_dungeon()
  28.     # Draw the dungeon on the screen
  29.     draw_dungeon()
  30.  
  31. # Initializes the grid with all cells set to walls (represented by 1)
  32. func initialize_grid():
  33.     for x in range(WIDTH):
  34.         grid.append([])  # Add a new row to the grid
  35.         for y in range(HEIGHT):
  36.             grid[x].append(1)  # Fill each cell in the row with 1 (wall)
  37.  
  38. # Main function to generate the dungeon by placing rooms and connecting them
  39. func generate_dungeon():
  40.     for i in range(MAX_ROOMS):
  41.         # Generate a room with random size and position
  42.         var room = generate_room()
  43.         # Attempt to place the room in the grid
  44.         if place_room(room):
  45.             # If this isn't the first room, connect it to the previous room
  46.             if rooms.size() > 0:
  47.                 connect_rooms(rooms[-1], room)  # Connect the new room to the last placed room
  48.             # Add the room to the list of rooms in the dungeon
  49.             rooms.append(room)
  50.  
  51. # Generates a room with random width, height, and position within the grid
  52. func generate_room():
  53.     # Determine room width and height randomly within the specified range
  54.     var width = randi() % (MAX_ROOM_SIZE - MIN_ROOM_SIZE + 1) + MIN_ROOM_SIZE
  55.     var height = randi() % (MAX_ROOM_SIZE - MIN_ROOM_SIZE + 1) + MIN_ROOM_SIZE
  56.     # Position the room randomly within the grid, ensuring it fits within the boundaries
  57.     var x = randi() % (WIDTH - width - 1) + 1
  58.     var y = randi() % (HEIGHT - height - 1) + 1
  59.     # Return the room as a Rect2 object representing its position and size
  60.     return Rect2(x, y, width, height)
  61.  
  62. # Attempts to place the room on the grid, ensuring no overlap with existing rooms
  63. func place_room(room):
  64.     # Check if the room overlaps with any existing floors (cells set to 0)
  65.     for x in range(room.position.x, room.end.x):
  66.         for y in range(room.position.y, room.end.y):
  67.             if grid[x][y] == 0:  # If the cell is already a floor
  68.                 return false  # Room cannot be placed, return false
  69.    
  70.     # If no overlap is found, mark the room area as floors (set cells to 0)
  71.     for x in range(room.position.x, room.end.x):
  72.         for y in range(room.position.y, room.end.y):
  73.             grid[x][y] = 0  # 0 represents a floor
  74.     return true  # Room successfully placed, return true
  75.  
  76. # Connects two rooms with a corridor, allowing for a customizable corridor width
  77. func connect_rooms(room1, room2, corridor_width=1):
  78.     # Determine the starting point for the corridor (center of room1)
  79.     var start = Vector2(
  80.         int(room1.position.x + room1.size.x / 2),
  81.         int(room1.position.y + room1.size.y / 2)
  82.     )
  83.     # Determine the ending point for the corridor (center of room2)
  84.     var end = Vector2(
  85.         int(room2.position.x + room2.size.x / 2),
  86.         int(room2.position.y + room2.size.y / 2)
  87.     )
  88.    
  89.     var current = start
  90.    
  91.     # First, move horizontally towards the end point
  92.     while current.x != end.x:
  93.         # Move one step left or right
  94.         current.x += 1 if end.x > current.x else -1
  95.         # Create a corridor with the specified width
  96.         for i in range(-int(corridor_width / 2), int(corridor_width / 2) + 1):
  97.             for j in range(-int(corridor_width / 2), int(corridor_width / 2) + 1):
  98.                 # Ensure we don't go out of grid bounds
  99.                 if current.y + j >= 0 and current.y + j < HEIGHT and current.x + i >= 0 and current.x + i < WIDTH:
  100.                     grid[current.x + i][current.y + j] = 0  # Set cells to floor
  101.  
  102.     # Then, move vertically towards the end point
  103.     while current.y != end.y:
  104.         # Move one step up or down
  105.         current.y += 1 if end.y > current.y else -1
  106.         # Create a corridor with the specified width
  107.         for i in range(-int(corridor_width / 2), int(corridor_width / 2) + 1):
  108.             for j in range(-int(corridor_width / 2), int(corridor_width / 2) + 1):
  109.                 # Ensure we don't go out of grid bounds
  110.                 if current.x + i >= 0 and current.x + i < WIDTH and current.y + j >= 0 and current.y + j < HEIGHT:
  111.                     grid[current.x + i][current.y + j] = 0  # Set cells to floor
  112.  
  113. # Draws the dungeon on the screen by creating visual representations of the grid
  114. func draw_dungeon():
  115.     # Loop through each cell in the grid
  116.     for x in range(WIDTH):
  117.         for y in range(HEIGHT):
  118.             var tile_position = Vector2i(x, y)
  119.             if grid[x][y] == 0:
  120.                 tile_map_layer.set_cell(tile_position,0,floor_tile)
  121.             elif grid[x][y] == 1:
  122.                 if y < HEIGHT - 1 and grid[x][y + 1] == 0:
  123.                     tile_map_layer.set_cell(tile_position, 0, wall_tile_bottom)
  124.                 elif y > 0 and grid[x][y - 1] == 0:
  125.                     tile_map_layer.set_cell(tile_position, 0, wall_tile_top)
  126.                 else:
  127.                     tile_map_layer.set_cell(tile_position, 0, Vector2i(-1, -1))
  128.             else:
  129.                 tile_map_layer.set_cell(tile_position, 0, Vector2i(-1, -1))
  130.  
Advertisement
Add Comment
Please, Sign In to add comment