internal void Update() { // If all hashsets but one are empty, then the encounter is over. int nonEmptyHashsets = 0; foreach (var hashset in encounterMembers) { if (hashset.Count > 0) { nonEmptyHashsets++; } } if (nonEmptyHashsets >= 2) { // Encounter isn't over. // Update epicenter Vector2 sum = Vector2.zero; int divider = 0; foreach (var fac in encounterMembers) { foreach (var ship in fac) { sum += (Vector2)ship.transform.position; divider++; } } epicenter = sum / divider; // Find ships in range to add to the encounter. // TODO generalize to all factions. var playerShipsInRange = Ownership.GetFactionMembers(1).Where(o => Vector3.Distance(o.transform.position, epicenter) < 50f); var pirateShipsInRange = Ownership.GetFactionMembers(2).Where(o => Vector3.Distance(o.transform.position, epicenter) < 50f); foreach (var ship in playerShipsInRange) { if (encounterMembers[1].Contains(ship.GetComponent <EncounterAgent>()) == false) { AddToEncounter(ship.GetComponent <EncounterAgent>()); } } foreach (var ship in pirateShipsInRange) { if (encounterMembers[2].Contains(ship.GetComponent <EncounterAgent>()) == false) { AddToEncounter(ship.GetComponent <EncounterAgent>()); } } } else { // Encounter is over. Remove all current members. EndEncounter(); } }
public override void Start() { uint myFaction = controlledShip.GetComponent <Ownership>().OwnerFactionID; List <Ownership[]> potentialEnemies = new List <Ownership[]>(); for (int facID = 1; facID < Ownership.FactionCount; facID++) { if (facID != myFaction) { potentialEnemies.Add(Ownership.GetFactionMembers(facID)); } } float shortestSquaredDist = 0f; EncounterAgent closestEnemy = null; foreach (var enemyGroup in potentialEnemies) { foreach (var enemy in enemyGroup) { EncounterAgent encounterAgent = enemy.GetComponent <EncounterAgent>(); Dockable dockableComponent = enemy.GetComponent <Dockable>(); if (encounterAgent != null && (dockableComponent == null || dockableComponent.Docked == false)) { float squaredDist = (controlledShip.transform.position - enemy.transform.position).sqrMagnitude; if (squaredDist < maxDistance * maxDistance && (closestEnemy == null || squaredDist < shortestSquaredDist)) { closestEnemy = encounterAgent; shortestSquaredDist = squaredDist; } } } } if (closestEnemy != null) { Succeed(); targetFoundCallback(closestEnemy); } else { Fail("NO ENEMY IN RANGE"); } }