/// <summary> /// Sets the current coverobject according to given node type /// </summary> /// <param name="nodeType"></param> public void setCurrentCoverObject(System.Type nodeType) { if (nodeType == typeof(HexaNode)) { currentCoverObject = coverObjects[0].GetComponent <CoverObject>(); } }
Vector3 FindCoverAdvanced() //Finding the nearest reachable cover object { Vector3 initialPoint = transform.position; viablePoints = new List <CoverPoint>(); //------------ Find all cover objects within range for (int i = 0; i < coverObjects.Length; i++) { CoverObject cover = coverObjects[i]; NavMeshPath path = new NavMeshPath(); agent.CalculatePath(cover.transform.position, path); //If the cover is reachable if (agent.pathStatus == NavMeshPathStatus.PathComplete) { //If within range if (Vector3.Distance(transform.position, cover.transform.position) < AP * 2.0f) { //Find and add all furthest points int furthest = cover.FurthestSide(player.transform.position); for (int id = 0; id < cover.coverPoints.Length; id++) { CoverPoint point = cover.coverPoints[id]; if (cover.isTakenList[id] == false) { if (point.side == furthest) { if (CheckLineOfSight(cover.coverPoints[id].pos)) { viablePoints.Add(point); isShooting = true; } } } } } } } if (viablePoints.Count > 0) { if (currentCover != null) { currentCover.isTakenList[coverPointID] = false; } int randomPoint = Random.Range(0, viablePoints.Count); coverPointID = viablePoints[randomPoint].id; currentCover = viablePoints[randomPoint].parent; currentCover.isTakenList[coverPointID] = true; return(viablePoints[randomPoint].pos); } else { return(FindCoverSimple()); } }
public void FindCover() { List <CoverObject> AvailableCover = new List <CoverObject>(); for (int i = 0; i < mGameController.CoverObjects.Count; i++) { Debug.Log(Vector3.Distance(mGameController.CoverObjects[i].transform.position, transform.position)); if (Vector3.Distance(mGameController.CoverObjects[i].transform.position, transform.position) <= MoveRange) { AvailableCover.Add(mGameController.CoverObjects[i]); } } if (AvailableCover.Count > 0) { CoverTarget = AvailableCover[0]; for (int i = 0; i < AvailableCover.Count; i++) { if (Vector3.Distance(CoverTarget.transform.position, transform.position) > Vector3.Distance(AvailableCover[i].transform.position, transform.position)) { CoverTarget = AvailableCover[i]; } } } if (CoverTarget != null) { StartCoroutine(Constant()); } }
Vector3 FindCoverSimple() //Finding the nearest reachable cover object { Vector3 bestPoint = transform.position; CoverObject bestCover = null; float bestCoverDist = 100.0f; float bestPointDist = 0.0f; //------------ First find the nearest cover object ------------ for (int i = 0; i < coverObjects.Length; i++) { CoverObject cover = coverObjects[i]; NavMeshPath path = new NavMeshPath(); agent.CalculatePath(cover.transform.position, path); //If the cover is within reachable distance if (agent.pathStatus == NavMeshPathStatus.PathComplete) { float dist = Vector3.Distance(transform.position, cover.transform.position); if (dist < bestCoverDist) //If the distance is the best distance { bestCoverDist = dist; bestCover = cover; } } } //------------- Then find the furthest cover point on the cover object from the enemy --------- if (bestCover != null) { int bestPointID = -1; for (int id = 0; id < bestCover.coverPoints.Length; id++) { CoverPoint coverPoint = bestCover.coverPoints[id]; float dist = Vector3.Distance(coverPoint.pos, player.transform.position); if (dist > bestPointDist && bestCover.coverPoints[id].isTaken == false) { currentCover = bestCover; bestPointID = coverPoint.id; coverPointID = coverPoint.id; bestCover.coverPoints[id].isTaken = true; currentCover.coverPoints[coverPointID].isTaken = true; bestPointDist = dist; bestPoint = coverPoint.pos; } } } return(bestPoint); }