Improved world generation

master
Martin Felis 2021-07-09 21:57:11 +02:00
parent 611e93aa47
commit 2fd955c362
1 changed files with 21 additions and 18 deletions

View File

@ -51,7 +51,6 @@ func _process(_delta):
if len(player_navigation_path) > 1: if len(player_navigation_path) > 1:
var player_coord = Globals.WorldToHexCenter(PlayerChar.transform.origin) var player_coord = Globals.WorldToHexCenter(PlayerChar.transform.origin)
print (player_coord, " nav_path_0 ", player_navigation_path[0])
if (player_coord - player_navigation_path[0]).length_squared() < 0.1: if (player_coord - player_navigation_path[0]).length_squared() < 0.1:
player_navigation_path.remove(0) player_navigation_path.remove(0)
@ -60,16 +59,6 @@ func _process(_delta):
pass pass
func check_island_location_valid(new_island):
var islands = Islands.get_children()
for island in islands:
if island.check_overlap(new_island):
return true
return false
func get_tile_type(world_coord: Vector2): func get_tile_type(world_coord: Vector2):
for island in Islands.get_children(): for island in Islands.get_children():
var tile = island.get_tile_by_world_coord(world_coord) var tile = island.get_tile_by_world_coord(world_coord)
@ -87,7 +76,8 @@ func add_island_at(file_name, offset_world: Vector2):
func populate_ocean_grid(): func populate_ocean_grid():
Globals.OceanGrid.remove_obstacles(Globals.OceanGrid.get_obstacles()) var obstacles = Globals.OceanGrid.get_obstacles()
Globals.OceanGrid.remove_obstacles(obstacles.keys())
Globals.OceanGrid.set_bounds(Vector2.ONE * -500, Vector2.ONE * 500) Globals.OceanGrid.set_bounds(Vector2.ONE * -500, Vector2.ONE * 500)
for island in Islands.get_children(): for island in Islands.get_children():
@ -96,18 +86,31 @@ func populate_ocean_grid():
Globals.OceanGrid.add_obstacles(grid_coords) Globals.OceanGrid.add_obstacles(grid_coords)
func check_island_location_valid(new_island):
var grid_origin_world_coord = Globals.HexToWorld(Vector2(0,0))
if new_island.get_tile_by_world_coord(grid_origin_world_coord) != null:
return false
var islands = Islands.get_children()
for island in islands:
if island.check_overlap(new_island):
return false
return true
func generate(): func generate():
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
rng.randomize() rng.randomize()
clear_islands() clear_islands()
var radius = 1300 var radius = 800
var num_islands = 10 var num_islands = 10
for i in range (num_islands): for i in range (num_islands):
var island = Island.new() var island = Island.new()
var island_index = i % 4 var island_index = i % 2
var file_name = "user://pirate_game_island_" + str(island_index) + ".island" var file_name = "user://pirate_game_island_" + str(island_index) + ".island"
island.load_island(file_name) island.load_island(file_name)
@ -117,17 +120,17 @@ func generate():
var location_valid = check_island_location_valid(island) var location_valid = check_island_location_valid(island)
var overlap_retry_num = 0 var overlap_retry_num = 0
var overlap_retry_max = 10 var overlap_retry_max = 10
while location_valid and overlap_retry_num < overlap_retry_max: while !location_valid and overlap_retry_num < overlap_retry_max:
if overlap_retry_num % 4 == 0: if overlap_retry_num % 4 == 0:
radius = radius + 500 radius = radius + 200
overlap_retry_num = overlap_retry_num + 1 overlap_retry_num = overlap_retry_num + 1
rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius)) rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius))
island.offset_world = Globals.WorldToHexCenter(rand_coord) island.offset_world = Globals.WorldToHexCenter(rand_coord)
location_valid = check_island_location_valid(island) location_valid = check_island_location_valid(island)
if location_valid: if !location_valid:
print ("Could not place island!") print ("Could not place island! steps: " + str(overlap_retry_num))
else: else:
print ("Placed after " + str(overlap_retry_num) + " retries.") print ("Placed after " + str(overlap_retry_num) + " retries.")
Islands.add_child(island) Islands.add_child(island)