Files
tgcc-artest/Scripts/CameraMove.gd
Matteo Sonaglioni 461a08aa6d add fake shadow
2026-03-16 00:47:06 +01:00

163 lines
5.1 KiB
GDScript

extends Node3D
@export_group("Riferimenti Camere")
@export var camera_iso : Camera3D
@export var pivot_iso : Node3D # <-- NUOVO: Trascina qui il nodo PivotIso!
@export var camera_frontale : Camera3D
@export var camera_laterale : Camera3D
@export var camera_laterale_2 : Camera3D
@export var camera_alta : Camera3D
@export_group("Movimento Camera 1")
@export var move_speed : float = 20.0
@export var rotation_speed : float = 0.2
@export_group("Cinematic Mode")
@export var tempo_cambio_camera : float = 10.0
# --- NUOVO: Variabili per lo Zoom Cinematico ---
@export_group("Zoom Isometrico")
@export var size_normale : float = 35.0
@export var size_zoom : float = 60.0
@export var tempo_zoom : float = 1.2
var is_zoomed : bool = false
var zoom_tween : Tween
var is_moving_enabled : bool = true
# --- Variabili Cinematic Mode ---
var is_cinematic_mode : bool = false
var cinematic_timer : float = 0.0
var cinematic_index : int = 0
var array_camere : Array[Camera3D] = []
# --- Variabile per il Reset ---
var transform_iniziale_pivot : Transform3D
func _ready():
# Salviamo la posizione e rotazione del PIVOT, non della camera!
if pivot_iso:
transform_iniziale_pivot = pivot_iso.transform
if camera_iso: array_camere.append(camera_iso)
if camera_frontale: array_camere.append(camera_frontale)
if camera_laterale: array_camere.append(camera_laterale)
if camera_laterale_2: array_camere.append(camera_laterale_2)
if camera_alta: array_camere.append(camera_alta)
if camera_iso:
camera_iso.make_current()
func _input(event):
if event is InputEventKey and event.pressed and not event.echo:
# --- TASTO R: RESET DELLA CAMERA ISOMETRICA ---
if event.keycode == KEY_R and is_moving_enabled and pivot_iso:
# Riporta il Pivot al centro esatto sopra il treno
pivot_iso.transform = transform_iniziale_pivot
print("Camera Isometrica resettata!")
return
# --- TASTO C: TOGGLE CINEMATIC MODE ---
if event.keycode == KEY_C:
is_cinematic_mode = !is_cinematic_mode
if is_cinematic_mode:
cinematic_timer = 0.0
for i in range(array_camere.size()):
if array_camere[i].current:
cinematic_index = i
break
print("Cinematic Mode: ATTIVATA")
else:
print("Cinematic Mode: DISATTIVATA")
return
# --- NUOVO: TASTO Z: ZOOM DOLCE CAMERA ISOMETRICA ---
if event.keycode == KEY_Z and camera_iso:
is_zoomed = !is_zoomed
var target_size = size_zoom if is_zoomed else size_normale
# Se c'è già un'animazione in corso, fermala per evitare conflitti
if zoom_tween and zoom_tween.is_valid():
zoom_tween.kill()
zoom_tween = create_tween()
zoom_tween.set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN_OUT)
zoom_tween.tween_property(camera_iso, "size", target_size, tempo_zoom)
return
# --- CONTROLLI MANUALI (1-5) ---
var intervento_manuale = false
if event.keycode == KEY_1 and camera_iso:
camera_iso.make_current()
is_moving_enabled = true
intervento_manuale = true
elif event.keycode == KEY_2 and camera_frontale:
camera_frontale.make_current()
is_moving_enabled = false
intervento_manuale = true
elif event.keycode == KEY_3 and camera_laterale:
camera_laterale.make_current()
is_moving_enabled = false
intervento_manuale = true
elif event.keycode == KEY_4 and camera_laterale_2:
camera_laterale_2.make_current()
is_moving_enabled = false
intervento_manuale = true
elif event.keycode == KEY_5 and camera_alta:
camera_alta.make_current()
is_moving_enabled = false
intervento_manuale = true
if intervento_manuale and is_cinematic_mode:
is_cinematic_mode = false
print("Cinematic Mode: DISATTIVATA (Intervento manuale)")
# --- ROTAZIONE MOUSE (Applicata al PIVOT!) ---
if is_moving_enabled and pivot_iso:
if event is InputEventMouseMotion and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
# Ruotando il pivot, la camera farà una bellissima orbita circolare!
pivot_iso.rotate_y(deg_to_rad(-event.relative.x * rotation_speed))
func _process(delta):
if is_cinematic_mode and array_camere.size() > 0:
cinematic_timer += delta
if cinematic_timer >= tempo_cambio_camera:
cinematic_timer = 0.0
cinematic_index = (cinematic_index + 1) % array_camere.size()
var next_cam = array_camere[cinematic_index]
next_cam.make_current()
is_moving_enabled = (next_cam == camera_iso)
# --- LOGICA DI MOVIMENTO WASD (Applicata al PIVOT!) ---
if not is_moving_enabled or pivot_iso == null:
return
var input_dir = Vector3.ZERO
if Input.is_key_pressed(KEY_W): input_dir.z -= 1
if Input.is_key_pressed(KEY_S): input_dir.z += 1
if Input.is_key_pressed(KEY_A): input_dir.x -= 1
if Input.is_key_pressed(KEY_D): input_dir.x += 1
if input_dir != Vector3.ZERO:
input_dir = input_dir.normalized()
# Prendiamo le direzioni dal Pivot, così WASD segue dove stiamo guardando
var forward = pivot_iso.transform.basis.z
var right = pivot_iso.transform.basis.x
forward.y = 0
right.y = 0
forward = forward.normalized()
right = right.normalized()
var motion = (forward * input_dir.z + right * input_dir.x)
# Muoviamo il centro dell'orbita (il Pivot)
pivot_iso.position += motion * move_speed * delta