Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented horn sound base functionality #73

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Assets/Scripts/Behaviours/Ped/Ped.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/Behaviours/Ped/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -204,7 +206,6 @@ void ReadEvents()

if (customInput.GetKeyDown (KeyCode.R))
m_ped.OnFlyThroughButtonPressed();

}

private void ReadCameraInput ()
Expand Down
5 changes: 5 additions & 0 deletions Assets/Scripts/Behaviours/Ped/States/BaseScriptState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Net;
using System;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary using


namespace SanAndreasUnity.Behaviours.Peds.States
{
Expand Down Expand Up @@ -306,6 +307,10 @@ public virtual void OnWeaponFiredFromServer(Weapon weapon, Vector3 firePos)
weapon.PlayFireSound();
}

public virtual void OnHornButtonPressed()
{

}
}

}
11 changes: 10 additions & 1 deletion Assets/Scripts/Behaviours/Ped/States/VehicleSittingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
60 changes: 52 additions & 8 deletions Assets/Scripts/Behaviours/Vehicles/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vehicle> s_vehicles = new List<Vehicle>();
Expand Down Expand Up @@ -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;
Expand All @@ -173,7 +179,7 @@ private void Awake()
this.NetTransform = this.GetComponent<Mirror.NetworkTransform>();
_props = new MaterialPropertyBlock();
m_radioAudioSource = GetComponent<AudioSource>();
}
}

void OnEnable()
{
Expand All @@ -182,6 +188,7 @@ void OnEnable()

void OnDisable()
{
Destroy(horn);
s_vehicles.Remove(this);
}

Expand All @@ -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<AudioSource>();
hornAudioSource.clip = _vehicleHornSound;
}

public void SetColors(params int[] clrIndices)
Expand Down Expand Up @@ -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; }

Expand Down Expand Up @@ -436,7 +463,11 @@ private void Update()
m_radioAudioSource.Stop();
}
}
}

if (IsHornOn) { PlayHornSound(); }
else { hornAudioSource.loop = false; }
IsHornOn = false;
}

private void FixedUpdate()
{
Expand All @@ -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;
}

}
}
9 changes: 6 additions & 3 deletions Assets/Scripts/Behaviours/Vehicles/VehicleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions Assets/Scripts/Behaviours/Vehicles/Vehicle_Radio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading