You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.5 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BuildSystem
{
public enum GridType {
General, Special, Initial
}
public class Grid
{
public int x { get; }
public int y { get; }
public int z { get; }
public Vector3Int logicPos;
[Header("相连的Grid")]
public Grid up;
public Grid down;
public Grid left;
public Grid right;
//四个方向有无车道 上右下左
public IsHave[] isHaves = new IsHave[4] { IsHave.None, IsHave.None , IsHave.None , IsHave.None };
public Vector3 pos { get; }
public bool isPlace = false;
public GridType gridType = GridType.General;
public PlaceObject placeObject;
public List<Grid> ground = new List<Grid>();
//放置的道路类型
public RoadType roadType;
//四个方向的车道数量 上右下左
public PlaceObjectType[] type = new PlaceObjectType[4] { PlaceObjectType.None, PlaceObjectType.None, PlaceObjectType.None, PlaceObjectType.None};
public Grid(Vector3Int logicPos) {
this.logicPos = logicPos;
x = this.logicPos.x;
y = this.logicPos.y;
z = this.logicPos.z;
float gridSize = BuildSystemManager.Instance.gridSize;
pos = BuildSystemManager.Instance.startPos + new Vector3(
gridSize / 2 + x * gridSize,
0,
gridSize / 2 + z * gridSize);
up = null;
down = null;
left = null;
right = null;
}
public Grid(int x,int y ,int z)
{
this.x = x;
this.y = y;
this.z = z;
logicPos = new Vector3Int(x,y,z); ;
float gridSize = BuildSystemManager.Instance.gridSize;
pos = BuildSystemManager.Instance.startPos + new Vector3(
gridSize / 2 + x * gridSize,
0,
gridSize / 2 + z * gridSize);
up = null;
down = null;
left = null;
right = null;
}
/// <summary>
/// 判断是否闭环
/// </summary>
/// <returns></returns>
public bool isClosedLoop() {
if (isHaves[0]==IsHave.Yes) {
if (up != null && up.placeObject && up.gridType != GridType.Special&& up.isHaves[2] != IsHave.Yes) {
return false;
}
}
if (isHaves[1] == IsHave.Yes)
{
if (right != null && right.placeObject && right.gridType != GridType.Special && right.isHaves[3] != IsHave.Yes)
{
return false;
}
}
if (isHaves[2] == IsHave.Yes)
{
if (down != null && down.placeObject && down.gridType != GridType.Special && down.isHaves[0] != IsHave.Yes)
{
return false;
}
}
if (isHaves[3] == IsHave.Yes)
{
if (left != null && left.placeObject && left.gridType != GridType.Special && left.isHaves[1] != IsHave.Yes)
{
return false;
}
}
return true;
}
/// <summary>
/// 移除物体初始化
/// </summary>
public void RemoveInit() {
isHaves = new IsHave[4] { IsHave.None, IsHave.None, IsHave.None, IsHave.None };
isPlace = false;
gridType = GridType.General;
roadType = RoadType.Straight;
type = new PlaceObjectType[4] { PlaceObjectType.None, PlaceObjectType.None, PlaceObjectType.None, PlaceObjectType.None };
}
/// <summary>
/// 记录周围不为空的相邻地块
/// </summary>
public void InitGround() {
if (up!=null && up.placeObject && up.gridType != GridType.Special)
{
ground.Add(up);
}
if (down != null && down.placeObject && down.gridType != GridType.Special)
{
ground.Add(down);
}
if (left != null && left.placeObject && left.gridType != GridType.Special)
{
ground.Add(left);
}
if (right != null && right.placeObject&& right.gridType != GridType.Special)
{
ground.Add(right);
}
}
}
}