FantasyRun/tile_generator.gd
Matthew Welch 5669da34c5 update type hints
fix issue with signal for tiles exiting the screen
2024-10-12 15:19:15 -07:00

83 lines
2.0 KiB
GDScript

extends Node
var tile_set: FR_TileSet = preload("res://tile_set1.tres") as FR_TileSet
var tiles: Array[Tile]
var tile_index: int = 0
var run_started: bool = false
var tile_parent: Node
var character: Character
var current_tile: Tile:
get = get_current_tile
func _ready() -> void:
pass
func _process(delta: float) -> void:
if not run_started:
return
if tiles.size() == 0:
return
if tiles.size() < 15:
add_tile()
if is_equal_approx(current_tile.follow.progress_ratio, 1) and has_next_tile():
if current_tile.has_next_path():
return
tile_index += 1
character.tile = current_tile
func start_run(tile_parent: Node) -> void:
self.tile_parent = tile_parent
character = Utils.get_first_node_of_type(get_tree().root, Character) as Character
get_current_tiles()
if tiles.size() == 0:
var tile: Tile = generate_tile()
tiles.append(tile)
tile_parent.add_child(tile)
character.tile = current_tile
run_started = true
func get_current_tiles() -> void:
tiles.assign(Utils.get_nodes_of_class(get_tree().root, Tile))
for tile in tiles:
tile.left_screen.connect(tile_left_screen, CONNECT_ONE_SHOT)
func generate_tile() -> Tile:
var index = randi_range(0, tile_set.strait_tiles.size()-1)
var tile_scene: PackedScene = tile_set.strait_tiles[index]
var tile: Tile = tile_scene.instantiate() as Tile
tile.left_screen.connect(tile_left_screen, CONNECT_ONE_SHOT)
return tile
func add_tile() -> void:
var last_tile: Tile = tiles[tiles.size()-1]
var tile: Tile = generate_tile()
tiles.append(tile)
tile_parent.add_child(tile)
tile.global_position = last_tile.spawn_point.global_position
tile.global_rotation = last_tile.spawn_point.global_rotation
func has_next_tile() -> bool:
return tile_index + 1 < tiles.size()
func get_current_tile() -> Tile:
return tiles[tile_index]
func tile_left_screen(tile: Tile):
var index: int = tiles.find(tile)
tiles.remove_at(index)
if index < tile_index:
tile_index -= 1
await get_tree().create_timer(1.0).timeout
tile.queue_free()