Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using BEPUphysics;
  2. using BEPUphysics.Character;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Diagnostics;
  6. using BEPUphysics.BroadPhaseEntries.MobileCollidables;
  7. using ConversionHelper;
  8. using BEPUutilities;
  9.  
  10. namespace BEPUphysicsDemos.AlternateMovement
  11. {
  12.     public class CharacterControllerInput
  13.     {
  14.         public Camera Camera;
  15.  
  16.         public CharacterController CharacterController;
  17.  
  18.         public CharacterCameraControlScheme CameraControlScheme;
  19.  
  20.         public bool IsActive;
  21.  
  22.         public Space Space;
  23.  
  24.  
  25.         public CharacterControllerInput(Space owningSpace, Camera camera, DemosGame game)
  26.         {
  27.             CharacterController = new CharacterController();
  28.             Camera = camera;
  29.             CameraControlScheme = new CharacterCameraControlScheme(CharacterController, camera, game);
  30.  
  31.             Space = owningSpace;
  32.         }
  33.  
  34.         public void Activate()
  35.         {
  36.             if (!IsActive)
  37.             {
  38.                 IsActive = true;
  39.                 Space.Add(CharacterController);
  40.                 CharacterController.Body.Position = Camera.Position - new Vector3(0, CameraControlScheme.StandingCameraOffset, 0);
  41.             }
  42.         }
  43.  
  44.         public void Deactivate()
  45.         {
  46.             if (IsActive)
  47.             {
  48.                 IsActive = false;
  49.                 Space.Remove(CharacterController);
  50.             }
  51.         }
  52.  
  53.         public void Update(float dt, KeyboardState previousKeyboardInput, KeyboardState keyboardInput, GamePadState previousGamePadInput, GamePadState gamePadInput)
  54.         {
  55.             if (IsActive)
  56.             {
  57.                 CameraControlScheme.Update(dt);
  58.  
  59.                 Vector2 totalMovement = Vector2.Zero;
  60.  
  61.                 // Changed the keybinds to sanity!
  62.                 if (keyboardInput.IsKeyDown(Keys.W))
  63.                 {
  64.                     totalMovement += new Vector2(0, 1);
  65.                 }
  66.                 if (keyboardInput.IsKeyDown(Keys.S))
  67.                 {
  68.                     totalMovement += new Vector2(0, -1);
  69.                 }
  70.                 if (keyboardInput.IsKeyDown(Keys.A))
  71.                 {
  72.                     totalMovement += new Vector2(-1, 0);
  73.                 }
  74.                 if (keyboardInput.IsKeyDown(Keys.D))
  75.                 {
  76.                     totalMovement += new Vector2(1, 0);
  77.                 }
  78.                 if (totalMovement == Vector2.Zero)
  79.                 {
  80.                     CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Zero;
  81.                 }
  82.                 else
  83.                 {
  84.                     CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Normalize(totalMovement);
  85.                 }
  86.                 if (previousKeyboardInput.IsKeyUp(Keys.Space) && keyboardInput.IsKeyDown(Keys.Space))
  87.                 {
  88.                     CharacterController.Jump();
  89.                 }
  90.                 CharacterController.ViewDirection = Camera.WorldMatrix.Forward;
  91.                 // --------- This (all the below) is the key addition! --------
  92.                 if (flying)
  93.                 {
  94.                     CharacterController.Body.Position += Camera.WorldMatrix.Forward * totalMovement.Y + Camera.WorldMatrix.Right * totalMovement.X;
  95.                 }
  96.                 if (keyboardInput.IsKeyDown(Keys.X) && !previousKeyboardInput.IsKeyDown(Keys.X))
  97.                 {
  98.                     flying = !flying;
  99.                     if (flying)
  100.                     {
  101.                         mass = CharacterController.Body.Mass;
  102.                         CharacterController.Body.Mass = 0;
  103.                         CharacterController.Body.LinearVelocity = Vector3.Zero;
  104.                     }
  105.                     else
  106.                     {
  107.                         CharacterController.Body.Mass = mass;
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.  
  113.         bool flying = false;
  114.  
  115.         float mass;
  116.  
  117.         Quaternion orient;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement