protected override void OnUpdate() { var sexTypes = GetComponentDataFromEntity <SexData>(true); var adultData = GetComponentDataFromEntity <Adult>(true); Entities .WithReadOnly(adultData) .WithReadOnly(sexTypes) .ForEach(( ref LookingForMate lookingForMate, in Translation position, in AnimalTypeData animalType, in SexData sexType, in DynamicBuffer <BucketAnimalData> sensedAnimals) => { int closestMateIndex = -1; float closestMateDistance = 0f; for (int i = 0; i < sensedAnimals.Length; i++) { var sensedAnimalInfo = sensedAnimals[i]; AnimalTypeData targetAnimalType = sensedAnimalInfo.AnimalTypeData; SexData targetSexType = sexTypes[sensedAnimalInfo.Entity]; bool targetAdult = adultData.Exists(sensedAnimalInfo.Entity); float3 targetPosition = sensedAnimalInfo.Position; float targetDistance = math.distance(targetPosition, position.Value); if (animalType.AnimalTypeId != targetAnimalType.AnimalTypeId) { continue; // Not the same type of animal } if (closestMateIndex != -1 && targetDistance >= closestMateDistance) { continue; // Not the closest } if (sexType.Sex == targetSexType.Sex) { continue; // Not the opposite sex } if (!targetAdult) { continue; // Not an adult } closestMateIndex = i; closestMateDistance = targetDistance; } // Set result if (closestMateIndex != -1) { lookingForMate.HasFound = true; lookingForMate.Entity = sensedAnimals[closestMateIndex].Entity; lookingForMate.Position = sensedAnimals[closestMateIndex].Position; } else { lookingForMate.HasFound = false; } }).ScheduleParallel();
private static void Main(string[] args) { try { Input = new StreamReader("Files/input.txt"); } catch { Console.WriteLine("There are some errors with the input file. Input file is not exists or it is empty"); Console.ReadKey(); return; } AnimalOwners = new Dictionary <string, AnimalOwner>(); AnimalsTypes = new Dictionary <string, AnimalTypeData>(); while (!Input.EndOfStream) { string[] BufferStringArray = Input.ReadLine().Split(','); if (!AnimalOwners.ContainsKey(BufferStringArray[0])) { AnimalOwners[BufferStringArray[0]] = new AnimalOwner(BufferStringArray[0]); } if (!AnimalsTypes.ContainsKey(BufferStringArray[1])) { AnimalsTypes[BufferStringArray[1]] = new AnimalTypeData(); } Animal BufferAnimal = new Animal ( BufferStringArray[1], uint.Parse(BufferStringArray[3]), AnimalOwners[BufferStringArray[0]], BufferStringArray[2] == string.Empty ? Animal.Nameless : BufferStringArray[2] ); AnimalOwners[BufferStringArray[0]].OwnAnimals.Add(BufferAnimal); var CachedTypeData = AnimalsTypes[BufferStringArray[1]]; CachedTypeData.Animals.Add(BufferAnimal); CachedTypeData.Owners.Add(BufferStringArray[0]); CachedTypeData.TypeAge.UpdateAge(BufferAnimal.Age); } Console.WriteLine("Type 1/2/3/4 to continue"); var Key = Console.ReadKey().KeyChar; Console.WriteLine(); switch (Key) { case '1': { foreach (var animalOwner in AnimalOwners.Values) { int counter = 0; HashSet <string> bufferTypes = new HashSet <string>(); foreach (var animal in animalOwner.OwnAnimals) { if (bufferTypes.Add(animal.Name)) { counter++; } } Console.WriteLine(string.Format("[{0}] has animals of [{1}] different types", animalOwner.Name, counter)); } break; } case '2': { Console.WriteLine("Enter an animal type"); string bufferTypeString = Console.ReadLine(); if (!AnimalsTypes.ContainsKey(bufferTypeString)) { Console.WriteLine(string.Format("There are no animals of the selected type")); break; } foreach (var owner in AnimalsTypes[bufferTypeString].Owners) { Console.WriteLine(string.Format("Animal owner: [{0}]", owner)); foreach (var animal in AnimalOwners[owner].OwnAnimals) { if (animal.Type == bufferTypeString) { Console.WriteLine(string.Format(" Animal name: [{0}]", animal.Name)); } } Console.WriteLine(); } break; } case '3': { Console.WriteLine("Enter an animal name"); string bufferString = Console.ReadLine(); HashSet <string> bufferTypes = new HashSet <string>(); foreach (var animals in AnimalsTypes.Values) { foreach (var animal in animals.Animals) { if (animal.Name == bufferString) { bufferTypes.Add(animal.Type); break; } } } Console.WriteLine(string.Format("Such name have animals of [{0}] different types", bufferTypes.Count)); break; } case '4': { foreach (var type in AnimalsTypes.Keys) { Console.WriteLine(string.Format("Type [{0,8}]: MIN age = [{1}], MAX age = [{2}]", type, AnimalsTypes[type].TypeAge.Min, AnimalsTypes[type].TypeAge.Max)); } break; } default: { Console.WriteLine("Wrong input"); break; } } Console.ReadKey(); }
protected override void OnUpdate() { var blockedCells = worldGridSystem.BlockedCells; var waterCells = worldGridSystem.WaterCells; var grid = worldGridSystem.Grid; var directions = GetComponentDataFromEntity <MovementInput>(); Entities .WithReadOnly(blockedCells) .WithReadOnly(waterCells) .WithReadOnly(directions) .ForEach((Entity entity, int entityInQueryIndex, ref LookingForPrey lookingForPrey, in Translation position, in DynamicBuffer <PreyTypesElement> preyTypeBuffer, in DynamicBuffer <BucketAnimalData> sensedAnimals, in MovementTerrain movementTerrain) => { bool onLand = movementTerrain.MovesOnLand; bool inWater = movementTerrain.MovesOnWater; int closestPreyIndex = -1; float closestPreyDistance = 0f; // Check all animals that we can sense for (int i = 0; i < sensedAnimals.Length; i++) { var sensedAnimalInfo = sensedAnimals[i]; AnimalTypeData targetAnimalType = sensedAnimalInfo.AnimalTypeData; float3 targetPosition = sensedAnimalInfo.Position; float targetDistance = math.distance(targetPosition, position.Value); if (!IsPrey(targetAnimalType, preyTypeBuffer)) { continue; // Not prey } if (closestPreyIndex != -1 && targetDistance >= closestPreyDistance) { continue; // Not the closest } closestPreyIndex = i; closestPreyDistance = targetDistance; } // Set result if (closestPreyIndex != -1) { float3 preyPosition = sensedAnimals[closestPreyIndex].Position; lookingForPrey.HasFound = true; lookingForPrey.Entity = sensedAnimals[closestPreyIndex].Entity; lookingForPrey.Position = preyPosition; int length = 3; // Might need adjusting float3 predictedPosition; do { predictedPosition = preyPosition + length * math.normalizesafe(directions[lookingForPrey.Entity].Direction); length--; }while (length >= 0 && !WorldGridSystem.IsWalkable(grid, blockedCells, waterCells, onLand, inWater, grid.GetGridPosition(predictedPosition))); lookingForPrey.PredictedPosition = predictedPosition; } else { lookingForPrey.HasFound = false; } }).ScheduleParallel();