Advertisement
Dirt113

DHPawnMovementComponent.cpp

Nov 17th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. // Copyright 2015 Dirt Productions. All rights reserved.
  2.  
  3. #include "DistantHome.h"
  4. #include "Public/Player/Pawns/DHPawn.h"
  5. #include "Public/Player/Pawns/DHPawnMovementComponent.h"
  6.  
  7. /** Default constructor */
  8. UDHPawnMovementComponent::UDHPawnMovementComponent(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
  9. {
  10.     MovementState.bCanFly = false;
  11.     MovementState.bCanWalk = true;
  12. }
  13.  
  14. /** Called every frame */
  15. void UDHPawnMovementComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisFunction)
  16. {
  17.     Super::TickComponent(DeltaTime, TickType, ThisFunction);
  18.  
  19.     // Make sure everything is valid, and movement is allowed
  20.     if (PawnOwner && UpdatedComponent && !ShouldSkipUpdate(DeltaTime))
  21.     {
  22.  
  23.         // Get and clear movement vector
  24.         FVector DesiredMovement = ConsumeInputVector() * DeltaTime * GetGlobalBaseSpeed();
  25.  
  26.         if (!DesiredMovement.IsNearlyZero())
  27.         {
  28.             FHitResult Hit;
  29.             SafeMoveUpdatedComponent(DesiredMovement, UpdatedComponent->GetComponentRotation(), true, Hit);
  30.  
  31.             // Attempt to slide if component collides with object
  32.             if (Hit.IsValidBlockingHit()) SlideAlongSurface(DesiredMovement, 1.0f - Hit.Time, Hit.Normal, Hit);
  33.         }
  34.     }
  35. }
  36.  
  37. /** Returns max speed */
  38. float UDHPawnMovementComponent::GetMaxSpeed() const
  39. {
  40.     // Default return value if 450
  41.     float ReturnValue = 450.0f;
  42.  
  43.     // Modify return value based on pawn state if pawn owner is a DHPawn
  44.     const ADHPawn* DHPawnOwner = Cast<ADHPawn>(PawnOwner);
  45.     if (DHPawnOwner)
  46.     {
  47.         // Set return value to global base speed of pawn owner
  48.         ReturnValue = DHPawnOwner->GlobalBaseSpeed;
  49.  
  50.         if (DHPawnOwner->IsCrouching()) ReturnValue *= 0.85f;
  51.         if (DHPawnOwner->IsAltUsingItem()) ReturnValue *= DHPawnOwner->GetCurrentItem()->UsableItemData.AltUseSpeedModifier;
  52.         if (DHPawnOwner->IsUsingItem()) ReturnValue *= DHPawnOwner->GetCurrentItem()->UsableItemData.UseSpeedModifier;
  53.         if (!DHPawnOwner->IsUsingItem() && !DHPawnOwner->IsAltUsingItem()) ReturnValue *= DHPawnOwner->GetCurrentItem()->UsableItemData.IdleSpeedModifier;
  54.     }
  55.  
  56.     return ReturnValue;
  57. }
  58.  
  59. /** Returns global base speed of pawn owner */
  60. float UDHPawnMovementComponent::GetGlobalBaseSpeed() const
  61. {
  62.     // Default return value is 450
  63.     float ReturnValue = 450.0f;
  64.  
  65.     const ADHPawn* DHPawnOwner = Cast<ADHPawn>(PawnOwner);
  66.     if (DHPawnOwner) ReturnValue = DHPawnOwner->GlobalBaseSpeed;
  67.  
  68.     return ReturnValue;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement