72 lines
2.3 KiB
Plaintext
72 lines
2.3 KiB
Plaintext
extends Node3D
|
|
class_name LevelGenerator
|
|
|
|
var levelGrid: Array[Array]
|
|
@export var gridSize: int = 250
|
|
|
|
var spawnableRooms: Array[RoomData]
|
|
|
|
@export var testTexture: CompressedTexture2D
|
|
|
|
var roomLoader: RoomImageLoader = RoomImageLoader.new()
|
|
|
|
func _ready() -> void:
|
|
for x in gridSize:
|
|
var newRow: Array
|
|
levelGrid.push_back(newRow)
|
|
for y in gridSize:
|
|
var newCell := GridCell.new()
|
|
levelGrid[x].push_back(newCell)
|
|
|
|
addArrays2D(levelGrid,roomLoader.loadRoomData(testTexture.get_image()))
|
|
rotateArray2D(levelGrid,4)
|
|
printLevelGrid()
|
|
|
|
|
|
func addGridCells(cell1: GridCell,cell2: GridCell) -> GridCell:
|
|
var returnCell: GridCell = GridCell.new()
|
|
returnCell.spaceTaken = cell1.spaceTaken or cell2.spaceTaken
|
|
returnCell.door = cell2.door
|
|
returnCell.doorOrientation = cell2.doorOrientation
|
|
returnCell.biome = cell1.biome
|
|
return returnCell
|
|
func addArrays2D(array1: Array[Array], array2: Array[Array], arr2pos: Vector2i = Vector2i(0,0)) -> void:
|
|
if array1.size() < (arr2pos.x - 1) + array2.size(): return
|
|
if array1[arr2pos.y].size() < (arr2pos.y - 1) + array2[0].size(): return
|
|
for x in array2.size():
|
|
for y in array2[x].size():
|
|
array1[x+arr2pos.x][y+arr2pos.y] = addGridCells(array1[x+arr2pos.x][y+arr2pos.y],array2[x][y])
|
|
func rotateArray2D(array: Array[Array], numberOfRotationsBy90Degrees: int = 1) -> void:
|
|
var size: int = array.size()
|
|
var layerCount: int = size/2
|
|
|
|
for x in numberOfRotationsBy90Degrees%4:
|
|
for layer in layerCount:
|
|
var first: int = layer
|
|
var last: int = size - first - 1
|
|
|
|
for element in range(first, last):
|
|
var offset = element - first
|
|
|
|
var top = array[first][element]
|
|
var right = array[element][last]
|
|
var bot = array[last][last-offset]
|
|
var left = array[last-offset][first]
|
|
|
|
array[element][last] = top
|
|
array[last][last-offset] = right
|
|
array[last-offset][first] = bot
|
|
array[first][element] = left
|
|
func addObject(AddedObject:PackedScene, Parent: Node3D, Position: Vector3, Rotation: Vector3= Vector3(0,0,0)):
|
|
var obj = AddedObject.instantiate()
|
|
Parent.add_child(obj)
|
|
obj.position = Position
|
|
obj.rotation = Rotation
|
|
return obj
|
|
func printLevelGrid() -> void:
|
|
var debugCube: PackedScene = preload("res://test/debugCube.tscn")
|
|
for x in levelGrid.size():
|
|
for y in levelGrid[x].size():
|
|
if levelGrid[x][y].spaceTaken:
|
|
addObject(debugCube,self,Vector3(x,0,y))
|