SanAndreasUnity/Assets/Scripts/Behaviours/OutOfRangeDestroyer.cs

53 lines
1.5 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using UnityEngine;
2019-06-24 01:26:13 +00:00
using SanAndreasUnity.Utilities;
using System.Linq;
2020-05-31 17:07:22 +00:00
2019-06-24 01:26:13 +00:00
namespace SanAndreasUnity.Behaviours
2020-05-31 17:07:22 +00:00
{
2019-06-24 00:04:34 +00:00
public class OutOfRangeDestroyer : MonoBehaviour
2020-05-31 17:07:22 +00:00
{
2019-06-24 00:04:34 +00:00
public float timeUntilDestroyed = 5;
public float range = 250;
2020-05-31 17:07:22 +00:00
2019-06-24 00:04:34 +00:00
private float timeSinceOutOfRange = 0;
2019-06-24 01:29:44 +00:00
2019-06-24 01:26:13 +00:00
void Start()
2020-05-31 17:07:22 +00:00
{
2019-06-24 01:26:13 +00:00
if (NetUtils.IsServer)
this.StartCoroutine(this.DestroyCoroutine());
}
System.Collections.IEnumerator DestroyCoroutine()
{
while (true)
{
yield return new WaitForSeconds(1.0f);
// obtain focus points
var focusPoints = Net.Player.AllPlayersEnumerable.Where(p => p.OwnedPed != null).Select(p => p.OwnedPed.transform);
if (Camera.main != null)
focusPoints = focusPoints.Append(Camera.main.transform);
2019-06-25 20:26:38 +00:00
// check if we are in range of any focus point
Vector3 thisPosition = this.transform.position;
bool isInRange = focusPoints.Any(point => point.Distance(thisPosition) < this.range);
if (isInRange) {
this.timeSinceOutOfRange = 0;
2019-06-24 01:26:13 +00:00
} else {
2019-06-25 20:26:38 +00:00
this.timeSinceOutOfRange += 1.0f;
2019-06-24 01:26:13 +00:00
}
2019-06-25 20:26:38 +00:00
2019-06-24 01:26:13 +00:00
if (this.timeSinceOutOfRange >= this.timeUntilDestroyed) {
// timeout expired
Destroy(this.gameObject);
break;
}
}
2020-05-31 17:07:22 +00:00
}
}
2019-06-24 00:04:34 +00:00
}