GodotComponentTest/scenes/RandomWalkWorld.gd

179 lines
4.8 KiB
GDScript

extends Spatial
var GameTile = load("res://utils/GameTile.gd")
var HexCell = preload("res://addons/gdhexgrid/HexCell.gd")
onready var HexGrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var IslandGenerator = preload("res://utils/IslandGenerator.gd").new()
onready var highlight = $Highlight
onready var tiles = $Tiles
var has_tiles_initialized = false
var tile_map = {}
var active_tile = null
var island_count = 5
export var size = Vector2(20, 20)
export var generate_flag: bool = false setget do_generate
func _ready():
assert(get_node("Tiles") != null)
generate()
pass
func _process(_delta):
pass
func add_tile_for_hex(game_tile):
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var tile = HexTile3D.instance()
var pos = hexgrid.get_hex_center_from_offset(game_tile.offset_coords)
var height = -2.0 # if game_tile.type == GameTile.TileType.Sand else 0.1 + rand_range(0.0, 0.05)
tiles.add_child(tile)
tile_map[game_tile.offset_coords] = tile
tile.set_tiletype(game_tile.type)
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.game_tile = game_tile
return tile
func reset():
var tiles = get_node("Tiles")
print ("clearing old tiles: " + str(tiles.get_child_count()))
for tile in tiles.get_children():
tiles.remove_child(tile)
tile.queue_free()
tile_map = {}
func generate():
reset()
generate_base_tiles()
for i in range (island_count):
generate_island(Vector2(
rand_range(-size.x / 2, size.x / 2),
rand_range(-size.y / 2, size.y / 2)
))
mark_sand_tiles()
# smooth_water_tiles()
# smooth_water_tiles()
# smooth_water_tiles()
# smooth_water_tiles()
func generate_base_tiles():
print ("generating world")
for i in range (-size.x / 2, size.x / 2):
for j in range (-size.y / 2, size.y / 2):
var tile = GameTile.new()
tile.type = GameTile.TileType.Water
tile.offset_coords = Vector2(i,j)
add_tile_for_hex(tile)
func generate_island(start_pos: Vector2):
var generated_tiles = IslandGenerator.random_walk(20, start_pos)
generated_tiles = IslandGenerator.extrude_tiles(generated_tiles)
# generated_tiles = IslandGenerator.mark_sand_tiles(generated_tiles)
for island_tile in generated_tiles.values():
if not island_tile.offset_coords in tile_map.keys():
continue
var world_tile = tile_map[island_tile.offset_coords]
world_tile.transform.origin[1] = 0
world_tile.set_tiletype(island_tile.type)
print ("Generated island with " + str(tiles.get_child_count()) + " nodes")
func mark_sand_tiles():
for tile in tile_map.values():
if tile.game_tile.type == GameTile.TileType.Water:
continue
tile.set_tiletype(GameTile.TileType.Grass if randi() % 2 else GameTile.TileType.DeepGrass)
var tile_neighbours = tile.game_tile.get_all_adjacent()
for neighbour in tile_neighbours:
var tile_on_map = neighbour.offset_coords in tile_map.keys()
if not tile_on_map:
continue
var neighbour_tile = tile_map[neighbour.offset_coords]
if neighbour_tile.game_tile.type == GameTile.TileType.Water:
tile.set_tiletype(GameTile.TileType.Sand)
func smooth_water_tiles():
# horizontal smoothing
for i in range ((-size.x / 2) + 1, (size.x / 2) - 1):
for j in range ((-size.y / 2) + 1, (size.y / 2) - 1):
var prev = tile_map[Vector2(i - 1,j)]
var current = tile_map[Vector2(i,j)]
var next = tile_map[Vector2(i + 1,j)]
if current.game_tile.type != GameTile.TileType.Water:
continue
current.transform.origin[1] = min(-0.2, (prev.transform.origin[1] + next.transform.origin[1]) * 0.5)
# vertical smoothings
for i in range ((-size.x / 2) + 1, (size.x / 2) - 1):
for j in range ((-size.y / 2) + 1, (size.y / 2) - 1):
var prev = tile_map[Vector2(i,j - 1)]
var current = tile_map[Vector2(i,j)]
var next = tile_map[Vector2(i,j + 1)]
if current.game_tile.type != GameTile.TileType.Water:
continue
current.transform.origin[1] = min(-0.2, (prev.transform.origin[1] + next.transform.origin[1]) * 0.5)
func do_generate(flag: bool):
print ("Generating Level")
generate_flag = false
# generate()
func _on_ClckArea_input_event(_camera, _event, position, _normal, _shape_idx):
var hex_at_position = HexGrid.get_hex_at(position)
var offset_coords = hex_at_position.offset_coords
var hex_center = HexGrid.get_hex_center_from_offset(offset_coords)
highlight.transform.origin = Vector3 (hex_center.x, 0.0, hex_center.y)
var world_tile = null
if offset_coords in tile_map.keys():
world_tile = tile_map[offset_coords]
if active_tile != null and world_tile != active_tile:
pass
if active_tile == world_tile:
return
if world_tile == null:
return
active_tile = world_tile
func _on_ClckArea_mouse_entered():
highlight.visible = true
func _on_ClckArea_mouse_exited():
highlight.visible = false