GodotComponentTest/scenes/Island.gd

65 lines
1.6 KiB
GDScript
Raw Normal View History

2022-09-15 12:32:45 +02:00
tool
extends Spatial
class_name Island
2022-09-21 14:09:31 +02:00
onready var IslandTile = preload("res://utils/IslandTile.gd")
2022-09-15 12:32:45 +02:00
export var generate: bool = false setget do_generate
export var level_size: int = 10
signal island_tile_selected
signal island_tile_hover
2022-09-15 14:15:23 +02:00
func reset():
2022-09-15 12:32:45 +02:00
var tiles = $Tiles
for node in tiles.get_children():
tiles.remove_child(node)
node.queue_free()
2022-09-15 14:15:23 +02:00
func create_tile_for_hex(hex):
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(hex.offset_coords)
2022-09-21 14:09:31 +02:00
var height = 0.0 if hex.type == IslandTile.TileType.Sand else 0.1 + rand_range(0.0, 0.05)
2022-09-15 14:15:23 +02:00
2022-09-21 14:09:31 +02:00
tile.set_tiletype(hex.type)
2022-09-15 14:15:23 +02:00
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.connect("tile_selected", self, "on_tile_selected")
tile.connect("tile_mouse_entered", self, "on_tile_hover")
return tile
func generate():
2022-09-21 14:09:31 +02:00
var IslandGenerator = preload("res://utils/IslandGenerator.gd").new()
2022-09-15 14:15:23 +02:00
2022-09-21 14:09:31 +02:00
var tiles = $Tiles
2022-09-15 14:15:23 +02:00
reset()
2022-09-21 14:09:31 +02:00
var generated_tiles = IslandGenerator.random_walk(level_size)
generated_tiles = IslandGenerator.extrude_tiles(generated_tiles)
generated_tiles = IslandGenerator.mark_sand_tiles(generated_tiles)
2022-09-15 14:15:23 +02:00
2022-09-21 14:09:31 +02:00
for island_tile in generated_tiles.values():
var scene_tile = create_tile_for_hex(island_tile)
tiles.add_child(scene_tile)
2022-09-15 12:32:45 +02:00
print ("Generated island with " + str(tiles.get_child_count()) + " nodes")
func do_generate(flag: bool):
print ("Generating Level")
generate()
func on_tile_selected(tile):
emit_signal("island_tile_selected", self, tile)
func on_tile_hover(tile):
emit_signal("island_tile_hover", self, tile)