GodotComponentTest/scenes/Island.gd

65 lines
1.7 KiB
GDScript
Raw Normal View History

2022-09-15 12:32:45 +02:00
tool
extends Spatial
class_name Island
2022-10-07 16:14:05 +02:00
onready var GameTile = preload("res://utils/GameTile.gd")
2022-09-21 14:09:31 +02:00
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
2022-10-07 16:14:05 +02:00
func create_tile_for_hex(game_tile):
2022-09-15 14:15:23 +02:00
var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
var HexTile3D = preload("res://scenes/HexTile3D.tscn")
var tile = HexTile3D.instance()
2022-10-07 16:14:05 +02:00
var pos = hexgrid.get_hex_center_from_offset(game_tile.offset_coords)
var height = 0.0 if game_tile.type == GameTile.TileType.Sand else 0.1 + rand_range(0.0, 0.05)
2022-09-15 14:15:23 +02:00
2022-10-07 16:14:05 +02:00
tile.set_tiletype(game_tile.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")
2022-10-08 14:14:20 +02:00
func do_generate(_flag: bool):
2022-09-15 12:32:45 +02:00
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)