Advertisement
Guest User

Untitled

a guest
May 30th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include "TPWalker.h"
  2. #include "Math/BsVector3.h"
  3. #include "Math/BsMath.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "Components/BsCCamera.h"
  6. #include "BsApplication.h"
  7. #include "Physics/BsPhysics.h"
  8. #include "Resources/BsResources.h"
  9. #include "Resources/BsBuiltinResources.h"
  10. #include "Assets.h"
  11.  
  12. namespace bs
  13. {
  14.     Assets assets1;
  15.  
  16.     /** Initial movement speed. */
  17.     constexpr float START_SPEED = 2.0f; // m/s
  18.  
  19.     /** Maximum movement speed. */
  20.     constexpr float TOP_SPEED = 4.0f; // m/s
  21.  
  22.     /** Acceleration that determines how quickly to go from starting to top speed. */
  23.     constexpr float ACCELERATION = 2.0f;
  24.  
  25.     /** Multiplier applied to the speed when the fast move button is held. */
  26.     constexpr float FAST_MODE_MULTIPLIER = 2.0f;
  27.  
  28.     TPWalker::TPWalker(const HSceneObject& parent)
  29.         :Component(parent)
  30.     {
  31.         // Set a name for the component, so we can find it later if needed
  32.         setName("TPWalker");
  33.  
  34.         // Get handles for key bindings. Actual keys attached to these bindings will be registered during app start-up.
  35.         mMoveForward2 = VirtualButton("Forward2");
  36.         mMoveBack = VirtualButton("Back");
  37.         mMoveLeft = VirtualButton("Left");
  38.         mMoveRight = VirtualButton("Right");
  39.         mFastMove = VirtualButton("FastMove");
  40.     }
  41.  
  42.     void TPWalker::characterMovement()
  43.     {
  44.         //gDebug().logDebug(toString(ressources.blue));
  45.  
  46.         // Check if any movement keys are being held
  47.         bool goingForward2 = gVirtualInput().isButtonHeld(mMoveForward2);
  48.         bool goingBack = gVirtualInput().isButtonHeld(mMoveBack);
  49.         bool goingLeft = gVirtualInput().isButtonHeld(mMoveLeft);
  50.         bool goingRight = gVirtualInput().isButtonHeld(mMoveRight);
  51.         bool fastMove = gVirtualInput().isButtonHeld(mFastMove);
  52.  
  53.         const Transform& tfrm = SO()->getTransform();
  54.  
  55.         // If the movement button is pressed, determine direction to move in
  56.         Vector3 direction = Vector3::ZERO;
  57.         if (goingForward2)
  58.         {
  59.             assets1.characterAnimation->play(assets1.characterWalkingAnimClip);
  60.             direction -= tfrm.getForward();
  61.         }
  62.         if (goingBack) direction += tfrm.getForward();
  63.         if (goingRight) direction += tfrm.getRight();
  64.         if (goingLeft) direction -= tfrm.getRight();
  65.  
  66.         // Eliminate vertical movement
  67.         direction.y = 0.0f;
  68.         direction.normalize();
  69.  
  70.         const float frameDelta = gApplication().getFixedUpdateStep();
  71.  
  72.         // If a direction is chosen, normalize it to determine final direction.
  73.         if (direction.squaredLength() != 0)
  74.         {
  75.             direction.normalize();
  76.  
  77.             // Apply fast move multiplier if the fast move button is held.
  78.             float multiplier = 1.0f;
  79.             if (fastMove)
  80.                 multiplier = FAST_MODE_MULTIPLIER;
  81.  
  82.             // Calculate current speed of the camera
  83.             mCurrentSpeed = Math::clamp(mCurrentSpeed + ACCELERATION * frameDelta, START_SPEED, TOP_SPEED);
  84.             mCurrentSpeed *= multiplier;
  85.         }
  86.         else
  87.         {
  88.             mCurrentSpeed = 0.0f;
  89.         }
  90.  
  91.         // If the current speed isn't too small, move the camera in the wanted direction
  92.         Vector3 velocity(BsZero);
  93.         float tooSmall = std::numeric_limits<float>::epsilon();
  94.         if (mCurrentSpeed > tooSmall)
  95.             velocity = direction * mCurrentSpeed;
  96.  
  97.         // Note: Gravity is acceleration, but since the walker doesn't support falling, just apply it as a velocity
  98.         Vector3 velocity2 = direction * mCurrentSpeed;
  99.         SO()->move(velocity2 * frameDelta);
  100.     }
  101.  
  102.     void TPWalker::fixedUpdate()
  103.     {
  104.         //Assets assets4 = loadAssets();
  105.         characterMovement();
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement