mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2025-02-24 11:17:11 +00:00
coroutine done ?
This commit is contained in:
parent
beb76b6499
commit
f0cc7be1e7
1 changed files with 41 additions and 3 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Reference in a new issue