Files
CosmicClash/Game/scripts/ship.gd
T
2025-07-13 18:36:48 +01:00

88 lines
2.9 KiB
GDScript

class_name Ship
extends RigidBody3D
var speed = 10.0
@onready var camera : Camera3D = get_node("Camera3D")
@onready var ball = get_parent().get_node("Ball")
@onready var timer_label = get_node("HUD/Control/TimerLabel")
func _ready():
var game_scene = get_parent()
if game_scene and game_scene.has_signal("timer_updated"):
game_scene.timer_updated.connect(_on_timer_updated)
print("Connected to game scene timer signal")
else:
print("Game scene not found or doesn't have timer_updated signal")
func _on_timer_updated(minutes: int, seconds: int):
timer_label.text = "%02d:%02d" % [minutes, seconds]
func _physics_process(_delta):
if ball and camera:
# Adjust the offset based on your scene scale
# Increasing Y component to raise the camera higher above the vehicle
var offset = Vector3(0, 2, -2) # Adjust Y to change height
# Position the camera behind and above the vehicle
var behind_vehicle = global_transform.origin - global_transform.basis.z * offset.z
behind_vehicle += global_transform.basis.y * offset.y
camera.global_transform.origin = behind_vehicle
camera.look_at(ball.global_transform.origin, Vector3.UP)
func _integrate_forces(state):
# input_vector represents the movement input relative to the vehicle
var input_vector = get_input_vector()
# Scale the input_vector by speed
input_vector = input_vector.normalized() * speed
# Set the linear velocity based on the input
state.linear_velocity = input_vector
# Rotate the ship to face the movement direction
if input_vector.length() > 0.1: # Only rotate if there's significant movement
var target_direction = input_vector.normalized()
# Create a transform that looks in the target direction
var look_transform = Transform3D()
look_transform.origin = global_transform.origin
look_transform = look_transform.looking_at(global_transform.origin + target_direction, Vector3.UP)
# Apply the rotation to the ship
state.transform.basis = look_transform.basis
func get_input_vector() -> Vector3:
var input_vector = Vector3.ZERO
if camera:
var camera_transform = camera.global_transform
var forward_direction = -camera_transform.basis.z
var right_direction = camera_transform.basis.x
var y_movement = input_vector.y
if Input.is_action_pressed("move_forward"):
input_vector += forward_direction
if Input.is_action_pressed("move_backward"):
input_vector -= forward_direction
if Input.is_action_pressed("move_left"):
input_vector -= right_direction
if Input.is_action_pressed("move_right"):
input_vector += right_direction
# Zero out the Y component to restrict movement to the XZ plane
input_vector.y = 0
# Add back the preserved Y movement
input_vector.y = y_movement
if Input.is_action_pressed("move_up"):
input_vector.y += 1
if Input.is_action_pressed("move_down"):
input_vector.y -= 1
return input_vector.normalized()
else:
print("Camera not found")
return Vector3.ZERO