CQ_Intelligent-Technology-T.../Assets/Improt/UTS/Scripts/Paths/SpawnPoint.cs

104 lines
2.8 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class MovePathParams
{
}
public class SpawnPoint : MonoBehaviour
{
private WalkPath _walkPath;
private bool _isForward;
private int _pathIndex;
private Queue<MovePathParams> _movePathQueue = new Queue<MovePathParams>();
public List<Collider> cols = new List<Collider>();
public static SpawnPoint PeopleCreate(
string name, Vector3 spawnPoint, Vector3 nextPoint,
float lineSpacing, int pathIndex, bool isForward, WalkPath walkPath,
float boxHeight = 3f, float boxLength = 10f
)
{
var go = new GameObject(name);
go.transform.position = spawnPoint;
var cl = go.AddComponent<BoxCollider>();
var spComponent = go.AddComponent<SpawnPoint>();
cl.isTrigger = true;
cl.transform.localScale = new Vector3(lineSpacing - 0.05f, boxHeight, boxLength);
go.transform.LookAt(nextPoint);
go.transform.localPosition += new Vector3(0f, boxHeight / 2f, 0f);
go.transform.Translate(Vector3.forward * boxLength / 2f);
spComponent._walkPath = walkPath;
spComponent._isForward = isForward;
spComponent._pathIndex = pathIndex;
return spComponent;
}
public static SpawnPoint CarCreate(
string name, Vector3 spawnPoint, Vector3 nextPoint,
float lineSpacing, int pathIndex, bool isForward, WalkPath walkPath,
float boxHeight = 3f, float boxLength = 10f
)
{
var go = new GameObject(name);
go.transform.position = spawnPoint;
var cl = go.AddComponent<BoxCollider>();
var spComponent = go.AddComponent<SpawnPoint>();
cl.isTrigger = true;
cl.transform.localScale = new Vector3(lineSpacing - 0.05f, boxHeight, boxLength);
go.transform.LookAt(nextPoint);
go.transform.localPosition += new Vector3(0f, boxHeight / 2f, 0f);
go.transform.Translate(Vector3.forward * boxLength / 2f);
spComponent._walkPath = walkPath;
spComponent._isForward = isForward;
spComponent._pathIndex = pathIndex;
return spComponent;
}
public void AddToSpawnQuery(MovePathParams movePathParams)
{
_movePathQueue.Enqueue(movePathParams);
}
private void FixedUpdate()
{
for (var i = cols.Count - 1; i > -1; i--)
{
if (cols[i] == null)
cols.RemoveAt(i);
}
if (cols.Count == 0 && _movePathQueue.Count > 0)
{
MovePathParams movePathParams = _movePathQueue.Dequeue();
_walkPath.SpawnOnePeople(
_pathIndex, _isForward
);
}
}
private void OnTriggerEnter(Collider other)
{
cols.Add(other);
}
private void OnTriggerExit(Collider other)
{
cols.Remove(other);
}
}