void Start() { //run through the list, generate a list of PathSection //if the element is a platform, generate the node and set it to walkable for (int i = 0; i < waypoints.Length; i++) { Transform wp = waypoints[i]; //check if this is a platform, BuildManager would have add the component and have them layered if (waypoints[i] != null) { if (wp.gameObject.layer == LayerManager.LayerPlatform()) { //Debug.Log("platform"); PlatformTD platform = wp.gameObject.GetComponent <PlatformTD>(); path.Add(new PathSection(platform)); } else { path.Add(new PathSection(wp.position)); } } } //scan through the path, setup the platform pathSection if there's any //first initiate all the basic parameter for (int i = 0; i < path.Count; i++) { PathSection pSec = path[i]; if (path[i].platform != null) { //get previous and next pathSection PathSection sec1 = path[Mathf.Max(0, i - 1)]; PathSection sec2 = path[Mathf.Min(path.Count - 1, i + 1)]; //path[i].platform.SetNeighbouringWP(sec1, sec2); pSec.platform.SetPathObject(this, pSec, sec1, sec2); pSec.platform.SetWalkable(true); if (!pSec.platform.IsNodeGenerated()) { pSec.platform.GenerateNode(heightOffsetOnPlatform); } pSec.platform.SearchForNewPath(pSec); } } //now the basic have been setup, setup the path //~ for(int i=0; i<path.Count; i++){ //~ if(path[i].platform!=null){ //~ path.platform.GetPath(); //~ } //~ } if (generatePathObject) { StartCoroutine(CreateLinePath()); } }
//called by any external component to build tower public static string BuildTowerPointNBuild(UnitTower tower, Vector3 pos, PlatformTD platform) { //dont allow building of resource tower before game started if (tower.type == _TowerType.ResourceTower && GameControl.gameState == _GameState.Idle) { //GameMessage.DisplayMessage("Cant Build Tower before spawn start"); return("Cant Build Tower before spawn start"); } //type defined buildable check, obsolete //bool matched=false; //foreach(_TowerType type in platform.buildableType){ // if(tower.type==type){ // matched=true; // break; // } //} //if(!matched) return "Invalid Tower Type"; //check if there are sufficient resource int[] cost = tower.GetCost(); if (GameControl.HaveSufficientResource(cost)) { GameControl.SpendResource(cost); GameObject towerObj = (GameObject)Instantiate(tower.thisObj, pos, platform.thisT.rotation); UnitTower towerCom = towerObj.GetComponent <UnitTower>(); towerCom.InitTower(towerCount += 1); //register the tower to the platform if (platform != null) { platform.Build(pos, towerCom); } //~ if(tower.type!=_TowerType.Mine) //~ currentBuildInfo.platform.Build(currentBuildInfo.position, tower); //clear the build info and indicator for build manager ClearBuildPoint(); return(""); } //GameMessage.DisplayMessage("Insufficient Resource"); return("Insufficient Resource"); }
void Awake() { platform = (PlatformTD)target; GetTower(); EditorUtility.SetDirty(platform); //~ typeLabel=new string[7]; //~ typeLabel[0]="TurretTower"; //~ typeLabel[1]="AOETower"; //~ typeLabel[2]="DirectionalAOETower"; //~ typeLabel[3]="SupportTower"; //~ typeLabel[4]="ResourceTower"; //~ typeLabel[5]="Mine"; //~ typeLabel[6]="Block"; }
// Use this for initialization void InitPlatform() { if (autoSearchForPlatform) { LayerMask mask = 1 << LayerManager.LayerPlatform(); Collider[] platformCols = Physics.OverlapSphere(Vector3.zero, Mathf.Infinity, mask); platforms = new Transform[platformCols.Length]; for (int j = 0; j < platformCols.Length; j++) { platforms[j] = platformCols[j].transform; } } buildPlatforms = new PlatformTD[platforms.Length]; int i = 0; foreach (Transform basePlane in platforms) { //clear the platform of any unneeded collider ClearPlatformColliderRecursively(basePlane); //if the platform object havent got a platform componet on it, assign it PlatformTD platform = basePlane.gameObject.GetComponent <PlatformTD>(); if (platform == null) { platform = basePlane.gameObject.AddComponent <PlatformTD>(); //~ platform.buildableType=new _TowerType[7]; //~ //by default, all tower type is builidable //~ platform.buildableType[0]=_TowerType.TurretTower; //~ platform.buildableType[1]=_TowerType.AOETower; //~ platform.buildableType[2]=_TowerType.DirectionalAOETower; //~ platform.buildableType[3]=_TowerType.SupportTower; //~ platform.buildableType[4]=_TowerType.ResourceTower; //~ platform.buildableType[5]=_TowerType.Mine; //~ platform.buildableType[6]=_TowerType.Block; } buildPlatforms[i] = platform; buildPlatforms[i].InitTowerList(towerAvaiList); //make sure the plane is perfectly horizontal, rotation around the y-axis is presreved basePlane.eulerAngles = new Vector3(0, basePlane.rotation.eulerAngles.y, 0); //adjusting the scale float scaleX = Mathf.Floor(UnitUtility.GetWorldScale(basePlane).x *10 / gridSize) * gridSize * 0.1f; float scaleZ = Mathf.Floor(UnitUtility.GetWorldScale(basePlane).z *10 / gridSize) * gridSize * 0.1f; if (scaleX == 0) { scaleX = gridSize * 0.1f; } if (scaleZ == 0) { scaleZ = gridSize * 0.1f; } basePlane.localScale = new Vector3(scaleX, 1, scaleZ); //adjusting the texture if (AutoAdjustTextureToGrid) { Material mat = basePlane.renderer.material; float x = (UnitUtility.GetWorldScale(basePlane).x *10f) / gridSize; float z = (UnitUtility.GetWorldScale(basePlane).z *10f) / gridSize; mat.mainTextureOffset = new Vector2(0.5f, 0.5f); mat.mainTextureScale = new Vector2(x, z); } //get the platform component, if any //Platform p=basePlane.gameObject.GetComponent<Platform>(); //buildPlatforms[i]=new BuildPlatform(basePlane, p); i++; } }
public PathSection(PlatformTD p) { platform = p; }
static public NodeTD[] GenerateNode(PlatformTD platform, float heightOffset) { //check if node generator CheckInit(); //Debug.Log("generating nav node for "+platform.thisT); float timeStart = Time.realtimeSinceStartup; Transform platformT = platform.thisT; float gridSize = BuildManager.GetGridSize(); float scaleX = platform.thisT.localScale.x; float scaleZ = platform.thisT.localScale.z; int countX = (int)(10 * scaleX / gridSize); int countZ = (int)(10 * scaleZ / gridSize); //Debug.Log(countX+" "+countZ); float x = -scaleX * 10 / 2 / scaleX; float z = -scaleZ * 10 / 2 / scaleZ; Vector3 point = platformT.TransformPoint(new Vector3(x, 0, z)); thisT.position = point; thisT.rotation = platformT.rotation; thisT.position = thisT.TransformPoint(new Vector3(gridSize / 2, heightOffset, gridSize / 2)); NodeTD[] nodeGraph = new NodeTD[countZ * countX]; int counter = 0; for (int i = 0; i < countZ; i++) { for (int j = 0; j < countX; j++) { nodeGraph[counter] = new NodeTD(thisT.position, counter); counter += 1; thisT.position = thisT.TransformPoint(new Vector3(gridSize, 0, 0)); } thisT.position = thisT.TransformPoint(new Vector3(-(countX) * gridSize, 0, gridSize)); } thisT.position = Vector3.zero; thisT.rotation = Quaternion.identity; float timeUsed = Time.realtimeSinceStartup - timeStart; //Debug.Log("generate "+counter+" nodes, used "+timeUsed+"seconds"); counter = 0; foreach (NodeTD cNode in nodeGraph) { if (cNode.walkable) { //check if there's anything within the point LayerMask mask = 1 << LayerManager.LayerPlatform(); mask |= 1 << LayerManager.LayerTower(); if (LayerManager.LayerTerrain() >= 0) { mask |= 1 << LayerManager.LayerTerrain(); } Collider[] cols = Physics.OverlapSphere(cNode.pos, gridSize * 0.45f, ~mask); if (cols.Length > 0) { cNode.walkable = false; counter += 1; } } } //if(counter>0) Debug.Log(counter+" node is unwalkable"); float neighbourDistance = 0; float neighbourRange; if (nodeGenerator.connectDiagonalNeighbour) { neighbourRange = gridSize * 1.5f; } else { neighbourRange = gridSize * 1.1f; } timeStart = Time.realtimeSinceStartup; counter = 0; //assign the neighouring node for each node in the grid foreach (NodeTD currentNode in nodeGraph) { //only if that node is walkable if (currentNode.walkable) { //create an empty array List <NodeTD> neighbourNodeList = new List <NodeTD>(); List <float> neighbourCostList = new List <float>(); NodeTD[] neighbour = new NodeTD[8]; int id = currentNode.ID; if (id > countX - 1 && id < countX * countZ - countX) { //print("middle rows"); if (id != countX) { neighbour[0] = nodeGraph[id - countX - 1]; } neighbour[1] = nodeGraph[id - countX]; neighbour[2] = nodeGraph[id - countX + 1]; neighbour[3] = nodeGraph[id - 1]; neighbour[4] = nodeGraph[id + 1]; neighbour[5] = nodeGraph[id + countX - 1]; neighbour[6] = nodeGraph[id + countX]; if (id != countX * countZ - countX - 1) { neighbour[7] = nodeGraph[id + countX + 1]; } } else if (id <= countX - 1) { //print("first row"); if (id != 0) { neighbour[0] = nodeGraph[id - 1]; } if (nodeGraph.Length > id + 1) { neighbour[1] = nodeGraph[id + 1]; } if (countZ > 0) { if (nodeGraph.Length > id + countX - 1) { neighbour[2] = nodeGraph[id + countX - 1]; } if (nodeGraph.Length > id + countX) { neighbour[3] = nodeGraph[id + countX]; } if (nodeGraph.Length > id + countX + 1) { neighbour[4] = nodeGraph[id + countX + 1]; } } } else if (id >= countX * countZ - countX) { //print("last row"); neighbour[0] = nodeGraph[id - 1]; if (id != countX * countZ - 1) { neighbour[1] = nodeGraph[id + 1]; } neighbour[2] = nodeGraph[id - countX - 1]; neighbour[3] = nodeGraph[id - countX]; neighbour[4] = nodeGraph[id - countX + 1]; } //scan through all the node in the grid foreach (NodeTD node in neighbour) { //if this the node is not currentNode if (node != null && node.walkable) { //if this node is within neighbour node range neighbourDistance = GetHorizontalDistance(currentNode.pos, node.pos); if (neighbourDistance < neighbourRange) { //if nothing's in the way between these two LayerMask mask = 1 << LayerManager.LayerPlatform(); mask |= 1 << LayerManager.LayerTower(); if (!Physics.Linecast(currentNode.pos, node.pos, ~mask)) { //if the slop is not too steep //if(Mathf.Abs(GetSlope(currentNode.pos, node.pos))<=maxSlope){ //add to list //if(!node.walkable) Debug.Log("error"); neighbourNodeList.Add(node); neighbourCostList.Add(neighbourDistance); //}//else print("too steep"); } //else print("something's in the way"); } //else print("out of range "+neighbourDistance); } //else print("unwalkable"); } //set the list as the node neighbours array currentNode.SetNeighbour(neighbourNodeList, neighbourCostList); //if(neighbourNodeList.Count==0) //Debug.Log("no heighbour. node number "+counter+" "+neighbourNodeList.Count); } //Debug.Log(currentN.pos+" "+currentNode.neighbourNode.length); counter += 1; } //timeUsed=Time.realtimeSinceStartup-timeStart; //Debug.Log("connect neighbour for "+counter+" nodes, used "+timeUsed+"seconds"); return(nodeGraph); }