GodotComponentTest/utils/IslandGenerator.gd

71 lines
1.8 KiB
GDScript
Raw Normal View History

2022-09-21 14:09:31 +02:00
class_name IslandGenerator
extends Node
var IslandTile = preload("res://utils/IslandTile.gd")
var HexCell = preload("res://addons/gdhexgrid/HexCell.gd")
func hex_cell_to_island_tile (hex: HexCell) -> IslandTile:
var tile = IslandTile.new()
tile.fromHexCell(hex)
return tile
func random_walk(walk_size):
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var tiles = {}
var current_offset_coordinate = Vector2(0, 0)
var current_tile = hex_cell_to_island_tile(hexgrid.get_hex_from_offset(Vector2(0,0)))
tiles[current_tile.offset_coords] = current_tile
for i in range(walk_size - 1):
var neighbours = current_tile.get_all_adjacent()
var tile_exists = true
while tile_exists and len(neighbours) > 0:
var test_index = randi() % len(neighbours)
var test_tile = hex_cell_to_island_tile(neighbours[test_index])
if test_tile.offset_coords in tiles.keys():
neighbours.remove(test_index)
else:
tile_exists = false
current_tile = test_tile
if len(neighbours) == 0:
break
tiles[current_tile.offset_coords] = current_tile
return tiles
func extrude_tiles(tiles):
var old_tiles = tiles
tiles = {}
for tile in old_tiles.values():
if not tile.offset_coords in tiles.keys():
tiles[tile.offset_coords] = tile
var neighbours = tile.get_all_adjacent()
for neighbour in neighbours:
if neighbour.offset_coords in tiles.keys():
continue
neighbour = hex_cell_to_island_tile(neighbour)
tiles[neighbour.offset_coords] = neighbour
return tiles
func mark_sand_tiles(tiles):
for tile in tiles.values():
tile.type = IslandTile.TileType.Grass if randi() % 2 else IslandTile.TileType.DeepGrass
var tile_neighbours = tile.get_all_adjacent()
for neighbour in tile_neighbours:
if not neighbour.offset_coords in tiles.keys():
tile.type = IslandTile.TileType.Sand
break
return tiles