GodotComponentTest/scenes/World.gd

175 lines
4.6 KiB
GDScript
Raw Normal View History

2022-10-07 16:14:05 +02:00
extends Spatial
var GameTile = load("res://utils/GameTile.gd")
var HexCell = preload("res://addons/gdhexgrid/HexCell.gd")
2022-10-08 14:14:20 +02:00
onready var HexGrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var IslandGenerator = preload("res://utils/IslandGenerator.gd").new()
2022-10-07 16:14:05 +02:00
2022-10-08 14:14:20 +02:00
onready var tiles = $Tiles
2022-10-07 16:14:05 +02:00
var has_tiles_initialized = false
2022-10-08 14:14:20 +02:00
var tile_map = {}
var active_tile = null
var island_count = 5
export var size = [50, 50]
export var generate_flag: bool = false setget do_generate
2022-10-07 16:14:05 +02:00
func _ready():
2022-10-08 14:14:20 +02:00
assert(get_node("Tiles") != null)
generate()
2022-10-07 16:14:05 +02:00
pass
2022-10-08 14:14:20 +02:00
func _process(_delta):
2022-10-07 16:14:05 +02:00
pass
2022-10-08 14:14:20 +02:00
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[0] / 2, size[0] / 2),
rand_range(-size[1] / 2, size[1] / 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[0] / 2, size[0] / 2):
for j in range (-size[1] / 2, size[1] / 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[0] / 2) + 1, (size[0] / 2) - 1):
for j in range ((-size[1] / 2) + 1, (size[1] / 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] = (prev.transform.origin[1] + next.transform.origin[1]) * 0.5
# vertical smoothings
for i in range ((-size[0] / 2) + 1, (size[0] / 2) - 1):
for j in range ((-size[1] / 2) + 1, (size[1] / 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] = (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 offset_coords = HexGrid.get_hex_at(position).offset_coords
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:
active_tile._on_Area_mouse_exited()
if active_tile == world_tile:
return
if world_tile == null:
return
active_tile = world_tile
active_tile._on_Area_mouse_entered()
func _on_ClckArea_mouse_entered():
pass # Replace with function body.
func _on_ClckArea_mouse_exited():
pass # Replace with function body.