GodotComponentTest/utils/LevelGenerator.gd

45 lines
1.3 KiB
GDScript

tool
extends Node
export var generate = false setget do_generate
export var level_size = 10
func generate_level(level_node):
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var num_tiles_x = level_size
var num_tiles_z = level_size
for i in range(-num_tiles_x / 2, num_tiles_x / 2):
for j in range (-num_tiles_z / 2, num_tiles_z / 2):
var pos = hexgrid.get_hex_center_from_offset(Vector2(i, j))
var tile = HexTile3D.instance()
var height = (sin(pos.y * 0.3) * sin(pos.y * 0.8) * 0.8 + cos ((pos.x) * 0.9) * 1.24) * 0.5 + 0.4 + rand_range(-0.3, 0.3)
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.connect("tile_selected", self, "on_tile_selected")
tile.connect("tile_mouse_entered", self, "on_tile_hover")
level_node.add_child(tile)
var pos = hexgrid.get_hex_center3(Vector3(0, 0, 0))
print (pos)
var hex = hexgrid.get_hex_at(pos)
print (hex)
print ("Generated level with " + str(level_node.get_child_count()) + " nodes")
func do_generate(flag: bool):
print ("flag: " + str(flag))
var tiles = get_node("../Tiles")
if tiles == null:
print ("Cannot generate: node '../Tiles' not found!")
return
for node in tiles.get_children():
tiles.remove_child(node)
node.queue_free()
generate_level(tiles)