示例#1
0
    public IEnumerator <WaitCommand> Load()
    {
        /* ======== Load Core ======= */

        {
            // Initialize the unity thread dispatcher on the unity thread
            var unityThreadDispatcher = UnityThreadDispatcher.Instance;
        }

        yield return(SceneManager.LoadSceneAsync("Core", LoadSceneMode.Additive).WaitUntilDone());

        var applicationLifeCycleEvents = new GameObject("ApplicationLifeCycleEvents")
                                         .AddComponent <ComponentLifecycleEvents>();

        ILock applicationLock = GameObject.Find("ApplicationQuitter").GetComponent <ApplicationQuitter>();

        // TODO This needs to move to somewhere inside a scene where we can turn it on/off
        var gameSettingsProvider = Object.FindObjectOfType <GameSettingsProvider>();

        gameSettingsProvider.UpdateGameSettings(GameSettings.ReadSettingsFromDisk());
        var userConfigurableSystems = Object.FindObjectOfType <UserConfigurableSystems>();
        var gameSettingsApplier     = new GameSettingsApplier(userConfigurableSystems);

        gameSettingsProvider.SettingChanges
        .Subscribe(settings => gameSettingsApplier.ApplySettings(settings, gameSettingsProvider.ActiveVrMode));
        gameSettingsProvider.SettingChanges
        .Select(gameSettings => gameSettings.Audio)
        .CombineLatest(FMODUtil.StudioSystem(), (audioSettings, fmodSystem) => audioSettings)
        .Subscribe(audioSettings => {
            gameSettingsApplier.ApplyAudioSettings(audioSettings);
        });

        // TODO This needs to move to somewhere inside a scene where we can turn it on/off
        gameSettingsProvider.SettingChanges
        .Skip(1)
        .Throttle(TimeSpan.FromSeconds(2), Scheduler.ThreadPool)
        .Select(gameSettings => new SerializeTask <GameSettings> {
            FilePath = GameSettings.UserSettingsConfigPath.Value, SerializableValue = gameSettings
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(applicationLock.RunWithLock <SerializeTask <GameSettings> >(settingsConfig => {
            var gameSettings = settingsConfig.SerializableValue;
            gameSettings.Serialize2Disk(settingsConfig.FilePath);
        }));

        var inputSettings = gameSettingsProvider.SettingChanges
                            .Select(settings => InputSettings.FromGameSettings(settings.Input));
        var menuActionMapProvider      = Object.FindObjectOfType <MenuActionMapProvider>();
        var pilotActionMapProvider     = Object.FindObjectOfType <PilotActionMapProvider>();
        var parachuteActionMapProvider = Object.FindObjectOfType <ParachuteActionMapProvider>();
        JoystickActivator          joystickActivator = Object.FindObjectOfType <JoystickActivator>();
        InputBindings <MenuAction> menuInputBindings = new InputBindings <MenuAction>(
            MenuInput.Bindings.InitialMapping(),
            inputSettings,
            MenuInput.Bindings.DefaultControllerMappings.Value);
        InputBindings <WingsuitAction> pilotInputBindings = new InputBindings <WingsuitAction>(
            PilotInput.Bindings.InitialMapping(),
            inputSettings,
            PilotInput.Bindings.DefaultControllerMappings.Value);
        InputBindings <SpectatorAction> spectatorInputBindings = new InputBindings <SpectatorAction>(
            SpectatorInput.Bindings.InitialMapping(),
            inputSettings,
            SpectatorInput.Bindings.DefaultControllerMappings.Value);
        InputBindings <ParachuteAction> parachuteInputBindings = new InputBindings <ParachuteAction>(
            ParachuteControls.InitialMapping(),
            inputSettings,
            ParachuteControls.DefaultMappings.Value);

        joystickActivator.ActiveController.Subscribe(controllerId => {
            menuInputBindings.UpdateControllerId(controllerId);
            pilotInputBindings.UpdateControllerId(controllerId);
            spectatorInputBindings.UpdateControllerId(controllerId);
            parachuteInputBindings.UpdateControllerId(controllerId);
        });

        applicationLifeCycleEvents.OnDestroyEvent += () => {
            menuInputBindings.Dispose();
            pilotInputBindings.Dispose();
            parachuteInputBindings.Dispose();
            spectatorInputBindings.Dispose();
        };

        menuActionMapProvider.SetInputMappingSource(menuInputBindings.InputMappingChanges);
        parachuteActionMapProvider.SetInputMappingSource(parachuteInputBindings.InputMappingChanges);
        pilotActionMapProvider.SetInputMappingSource(pilotInputBindings.InputMappingChanges);
        // Spectator input
        var spectatorCamera = GameObject.Find("InGameSpectatorCamera")
                              .GetComponent <SpectatorCamera>();
        var menuClock = GameObject.Find("_RealtimeClock").GetComponent <AbstractUnityClock>();

        spectatorInputBindings.InputMappingChanges.Subscribe(actionMapConfig => {
            spectatorCamera.ActionMap = SpectatorInput.ActionMap.Create(actionMapConfig, menuClock);
        });

        // TODO Create serializer component and add it to the scene
        menuInputBindings.InputMappingChanges
        .Skip(1)
        .Select(
            actionMapConfig => new SerializeTask <InputSourceMapping <MenuAction> > {
            FilePath          = MenuInput.Bindings.CustomInputMappingFilePath.Value,
            SerializableValue = actionMapConfig.InputMapping
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(applicationLock.RunWithLock <SerializeTask <InputSourceMapping <MenuAction> > >(mappingConfig => {
            mappingConfig.SerializableValue.Serialize2Disk(mappingConfig.FilePath);
        }));
        pilotInputBindings.InputMappingChanges
        .Skip(1)
        .Select(actionMapConfig => new SerializeTask <InputSourceMapping <WingsuitAction> > {
            FilePath          = PilotInput.Bindings.CustomInputMappingFilePath.Value,
            SerializableValue = actionMapConfig.InputMapping
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(applicationLock.RunWithLock <SerializeTask <InputSourceMapping <WingsuitAction> > >(mappingConfig => {
            mappingConfig.SerializableValue.Serialize2Disk(mappingConfig.FilePath);
        }));
        spectatorInputBindings.InputMappingChanges
        .Skip(1)
        .Select(actionMapConfig => new SerializeTask <InputSourceMapping <SpectatorAction> > {
            FilePath          = SpectatorInput.Bindings.CustomInputMappingFilePath.Value,
            SerializableValue = actionMapConfig.InputMapping
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(applicationLock.RunWithLock <SerializeTask <InputSourceMapping <SpectatorAction> > >(mappingConfig => {
            mappingConfig.SerializableValue.Serialize2Disk(mappingConfig.FilePath);
        }));
        parachuteInputBindings.InputMappingChanges
        .Skip(1)
        .Select(actionMapConfig => new SerializeTask <InputSourceMapping <ParachuteAction> > {
            FilePath          = ParachuteControls.CustomInputMappingFilePath.Value,
            SerializableValue = actionMapConfig.InputMapping
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(applicationLock.RunWithLock <SerializeTask <InputSourceMapping <ParachuteAction> > >(mappingConfig => {
            mappingConfig.SerializableValue.Serialize2Disk(mappingConfig.FilePath);
        }));


        LoadVrConfig(gameSettingsProvider);

        var dependencyResolver = new GameObject("GlobalDependencyResolver").AddComponent <UnityDependencyResolver>();

        dependencyResolver.NonSerializableRefs.Add(new DependencyReference("fileWriterScheduler", Schedulers.FileWriterScheduler));
        dependencyResolver.NonSerializableRefs.Add(new DependencyReference("menuInputBindings", menuInputBindings));
        dependencyResolver.NonSerializableRefs.Add(new DependencyReference("pilotInputBindings", pilotInputBindings));
        dependencyResolver.NonSerializableRefs.Add(new DependencyReference("spectatorInputBindings", spectatorInputBindings));
        dependencyResolver.NonSerializableRefs.Add(new DependencyReference("parachuteInputBindings", parachuteInputBindings));
        dependencyResolver.Resolve();

        // Create and inject the player interface dynamically
        var cameraManager = Object.FindObjectOfType <CameraManager>();

        cameraManager.CreateCameraRig(gameSettingsProvider.ActiveVrMode, dependencyResolver.DependencyContainer);
        var canvasManager = Object.FindObjectOfType <UICanvasManager>();

        canvasManager.Initialize(cameraManager);

        if (gameSettingsProvider.ActiveVrMode == VrMode.Oculus)
        {
            new GameObject("__OculusGlobalEventEmitter").AddComponent <OculusGlobalEventEmitter>();
        }

        // Create a cursor
        var cursor = CreateCursor(gameSettingsProvider.ActiveVrMode, cameraManager.Rig.GetMainCamera());

        dependencyResolver.NonSerializableRefs.Add(new DependencyReference("cursor", cursor));
        var inputModule = GameObject.FindObjectOfType <CursorInputModule>();

        inputModule.Cursor           = cursor;
        inputModule.NavigationDevice = new MenuActionMapCursor(menuActionMapProvider);
        var raycasters = GameObject.FindObjectsOfType <PhysicsRayBasedRaycaster>();

        foreach (var raycaster in raycasters)
        {
            raycaster.SetCamera(cameraManager.Rig.GetMainCamera());
        }

        var hudPositioner = GameObject.Find("HUDElementPositioner").GetComponent <HudElementPositioner>();
        var hudElements   = Object.FindObjectsOfType <HudElement>();

        for (int i = 0; i < hudElements.Length; i++)
        {
            var hudElement = hudElements[i];
            hudPositioner.AddHudElement(hudElement);
        }

//        var courseManager = Object.FindObjectOfType<CourseManager>();
//        courseManager.CameraTransform = cameraManager.Rig.transform;

        var courseEditor = GameObject.Find("CourseEditor");

        courseEditor.SetActive(false);

        /* ======== Load Swiss Alps ======= */

        dependencyResolver.Resolve();

        yield return(SceneManager.LoadSceneAsync("SwissAlps", LoadSceneMode.Additive).WaitUntilDone());

        var windManager   = Object.FindObjectOfType <WindManager>();
        var windEffectors = Object.FindObjectsOfType <WindEffector>();

        for (int i = 0; i < windEffectors.Length; i++)
        {
            windEffectors[i].Manager = windManager;
        }

        var evts    = Object.FindObjectOfType <AbstractUnityEventSystem>();
        var turrets = Object.FindObjectsOfType <Turret>();

        for (int i = 0; i < turrets.Length; i++)
        {
            turrets[i].Initialize(evts);
        }

        dependencyResolver.Resolve();

        gameSettingsApplier.ApplySettings(gameSettingsProvider.ActiveSettings, gameSettingsProvider.ActiveVrMode);

        var optionsMenu = Object.FindObjectOfType <OptionsMenu>();

        optionsMenu.Initialize();

        var challengeManager = GameObject.FindObjectOfType <ChallengeManager>();

        challengeManager.Initialize();

        //        var networkConfig = GameObject.FindObjectOfType<NetworkConfig>();
        //        if (networkConfig.IsServer) {
        //            var server = GameObject.FindObjectOfType<VoloServer>();
        //            server.Host("Silly host name", networkConfig.ServerPort);
        //        }
        //        else {
        //            var client = GameObject.FindObjectOfType<VoloClient>();
        //            client.Join(new IPEndPoint(IPAddress.Parse("127.0.0.1"), networkConfig.ServerPort));
        //        }

        /* Clean up after ourselves, now's a good time */
        GC.Collect();
    }
示例#2
0
    void Awake()
    {
        // Hack to initialize the thread dispatcher in the unity thread
        {
            var threadDispathcer = UnityThreadDispatcher.Instance;
        }

        var applicationLifeCycleEvents = new GameObject("ApplicationLifeCycleEvents")
                                         .AddComponent <ComponentLifecycleEvents>();

        _gameSettingsProvider.UpdateGameSettings(GameSettings.ReadSettingsFromDisk());
        var gameSettingsApplier = new GameSettingsApplier(_userConfigurableSystems);

        _gameSettingsProvider.SettingChanges
        .Subscribe(settings => gameSettingsApplier.ApplySettings(settings, _gameSettingsProvider.ActiveVrMode));
        _gameSettingsProvider.SettingChanges
        .Select(gameSettings => gameSettings.Audio)
        .CombineLatest(FMODUtil.StudioSystem(), (audioSettings, fmodSystem) => audioSettings)
        .Subscribe(audioSettings => {
            gameSettingsApplier.ApplyAudioSettings(audioSettings);
        });

        _gameSettingsProvider.SettingChanges
        .Skip(1)
        .Throttle(TimeSpan.FromSeconds(2), Scheduler.ThreadPool)
        .Select(gameSettings => new SerializeTask <GameSettings> {
            FilePath = GameSettings.UserSettingsConfigPath.Value, SerializableValue = gameSettings
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(_quitter.RunWithLock <SerializeTask <GameSettings> >(settingsConfig => {
            var gameSettings = settingsConfig.SerializableValue;
            gameSettings.Serialize2Disk(settingsConfig.FilePath);
        }));

        var inputSettings = _gameSettingsProvider.SettingChanges
                            .Select(settings => InputSettings.FromGameSettings(settings.Input));
        InputBindings <MenuAction> menuInputBindings = new InputBindings <MenuAction>(
            MenuInput.Bindings.InitialMapping(),
            inputSettings,
            MenuInput.Bindings.DefaultControllerMappings.Value);
        InputBindings <WingsuitAction> pilotInputBindings = new InputBindings <WingsuitAction>(
            PilotInput.Bindings.InitialMapping(),
            inputSettings,
            PilotInput.Bindings.DefaultControllerMappings.Value);
        InputBindings <SpectatorAction> spectatorInputBindings = new InputBindings <SpectatorAction>(
            SpectatorInput.Bindings.InitialMapping(),
            inputSettings,
            SpectatorInput.Bindings.DefaultControllerMappings.Value);
        InputBindings <ParachuteAction> parachuteInputBindings = new InputBindings <ParachuteAction>(
            ParachuteControls.InitialMapping(),
            inputSettings,
            ParachuteControls.DefaultMappings.Value);

        _joystickActivator.ActiveController.Subscribe(controllerId => {
            menuInputBindings.UpdateControllerId(controllerId);
            pilotInputBindings.UpdateControllerId(controllerId);
            spectatorInputBindings.UpdateControllerId(controllerId);
            parachuteInputBindings.UpdateControllerId(controllerId);
        });

        // TODO Inject Inputbindings into options menu

        applicationLifeCycleEvents.OnDestroyEvent += () => {
            menuInputBindings.Dispose();
            parachuteInputBindings.Dispose();
            pilotInputBindings.Dispose();
            spectatorInputBindings.Dispose();
        };

        _menuActionMapProvider.SetInputMappingSource(menuInputBindings.InputMappingChanges);
        _pilotActionMapProvider.SetInputMappingSource(pilotInputBindings.InputMappingChanges);
        _parachuteActionMapProvider.SetInputMappingSource(parachuteInputBindings.InputMappingChanges);

        menuInputBindings.InputMappingChanges
        .Skip(1)
        .Select(actionMapConfig => new SerializeTask <InputSourceMapping <MenuAction> > {
            FilePath          = MenuInput.Bindings.CustomInputMappingFilePath.Value,
            SerializableValue = actionMapConfig.InputMapping
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(_quitter.RunWithLock <SerializeTask <InputSourceMapping <MenuAction> > >(mappingConfig => {
            mappingConfig.SerializableValue.Serialize2Disk(mappingConfig.FilePath);
        }));
        pilotInputBindings.InputMappingChanges
        .Skip(1)
        .Select(actionMapConfig => new SerializeTask <InputSourceMapping <WingsuitAction> > {
            FilePath          = PilotInput.Bindings.CustomInputMappingFilePath.Value,
            SerializableValue = actionMapConfig.InputMapping
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(_quitter.RunWithLock <SerializeTask <InputSourceMapping <WingsuitAction> > >(mappingConfig => {
            mappingConfig.SerializableValue.Serialize2Disk(mappingConfig.FilePath);
        }));
        spectatorInputBindings.InputMappingChanges
        .Skip(1)
        .Select(actionMapConfig => new SerializeTask <InputSourceMapping <SpectatorAction> > {
            FilePath          = SpectatorInput.Bindings.CustomInputMappingFilePath.Value,
            SerializableValue = actionMapConfig.InputMapping
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(_quitter.RunWithLock <SerializeTask <InputSourceMapping <SpectatorAction> > >(mappingConfig => {
            mappingConfig.SerializableValue.Serialize2Disk(mappingConfig.FilePath);
        }));
        parachuteInputBindings.InputMappingChanges
        .Skip(1)
        .Select(actionMapConfig => new SerializeTask <InputSourceMapping <ParachuteAction> > {
            FilePath          = ParachuteControls.CustomInputMappingFilePath.Value,
            SerializableValue = actionMapConfig.InputMapping
        })
        .ObserveOn(Schedulers.FileWriterScheduler)
        .Subscribe(_quitter.RunWithLock <SerializeTask <InputSourceMapping <ParachuteAction> > >(mappingConfig => {
            mappingConfig.SerializableValue.Serialize2Disk(mappingConfig.FilePath);
        }));

        _optionsMenu.MenuInputBindings      = menuInputBindings;
        _optionsMenu.ParachuteInputBindings = parachuteInputBindings;
        _optionsMenu.PilotInputBindings     = pilotInputBindings;
        _optionsMenu.SpectatorInputBindings = spectatorInputBindings;
    }