AppResidencia/flecha/node_2d.gd

78 lines
1.9 KiB
GDScript

extends Node2D
@export var direction_count := 4 # 2, 4, u 8 direcciones
@export var interval := 5.0 # Tiempo entre giros
@export var margin_ratio := 0.1 # Margen visual
var directions: Array[int] = []
var arrow: Polygon2D
var timer: Timer
var last_angle: int = -1 # Última dirección usada (inicial -1 para que no se repita)
func _ready():
get_viewport().connect("size_changed", Callable(self, "_on_viewport_resized"))
# Crear flecha
arrow = Polygon2D.new()
add_child(arrow)
arrow.polygon = _generate_arrow_shape()
arrow.color = Color.DARK_RED
_update_arrow_transform()
# Crear y configurar el temporizador
timer = Timer.new()
timer.wait_time = interval
timer.autostart = true
timer.one_shot = false
add_child(timer)
timer.connect("timeout", Callable(self, "_on_timer_timeout"))
# Configurar posibles direcciones
_set_possible_directions()
change_direction()
func _generate_arrow_shape() -> PackedVector2Array:
return [
Vector2(0, -1),
Vector2(0.4, 0.4),
Vector2(0.2, 0.4),
Vector2(0.2, 1),
Vector2(-0.2, 1),
Vector2(-0.2, 0.4),
Vector2(-0.4, 0.4),
]
func _update_arrow_transform():
var screen_size = get_viewport().get_visible_rect().size
var min_dim = min(screen_size.x, screen_size.y)
var usable_space = min_dim * (1.0 - margin_ratio * 2)
arrow.scale = Vector2(usable_space / 2, usable_space / 2)
arrow.position = screen_size / 2
func _on_viewport_resized():
_update_arrow_transform()
func _set_possible_directions():
match direction_count:
2:
directions = [0, 180]
4:
directions = [0, 90, 180, 270]
8:
directions = [0, 45, 90, 135, 180, 225, 270, 315]
_:
directions = [0, 180]
func change_direction():
var possible = directions.duplicate()
if last_angle != -1 and possible.size() > 1:
possible.erase(last_angle)
var angle = possible[randi() % possible.size()]
last_angle = angle
arrow.rotation_degrees = angle
func _on_timer_timeout():
change_direction()