77 lines
1.9 KiB
GDScript
77 lines
1.9 KiB
GDScript
class_name IslandGenerator
|
|
extends Node
|
|
|
|
var GameTile = load("res://utils/GameTile.gd")
|
|
var HexCell = preload("res://addons/gdhexgrid/HexCell.gd")
|
|
var rng = RandomNumberGenerator.new()
|
|
|
|
|
|
func _ready():
|
|
rng.randomize()
|
|
|
|
|
|
func hex_cell_to_island_tile (hex: HexCell) -> GameTile:
|
|
var tile = GameTile.new()
|
|
tile.fromHexCell(hex)
|
|
return tile
|
|
|
|
|
|
func random_walk(walk_size, start_offset = Vector2(0,0)):
|
|
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
|
|
var tiles = {}
|
|
|
|
var current_tile = hex_cell_to_island_tile(hexgrid.get_hex_from_offset(start_offset))
|
|
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 = rng.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 = GameTile.TileType.Grass if rng.randi() % 2 else GameTile.TileType.DeepGrass
|
|
var tile_neighbours = tile.get_all_adjacent()
|
|
|
|
for neighbour in tile_neighbours:
|
|
if not neighbour.offset_coords in tiles.keys():
|
|
tile.type = GameTile.TileType.Sand
|
|
break
|
|
|
|
return tiles
|