Example #1
0
        protected override void Initialize()
        {
            // ----- Initialize Services.
              // The services are stored in Game.Services to make them accessible for all
              // game components.

              // Add the input service, which manages device input, button presses, etc.
              _inputManager = new InputManager(false);
              Services.AddService(typeof(IInputService), _inputManager);

              // Add the animation service.
              _animationManager = new AnimationManager();
              Services.AddService(typeof(IAnimationService), _animationManager);

              // Add the physics simulation to the service and add some force effects.
              // (TODO: Create a IPhysicsService that owns the simulation.)
              _simulation = new Simulation();
              _simulation.ForceEffects.Add(new Gravity());
              _simulation.ForceEffects.Add(new Damping());
              Services.AddService(typeof(Simulation), _simulation);

              // ----- Add GameComponents
              Components.Add(new GamerServicesComponent(this));               // XNA gamer services needed for avatars.
              Components.Add(new Camera(this));                               // Controls the camera.
              Components.Add(new BallShooter(this));                          // Shoot balls at the target position.
              Components.Add(new Grab(this));                                 // Allows to grab objects with the mouse/gamepad
              Components.Add(new RigidBodyRenderer(this) { DrawOrder = 10 }); // Renders all rigid bodies of the simulation.
              Components.Add(new Reticle(this) { DrawOrder = 20 });           // Draws a cross-hair for aiming.
              Components.Add(new Help(this) { DrawOrder = 30 });              // Draws help text.
              Components.Add(new Profiler(this) { DrawOrder = 40 });          // Displays profiling data.

              // Initialize the sample factory methods. All samples must be added to this
              // array to be shown.
              _createSampleDelegates = new Func<Sample>[]
              {
            () => new BasicAvatarSample(this),
            () => new WrappedAnimationSample(this),
            () => new BakedAnimationSample(this),
            () => new CustomAnimationSample(this),
            () => new AttachmentSample(this),
            () => new RagdollSample(this),
            () => new AdvancedRagdollSample(this),
              };

              // Start the first sample in the array.
              _activeSampleIndex = 0;
              _activeSample = _createSampleDelegates[0]();
              Components.Add(_activeSample);

              base.Initialize();
        }
Example #2
0
        private void SwitchSamples()
        {
            // If left arrow or DPadLeft is pressed, we want to switch to the previous sample (index - 1).
              int sampleIndexIncrement = 0;
              if (_inputManager.IsPressed(Keys.Left, false) || _inputManager.IsPressed(Buttons.DPadLeft, false, PlayerIndex.One))
              {
            sampleIndexIncrement = -1;
              }

              // If right arrow or DPadRight is pressed, we want to switch to the next sample (index + 1).
              if (_inputManager.IsPressed(Keys.Right, false) || _inputManager.IsPressed(Buttons.DPadRight, false, PlayerIndex.One))
              {
            sampleIndexIncrement = +1;
              }

              if (sampleIndexIncrement != 0)
              {
            // Switch samples.

            // Stop current sample.
            Components.Remove(_activeSample);
            _activeSample.Dispose();

            // Clean up memory.
            GC.Collect();

            // Get index of next sample.
            _activeSampleIndex += sampleIndexIncrement;
            if (_activeSampleIndex >= _createSampleDelegates.Length)
              _activeSampleIndex = 0;
            else if (_activeSampleIndex < 0)
              _activeSampleIndex = _createSampleDelegates.Length - 1;

            // Add next sample.
            _activeSample = _createSampleDelegates[_activeSampleIndex]();
            Components.Add(_activeSample);
              }
        }