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 1 commit
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 @@ -632,6 +632,12 @@ public void OnFlyThroughButtonPressed ()
this.CurrentState.OnFlyThroughButtonPressed ();
}

public void OnHornButtonPressed()
{
if (this.CurrentState != null)
this.CurrentState.OnHornButtonPressed();
}


void OnDrawGizmosSelected ()
{
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Behaviours/Ped/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ void ReadEvents()
if (customInput.GetKeyDown (KeyCode.R))
m_ped.OnFlyThroughButtonPressed();

if (customInput.GetKey(KeyCode.H))
m_ped.OnHornButtonPressed();

}

private void ReadCameraInput ()
Expand Down
6 changes: 6 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,11 @@ public virtual void OnWeaponFiredFromServer(Weapon weapon, Vector3 firePos)
weapon.PlayFireSound();
}

public virtual void OnHornButtonPressed()
{
if (Ped.Instance.CurrentVehicle == null) return;
Ped.Instance.CurrentVehicle.IsHornOn = true;
}
}

}
57 changes: 51 additions & 6 deletions Assets/Scripts/Behaviours/Vehicles/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,13 @@ public bool IsNightToggled
}
}

private VehicleController _controller;
public bool IsHornOn
{
get; set;
}

private AudioSource horn;
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,8 @@ private void Awake()
this.NetTransform = this.GetComponent<Mirror.NetworkTransform>();
_props = new MaterialPropertyBlock();
radio = GetComponent<AudioSource>();
}
horn = this.gameObject.AddComponent<AudioSource>();
}

void OnEnable()
{
Expand All @@ -191,7 +198,7 @@ void Start()

currentRadioStationIndex = Random.Range(0, RadioStation.stations.Length);

Debug.LogFormat("Created vehicle - id {0}, name {1}, time: {2}", this.Definition.Id,
Debug.LogFormat("Created vehicle - id {0}, name {1}, time: {2}", this.Definition.Id,
Copy link
Owner

Choose a reason for hiding this comment

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

don't do style changes

this.Definition.GameName, F.CurrentDateForLogging);
}

Expand Down Expand Up @@ -295,7 +302,29 @@ private void SetLight(int index, float brightness)
_colorsChanged = true;
}

public VehicleDef Definition { get; private set; }
public virtual void PlayHornSound()
{
if (!IsHornOn) return;
if (horn.clip == null)
{
//TODO add vehicle dependent horn sound
var audioClip = Audio.AudioManager.CreateAudioClipFromSfx("GENRL", 67, 3);
horn.playOnAwake = false;
horn.spatialBlend = 1;
horn.maxDistance = 15;
horn.clip = MakeSubclip(audioClip, audioClip.length / 1.75f, (audioClip.length / 1.75f) + 0.05f);
}
if (horn && horn.clip)
{
if (!horn.isPlaying)
{
horn.loop = true;
horn.Play();
}
}
}

public VehicleDef Definition { get; private set; }

public Transform DriverTransform { get; private set; }

Expand Down Expand Up @@ -435,7 +464,11 @@ private void Update()
radio.Stop();
}
}
}

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

private void FixedUpdate()
{
Expand All @@ -461,5 +494,17 @@ public void ApplySyncRate(float syncRate)
this.NetTransform.syncInterval = 1.0f / syncRate;
}

}
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
10 changes: 10 additions & 0 deletions Assets/Scripts/Utilities/CustomInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down