using System.Collections; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif namespace TBTK{ public class GridGenerator : MonoBehaviour { #if UNITY_EDITOR public static bool inspector=false; #endif public const float spaceXHex=0.75f; public const float spaceZHex=0.865f; [HideInInspector] [Space(8)] public Transform gridParent; [Space(8)] public Transform nodePrefabSq; public Transform nodePrefabHex; //~ [Space(8)] //~ [HideInInspector] public Transform obstaclePrefabSq; //~ [HideInInspector] public Transform obstaclePrefabHex; //~ [HideInInspector] public Transform wallPrefab; [Space(8)] public List obstaclePrefabSqF; public List obstaclePrefabSqH; public List obstaclePrefabHexF; public List obstaclePrefabHexH; public List wallPrefabF; public List wallPrefabH; [Space(8)] public Material matHexFog; public Material matSqFog; [HideInInspector] [Space(8)] public Transform gridObjParent; public static Transform GetGridObjParent(){ if(instance.gridObjParent==null){ instance.gridObjParent=new GameObject("GridItems").transform; instance.gridObjParent.parent=instance.transform.parent; } return instance.gridObjParent; } public static GridGenerator instance; public void Init(){ instance=this; } [ContextMenu ("Clear")] public void Clear(){ DeleteGrid(); } //public int seed=10; [ContextMenu ("Generate")] public void Generate(){ Generate(null); } public void Generate(GridManager gm){ if(gm==null) gm=gameObject.GetComponent(); if(gm==null) return; Debug.Log("Generate Grid"); instance=this; gm.SetupInstance(); GridManager.InitNodeDirList(); DeleteGrid(); gm.CacheGridSetting(); //Random.InitState(seed); if(gm.gridType==_GridType.SquareGrid) GenerateSquare(gm); if(gm.gridType==_GridType.HexGrid) GenerateHex(gm); } public void GenerateHex(GridManager gm){ float offsetX=gm.GetOffsetX();//dimensionX*gm.gridSize/2f-gm.gridSize*.5f; float offsetZ=gm.GetOffsetZ();//dimensionZ*gm.gridSize/2f-gm.gridSize*.5f; float spaceX=spaceXHex*gm._GetNodeSize();//*GridToTileRatio; float spaceZ=spaceZHex*gm._GetNodeSize();//*GridToTileRatio; List allNodeList=new List(); for(int x=0; x list=new List(); int count=0; for(int z=0; z allNodeList=new List(); int count=0; for(int x=0; x list=new List(); for(int z=0; z allNodeList){ for(int x=0; x(); comMesh.matHexFog=matHexFog; comMesh.matSqFog=matSqFog; Transform prefab=(gm.gridType==_GridType.SquareGrid ? nodePrefabSq : nodePrefabHex); Debug.Log("Generate common doing something to nodes' objT"); for(int x=0; x()); } node.objT.gameObject.GetComponent().enabled=node.walkable; //node.objT.gameObject.SetActive(node.walkable); } } //generate obstacle int obsCount=(int)(allNodeList.Count*Mathf.Min(gm.obstacleRate, 0.5f)); loopCount=obsCount*3; for(int i=0; i neighbourList=node.GetNeighbourList(true); if(neighbourList.Count<=1){ //Debug.Log("node has no neighbour "+node.objT.name+" "+node.GetNeighbourList(false).Count); return false; } Node neighbour=neighbourList[Rand.Range(0, neighbourList.Count)]; if(neighbour==null) return false; node.AddWall(node.GetPos()+(neighbour.GetPos()-node.GetPos()), wallPrefab); //~ for(int i=0; i path=AStar.SearchWalkableNode(node, neighbourList[i], false); //~ if(path.Count==0){ flag=false; break; } //~ } //~ if(!flag) node.RemoveWall(neighbour.GetPos()-node.GetPos()); List path=AStar.SearchWalkableNode(node, neighbour, AStar.BypassUnitCode(false)); if(path.Count<=0) node.RemoveWall(node.GetPos()+(neighbour.GetPos()-node.GetPos())); return path.Count>0; //~ if(!flag){ //~ node.RemoveWall(node.GetPos()+(neighbour.GetPos()-node.GetPos())); //~ Debug.Log("no cant do"); //~ } //~ return flag; } private bool CheckAccessObstacle(Node node, bool flag=true, int sIdx=0){ List neighbours=node.GetNeighbourList(); bool cachedWalkable=node.walkable; node.walkable=false; for(int i=0; i path=AStar.SearchWalkableNode(neighbours[sIdx], neighbours[i], AStar.BypassUnitCode(false)); if(path.Count==0){ flag=false; break; } } node.walkable=cachedWalkable; return flag; } public void DeleteGrid(){ ClearUnit(); ClearCollectible(); GridManager gm=gameObject.GetComponent(); if(gm==null) return; CacheDeploymentAndSpawnArea(gm); for(int x=0; x=0){ for(int i=0; i=0){ for(int i=0; i(); if(gm==null) return; for(int x=0; x=0){ if(!gm.grid[x][z].walkable || gm.grid[x][z].HasObstacle()) gm.grid[x][z].ClearDeploymentFac(); } if(gm.grid[x][z].spawnGroupFacID>=0){ if(!gm.grid[x][z].walkable || gm.grid[x][z].HasObstacle()) gm.grid[x][z].ClearSpawnGroup(); } } } } //~ public static Node GetNode(int x, int z){ //~ //Debug.Log(x+","+z+" "+instance.grid.Count+" "+instance.nList.Count); //~ //return instance.nList[x].myList[z]; //~ return instance.grid[x][z]; //~ } //~ public static Node GetNode(Vector3 point){ //~ Vector2 coor=new Vector2(point.x, point.z)/GridGenerator.GetNodeSize(); //~ return GetNode((int)Mathf.Round(coor.x), (int)Mathf.Round(coor.y)); //~ } public void GenerateUnit(){ UnitManager um=ClearUnit(); if(um==null) return; for(int i=0; i(); UnitManager um=(UnitManager)FindObjectOfType(typeof(UnitManager)); if(um==null){ Debug.LogWarning("No UnitManager in the scene"); return null; } for(int x=0; x list; public ListWrapper(List l){ list=l; } } //~ [System.Serializable] //~ public class Boundary{ //~ public int minX=0; public int maxX=0; //~ public int minZ=0; public int maxZ=0; //~ public bool CheckBound(Node node){ //~ Verify(); //~ if(node.idxX<=minX || node.idxX>=maxX) return false; //~ if(node.idxZ<=minZ || node.idxZ>=maxZ) return false; //~ return true; //~ } //~ public void Verify(){ //~ if(maxX