void CheckShipsForTarget(ref AiTarget target, ShipLevelComponent level, ShipViewComponent view, GroupsEnumerable <ShipLevelComponent, ShipViewComponent> allShips) { // Check ships. var minDistance = 10f; // todo: get number from ship definition. var closestTarget = new EGID(); var closestPosition = uint2.zero; foreach (var((shipLevels, shipViews, count), group) in allShips) { for (var i = 0; i < count; i++) { var otherView = shipViews[i]; var otherLevel = shipLevels[i]; // We only care about ships we can sink so the level must be below ours. if (otherLevel.Level >= level.Level) { continue; } var distance = math.distance(otherView.Transform.Position, view.Transform.Position); if (distance < minDistance) { closestTarget = otherView.ID; closestPosition = _gridUtils.WorldToCellPosition(otherView.Transform.Position.xy); minDistance = distance; } } } // Check if we have already have a target target.Locator = entitiesDB.GetLocator(closestTarget); target.Position = closestPosition; }
void CheckCoinsForTarget(ref AiTarget target, ShipViewComponent view, EntityCollection <CoinViewComponent> allCoins) { // Check ships. var minDistance = 15f; // todo: get number from ship definition. var closestCoinId = new EGID(); var closestPosition = uint2.zero; foreach (var coinView in allCoins) { var distance = math.distance(coinView.Transform.Position, view.Transform.Position); if (distance < minDistance) { closestCoinId = coinView.ID; closestPosition = _gridUtils.WorldToCellPosition(coinView.Transform.Position.xy); minDistance = distance; } } // Better to do it only once since there is a double indirection here. target.Locator = entitiesDB.GetLocator(closestCoinId); target.Position = closestPosition; }