diff --git a/Assets/Scripts/Behaviours/Ped/Ped.cs b/Assets/Scripts/Behaviours/Ped/Ped.cs index d50ecbd12..d34ac2920 100644 --- a/Assets/Scripts/Behaviours/Ped/Ped.cs +++ b/Assets/Scripts/Behaviours/Ped/Ped.cs @@ -644,6 +644,12 @@ public void OnFlyThroughButtonPressed () this.CurrentState.OnFlyThroughButtonPressed (); } + public void OnHornButtonPressed() + { + if (this.CurrentState != null) + this.CurrentState.OnHornButtonPressed(); + } + internal void OnStartCollidingWithEnex(EntranceExitMapObject enex) { diff --git a/Assets/Scripts/Behaviours/Ped/PlayerController.cs b/Assets/Scripts/Behaviours/Ped/PlayerController.cs index cc0759c0b..73fc033f2 100644 --- a/Assets/Scripts/Behaviours/Ped/PlayerController.cs +++ b/Assets/Scripts/Behaviours/Ped/PlayerController.cs @@ -142,6 +142,8 @@ void ReadStates() m_ped.IsJumpOn = customInput.GetButton ("Jump"); + if(m_ped.CurrentVehicle) m_ped.CurrentVehicle.IsHornOn = customInput.GetKey(KeyCode.H); + Vector3 inputMove = Vector3.zero; if (m_smoothMovement) @@ -204,7 +206,6 @@ void ReadEvents() if (customInput.GetKeyDown (KeyCode.R)) m_ped.OnFlyThroughButtonPressed(); - } private void ReadCameraInput () diff --git a/Assets/Scripts/Behaviours/Ped/States/BaseScriptState.cs b/Assets/Scripts/Behaviours/Ped/States/BaseScriptState.cs index 3f380131e..5270ecbc9 100644 --- a/Assets/Scripts/Behaviours/Ped/States/BaseScriptState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/BaseScriptState.cs @@ -1,6 +1,7 @@ using UnityEngine; using SanAndreasUnity.Utilities; using SanAndreasUnity.Net; +using System; namespace SanAndreasUnity.Behaviours.Peds.States { @@ -306,6 +307,10 @@ public virtual void OnWeaponFiredFromServer(Weapon weapon, Vector3 firePos) weapon.PlayFireSound(); } + public virtual void OnHornButtonPressed() + { + + } } } diff --git a/Assets/Scripts/Behaviours/Ped/States/VehicleSittingState.cs b/Assets/Scripts/Behaviours/Ped/States/VehicleSittingState.cs index 95d79b3fa..230230587 100644 --- a/Assets/Scripts/Behaviours/Ped/States/VehicleSittingState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/VehicleSittingState.cs @@ -107,7 +107,16 @@ public override void OnNextWeaponButtonPressed() base.OnNextWeaponButtonPressed(); } - public override void OnSubmitPressed() + public override void OnHornButtonPressed() + { + if (Ped.Instance.CurrentVehicle == null) base.OnHornButtonPressed(); + else + { + Ped.Instance.CurrentVehicle.IsHornOn = true; + } + } + + public override void OnSubmitPressed() { // exit the vehicle diff --git a/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs b/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs index 7e0792647..5767030fb 100644 --- a/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs +++ b/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs @@ -27,12 +27,11 @@ public enum VehicleBlinkerMode { None, Left, Right, Emergency } - #if CLIENT public partial class Vehicle : Networking.Networkable #else - public partial class Vehicle : MonoBehaviour + public partial class Vehicle : MonoBehaviour #endif { static List s_vehicles = new List(); @@ -159,7 +158,14 @@ public bool IsNightToggled } } - private VehicleController _controller; + public bool IsHornOn + { + get; set; + } + private AudioSource hornAudioSource; + AudioClip horn; + AudioClip _vehicleHornSound; + private VehicleController _controller; bool m_isServer => Net.NetStatus.IsServer; public bool IsControlledByLocalPlayer => Ped.Instance != null && Ped.Instance.CurrentVehicle == this && Ped.Instance.CurrentVehicleSeat.IsDriver; @@ -173,7 +179,7 @@ private void Awake() this.NetTransform = this.GetComponent(); _props = new MaterialPropertyBlock(); m_radioAudioSource = GetComponent(); - } + } void OnEnable() { @@ -182,6 +188,7 @@ void OnEnable() void OnDisable() { + Destroy(horn); s_vehicles.Remove(this); } @@ -192,7 +199,11 @@ void Start() currentRadioStationIndex = Random.Range(0, RadioStation.stations.Length); Debug.LogFormat("Created vehicle - id {0}, name {1}, time: {2}", this.Definition.Id, - this.Definition.GameName, F.CurrentDateForLogging); + this.Definition.GameName, F.CurrentDateForLogging); + horn = Audio.AudioManager.CreateAudioClipFromSfx("GENRL", 67, this.Definition.HornId); + _vehicleHornSound = MakeSubclip(horn, horn.length / 2f, horn.length); + hornAudioSource = this.gameObject.AddComponent(); + hornAudioSource.clip = _vehicleHornSound; } public void SetColors(params int[] clrIndices) @@ -295,7 +306,23 @@ private void SetLight(int index, float brightness) _colorsChanged = true; } - public VehicleDef Definition { get; private set; } + public virtual void PlayHornSound() + { + if (!IsHornOn) return; + hornAudioSource.playOnAwake = false; + hornAudioSource.spatialBlend = 1; + hornAudioSource.maxDistance = 15; + if (hornAudioSource && hornAudioSource.clip) + { + if (!hornAudioSource.isPlaying) + { + hornAudioSource.loop = true; + hornAudioSource.Play(); + } + } + } + + public VehicleDef Definition { get; private set; } public Transform DriverTransform { get; private set; } @@ -436,7 +463,11 @@ private void Update() m_radioAudioSource.Stop(); } } - } + + if (IsHornOn) { PlayHornSound(); } + else { hornAudioSource.loop = false; } + IsHornOn = false; + } private void FixedUpdate() { @@ -462,5 +493,18 @@ public void ApplySyncRate(float syncRate) this.NetTransform.syncInterval = 1.0f / syncRate; } - } + //TODO add vehicle type horn sound, rename the method appropriately + private AudioClip MakeSubclip(AudioClip clip, float start, float stop) + { + int frequency = clip.frequency; + float timeLength = stop - start; + int samplesLength = (int)(frequency * timeLength); + AudioClip newClip = AudioClip.Create(clip.name + "-sub", samplesLength, 1, frequency, false); + float[] data = new float[samplesLength]; + clip.GetData(data, (int)(frequency * start)); + newClip.SetData(data, 0); + return newClip; + } + + } } \ No newline at end of file diff --git a/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs b/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs index 45a4786ac..28fef1f5e 100644 --- a/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs +++ b/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs @@ -17,7 +17,8 @@ public class VehicleController : NetworkBehaviour [SyncVar] float m_net_acceleration; [SyncVar] float m_net_steering; [SyncVar] float m_net_braking; - [SyncVar(hook=nameof(OnNetPositionChanged))] Vector3 m_net_position; + [SyncVar] bool m_net_isHornOn; + [SyncVar(hook=nameof(OnNetPositionChanged))] Vector3 m_net_position; [SyncVar(hook=nameof(OnNetRotationChanged))] Quaternion m_net_rotation; [SyncVar] Vector3 m_net_linearVelocity; [SyncVar] Vector3 m_net_angularVelocity; @@ -141,7 +142,8 @@ void ProcessSyncvars() m_net_acceleration = m_vehicle.Accelerator; m_net_steering = m_vehicle.Steering; m_net_braking = m_vehicle.Braking; - m_net_position = m_vehicle.transform.position; + m_net_isHornOn = m_vehicle.IsHornOn; + m_net_position = m_vehicle.transform.position; m_net_rotation = m_vehicle.transform.rotation; m_net_linearVelocity = m_vehicle.RigidBody.velocity; m_net_angularVelocity = m_vehicle.RigidBody.angularVelocity; @@ -166,7 +168,8 @@ void ProcessSyncvars() m_vehicle.Accelerator = m_net_acceleration; m_vehicle.Steering = m_net_steering; m_vehicle.Braking = m_net_braking; - } + m_vehicle.IsHornOn = m_net_isHornOn; + } // update wheels if (!this.IsControlledByLocalPlayer || (this.IsControlledByLocalPlayer && !VehicleManager.Instance.controlWheelsOnLocalPlayer)) diff --git a/Assets/Scripts/Behaviours/Vehicles/Vehicle_Radio.cs b/Assets/Scripts/Behaviours/Vehicles/Vehicle_Radio.cs index 95ec6690f..e70d492c3 100644 --- a/Assets/Scripts/Behaviours/Vehicles/Vehicle_Radio.cs +++ b/Assets/Scripts/Behaviours/Vehicles/Vehicle_Radio.cs @@ -7,8 +7,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles public partial class Vehicle { private int currentRadioStationIndex; - private RadioStation CurrentRadioStation { get { return RadioStation.stations[currentRadioStationIndex]; } } - + private RadioStation CurrentRadioStation { get { return RadioStation.stations[currentRadioStationIndex]; } } private AudioSource m_radioAudioSource; public void PlayRadio() diff --git a/Assets/Scripts/Importing/Items/Definitions/VehicleDef.cs b/Assets/Scripts/Importing/Items/Definitions/VehicleDef.cs index b6b133854..e480f261d 100644 --- a/Assets/Scripts/Importing/Items/Definitions/VehicleDef.cs +++ b/Assets/Scripts/Importing/Items/Definitions/VehicleDef.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Globalization; +using UnityEngine; namespace SanAndreasUnity.Importing.Items.Definitions { @@ -21,8 +23,225 @@ public enum VehicleType public class VehicleDef : Definition, IObjectDefinition { public readonly int Id; + public readonly int HornId; - int IObjectDefinition.Id + public readonly Dictionary HornDictionary = new Dictionary() + { + { 400, 7}, + { 401, 2}, + { 402, 2}, + { 403, 9}, + { 404, 7}, + { 405, 3}, + { 406, 4}, + { 407, 4}, + { 408, 5}, + { 409, 2}, + { 410, 1}, + { 411, 8}, + { 412, 2}, + { 413, 2}, + { 414, 7}, + { 415, 8}, + { 416, 7}, + { 417, 10}, + { 418, 1}, + { 419, 2}, + { 420, 5}, + { 421, 3}, + { 422, 7}, + { 423, 5}, + { 424, 6}, + { 425, 10}, + { 426, 8}, + { 427, 9}, + { 428, 4}, + { 429, 3}, + { 430, 10}, + { 431, 5}, + { 432, 9}, + { 433, 9}, + { 434, 3}, + { 435, 10}, + { 436, 1}, + { 437, 5}, + { 438, 4}, + { 439, 7}, + { 440, 1}, + { 441, 10}, + { 442, 5}, + { 443, 9}, + { 444, 9}, + { 445, 3}, + { 446, 10}, + { 447, 10}, + { 448, 1}, + { 449, 10}, + { 450, 10}, + { 451, 8}, + { 452, 10}, + { 453, 10}, + { 454, 10}, + { 455, 9}, + { 456, 4}, + { 457, 1}, + { 458, 3}, + { 459, 2}, + { 460, 10}, + { 461, 6}, + { 462, 1}, + { 463, 1}, + { 464, 10}, + { 465, 10}, + { 466, 5}, + { 467, 2}, + { 468, 6}, + { 469, 10}, + { 470, 9}, + { 471, 1}, + { 472, 10}, + { 473, 10}, + { 474, 4}, + { 475, 8}, + { 476, 10}, + { 477, 3}, + { 478, 2}, + { 479, 7}, + { 480, 8}, + { 481, 0}, + { 482, 7}, + { 483, 2}, + { 484, 10}, + { 485, 1}, + { 486, 9}, + { 487, 10}, + { 488, 10}, + { 489, 5}, + { 490, 5}, + { 491, 3}, + { 492, 7}, + { 493, 10}, + { 494, 3}, + { 495, 5}, + { 496, 8}, + { 497, 10}, + { 498, 3}, + { 499, 2}, + { 500, 6}, + { 501, 10}, + { 502, 3}, + { 503, 7}, + { 504, 2}, + { 505, 5}, + { 506, 4}, + { 507, 7}, + { 508, 4}, + { 509, 0}, + { 510, 0}, + { 511, 10}, + { 512, 10}, + { 513, 10}, + { 514, 9}, + { 515, 9}, + { 516, 4}, + { 517, 8}, + { 518, 3}, + { 519, 10}, + { 520, 10}, + { 521, 7}, + { 522, 6}, + { 523, 6}, + { 524, 4}, + { 525, 4}, + { 526, 3}, + { 527, 8}, + { 528, 5}, + { 529, 2}, + { 530, 1}, + { 531, 7}, + { 532, 4}, + { 533, 2}, + { 534, 3}, + { 535, 7}, + { 536, 2}, + { 537, 10}, + { 538, 10}, + { 539, 10}, + { 540, 3}, + { 541, 2}, + { 542, 7}, + { 543, 7}, + { 544, 9}, + { 545, 8}, + { 546, 3}, + { 547, 1}, + { 548, 10}, + { 549, 2}, + { 550, 8}, + { 551, 6}, + { 552, 7}, + { 553, 10}, + { 554, 7}, + { 555, 2}, + { 556, 9}, + { 557, 9}, + { 558, 8}, + { 559, 3}, + { 560, 7}, + { 561, 3}, + { 562, 3}, + { 563, 10}, + { 564, 10}, + { 565, 2}, + { 566, 3}, + { 567, 7}, + { 568, 7}, + { 569, 10}, + { 570, 10}, + { 571, 1}, + { 572, 1}, + { 573, 4}, + { 574, 5}, + { 575, 3}, + { 576, 5}, + { 577, 10}, + { 578, 4}, + { 579, 7}, + { 580, 2}, + { 581, 6}, + { 582, 4}, + { 583, 1}, + { 584, 10}, + { 585, 8}, + { 586, 7}, + { 587, 5}, + { 588, 4}, + { 589, 6}, + { 590, 10}, + { 591, 10}, + { 592, 10}, + { 593, 10}, + { 594, 10}, + { 595, 10}, + { 596, 2}, + { 597, 2}, + { 598, 7}, + { 599, 4}, + { 600, 7}, + { 601, 9}, + { 602, 3}, + { 603, 8}, + { 604, 5}, + { 605, 7}, + { 606, 10}, + { 607, 10}, + { 608, 10}, + { 609, 3}, + { 610, 10}, + { 611, 10} + }; + + int IObjectDefinition.Id { get { return Id; } } @@ -52,8 +271,9 @@ int IObjectDefinition.Id public VehicleDef(string line) : base(line) { Id = GetInt(0); + HornId = HornDictionary[Id]; - ModelName = GetString(1); + ModelName = GetString(1); TextureDictionaryName = GetString(2); VehicleType = (VehicleType)Enum.Parse(typeof(VehicleType), GetString(3), true); diff --git a/Assets/Scripts/Utilities/CustomInput.cs b/Assets/Scripts/Utilities/CustomInput.cs index f66ce541e..a0a98e1ea 100644 --- a/Assets/Scripts/Utilities/CustomInput.cs +++ b/Assets/Scripts/Utilities/CustomInput.cs @@ -88,6 +88,16 @@ public bool GetKeyDown(KeyCode keyCode){ return Input.GetKeyDown(keyCode); } + public bool GetKey(KeyCode keyCode) + { + if (!this.IsActive) + return Input.GetKey(keyCode); + bool value = false; + if (keysDown.TryGetValue(keyCode, out value)) + return value; + return Input.GetKey(keyCode); + } + public void SetKeyDown(KeyCode keyCode, bool pressed) { keysDown [keyCode] = pressed;