2
0
Fork 0
mirror of https://github.com/GTA-ASM/SanAndreasUnity synced 2025-02-24 11:17:11 +00:00

coroutine done ?

This commit is contained in:
in0finite 2019-06-24 03:26:13 +02:00
parent beb76b6499
commit f0cc7be1e7

View file

@ -1,6 +1,8 @@
using UnityEngine;
using SanAndreasUnity.Utilities;
using System.Linq;
namespace SanAndreasUnity
namespace SanAndreasUnity.Behaviours
{
public class OutOfRangeDestroyer : MonoBehaviour
@ -11,9 +13,45 @@ namespace SanAndreasUnity
private float timeSinceOutOfRange = 0;
private void Start()
void Start()
{
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);
if (focusPoints.Count() < 1) {
// no focus points
// don't do anything
} else {
// 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;
} else {
this.timeSinceOutOfRange += 1.0f;
}
}
if (this.timeSinceOutOfRange >= this.timeUntilDestroyed) {
// timeout expired
Destroy(this.gameObject);
break;
}
}
}
private void Update()