/// <summary> /// Initializes a new instance of the <see cref="ShipNavigator"/> class. /// </summary> /// <param name="t">Ship Transform</param> /// <param name="data">Ship data.</param> public ShipNavigator(Transform t, ShipData data) { _transform = t; _data = data; _rigidbody = t.rigidbody; _rigidbody.useGravity = false; _eventMgr = GameEventManager.Instance; _gameTime = GameTime.Instance; _gameStatus = GameStatus.Instance; _generalSettings = GeneralSettings.Instance; Subscribe(); _gameSpeedMultiplier = _gameTime.GameSpeed.SpeedMultiplier(); // FIXME where/when to get initial GameSpeed before first GameSpeed change? _thrustHelper = new ThrustHelper(0F, 0F, _data.FullStlEnginePower); }
public bool ChangeSpeed(float newSpeedRequest) { float newSpeed = Mathf.Clamp(newSpeedRequest, Constants.ZeroF, _data.FullStlSpeed); float previousRequestedSpeed = _data.RequestedSpeed; float newSpeedToRequestedSpeedRatio = (previousRequestedSpeed != Constants.ZeroF) ? newSpeed / previousRequestedSpeed : Constants.ZeroF; if (ThrustHelper.SpeedTargetRange.Contains(newSpeedToRequestedSpeedRatio)) { D.Warn("{1} ChangeSpeed Command to {0} (Max = {2}) not executed. Target speed unchanged.", newSpeedRequest, _transform.name, _data.FullStlSpeed); return false; } _data.RequestedSpeed = newSpeed; float thrustNeededToMaintainRequestedSpeed = newSpeed * _data.Mass * _data.Drag; _thrustHelper = new ThrustHelper(newSpeed, thrustNeededToMaintainRequestedSpeed, _data.FullStlEnginePower); D.Log("{0} adjusting thrust to achieve requested speed {1}.", _data.Name, newSpeed); _thrust = AdjustThrust(); if (_speedJob == null || !_speedJob.IsRunning) { _speedJob = new Job(ExecuteSpeedChange(), toStart: true, onJobComplete: delegate { string message = "{0} thrust stopped. Coasting speed is {1}."; D.Log(message, _data.Name, _data.CurrentSpeed); }); } return true; }