GodotComponentTest/utils/IslandGenerator.gd

77 lines
1.9 KiB
GDScript
Raw Normal View History

2022-09-21 14:09:31 +02:00
class_name IslandGenerator
extends Node
2022-10-07 16:14:05 +02:00
var GameTile = load("res://utils/GameTile.gd")
2022-09-21 14:09:31 +02:00
var HexCell = preload("res://addons/gdhexgrid/HexCell.gd")
2022-10-08 14:14:20 +02:00
var rng = RandomNumberGenerator.new()
func _ready():
rng.randomize()
2022-09-21 14:09:31 +02:00
2022-10-07 16:14:05 +02:00
func hex_cell_to_island_tile (hex: HexCell) -> GameTile:
var tile = GameTile.new()
2022-09-21 14:09:31 +02:00
tile.fromHexCell(hex)
return tile
2022-10-07 16:14:05 +02:00
2022-09-21 14:09:31 +02:00
2022-10-08 14:14:20 +02:00
func random_walk(walk_size, start_offset = Vector2(0,0)):
2022-09-21 14:09:31 +02:00
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var tiles = {}
2022-10-08 14:14:20 +02:00
var current_tile = hex_cell_to_island_tile(hexgrid.get_hex_from_offset(start_offset))
2022-09-21 14:09:31 +02:00
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:
2022-10-08 14:14:20 +02:00
var test_index = rng.randi() % len(neighbours)
2022-09-21 14:09:31 +02:00
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
2022-10-07 16:14:05 +02:00
2022-09-21 14:09:31 +02:00
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():
2022-10-08 14:14:20 +02:00
tile.type = GameTile.TileType.Grass if rng.randi() % 2 else GameTile.TileType.DeepGrass
2022-09-21 14:09:31 +02:00
var tile_neighbours = tile.get_all_adjacent()
for neighbour in tile_neighbours:
if not neighbour.offset_coords in tiles.keys():
2022-10-07 16:14:05 +02:00
tile.type = GameTile.TileType.Sand
2022-09-21 14:09:31 +02:00
break
return tiles