// ╔════════════════════════════════════════════════════════════════╗ // ║ Copyright © 2025 NWH Coding d.o.o. All rights reserved. ║ // ║ Licensed under Unity Asset Store Terms of Service: ║ // ║ https://unity.com/legal/as-terms ║ // ║ Use permitted only in compliance with the License. ║ // ║ Distributed "AS IS", without warranty of any kind. ║ // ╚════════════════════════════════════════════════════════════════╝ #region using System; using UnityEngine; using UnityEngine.Serialization; #endregion namespace NWH.DWP2.ShipController { /// /// Lateral thruster for sideways ship movement. /// Bow thrusters are mounted at the front for tight turning and docking, stern thrusters at the rear. /// Multiple thrusters of each type can be added for increased lateral control. /// [Serializable] public class Thruster { /// /// Visual rotation direction of the thruster propeller. /// public enum RotationDirection { Left, Right, } /// /// Position of the thruster on the ship. /// Determines which input controls it and the direction of thrust application. /// public enum ThrusterPosition { BowThruster, SternThruster, } /// /// Maximum thrust force in Newtons. /// [Tooltip("Max thrust in [N].")] public float maxThrust; /// /// Display name for this thruster. /// [Tooltip("Name of the thruster - can be any string.")] public string name = "Thruster"; /// /// Position relative to ship transform where thrust force is applied. /// [Tooltip("Relative force application position.")] public Vector3 position; [FormerlySerializedAs("rotationDirection")] [Tooltip("Rotation direction of the propeller. Visual only.")] public RotationDirection propellerRotationDirection = RotationDirection.Right; [Tooltip("Rotation speed of the propeller if assigned. Visual only.")] public float propellerRotationSpeed = 1000f; [Tooltip("Optional. Transform representing a propeller. Visual only.")] public Transform propellerTransform; [Tooltip("Time needed to reach maxThrust.")] public float spinUpSpeed = 1f; /// /// Whether this is a bow or stern thruster. /// public ThrusterPosition thrusterPosition = ThrusterPosition.BowThruster; private AdvancedShipController sc; private float thrust; /// /// World space position where thrust force is applied. /// public Vector3 WorldPosition { get { return sc.transform.TransformPoint(position); } } /// /// Current input value for this thruster from -1 to 1. /// Automatically retrieves from the appropriate input channel based on thrusterPosition. /// public float Input { get { float input = 0; if (thrusterPosition == ThrusterPosition.BowThruster) { input = -sc.input.BowThruster; } else { input = -sc.input.SternThruster; } return input; } } /// /// Initializes the thruster with a reference to its parent ship controller. /// /// The ship controller this thruster belongs to. public void Initialize(AdvancedShipController sc) { this.sc = sc; } public virtual void Update() { float newThrust = maxThrust * -Input; thrust = Mathf.MoveTowards(thrust, newThrust, spinUpSpeed * maxThrust * Time.fixedDeltaTime); sc.vehicleRigidbody.AddForceAtPosition(thrust * sc.transform.right, WorldPosition); if (propellerTransform != null) { float zRotation = Input * propellerRotationSpeed * Time.fixedDeltaTime; if (propellerRotationDirection == RotationDirection.Right) { zRotation = -zRotation; } propellerTransform.RotateAround(propellerTransform.position, propellerTransform.forward, zRotation); } } } }