GodotComponentTest/scenes/World.gd

179 lines
4.8 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-09 19:36:08 +02:00
onready var highlight = $Highlight
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
2022-10-09 19:36:08 +02:00
export var size = Vector2(20, 20)
2022-10-08 14:14:20 +02:00
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(
2022-10-09 19:36:08 +02:00
rand_range(-size.x / 2, size.x / 2),
rand_range(-size.y / 2, size.y / 2)
2022-10-08 14:14:20 +02:00
))
mark_sand_tiles()
smooth_water_tiles()
smooth_water_tiles()
smooth_water_tiles()
smooth_water_tiles()
func generate_base_tiles():
print ("generating world")
2022-10-09 19:36:08 +02:00
for i in range (-size.x / 2, size.x / 2):
for j in range (-size.y / 2, size.y / 2):
2022-10-08 14:14:20 +02:00
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
2022-10-09 19:36:08 +02:00
for i in range ((-size.x / 2) + 1, (size.x / 2) - 1):
for j in range ((-size.y / 2) + 1, (size.y / 2) - 1):
2022-10-08 14:14:20 +02:00
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
2022-10-09 19:36:08 +02:00
current.transform.origin[1] = min(-0.2, (prev.transform.origin[1] + next.transform.origin[1]) * 0.5)
2022-10-08 14:14:20 +02:00
# vertical smoothings
2022-10-09 19:36:08 +02:00
for i in range ((-size.x / 2) + 1, (size.x / 2) - 1):
for j in range ((-size.y / 2) + 1, (size.y / 2) - 1):
2022-10-08 14:14:20 +02:00
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
2022-10-09 19:36:08 +02:00
current.transform.origin[1] = min(-0.2, (prev.transform.origin[1] + next.transform.origin[1]) * 0.5)
2022-10-08 14:14:20 +02:00
func do_generate(flag: bool):
print ("Generating Level")
generate_flag = false
# generate()
func _on_ClckArea_input_event(_camera, _event, position, _normal, _shape_idx):
2022-10-09 19:36:08 +02:00
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)
2022-10-08 14:14:20 +02:00
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:
2022-10-09 19:36:08 +02:00
pass
2022-10-08 14:14:20 +02:00
if active_tile == world_tile:
return
if world_tile == null:
return
active_tile = world_tile
func _on_ClckArea_mouse_entered():
2022-10-09 19:36:08 +02:00
highlight.visible = true
2022-10-08 14:14:20 +02:00
func _on_ClckArea_mouse_exited():
2022-10-09 19:36:08 +02:00
highlight.visible = false