public SelectInfo(BuildPlatform p, int ID) { platform = p; nodeID = ID; if (AvailableForBuild()) { buildableList = new List <UnitTower>(TowerManager.GetBuildableList()); //for limiting amount of tower in the scene according to prefabID for (int i = 0; i < buildableList.Count; i++) { if (TowerManager.CheckTowerCounterLimit(buildableList[i].prefabID, buildableList[i].limitInScene)) { continue; } buildableList.RemoveAt(i); i -= 1; } for (int i = 0; i < platform.unavailablePrefabIDList.Count; i++) { for (int n = 0; n < buildableList.Count; n++) { if (buildableList[n].prefabID == platform.unavailablePrefabIDList[i]) { buildableList.RemoveAt(n); break; } } } if (PathBlocked()) //can only build mine { for (int i = 0; i < buildableList.Count; i++) { if (!buildableList[i].IsMine()) { buildableList.RemoveAt(i); i -= 1; } } } } }
public static void BuildTower(UnitTower prefab, BuildPlatform platform, int nodeID, bool useRsc = true) { Debug.Log("BuildTower " + prefab); if (useRsc) { if (!RscManager.HasSufficientRsc(prefab.GetCost())) { Debug.Log("Insufficient resources"); return; } Debug.Log("Get cost " + prefab.GetCost()[0]); RscManager.SpendRsc(prefab.GetCost()); } NodeTD node = platform.GetNode(nodeID); GameObject obj = (GameObject)Instantiate(prefab.gameObject, node.pos, platform.GetRot() * Quaternion.Euler(-90, 0, 0)); UnitTower tower = obj.GetComponent <UnitTower>(); AddTower(tower, platform, nodeID); }
public static void AddTower(UnitTower tower, BuildPlatform platform = null, int nodeID = -1) { tower.isPreview = false; tower.instanceID = instance.towerCounter; instance.towerCounter += 1; instance.activeTowerList.Add(tower); if (tower.IsSupport()) { instance.supportTowerList.Add(tower); } if (platform != null && nodeID >= 0) { tower.SetBuildPoint(platform, nodeID); platform.BuildTower(nodeID, tower); } //for limiting tower count in the scene according to prefabID int idx = instance.towerCounterPrefabID.IndexOf(tower.prefabID); if (idx < 0) { instance.towerCounterPrefabID.Add(tower.prefabID); instance.towerCounterList.Add(1); } else { instance.towerCounterList[idx] += 1; } UnitTower.NewTower(tower); TDTK.OnNewTower(tower); }
public static NodeTD[] GenerateNode(BuildPlatform platform, float gridSize = 1, float heightOffset = 0) { if (refT == null) { refT = new GameObject("RefT").transform; refT.parent = TowerManager.GetInstance().transform; } Transform platformT = platform.transform; float scaleX = platformT.localScale.x; float scaleZ = platformT.localScale.y; int countX = (int)(scaleX / gridSize); int countZ = (int)(scaleZ / gridSize); float x = -scaleX / 2 / scaleX; float z = -scaleZ / 2 / scaleZ; Vector3 point = platformT.TransformPoint(new Vector3(x, z, 0)); refT.position = point; refT.rotation = platformT.rotation * Quaternion.Euler(-90, 0, 0); refT.position = refT.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(refT.position, counter); counter += 1; refT.position = refT.TransformPoint(new Vector3(gridSize, 0, 0)); } refT.position = refT.TransformPoint(new Vector3(-(countX) * gridSize, 0, gridSize)); } refT.position = Vector3.zero; refT.rotation = Quaternion.identity; LayerMask mask = 1 << TDTK.GetLayerPlatform() | 1 << TDTK.GetLayerTower() | 1 << TDTK.GetLayerTerrain() | 1 << TDTK.GetLayerNoBuild(); LayerMask maskTowerBlock = 1 << TDTK.GetLayerNoBuild(); counter = 0; foreach (NodeTD cNode in nodeGraph) { //check if there's anything within the point Collider[] cols = Physics.OverlapSphere(cNode.pos, gridSize * 0.45f, ~mask); if (cols.Length > 0) { cNode.SetWalkable(false); counter += 1; } cols = Physics.OverlapSphere(cNode.pos, gridSize * 0.45f, maskTowerBlock); if (cols.Length > 0) { cNode.SetBlockedForTower(true); } } float neighbourDistance = 0; float neighbourRange = gridSize * 1.1f; //if(instance.connectDiagonalNeighbour) neighbourRange=gridSize*1.5f; //else neighbourRange=gridSize*1.1f; 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.IsWalkable()) { //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]; } if (id != countX * (countZ - 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.IsWalkable()) { //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 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); } counter += 1; } return(nodeGraph); }
public static void ConnectPlatform(BuildPlatform plat1, BuildPlatform plat2, List <Vector3> wpList) { }
public static void SelectNode(BuildPlatform platform, int nodeID) { instance._SelectNode(platform, nodeID); }
public static SelectInfo GetSelectInfo(Vector3 pointer, int ID = -1, float towerSize = 1) { InitMask(); Ray ray = CameraControl.GetMainCam().ScreenPointToRay(pointer); RaycastHit hit; //for free from drag and drop mode if (UseFreeFormMode() && instance.dndTower != null) { if (Physics.Raycast(ray, out hit, Mathf.Infinity, maskTerrain)) { Collider[] obstacles = Physics.OverlapSphere(hit.point, towerSize, ~maskTerrain); if (obstacles.Length == 1 && obstacles[0].gameObject == instance.dndTower.gameObject) { obstacles = new Collider[0]; } if (obstacles.Length == 0) { return(new SelectInfo(hit.point)); } else { return(new SelectInfo("Invalid build-point!", hit.point)); } } return(new SelectInfo("No valid build-point has been found")); } //try to detect a platform and determine the node on it bool flag = Physics.Raycast(ray, out hit, Mathf.Infinity, RaycastForTerrain() ? mask : maskPlatform); if (flag) { if (hit.collider.gameObject.layer == TDTK.GetLayerPlatform()) { BuildPlatform platform = hit.collider.gameObject.GetComponent <BuildPlatform>(); if (platform != null) { Vector3 pos = platform.GetTilePos(hit.point, GetGridSize()); NodeTD node = platform.GetNearestNode(pos); if (ID > 0) { if (lastSelectID == ID && sInfo.nodeID == node.ID) { return(sInfo); } else { lastSelectID = ID; } } return(new SelectInfo(platform, node.ID)); } } else if (RaycastForTerrain()) { return(new SelectInfo("No platform has been found", hit.point)); } } return(new SelectInfo("No platform has been found")); }
public override void Awake() { base.Awake(); instance = (BuildPlatform)target; }
//recalculate path on one of the waypoint platform, called when tower is built or sold public void UpdatePlatformPath(BuildPlatform platform, UnitTower tower = null) { bool requireUpdate = false; for (int i = 0; i < wpSecList.Count; i++) { if (wpSecList[i].platform == platform) { if (tower != null) //if the tower isn't built in the path, there's no need to update the path { bool inPath = false; int count = wpSecList[i].branch ? nextPath.Count : 1; for (int j = 0; j < count; j++) { List <Vector3> subPath = wpSecList[i].GetWaypointList(j); for (int n = 0; n < subPath.Count; n++) { if (tower.GetPos() == subPath[n]) { inPath = true; break; } if (AStar.EnablePathSmoothing()) { float gridSize = TowerManager.GetGridSize() * 1.1f; if (n < subPath.Count - 1) // && (subPath[n].y!=subPath[n+1].y && subPath[n].y!=subPath[n+1].y)){ { if (Vector3.Distance(subPath[n], tower.GetPos()) < gridSize && Vector3.Distance(subPath[n + 1], tower.GetPos()) < gridSize) { inPath = true; break; } } } } } if (!inPath) { continue; } } wpSecList[i].UpdatePlatformPath(); bool pathIsBlocked = wpSecList[i].wpList.Count == 0; if (i == wpSecList.Count - 1 && HasBranchingPlatformEnd()) { for (int n = 0; n < nextPath.Count; n++) { if (wpSecList[i].wpListList[n].Count >= 0) { pathIsBlocked = false; break; } } } if (pathIsBlocked) //if(wpSecList[i].wpList.Count==0){ //add to a list and then call reverse later, //in case calling Reverse() on the unit cause it to reverse to prev path intantly and remove itself on the path { List <UnitCreep> reverseList = new List <UnitCreep>(); for (int n = 0; n < creepOnPath.Count; n++) { if (creepOnPath[n].wpIdx > i) { continue; } List <Vector3> newPath = wpSecList[i].GetPathForUnit(creepOnPath[n]); if (newPath.Count == 0) { reverseList.Add(creepOnPath[n]); } } for (int n = 0; n < reverseList.Count; n++) { reverseList[n].Reverse(); } } else { for (int n = 0; n < creepOnPath.Count; n++) { if (creepOnPath[n].wpIdx != i) { continue; } creepOnPath[n].ForceAltPath(); } } requireUpdate = true; } } if (requireUpdate) { UpdateDistance(); } }
public void _Init() { dynamicOffset = Mathf.Min(dynamicOffset, TowerManager.GetGridSize() * 0.45f); if (!loop) { for (int i = 0; i < nextPath.Count; i++) { if (nextPath[i] == null) { nextPath.RemoveAt(i); i -= 1; continue; } nextPath[i].prevPath.Add(this); nextPath[i].blockedEntryPoint.Add(false); } } for (int i = 0; i < waypointTList.Count; i++) { if (waypointTList[i] == null) { waypointTList.RemoveAt(i); i -= 1; continue; } bool isPlatform = false; if (waypointTList[i].gameObject.layer == TDTK.GetLayerPlatform()) { BuildPlatform platform = waypointTList[i].GetComponent <BuildPlatform>(); if (platform != null) { if (i > 0 && i < waypointTList.Count - 1) { isPlatform = true; wpSecList.Add(new WPSection(i, this, platform, waypointTList[i - 1].position, waypointTList[i + 1].position)); platform.AddPath(this); } if (i == waypointTList.Count - 1 && nextPath.Count > 0) { isPlatform = true; List <Vector3> nextPosList = new List <Vector3>(); for (int n = 0; n < nextPath.Count; n++) { nextPosList.Add(nextPath[n].GetFirstWP()); } wpSecList.Add(new WPSection(i, this, platform, waypointTList[i - 1].position, nextPosList)); platform.AddPath(this); } } } if (!isPlatform) { wpSecList.Add(new WPSection(i, this, waypointTList[i])); } } UpdateDistance(); //Update path distance UpdateDistance(true); //for bypass }
public void SetBuildPoint(BuildPlatform platform, int ID) { buildPlatform = platform; nodeID = ID; }