Esempio n. 1
0
        public LevelHandler(App root, LevelInitialization data)
        {
            this.root = root;

            levelFileName = data.LevelName;
            episodeName   = data.EpisodeName;
            difficulty    = data.Difficulty;

            gravity = DefaultGravity;

            collisions = new DynamicTreeBroadPhase <ActorBase>();

            api          = new ActorApi(this);
            eventSpawner = new EventSpawner(api);

            rootObject = new GameObject();
            rootObject.AddComponent(new LocalController(this));
            AddObject(rootObject);

            // Load level
            LoadLevel(levelFileName, episodeName);

            // Create HUD
            Hud hud = rootObject.AddComponent <Hud>();

            // Process carry overs
            if (data.PlayerCarryOvers != null)
            {
                for (int i = 0; i < data.PlayerCarryOvers.Length; i++)
                {
                    Vector2 spawnPosition = eventMap.GetSpawnPosition(data.PlayerCarryOvers[i].Type);
                    if (spawnPosition == new Vector2(-1, -1))
                    {
                        spawnPosition = eventMap.GetSpawnPosition(PlayerType.Jazz);
                        if (spawnPosition == new Vector2(-1, -1))
                        {
                            continue;
                        }
                    }

                    Player player = new Player();
                    player.OnActivated(new ActorActivationDetails {
                        Api    = api,
                        Pos    = new Vector3(spawnPosition, PlayerZ),
                        Params = new[] { (ushort)data.PlayerCarryOvers[i].Type, (ushort)i }
                    });
                    AddPlayer(player);

                    if (i == 0)
                    {
                        player.AttachToHud(hud);
                    }

                    player.ReceiveLevelCarryOver(data.ExitType, ref data.PlayerCarryOvers[i]);
                }
            }

            Player  targetPlayer;
            Vector3 targetPlayerPosition;

            if (players.Count > 0)
            {
                targetPlayer         = players[0];
                targetPlayerPosition = targetPlayer.Transform.Pos;

                // Setup all cameras
                float relativeViewRange = (1f / players.Count);
                for (int i = 0; i < players.Count; i++)
                {
                    GameObject camera          = new GameObject(/*"MainCamera " + i*/);
                    Transform  cameraTransform = camera.AddComponent <Transform>();

                    Camera cameraInner = camera.AddComponent <Camera>();
                    cameraInner.NearZ          = NearZ;
                    cameraInner.FarZ           = FarZ;
                    cameraInner.Projection     = ProjectionMode.Orthographic;
                    cameraInner.VisibilityMask = VisibilityFlag.Group0 | VisibilityFlag.ScreenOverlay | (VisibilityFlag)(1 << i);

                    switch (players.Count)
                    {
                    case 1: cameraInner.TargetRect = new Rect(0f, 0f, 1f, 1f); break;

                    case 2: cameraInner.TargetRect = new Rect(0f, i * relativeViewRange, 1f, relativeViewRange); break;

                    case 3: cameraInner.TargetRect = new Rect(0f, i * relativeViewRange, 1f, relativeViewRange); break;

                    case 4: cameraInner.TargetRect = new Rect((i % 2) * 0.5f, (i / 2) * 0.5f, 0.5f, 0.5f); break;
                    }

                    // Create controller
                    CameraController cameraController = camera.AddComponent <CameraController>();
                    cameraController.ViewBounds = levelBounds;

                    // Bind camera to player
                    cameraInner.RenderingSetup = new LevelRenderSetup(this);

                    Player currentPlayer = players[i];
                    cameraTransform.Pos           = new Vector3(currentPlayer.Transform.Pos.Xy, 0);
                    cameraController.TargetObject = currentPlayer;

                    ((ICmpFixedUpdatable)cameraController).OnFixedUpdate(1f);
                    camera.Parent = rootObject;

                    cameras.Add(camera);

                    if (i == 0)
                    {
                        // First camera is always sound listener
                        DualityApp.Sound.Listener = camera;
                    }
                }
            }
            else
            {
                GameObject camera          = new GameObject(/*"MainCamera " + i*/);
                Transform  cameraTransform = camera.AddComponent <Transform>();

                Camera cameraInner = camera.AddComponent <Camera>();
                cameraInner.NearZ          = NearZ;
                cameraInner.FarZ           = FarZ;
                cameraInner.Projection     = ProjectionMode.Orthographic;
                cameraInner.VisibilityMask = VisibilityFlag.Group0 | VisibilityFlag.ScreenOverlay | (VisibilityFlag)(1 << 0);

                // Create controller
                CameraController cameraController = camera.AddComponent <CameraController>();
                cameraController.ViewBounds = levelBounds;

                // Bind camera to player
                cameraInner.RenderingSetup = new LevelRenderSetup(this);

                cameraTransform.Pos = new Vector3(levelBounds.Center, 0);

                ((ICmpFixedUpdatable)cameraController).OnFixedUpdate(1f);
                camera.Parent = rootObject;

                cameras.Add(camera);

                // First camera is always sound listener
                DualityApp.Sound.Listener = camera;

                hud.BeginFadeIn(true);
            }

            // Common sounds
            commonResources = ContentResolver.Current.RequestMetadata("Common/Scenery");
        }