Boat/Boats/boat.gd

72 lines
2.1 KiB
GDScript3
Raw Normal View History

2025-03-25 10:49:02 +00:00
@icon("uid://3ugrjpybrl4e")
2025-03-24 17:01:51 +00:00
extends RigidBody3D
class_name Boat
2025-03-24 15:01:39 +00:00
@export_category("Controls")
@export var max_thrust_force: float = 2048 * 3.0
2025-03-25 17:23:14 +00:00
@export var max_steering: float = 50.0
2025-03-24 15:01:39 +00:00
@export_category("Debug")
@export var debug: bool = false
2025-03-25 17:23:14 +00:00
var steering: float = 0 # steering rudder angle in radians
var thrust_force: float = 0 # forward thrust force in Newtons
2025-04-07 16:23:29 +00:00
var cam_rotation = 0.0
2025-03-25 10:49:02 +00:00
2025-03-25 17:23:14 +00:00
var is_docked: bool = false
var submerged := false
2025-03-25 10:49:02 +00:00
2025-03-25 17:23:14 +00:00
func _ready():
add_to_group("Boats", true)
2025-03-24 15:01:39 +00:00
2025-03-25 17:23:14 +00:00
func _process(_delta):
if Input.get_action_strength("move_forward") > 0.0:
thrust(Input.get_action_strength("move_forward"))
2025-03-25 17:23:14 +00:00
if Input.get_action_strength("turn_right") > 0.0:
steer_right()
if Input.get_action_strength("turn_left") > 0.0:
steer_left()
if Input.get_action_strength("camera_left") > 0.0:
2025-04-07 16:23:29 +00:00
cam_rotation += 1.0 * _delta
2025-03-25 17:23:14 +00:00
if Input.get_action_strength("camera_right") > 0.0:
2025-04-07 16:23:29 +00:00
cam_rotation += -1.0 * _delta
$CamRoot.global_rotation.y += cam_rotation
$CamRoot.global_rotation.x = 0.0
$CamRoot.global_rotation.z = 0.0
$CamRoot.global_position.y = 0.0
2025-04-07 16:23:29 +00:00
cam_rotation =0.0
func _physics_process(delta):
## Code for user-input movement
if thrust_force > 0.0:
## TODO: Need to apply a force to the side based on steering value, to simulate ship axis rotation when turning
##
## IDEA: Maybe even apply_torque() should be removed and we simulate a turning with a force applying to the back of the ship
## but maybe it's too realistic for the game i want
var wanted_force: Vector3 = global_transform.basis.x.normalized() * Vector3(1, 0, 1) * thrust_force * delta
apply_central_force(wanted_force)
angular_damp = linear_velocity.length() # make the boat less affected by the sidewave when full speed
apply_torque(global_transform.basis.y.normalized() * steering * clamp(linear_velocity.length(), 0.35, 1.0)) # for sideways motion
reset_forces()
2025-03-25 17:23:14 +00:00
func thrust(_strength := 1.0):
2025-03-25 17:23:14 +00:00
if not is_docked:
thrust_force = max_thrust_force * _strength
2025-03-25 17:23:14 +00:00
func steer_right(_strength := 1.0):
2025-03-25 17:23:14 +00:00
if not is_docked:
steering = - PI * max_steering
2025-03-25 17:23:14 +00:00
func steer_left(_strength := 1.0):
2025-03-25 17:23:14 +00:00
if not is_docked:
steering = PI * max_steering
func reset_forces():
thrust_force = 0
steering = 0