Exemplo n.º 1
0
        private IEnumerable <Entity> SpawnEntitiesUsingRandomDistribution(EntitySpawnPoint entitySpawnPoint, DstData dstData)
        {
            float rollingProbabilityDensity = dstData.prefabs.Sum(prefab => prefab.probability / entitySpawnPoint.Density);

            if (rollingProbabilityDensity <= 0)
            {
                yield break;
            }

            double randomNumber = random.NextDouble();

            if (rollingProbabilityDensity > 1f)
            {
                randomNumber *= rollingProbabilityDensity;
            }

            double     rollingProbability = 0;
            PrefabData selectedPrefab     = dstData.prefabs.FirstOrDefault(prefab =>
            {
                if (prefab.probability == 0)
                {
                    return(false);
                }

                float probabilityDensity = prefab.probability / entitySpawnPoint.Density;
                rollingProbability      += probabilityDensity;
                bool isValidSpawn        = IsValidSpawnType(prefab.classId, entitySpawnPoint.CanSpawnCreature);
                return(rollingProbability >= randomNumber && isValidSpawn);
            });

            WorldEntityInfo worldEntityInfo;

            if (!ReferenceEquals(selectedPrefab, null) && worldEntitiesByClassId.TryGetValue(selectedPrefab.classId, out worldEntityInfo))
            {
                for (int i = 0; i < selectedPrefab.count; i++)
                {
                    IEnumerable <Entity> entities = CreateEntityWithChildren(entitySpawnPoint,
                                                                             worldEntityInfo.techType,
                                                                             worldEntityInfo.cellLevel,
                                                                             selectedPrefab.classId);
                    foreach (Entity entity in entities)
                    {
                        yield return(entity);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private IEnumerable <Entity> SpawnEntitiesUsingRandomDistribution(EntitySpawnPoint entitySpawnPoint, DstData dstData, DeterministicBatchGenerator deterministicBatchGenerator)
        {
            List <PrefabData> allowedPrefabs = filterAllowedPrefabs(dstData.prefabs, entitySpawnPoint);

            float rollingProbabilityDensity = allowedPrefabs.Sum(prefab => prefab.probability / entitySpawnPoint.Density);

            if (rollingProbabilityDensity <= 0)
            {
                yield break;
            }

            double randomNumber = deterministicBatchGenerator.NextDouble();

            if (rollingProbabilityDensity > 1f)
            {
                randomNumber *= rollingProbabilityDensity;
            }

            double     rollingProbability = 0;
            PrefabData selectedPrefab     = allowedPrefabs.FirstOrDefault(prefab =>
            {
                if (prefab.probability == 0)
                {
                    return(false);
                }

                float probabilityDensity = prefab.probability / entitySpawnPoint.Density;

                rollingProbability += probabilityDensity;

                return(rollingProbability >= randomNumber);
            });

            WorldEntityInfo worldEntityInfo;

            if (!ReferenceEquals(selectedPrefab, null) && worldEntitiesByClassId.TryGetValue(selectedPrefab.classId, out worldEntityInfo))
            {
                for (int i = 0; i < selectedPrefab.count; i++)
                {
                    IEnumerable <Entity> entities = CreateEntityWithChildren(entitySpawnPoint,
                                                                             worldEntityInfo.techType,
                                                                             worldEntityInfo.cellLevel,
                                                                             selectedPrefab.classId,
                                                                             deterministicBatchGenerator);
                    foreach (Entity entity in entities)
                    {
                        yield return(entity);
                    }
                }
            }
        }