FantasyRun/Tiles/tile_turn.gd

65 lines
1.6 KiB
GDScript3
Raw Permalink Normal View History

extends Tile
class_name TileTurn
2024-10-13 14:49:58 -07:00
enum Direction {
Forward,
Left,
Right,
Both
}
@export var turn_direction: Direction = Direction.Forward
@onready var area_3d: Area3D = $Area3D as Area3D
@onready var path_forward: TilePath = $PathForward as TilePath
@onready var path_right: TilePath = $PathRight as TilePath
@onready var path_left: TilePath = $PathLeft as TilePath
var connected: bool = false
2024-10-13 14:49:58 -07:00
var current_direction: Direction = Direction.Forward
var character: Character
var on_main_path: bool:
get:
return path_index == 0
func _process(delta: float) -> void:
var character_found = false
for area in area_3d.get_overlapping_areas():
if area is Character:
character_found = true
if connected:
break
character = area as Character
character.turn.connect(turn)
connected = true
if not character_found and connected:
character.turn.disconnect(turn)
connected = false
super(delta)
func load_path() -> void:
paths.append($MainPath)
paths.append($PathForward)
2024-10-13 14:49:58 -07:00
func turn(direction: Direction) -> void:
if on_main_path:
2024-10-12 16:16:43 -07:00
var next_path: TilePath = get_path_by_direction(direction)
2024-10-13 14:49:58 -07:00
if next_path == null or (direction != turn_direction and direction != Direction.Both):
2024-10-12 16:16:43 -07:00
return
current_direction = direction
2024-10-12 16:16:43 -07:00
paths[path_index+1] = next_path
2024-10-13 14:49:58 -07:00
func get_path_by_direction(direction: Direction) -> TilePath:
if direction == Direction.Forward and path_forward != null:
return path_forward
2024-10-13 14:49:58 -07:00
elif direction == Direction.Right and path_right != null:
return path_right
2024-10-13 14:49:58 -07:00
elif direction == Direction.Left and path_left != null:
return path_left
return null