Guest User

character.cpp

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×