public void GoToNextPoint() { if (searchCellsLeft.Count == 0) { hunterAI.GetComponent <InfectedBrain>().BeginBehavior(BehaviorTypes.IDLE); return; } searchCellsLeft.Remove(FindCellAtPos(hunterAI.GetComponent <InfectedBrain>().targetAStar.position)); if (searchCellsLeft.Count == 0) { hunterAI.GetComponent <InfectedBrain>().BeginBehavior(BehaviorTypes.IDLE); return; } Vector2Int target = searchCellsLeft[0]; SearchMapCellData smcdTarget = cells[target]; float priorityTarget = smcdTarget.distanceFromLastSeenPlayerPos * sizeOfCells.x + Vector2.Distance(smcdTarget.pos, hunterAI.transform.position); foreach (Vector2Int tmp in searchCellsLeft) { SearchMapCellData smcdTmp = cells[tmp]; float priorityTmp = smcdTmp.distanceFromLastSeenPlayerPos + Vector2.Distance(smcdTmp.pos, hunterAI.transform.position); if (priorityTarget > priorityTmp) { priorityTarget = priorityTmp; target = tmp; } } hunterAI.GetComponent <InfectedBrain>().targetAStar.transform.position = cells[target].pos; }
private void Update() { if (hunterAI.GetComponent <InfectedBrain>().awareOfPlayer) { searchCellsLeft.Clear(); searchCells.Clear(); Vector2Int cellKey = FindCellAtPos(hunterAI.GetComponent <SightDetector>().player.transform.position); searchCells.Add(cellKey); searchCellsLeft.Add(cellKey); SearchMapCellData data = cells[cellKey]; data.distanceFromLastSeenPlayerPos = 0; cells[cellKey] = data; } else if (hunterAI.GetComponent <InfectedBrain>().currentBehavior == BehaviorTypes.WANDER_TOWARDS) { int count = searchCells.Count; for (int i = 0; i < count; i++) { Vector2Int key = searchCells[i]; SearchKeyAttemptAdd(key, new Vector2Int(key.x + 1, key.y)); SearchKeyAttemptAdd(key, new Vector2Int(key.x - 1, key.y)); SearchKeyAttemptAdd(key, new Vector2Int(key.x, key.y + 1)); SearchKeyAttemptAdd(key, new Vector2Int(key.x, key.y - 1)); } if (Vector2.Distance(hunterAI.GetComponent <InfectedBrain>().targetAStar.position, hunterAI.transform.position) <= hunterAI.GetComponent <AIPath>().endReachedDistance * 1) { GoToNextPoint(); } } }
void SearchKeyAttemptAdd(Vector2Int key, Vector2Int temp) { if (!searchCells.Contains(temp) && cells.ContainsKey(temp)) { searchCells.Add(temp); searchCellsLeft.Add(temp); SearchMapCellData data = cells[temp]; data.distanceFromLastSeenPlayerPos = cells[key].distanceFromLastSeenPlayerPos + 1; cells[temp] = data; } }
void RefreshExposure() { List <Vector2Int> e = new List <Vector2Int>(cells.Keys); foreach (Vector2Int entry in e) { SearchMapCellData data = cells[entry]; Vector2 dir = data.pos - hunterAI.transform.position; data.isExposed = Vector2.Angle(dir, hunterAI.transform.up) < hunterAI.GetComponent <SightDetector>().viewConeAngle / 2 && dir.magnitude <= hunterAI.GetComponent <SightDetector>().viewConeDistance && Physics2D.Raycast(hunterAI.transform.position, dir, dir.magnitude, wallLayer).transform == null; if (data.isExposed && hunterAI.GetComponent <InfectedBrain>().currentBehavior == BehaviorTypes.WANDER_TOWARDS && searchCellsLeft.Contains(entry)) { searchCellsLeft.Remove(entry); } cells[entry] = data; } }