2019-11-08 11:54:45 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours.Vehicles
|
|
|
|
|
{
|
|
|
|
|
public partial class Vehicle
|
|
|
|
|
{
|
2020-05-04 23:47:50 +00:00
|
|
|
|
private int m_currentRadioStationIndex;
|
2020-05-05 12:02:35 +00:00
|
|
|
|
public int CurrentRadioStationIndex => m_currentRadioStationIndex;
|
|
|
|
|
|
2019-11-18 13:56:12 +00:00
|
|
|
|
private AudioSource m_radioAudioSource;
|
2020-05-05 12:02:35 +00:00
|
|
|
|
public AudioSource RadioAudioSource => m_radioAudioSource;
|
2019-11-08 11:54:45 +00:00
|
|
|
|
|
2020-05-04 23:39:16 +00:00
|
|
|
|
bool m_isPlayingRadio = false;
|
2020-05-05 12:02:35 +00:00
|
|
|
|
public bool IsPlayingRadio => m_isPlayingRadio;
|
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
bool m_radio_pedAssignedToVehicleLastFrame = false;
|
2020-05-05 12:02:35 +00:00
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
bool m_isWaitingForNewRadioSound = false;
|
2020-05-05 12:02:35 +00:00
|
|
|
|
public bool IsWaitingForNewRadioSound => m_isWaitingForNewRadioSound;
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
2020-05-07 16:11:31 +00:00
|
|
|
|
public float TimeWhenRadioStationChanged { get; private set; } = float.NegativeInfinity;
|
|
|
|
|
public float TimeSinceRadioStationChanged => Time.time - this.TimeWhenRadioStationChanged;
|
|
|
|
|
|
2020-05-04 15:56:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Awake_Radio()
|
|
|
|
|
{
|
|
|
|
|
m_radioAudioSource = this.GetComponent<AudioSource>();
|
2020-05-05 16:23:52 +00:00
|
|
|
|
m_currentRadioStationIndex = Random.Range(0, RadioStation.stations.Length);
|
2020-05-04 15:56:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-07 20:21:05 +00:00
|
|
|
|
void OnDisable_Radio()
|
|
|
|
|
{
|
|
|
|
|
// need to destroy audio clip here, because otherwise it will stay in memory
|
|
|
|
|
this.StopPlayingRadio();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 15:56:22 +00:00
|
|
|
|
void Start_Radio()
|
|
|
|
|
{
|
2020-05-05 16:23:52 +00:00
|
|
|
|
|
2020-05-04 15:56:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 21:57:52 +00:00
|
|
|
|
void OnPedAssignedToVehicle_Radio(Ped ped, Seat seat)
|
2020-05-04 23:39:16 +00:00
|
|
|
|
{
|
2020-05-04 23:47:50 +00:00
|
|
|
|
m_radio_pedAssignedToVehicleLastFrame = true;
|
2020-05-04 23:39:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 15:56:22 +00:00
|
|
|
|
void Update_Radio()
|
|
|
|
|
{
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
bool pedAssignedToVehicleLastFrame = m_radio_pedAssignedToVehicleLastFrame;
|
|
|
|
|
m_radio_pedAssignedToVehicleLastFrame = false;
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
|
|
|
|
if (m_isPlayingRadio)
|
2020-05-04 15:56:22 +00:00
|
|
|
|
{
|
2020-05-04 23:39:16 +00:00
|
|
|
|
// check if we should stop playing radio sound
|
|
|
|
|
|
|
|
|
|
// radio turned off => no sound
|
2020-05-04 23:47:50 +00:00
|
|
|
|
if (m_currentRadioStationIndex < 0)
|
2020-05-04 23:39:16 +00:00
|
|
|
|
{
|
|
|
|
|
this.StopPlayingRadio();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no local ped inside => no sound
|
|
|
|
|
if (!this.IsLocalPedInside())
|
|
|
|
|
{
|
|
|
|
|
this.StopPlayingRadio();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we should continue playing sound
|
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
if (m_isWaitingForNewRadioSound)
|
2020-05-04 23:39:16 +00:00
|
|
|
|
{
|
|
|
|
|
// we are waiting for sound to load
|
|
|
|
|
// don't do anything
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-05-04 15:56:22 +00:00
|
|
|
|
{
|
2020-05-04 23:39:16 +00:00
|
|
|
|
// check if sound finished playing
|
|
|
|
|
if (!m_radioAudioSource.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
// sound finished playing
|
|
|
|
|
// switch to next stream in a current radio station
|
|
|
|
|
this.StopPlayingRadio();
|
2020-05-04 23:47:50 +00:00
|
|
|
|
RadioStation.stations[m_currentRadioStationIndex].NextClip();
|
2020-05-04 23:39:16 +00:00
|
|
|
|
this.StartPlayingRadio(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-07 21:06:37 +00:00
|
|
|
|
// update current station time - this is needed in case vehicle gets destroyed - current time will not be updated - or maybe it will ?
|
2020-05-04 23:47:50 +00:00
|
|
|
|
RadioStation.stations[m_currentRadioStationIndex].currentTime = m_radioAudioSource.time;
|
2020-05-04 15:56:22 +00:00
|
|
|
|
}
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
2020-05-04 15:56:22 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-04 23:39:16 +00:00
|
|
|
|
// not playing radio sound
|
|
|
|
|
// check if we should start playing
|
|
|
|
|
// this can happen only if local ped entered vehicle
|
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
if (pedAssignedToVehicleLastFrame)
|
2020-05-04 15:56:22 +00:00
|
|
|
|
{
|
2020-05-04 23:39:16 +00:00
|
|
|
|
if (this.IsLocalPedInside())
|
|
|
|
|
{
|
|
|
|
|
// local ped is in vehicle
|
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
if (m_currentRadioStationIndex >= 0)
|
2020-05-04 23:39:16 +00:00
|
|
|
|
{
|
|
|
|
|
// start playing sound
|
|
|
|
|
this.StartPlayingRadio(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-04 15:56:22 +00:00
|
|
|
|
}
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
|
|
|
|
// continue not playing radio sound
|
|
|
|
|
|
2020-05-04 15:56:22 +00:00
|
|
|
|
}
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
2020-05-04 15:56:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 23:39:16 +00:00
|
|
|
|
void StopPlayingRadio()
|
2019-11-08 11:54:45 +00:00
|
|
|
|
{
|
2020-05-04 23:39:16 +00:00
|
|
|
|
if (!m_isPlayingRadio)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_isPlayingRadio = false;
|
2020-05-04 23:47:50 +00:00
|
|
|
|
m_isWaitingForNewRadioSound = false;
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
2020-05-07 21:04:14 +00:00
|
|
|
|
// save current station's time
|
|
|
|
|
if (m_currentRadioStationIndex >= 0 && m_radioAudioSource.isPlaying)
|
2020-05-04 23:47:50 +00:00
|
|
|
|
RadioStation.stations[m_currentRadioStationIndex].currentTime = m_radioAudioSource.time;
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
2019-11-18 13:56:12 +00:00
|
|
|
|
m_radioAudioSource.Stop();
|
2020-05-07 20:21:05 +00:00
|
|
|
|
|
|
|
|
|
this.DestroyCurrentRadioClip();
|
2020-05-04 23:39:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StartPlayingRadio(bool playImmediately)
|
|
|
|
|
{
|
|
|
|
|
if (m_isPlayingRadio)
|
|
|
|
|
return;
|
2020-05-04 23:47:50 +00:00
|
|
|
|
if (m_currentRadioStationIndex < 0)
|
2020-05-04 23:39:16 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_isPlayingRadio = true;
|
2020-05-04 23:47:50 +00:00
|
|
|
|
m_isWaitingForNewRadioSound = false;
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
|
|
|
|
if (playImmediately)
|
|
|
|
|
this.LoadAndPlayRadioSoundNow();
|
|
|
|
|
else
|
|
|
|
|
this.RequestNewRadioSound();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RequestNewRadioSound()
|
|
|
|
|
{
|
|
|
|
|
this.CancelInvoke(nameof(this.LoadRadioSoundDelayed));
|
2020-05-04 23:47:50 +00:00
|
|
|
|
m_isWaitingForNewRadioSound = true;
|
|
|
|
|
this.Invoke(nameof(this.LoadRadioSoundDelayed), 1.0f);
|
2020-05-04 23:39:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LoadRadioSoundDelayed()
|
|
|
|
|
{
|
2020-05-04 23:47:50 +00:00
|
|
|
|
if (!m_isWaitingForNewRadioSound) // canceled in the meantime
|
2020-05-04 23:39:16 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
m_isWaitingForNewRadioSound = false;
|
2020-05-04 23:39:16 +00:00
|
|
|
|
|
|
|
|
|
if (!m_isPlayingRadio)
|
|
|
|
|
return;
|
2020-05-04 23:47:50 +00:00
|
|
|
|
if (m_currentRadioStationIndex < 0)
|
2020-05-04 23:39:16 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.LoadAndPlayRadioSoundNow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LoadAndPlayRadioSoundNow()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
m_radioAudioSource.Stop(); // just in case
|
2019-11-18 14:21:13 +00:00
|
|
|
|
|
2020-05-07 20:21:05 +00:00
|
|
|
|
this.DestroyCurrentRadioClip();
|
2019-11-18 14:21:13 +00:00
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
var clip = RadioStation.stations[m_currentRadioStationIndex].LoadCurrentClip();
|
2019-11-08 11:54:45 +00:00
|
|
|
|
if (clip != null)
|
|
|
|
|
{
|
2019-11-18 13:56:12 +00:00
|
|
|
|
m_radioAudioSource.clip = clip;
|
2020-05-04 23:47:50 +00:00
|
|
|
|
m_radioAudioSource.time = RadioStation.stations[m_currentRadioStationIndex].currentTime;
|
2019-11-18 13:56:12 +00:00
|
|
|
|
m_radioAudioSource.Play();
|
2019-11-08 11:54:45 +00:00
|
|
|
|
|
2020-05-07 21:05:03 +00:00
|
|
|
|
// this shouldn't be done because if the audio source is stopped and started again (eg. when ped exits the vehicle),
|
|
|
|
|
// the clip may get destroyed before the audio source finishes playing
|
|
|
|
|
//Destroy(clip, clip.length);
|
2019-11-08 11:54:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-07 20:21:05 +00:00
|
|
|
|
void DestroyCurrentRadioClip()
|
|
|
|
|
{
|
|
|
|
|
if (m_radioAudioSource.clip != null)
|
|
|
|
|
{
|
|
|
|
|
Destroy(m_radioAudioSource.clip);
|
|
|
|
|
m_radioAudioSource.clip = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 11:54:45 +00:00
|
|
|
|
public void SwitchRadioStation(bool next)
|
|
|
|
|
{
|
2020-05-04 23:47:50 +00:00
|
|
|
|
int index = m_currentRadioStationIndex;
|
2019-11-18 14:08:57 +00:00
|
|
|
|
|
2019-11-08 11:54:45 +00:00
|
|
|
|
if (next)
|
|
|
|
|
{
|
2020-05-04 23:39:16 +00:00
|
|
|
|
index++;
|
2019-11-08 11:54:45 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-04 23:39:16 +00:00
|
|
|
|
index--;
|
|
|
|
|
if (index < -1)
|
|
|
|
|
index = RadioStation.stations.Length - 1;
|
2019-11-08 11:54:45 +00:00
|
|
|
|
}
|
2019-11-18 14:08:57 +00:00
|
|
|
|
|
2020-05-04 23:39:16 +00:00
|
|
|
|
this.SwitchRadioStation(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SwitchRadioStation(int index)
|
|
|
|
|
{
|
|
|
|
|
if (index < -1 || index >= RadioStation.stations.Length)
|
|
|
|
|
index = -1;
|
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
if (m_currentRadioStationIndex == index)
|
2020-05-04 23:39:16 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2020-05-07 16:11:31 +00:00
|
|
|
|
this.TimeWhenRadioStationChanged = Time.time;
|
|
|
|
|
|
2020-05-04 23:39:16 +00:00
|
|
|
|
this.StopPlayingRadio();
|
|
|
|
|
|
2020-05-04 23:47:50 +00:00
|
|
|
|
m_currentRadioStationIndex = index;
|
2019-11-18 14:08:57 +00:00
|
|
|
|
|
2020-05-04 23:39:16 +00:00
|
|
|
|
if (this.IsLocalPedInside())
|
|
|
|
|
this.StartPlayingRadio(false);
|
2019-11-08 11:54:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|