Advertisement
Guest User

cpp file

a guest
Jan 16th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. .cpp
  2. // Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
  3.  
  4. #include "lilknightgameCharacter.h"
  5. #include "HeadMountedDisplayFunctionLibrary.h"
  6. #include "Camera/CameraComponent.h"
  7. #include "Components/CapsuleComponent.h"
  8. #include "Components/InputComponent.h"
  9. #include "GameFramework/CharacterMovementComponent.h"
  10. #include "GameFramework/Controller.h"
  11. #include "GameFramework/SpringArmComponent.h"
  12.  
  13.  
  14. //////////////////////////////////////////////////////////////////////////
  15. // AlilknightgameCharacter
  16.  
  17. AlilknightgameCharacter::AlilknightgameCharacter()
  18. {
  19.  
  20. // Set size for collision capsule
  21. GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
  22.  
  23. // set our turn rates for input
  24. BaseTurnRate = 45.f;
  25. BaseLookUpRate = 45.f;
  26.  
  27. // Don't rotate when the controller rotates. Let that just affect the camera.
  28. bUseControllerRotationPitch = false;
  29. bUseControllerRotationYaw = false;
  30. bUseControllerRotationRoll = false;
  31.  
  32. // Configure character movement
  33. GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
  34. GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
  35. GetCharacterMovement()->JumpZVelocity = 600.f;
  36. GetCharacterMovement()->AirControl = 0.2f;
  37.  
  38. // Create a camera boom (pulls in towards the player if there is a collision)
  39. CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  40. CameraBoom->SetupAttachment(RootComponent);
  41. CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
  42. CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
  43.  
  44. // Create a follow camera
  45. FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  46. FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  47. FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  48.  
  49. // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
  50. // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
  51. }
  52.  
  53. //////////////////////////////////////////////////////////////////////////
  54. // Input
  55.  
  56. void AlilknightgameCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
  57. {
  58.  
  59. InputComponent->BindAction("Attack", IE_Pressed, this, &AlilknightgameCharacter::Attack);
  60.  
  61.  
  62. InputComponent->BindAction("Shield", IE_Pressed, this, &AlilknightgameCharacter::Shield);
  63.  
  64.  
  65. // Set up gameplay key bindings
  66. check(PlayerInputComponent);
  67. PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  68. PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  69.  
  70.  
  71.  
  72. PlayerInputComponent->BindAxis("MoveForward", this, &AlilknightgameCharacter::MoveForward);
  73. PlayerInputComponent->BindAxis("MoveRight", this, &AlilknightgameCharacter::MoveRight);
  74.  
  75. // We have 2 versions of the rotation bindings to handle different kinds of devices differently
  76. // "turn" handles devices that provide an absolute delta, such as a mouse.
  77. // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  78. PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  79. PlayerInputComponent->BindAxis("TurnRate", this, &AlilknightgameCharacter::TurnAtRate);
  80. PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  81. PlayerInputComponent->BindAxis("LookUpRate", this, &AlilknightgameCharacter::LookUpAtRate);
  82.  
  83. // handle touch devices
  84. PlayerInputComponent->BindTouch(IE_Pressed, this, &AlilknightgameCharacter::TouchStarted);
  85. PlayerInputComponent->BindTouch(IE_Released, this, &AlilknightgameCharacter::TouchStopped);
  86.  
  87. // VR headset functionality
  88. PlayerInputComponent->BindAction("ResetVR", IE_Pressed, this, &AlilknightgameCharacter::OnResetVR);
  89. }
  90.  
  91.  
  92. void AlilknightgameCharacter::OnResetVR()
  93. {
  94. UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition();
  95. }
  96.  
  97. void AlilknightgameCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
  98. {
  99. Jump();
  100. }
  101.  
  102. void AlilknightgameCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
  103. {
  104. StopJumping();
  105. }
  106.  
  107. void AlilknightgameCharacter::TurnAtRate(float Rate)
  108. {
  109. // calculate delta for this frame from the rate information
  110. AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  111. }
  112.  
  113. void AlilknightgameCharacter::LookUpAtRate(float Rate)
  114. {
  115. // calculate delta for this frame from the rate information
  116. AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  117. }
  118.  
  119. void AlilknightgameCharacter::MoveForward(float Value)
  120. {
  121. if ((Controller != NULL) && (Value != 0.0f))
  122. {
  123. // find out which way is forward
  124. const FRotator Rotation = Controller->GetControlRotation();
  125. const FRotator YawRotation(0, Rotation.Yaw, 0);
  126.  
  127. // get forward vector
  128. const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  129. AddMovementInput(Direction, Value);
  130. }
  131. }
  132.  
  133. void AlilknightgameCharacter::MoveRight(float Value)
  134. {
  135. if ( (Controller != NULL) && (Value != 0.0f) )
  136. {
  137. // find out which way is right
  138. const FRotator Rotation = Controller->GetControlRotation();
  139. const FRotator YawRotation(0, Rotation.Yaw, 0);
  140.  
  141. // get right vector
  142. const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  143. // add movement in that direction
  144. AddMovementInput(Direction, Value);
  145. }
  146. }
  147.  
  148. void AlilknightgameCharacter::Attack() {
  149. isAttacking = true;
  150. GetWorld()->GetTimerManager().SetTimer(AttackHandle, this, &AlilknightgameCharacter::StopAttack, 1.5f,false);
  151.  
  152. }
  153. void AlilknightgameCharacter::StopAttack() {
  154. isAttacking = false;
  155. }
  156. void AlilknightgameCharacter::Shield() {
  157. isShielding = true;
  158. GetWorld()->GetTimerManager().SetTimer(AttackHandle, this, &AlilknightgameCharacter::StopShield, 1.5f, false);
  159. }
  160.  
  161. void AlilknightgameCharacter::StopShield() {
  162. isShielding = false;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement