SHARE
TWEET

Untitled

a guest May 30th, 2014 237 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #include "Demo.h"
  4. #include "DemoCharacter.h"
  5. #include "DemoProjectile.h"
  6. #include "UsableActor.h"
  7.  
  8. //////////////////////////////////////////////////////////////////////////
  9. // ADemoCharacter
  10.  
  11. ADemoCharacter::ADemoCharacter(const class FPostConstructInitializeProperties& PCIP)
  12.         : Super(PCIP)
  13. {
  14.         // Set size for collision capsule
  15.         CapsuleComponent->InitCapsuleSize(42.f, 96.0f);
  16.  
  17.         // set our turn rates for input
  18.         BaseTurnRate = 45.f;
  19.         BaseLookUpRate = 45.f;
  20.  
  21.         // Create a CameraComponent    
  22.         FirstPersonCameraComponent = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
  23.         FirstPersonCameraComponent->AttachParent = CapsuleComponent;
  24.         FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 64.f); // Position the camera
  25.  
  26.         // Default offset from the character location for projectiles to spawn
  27.         GunOffset = FVector(100.0f, 30.0f, 10.0f);
  28.  
  29.         // Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)
  30.         Mesh1P = PCIP.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("CharacterMesh1P"));
  31.         Mesh1P->SetOnlyOwnerSee(true);                  // only the owning player will see this mesh
  32.         Mesh1P->AttachParent = FirstPersonCameraComponent;
  33.         Mesh1P->RelativeLocation = FVector(0.f, 0.f, -150.f);
  34.         Mesh1P->bCastDynamicShadow = false;
  35.         Mesh1P->CastShadow = false;
  36.  
  37.         // Note: The ProjectileClass and the skeletal mesh/anim blueprints for Mesh1P are set in the
  38.         // derived blueprint asset named MyCharacter (to avoid direct content references in C++)
  39.  
  40.         MaxUseDistance = 800;
  41.         bHasNewFocus = true;
  42. }
  43.  
  44. /*
  45. Performs raytrace to find closest looked-at UsableActor.
  46. */
  47. AUsableActor* ADemoCharacter::GetUsableInView()
  48. {
  49.         FVector camLoc;
  50.         FRotator camRot;
  51.  
  52.         if (Controller == NULL)
  53.                 return NULL;
  54.  
  55.         Controller->GetPlayerViewPoint(camLoc, camRot);
  56.         const FVector start_trace = camLoc;
  57.         const FVector direction = camRot.Vector();
  58.         const FVector end_trace = start_trace + (direction * MaxUseDistance);
  59.  
  60.         FCollisionQueryParams TraceParams(FName(TEXT("")), true, this);
  61.         TraceParams.bTraceAsyncScene = true;
  62.         TraceParams.bReturnPhysicalMaterial = false;
  63.         TraceParams.bTraceComplex = true;
  64.  
  65.         FHitResult Hit(ForceInit);
  66.         GetWorld()->LineTraceSingle(Hit, start_trace, end_trace, COLLISION_PROJECTILE, TraceParams);
  67.  
  68.         return Cast<AUsableActor>(Hit.GetActor());
  69. }
  70.  
  71. /*
  72. Update actor currently being looked at by player.
  73. */
  74. void ADemoCharacter::Tick(float DeltaSeconds)
  75. {
  76.         Super::Tick(DeltaSeconds);
  77.  
  78.         if (Controller && Controller->IsLocalController())
  79.         {
  80.                 AUsableActor* usable = GetUsableInView();
  81.  
  82.                 // End Focus
  83.                 if (FocusedUsableActor != usable)
  84.                 {
  85.                         if (FocusedUsableActor)
  86.                         {
  87.                                 FocusedUsableActor->EndFocusItem();
  88.                         }
  89.  
  90.                         bHasNewFocus = true;
  91.                 }
  92.  
  93.                 // Assign new Focus
  94.                 FocusedUsableActor = usable;
  95.  
  96.                 // Start Focus.
  97.                 if (usable)
  98.                 {
  99.                         if (bHasNewFocus)
  100.                         {
  101.                                 usable->StartFocusItem();
  102.                                 bHasNewFocus = false;
  103.                         }
  104.                 }
  105.         }
  106. }
  107.  
  108. /*
  109. Runs on Server. Perform "OnUsed" on currently viewed UsableActor if implemented.
  110. */
  111. void ADemoCharacter::Use_Implementation()
  112. {
  113.         if (FocusedUsableActor)
  114.         {
  115.                 FocusedUsableActor->OnUsed(this);
  116.         }
  117. }
  118.  
  119. bool ADemoCharacter::Use_Validate()
  120. {
  121.         // No special server-side validation performed.
  122.         return true;
  123. }
  124.  
  125. //////////////////////////////////////////////////////////////////////////
  126. // Input
  127.  
  128. void ADemoCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  129. {
  130.         // set up gameplay key bindings
  131.         check(InputComponent);
  132.  
  133.         InputComponent->BindAction("Use", IE_Pressed, this, &ADemoCharacter::Use);
  134.  
  135.         InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  136.        
  137.         InputComponent->BindAction("Fire", IE_Pressed, this, &ADemoCharacter::OnFire);
  138.         InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &ADemoCharacter::TouchStarted);
  139.  
  140.         InputComponent->BindAxis("MoveForward", this, &ADemoCharacter::MoveForward);
  141.         InputComponent->BindAxis("MoveRight", this, &ADemoCharacter::MoveRight);
  142.        
  143.         // We have 2 versions of the rotation bindings to handle different kinds of devices differently
  144.         // "turn" handles devices that provide an absolute delta, such as a mouse.
  145.         // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  146.         InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  147.         InputComponent->BindAxis("TurnRate", this, &ADemoCharacter::TurnAtRate);
  148.         InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  149.         InputComponent->BindAxis("LookUpRate", this, &ADemoCharacter::LookUpAtRate);
  150. }
  151.  
  152. void ADemoCharacter::OnFire()
  153. {
  154.         // try and fire a projectile
  155.         if (ProjectileClass != NULL)
  156.         {
  157.                 const FRotator SpawnRotation = GetControlRotation();
  158.                 // MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position
  159.                 const FVector SpawnLocation = GetActorLocation() + SpawnRotation.RotateVector(GunOffset);
  160.  
  161.                 UWorld* const World = GetWorld();
  162.                 if (World != NULL)
  163.                 {
  164.                         // spawn the projectile at the muzzle
  165.                         World->SpawnActor<ADemoProjectile>(ProjectileClass, SpawnLocation, SpawnRotation);
  166.                 }
  167.         }
  168.  
  169.         // try and play the sound if specified
  170.         if (FireSound != NULL)
  171.         {
  172.                 UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
  173.         }
  174.  
  175.         // try and play a firing animation if specified
  176.         if(FireAnimation != NULL)
  177.         {
  178.                 // Get the animation object for the arms mesh
  179.                 UAnimInstance* AnimInstance = Mesh1P->GetAnimInstance();
  180.                 if(AnimInstance != NULL)
  181.                 {
  182.                         AnimInstance->Montage_Play(FireAnimation, 1.f);
  183.                 }
  184.         }
  185.  
  186. }
  187.  
  188. void ADemoCharacter::TouchStarted(const ETouchIndex::Type FingerIndex, const FVector Location)
  189. {
  190.         // only fire for first finger down
  191.         if (FingerIndex == 0)
  192.         {
  193.                 OnFire();
  194.         }
  195. }
  196.  
  197. void ADemoCharacter::MoveForward(float Value)
  198. {
  199.         if (Value != 0.0f)
  200.         {
  201.                 // find out which way is forward
  202.                 const FRotator Rotation = GetControlRotation();
  203.                 FRotator YawRotation(0, Rotation.Yaw, 0);
  204.  
  205.                 // Get forward vector
  206.                 const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  207.  
  208.                 // add movement in that direction
  209.                 AddMovementInput(Direction, Value);
  210.         }
  211. }
  212.  
  213. void ADemoCharacter::MoveRight(float Value)
  214. {
  215.         if (Value != 0.0f)
  216.         {
  217.                 // find out which way is right
  218.                 const FRotator Rotation = GetControlRotation();
  219.                 const FRotator YawRotation(0, Rotation.Yaw, 0);
  220.  
  221.                 // Get right vector
  222.                 const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  223.  
  224.                 // add movement in that direction
  225.                 AddMovementInput(Direction, Value);
  226.         }
  227. }
  228.  
  229. void ADemoCharacter::TurnAtRate(float Rate)
  230. {
  231.         // calculate delta for this frame from the rate information
  232.         AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  233. }
  234.  
  235. void ADemoCharacter::LookUpAtRate(float Rate)
  236. {
  237.         // calculate delta for this frame from the rate information
  238.         AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  239. }
RAW Paste Data
Top