Esempio n. 1
0
        public static IEnumerator LoadSimulationScene(SimulationSceneLoadConfig config, SimulationSceneLoadContext context, ISceneContext sceneContext)
        {
            // Load Scene
            var sceneName = NameForScene(config.SceneType);
            var options   = new LoadSceneParameters(LoadSceneMode.Additive, LocalPhysicsMode.Physics3D);

            SceneManager.LoadScene(sceneName, options);
            var scene = SceneManager.GetSceneByName(sceneName);

            context.PhysicsScene = scene.GetPhysicsScene();
            context.Scene        = scene;
            // We need to wait two frames before the scene and its GameObjects
            // are fully loaded
            yield return(new WaitForEndOfFrame());

            yield return(new WaitForEndOfFrame());

            // Find setup script in the new scene
            var prevActiveScene = SceneManager.GetActiveScene();

            SceneManager.SetActiveScene(scene);

            SimulationSceneSetup sceneSetup = null;
            var rootObjects = scene.GetRootGameObjects();

            for (int i = 0; i < rootObjects.Length; i++)
            {
                sceneSetup = rootObjects[i].GetComponent <SimulationSceneSetup>();
                if (sceneSetup != null)
                {
                    break;
                }
            }

            // Create the structures
            sceneSetup.BuildScene(config.SceneDescription, sceneContext);

            SceneManager.SetActiveScene(prevActiveScene);
            yield return(new WaitForFixedUpdate());

            SceneManager.SetActiveScene(scene);

            // Spawn Creatures
            var spawnOptions = new SimulationSpawnConfig(
                config.CreatureDesign,
                config.CreatureSpawnCount,
                context.PhysicsScene,
                sceneContext,
                config.LegacyOptions,
                config.SceneDescription
                );
            var creatures = sceneSetup.SpawnBatch(spawnOptions);

            SceneManager.SetActiveScene(prevActiveScene);
            context.Creatures = creatures;
        }
Esempio n. 2
0
        public Creature[] SpawnBatch(SimulationSpawnConfig options)
        {
            var template = new CreatureBuilder(options.Design).Build();

            // SetupCreature(template, physicsScene);
            template.PhysicsScene = options.PhysicsScene;
            template.RemoveMuscleColliders();
            template.SceneContext = options.SceneContext;
            template.Alive        = false;
            template.gameObject.SetActive(true);

            // Determine the spawn position
            // Update safe drop offset
            // Ensures that the creature isn't spawned into the ground
            var lowestY          = template.GetLowestPoint().y;
            var safeHeightOffset = lowestY < 0 ? -lowestY + 1f : 0f;

            // Calculate the drop height
            // float distanceFromGround = template.DistanceFromGround();

            var spawnPosition = template.transform.position;

            if (options.LegacyOptions.LegacyClimbingDropCalculation)
            {
                spawnPosition.y -= template.DistanceFromGround();
                spawnPosition.y += 0.5f;
            }
            else
            {
                spawnPosition.y -= template.SemiSafeDistanceFromGround();
                spawnPosition.y += safeHeightOffset;
                spawnPosition.y += options.SceneDescription.DropHeight;
            }

            var batch = new Creature[options.BatchSize];

            for (int i = 0; i < options.BatchSize; i++)
            {
                var builder  = new CreatureBuilder(options.Design);
                var creature = builder.Build();
                batch[i] = creature;
                creature.transform.position            = spawnPosition;
                creature.SceneContext                  = options.SceneContext;
                creature.usesLegacyRotationCalculation = options.LegacyOptions.LegacyRotationCalculation;
                creature.RefreshLineRenderers();
                creature.PhysicsScene = options.PhysicsScene;
            }

            template.gameObject.SetActive(false);
            Destroy(this.gameObject);
            return(batch);
        }