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 ground = new List(); //放置的道路类型 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; } /// /// 判断是否闭环 /// /// public bool isClosedLoop() { if (isHaves[0] == IsHave.Yes) { if (up != null && up.isPlace) { if (!(up.isHaves[2] == IsHave.Yes || up.gridType == GridType.Special)) { return false; } } else { return false; } } if (isHaves[1] == IsHave.Yes) { if (right != null && right.isPlace) { if (!(right.isHaves[3] == IsHave.Yes || right.gridType == GridType.Special)) { return false; } } else { return false; } } if (isHaves[2] == IsHave.Yes) { if (down != null && down.isPlace) { if (!(down.isHaves[0] == IsHave.Yes || down.gridType == GridType.Special)) { return false; } } else { return false; } } if (isHaves[3] == IsHave.Yes) { if (left != null && left.isPlace) { if (!(left.isHaves[1] == IsHave.Yes || left.gridType == GridType.Special)) { return false; } } else { return false; } } return true; } /// /// 移除物体初始化 /// 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 }; } /// /// 记录周围不为空的相邻地块 /// public void InitGround() { ground.Clear(); if (up!=null && up.placeObject && up.gridType != GridType.Special) { if (isHaves[0] == IsHave.Yes && up.isHaves[2] == IsHave.Yes) { ground.Add(up); } } if (down != null && down.placeObject && down.gridType != GridType.Special) { if (isHaves[2] == IsHave.Yes && down.isHaves[0] == IsHave.Yes) { ground.Add(down); } } if (left != null && left.placeObject && left.gridType != GridType.Special) { if (isHaves[3] == IsHave.Yes && left.isHaves[1] == IsHave.Yes) { ground.Add(left); } } if (right != null && right.placeObject&& right.gridType != GridType.Special) { if (isHaves[1] == IsHave.Yes && right.isHaves[3] == IsHave.Yes) { ground.Add(right); } } } } }