コード例 #1
0
ファイル: PrefabSpawner.cs プロジェクト: Buffes/Ecosystem
        public static void Spawn(GameObject prefab, int amount, WorldGridSystem worldGridSystem)
        {
            GridData   grid       = worldGridSystem.Grid;
            AnimalType animalType = prefab.GetComponentInChildren <AnimalTypeAuthoring>().animalType;

            bool lookingForFreeTile;
            int  length = grid.Length;

            for (int i = 0; i < amount; i++)
            {
                lookingForFreeTile = true;
                while (lookingForFreeTile)
                {
                    int n = Random.Range(0, length);
                    if (worldGridSystem.IsWalkable(animalType.Land, animalType.Water,
                                                   grid.GetGridPositionFromIndex(n)))
                    {
                        Vector3 spawnPos = grid.GetWorldPosition(grid.GetGridPositionFromIndex(n));
                        spawnPos.y = 1f;
                        GameObject animal = Instantiate(prefab, spawnPos, Quaternion.Euler(0, Random.Range(0, 360), 0));
                        animal.GetComponentInChildren <AgeAuthoring>().Age = animalType.Lifespan * Random.value;
                        lookingForFreeTile = false;
                    }
                }
            }
        }
コード例 #2
0
        protected override void OnUpdate()
        {
            var blockedCells = worldGridSystem.BlockedCells;
            var waterCells   = worldGridSystem.WaterCells;
            var grid         = worldGridSystem.Grid;
            var randomArray  = randomSystem.RandomArray;

            Entities
            .WithNativeDisableContainerSafetyRestriction(randomArray)
            .WithReadOnly(blockedCells)
            .WithReadOnly(waterCells)
            .ForEach((int nativeThreadIndex, Entity entity,
                      ref LookingForRandomTarget lookingForRandomTarget,
                      in Translation translation,
                      in MovementTerrain movementTerrain) =>
            {
                bool onLand  = movementTerrain.MovesOnLand;
                bool inWater = movementTerrain.MovesOnWater;

                int randomIndex = nativeThreadIndex % JobsUtility.MaxJobThreadCount;
                var random      = randomArray[randomIndex];

                float3 target = translation.Value;

                float cap = 4;  // cap + min = maximum distance from entity to pick a point.
                float min = 2;  // The minimum distance away from the entity to pick a point.
                int tries = 10; // To handle case of no possible targets.

                for (int i = 0; i < tries; i++)
                {
                    float2 tile = random.NextFloat2(new float2(-cap, -cap), new float2(cap, cap));
                    tile.x     += tile.x > 0 ? min : -min;
                    tile.y     += tile.y > 0 ? min : -min;

                    float3 potentialtarget = new float3(translation.Value.x + tile.x, 0f, translation.Value.z + tile.y);

                    if (WorldGridSystem.IsWalkable(grid, blockedCells, waterCells, onLand, inWater,
                                                   grid.GetGridPosition(potentialtarget)))
                    {
                        target = potentialtarget;
                        break;
                    }
                }

                // Set result
                if (math.distance(target, translation.Value) > 1f)
                {
                    lookingForRandomTarget.HasFound = true;
                    lookingForRandomTarget.Position = target;
                }
                else
                {
                    lookingForRandomTarget.HasFound = false;
                }

                randomArray[randomIndex] = random; // Necessary to update the generator.
            }).ScheduleParallel();
コード例 #3
0
        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();
コード例 #4
0
        protected override void OnUpdate()
        {
            var blockedCells = worldGridSystem.BlockedCells;
            var waterCells   = worldGridSystem.WaterCells;
            var grid         = worldGridSystem.Grid;
            var randomArray  = randomSystem.RandomArray;

            Entities
            .WithNativeDisableContainerSafetyRestriction(randomArray)
            .WithReadOnly(blockedCells)
            .WithReadOnly(waterCells)
            .ForEach((int nativeThreadIndex, Entity entity,
                      ref LookingForFleeTarget lookingForFleeTarget,
                      in LookingForPredator lookingForPredator,
                      in Translation translation,
                      in MovementTerrain movementTerrain) =>
            {
                if (!lookingForPredator.HasFound)
                {
                    return;                                   // No predator to flee from
                }
                bool onLand  = movementTerrain.MovesOnLand;
                bool inWater = movementTerrain.MovesOnWater;

                int randomIndex = nativeThreadIndex % JobsUtility.MaxJobThreadCount;
                var random      = randomArray[randomIndex];

                float3 target        = translation.Value;
                float3 diff          = target - lookingForPredator.Position;
                float diffLength     = Mathf.Sqrt(Mathf.Pow(diff.x, 2) + Mathf.Pow(diff.z, 2));
                float3 startingPoint = target + 3f * diff / diffLength;

                float max = 2;     //  maximum distance from startingPoint to pick a point.
                int tries = 10;    // To handle case of no possible targets.

                for (int i = 0; i < tries; i++)
                {
                    float2 tile = random.NextFloat2(new float2(-max, -max), new float2(max, max));
                    //tile.x += tile.x > 0 ? min : -min;
                    //tile.y += tile.y > 0 ? min : -min;

                    float3 potentialtarget = new float3(startingPoint.x + tile.x, 0f, startingPoint.z + tile.y);

                    if (WorldGridSystem.IsWalkable(grid, blockedCells, waterCells, onLand, inWater,
                                                   grid.GetGridPosition(potentialtarget)))
                    {
                        target = potentialtarget;
                        break;
                    }
                }

                // Set result
                if (math.distance(target, translation.Value) > 1f)
                {
                    lookingForFleeTarget.HasFound = true;
                    lookingForFleeTarget.Position = target;
                }
                else
                {
                    lookingForFleeTarget.HasFound = false;
                }

                randomArray[randomIndex] = random;     // Necessary to update the generator.
            }).ScheduleParallel();