321 lines
11 KiB
C#
321 lines
11 KiB
C#
|
|
using Config.config;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// ·<><C2B7>
|
|||
|
|
/// </summary>
|
|||
|
|
public partial class SectionRoad : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
[HideInInspector] public int maxIndex;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
[HideInInspector] public int minIndex;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
private int recordMaxIndex;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>¼<EFBFBD><C2BC>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
private int recordMinIndex;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// NPC<50><43><EFBFBD>ڵ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public Transform npcCarNode;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ
|
|||
|
|
/// </summary>
|
|||
|
|
public GameObject leftBuilding;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ҽ<EFBFBD><D2BD><EFBFBD>Ⱥ
|
|||
|
|
/// </summary>
|
|||
|
|
public GameObject rightBuilding;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public List<GameObject> lane;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ɵ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public List<Transform> generatePoints;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// NPC<50><43>
|
|||
|
|
/// </summary>
|
|||
|
|
private List<NPCCar> npcCars = new List<NPCCar>();
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
private List<Train> trains = new List<Train>();
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// ·<><C2B7>
|
|||
|
|
/// </summary>
|
|||
|
|
private List<GameObject> roadblockObjs = new List<GameObject>();
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
private List<int> generatingDistance = new List<int>() { 0, -15, -30, -45, -60 };
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ʼ<EFBFBD><CABC>
|
|||
|
|
/// </summary>
|
|||
|
|
public void Init()
|
|||
|
|
{
|
|||
|
|
minIndex = 0;
|
|||
|
|
maxIndex = lane.Count - 1;
|
|||
|
|
recordMaxIndex = maxIndex;
|
|||
|
|
recordMinIndex = minIndex;
|
|||
|
|
for (int i = 0; i < lane.Count; i++)
|
|||
|
|
{
|
|||
|
|
lane[i].gameObject.SetActive(true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public void InitLaneNumber(int number)
|
|||
|
|
{
|
|||
|
|
int currenLaneNumber = GetCurrentLaneNumber();
|
|||
|
|
bool isShow = currenLaneNumber > number ? false : true;//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ʾ
|
|||
|
|
bool isLeft = false;
|
|||
|
|
for (int i = 0; i < Mathf.Abs(currenLaneNumber - number); i++)
|
|||
|
|
{
|
|||
|
|
isLeft = ProbabilityManager.Instance.GetPercent(50);
|
|||
|
|
|
|||
|
|
if (isShow)
|
|||
|
|
{
|
|||
|
|
if (minIndex == recordMinIndex)
|
|||
|
|
{
|
|||
|
|
isLeft = false;
|
|||
|
|
}
|
|||
|
|
else if (maxIndex == recordMaxIndex)
|
|||
|
|
{
|
|||
|
|
isLeft = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (isLeft)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ
|
|||
|
|
{
|
|||
|
|
if (isShow)
|
|||
|
|
{
|
|||
|
|
lane[--minIndex].gameObject.SetActive(true);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
lane[minIndex++].gameObject.SetActive(false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
if (isShow)
|
|||
|
|
{
|
|||
|
|
lane[++maxIndex].gameObject.SetActive(true);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
lane[maxIndex--].gameObject.SetActive(false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
SetBuilding();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
|
|||
|
|
/// </summary>
|
|||
|
|
public void GenerateRoadblock(SectionRoad sectionRoad)
|
|||
|
|
{
|
|||
|
|
PutRoadblockObjs();
|
|||
|
|
for (int i = 0; i < sectionRoad.lane.Count; i++)
|
|||
|
|
{
|
|||
|
|
if (lane[i].activeInHierarchy && !sectionRoad.lane[i].activeInHierarchy)
|
|||
|
|
{
|
|||
|
|
for (int j = 0; j < 3; j++)
|
|||
|
|
{
|
|||
|
|
GameObject roadblockObj = CachePoolManager.Instance.Take(YooAssetManager.Instance.LoadAssetSync<GameObject>(PackagesName.GameData, "Roadblock"));
|
|||
|
|
roadblockObj.transform.position = lane[i].transform.position + Vector3.right * (300 - j * 20);
|
|||
|
|
roadblockObj.transform.localEulerAngles = Vector3.zero;
|
|||
|
|
roadblockObj.transform.localScale = Vector3.one;
|
|||
|
|
roadblockObjs.Add(roadblockObj);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
|
/// </summary>
|
|||
|
|
public void BothAreConsistent(SectionRoad sectionRoad)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < sectionRoad.lane.Count; i++)
|
|||
|
|
{
|
|||
|
|
this.lane[i].gameObject.SetActive(sectionRoad.lane[i].activeInHierarchy);
|
|||
|
|
}
|
|||
|
|
this.minIndex = sectionRoad.minIndex;
|
|||
|
|
this.maxIndex = sectionRoad.maxIndex;
|
|||
|
|
SetBuilding();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ؿճ<D8BF><D5B3><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public (Vector3, int) GetEmptyLane()
|
|||
|
|
{
|
|||
|
|
List<int> ints = new List<int> { 0, 1, 2, 3, 4, 5 };
|
|||
|
|
int number;
|
|||
|
|
while (true)
|
|||
|
|
{
|
|||
|
|
number = ints[Random.Range(0, ints.Count)];
|
|||
|
|
if (lane[number].activeInHierarchy && occupationIndex.Find(s => s.index == number) == null)
|
|||
|
|
{
|
|||
|
|
return (lane[number].transform.localPosition, number);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
ints.Remove(number);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>Żػ<C5BB><D8BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public void PutCar()
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < npcCars.Count; i++)
|
|||
|
|
{
|
|||
|
|
CachePoolManager.Instance.Put(npcCars[i].gameObject);
|
|||
|
|
}
|
|||
|
|
for (int i = 0; i < trains.Count; i++)
|
|||
|
|
{
|
|||
|
|
CachePoolManager.Instance.Put(trains[i].gameObject);
|
|||
|
|
}
|
|||
|
|
npcCars.Clear();
|
|||
|
|
trains.Clear();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// ·<>ϷŻػ<C5BB><D8BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public void PutRoadblockObjs()
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < roadblockObjs.Count; i++)
|
|||
|
|
{
|
|||
|
|
CachePoolManager.Instance.Put(roadblockObjs[i].gameObject);
|
|||
|
|
}
|
|||
|
|
roadblockObjs.Clear();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD>Ⱥ
|
|||
|
|
/// </summary>
|
|||
|
|
public void SetBuilding()
|
|||
|
|
{
|
|||
|
|
leftBuilding.transform.localPosition = new Vector3((lane[minIndex].transform.localPosition - Vector3.right * 13f).x, leftBuilding.transform.localPosition.y, 0);
|
|||
|
|
rightBuilding.transform.localPosition = new Vector3((lane[maxIndex].transform.localPosition + Vector3.right * 13f).x, rightBuilding.transform.localPosition.y, 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public void SetBuildingTheme(LevelConfig levelConfig)
|
|||
|
|
{
|
|||
|
|
GameObject building;
|
|||
|
|
switch (levelConfig.Theme)
|
|||
|
|
{
|
|||
|
|
case "Country":
|
|||
|
|
building = Instantiate(YooAssetManager.Instance.LoadAssetSync<GameObject>(PackagesName.GameData, "LeftCountryBuilding"));
|
|||
|
|
building.transform.SetParent(leftBuilding.transform);
|
|||
|
|
building.transform.localEulerAngles = Vector3.zero;
|
|||
|
|
building.transform.localScale = Vector3.one;
|
|||
|
|
building.transform.localPosition = Vector3.zero;
|
|||
|
|
building = Instantiate(YooAssetManager.Instance.LoadAssetSync<GameObject>(PackagesName.GameData, "RightCountryBuilding"));
|
|||
|
|
building.transform.SetParent(rightBuilding.transform);
|
|||
|
|
building.transform.localEulerAngles = Vector3.zero;
|
|||
|
|
building.transform.localScale = Vector3.one;
|
|||
|
|
building.transform.localPosition = Vector3.zero;
|
|||
|
|
break;
|
|||
|
|
case "City":
|
|||
|
|
building = Instantiate(YooAssetManager.Instance.LoadAssetSync<GameObject>(PackagesName.GameData, "LeftCityBuilding"));
|
|||
|
|
building.transform.SetParent(leftBuilding.transform);
|
|||
|
|
building.transform.localEulerAngles = Vector3.zero;
|
|||
|
|
building.transform.localScale = Vector3.one;
|
|||
|
|
building.transform.localPosition = Vector3.zero;
|
|||
|
|
building = Instantiate(YooAssetManager.Instance.LoadAssetSync<GameObject>(PackagesName.GameData, "RightCityBuilding"));
|
|||
|
|
building.transform.SetParent(rightBuilding.transform);
|
|||
|
|
building.transform.localEulerAngles = Vector3.zero;
|
|||
|
|
building.transform.localScale = Vector3.one;
|
|||
|
|
building.transform.localPosition = Vector3.zero;
|
|||
|
|
break;
|
|||
|
|
case "MountainousRegion":
|
|||
|
|
building = Instantiate(YooAssetManager.Instance.LoadAssetSync<GameObject>(PackagesName.GameData, "LeftMountainousRegionBuilding"));
|
|||
|
|
building.transform.SetParent(leftBuilding.transform);
|
|||
|
|
building.transform.localEulerAngles = Vector3.zero;
|
|||
|
|
building.transform.localScale = Vector3.one;
|
|||
|
|
building.transform.localPosition = Vector3.zero;
|
|||
|
|
building = Instantiate(YooAssetManager.Instance.LoadAssetSync<GameObject>(PackagesName.GameData, "RightMountainousRegionBuilding"));
|
|||
|
|
building.transform.SetParent(rightBuilding.transform);
|
|||
|
|
building.transform.localEulerAngles = Vector3.zero;
|
|||
|
|
building.transform.localScale = Vector3.one;
|
|||
|
|
building.transform.localPosition = Vector3.zero;
|
|||
|
|
break;
|
|||
|
|
case "Desert":
|
|||
|
|
building = Instantiate(YooAssetManager.Instance.LoadAssetSync<GameObject>(PackagesName.GameData, "LeftDesertBuilding"));
|
|||
|
|
building.transform.SetParent(leftBuilding.transform);
|
|||
|
|
building.transform.localEulerAngles = Vector3.zero;
|
|||
|
|
building.transform.localScale = Vector3.one;
|
|||
|
|
building.transform.localPosition = Vector3.zero;
|
|||
|
|
building = Instantiate(YooAssetManager.Instance.LoadAssetSync<GameObject>(PackagesName.GameData, "RightDesertBuilding"));
|
|||
|
|
building.transform.SetParent(rightBuilding.transform);
|
|||
|
|
building.transform.localEulerAngles = Vector3.zero;
|
|||
|
|
building.transform.localScale = Vector3.one;
|
|||
|
|
building.transform.localPosition = Vector3.zero;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>õ<EFBFBD><C3B5><EFBFBD>ǰ<EFBFBD><C7B0>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int GetCurrentLaneNumber()
|
|||
|
|
{
|
|||
|
|
int number = 0;
|
|||
|
|
for (int i = 0; i < lane.Count; i++)
|
|||
|
|
{
|
|||
|
|
if (lane[i].activeInHierarchy)
|
|||
|
|
{
|
|||
|
|
number++;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return number;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public (Vector3, int) GetBirthPoint()
|
|||
|
|
{
|
|||
|
|
int randomIndex = Random.Range(minIndex + 1, maxIndex);
|
|||
|
|
return (lane[randomIndex].transform.position + transform.forward * 20, randomIndex);
|
|||
|
|
}
|
|||
|
|
}
|