Advertisement
Guest User

Character.cpp

a guest
May 4th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1.  
  2. #include "TPSCharacter.h"
  3. #include "Engine/World.h"
  4. #include "Components/InputComponent.h"
  5. #include "Camera/CameraComponent.h"
  6. #include "GameFramework/SpringArmComponent.h"
  7. #include "GameFramework/PawnMovementComponent.h"
  8. #include "Components/CapsuleComponent.h"
  9. #include "components/SkeletalMeshComponent.h"
  10. #include "TPSHealthComponent.h"
  11. #include "TP_SHOOTER.h"
  12. #include "TPSWeapon.h"
  13.  
  14. // Sets default values
  15. ATPSCharacter::ATPSCharacter()
  16. {
  17.     PrimaryActorTick.bCanEverTick = true;
  18.  
  19.     SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
  20.     SpringArmComponent->bUsePawnControlRotation = true;
  21.     SpringArmComponent->SetupAttachment(RootComponent);
  22.  
  23.     GetMovementComponent()->GetNavAgentPropertiesRef().bCanCrouch = true;
  24.     GetCapsuleComponent()->SetCollisionResponseToChannel(COLLISION_WEAPON, ECR_Ignore);
  25.  
  26.     HealthComponent = CreateDefaultSubobject<UTPSHealthComponent>(TEXT("HealthComponent"));
  27.  
  28.     CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
  29.     CameraComponent->SetupAttachment(SpringArmComponent);
  30.    
  31.     ZoomedFOV = 65.0f;
  32.     ZoomInterpSpeed = 20.0f;
  33.     WeaponSocketName = "WeaponSocket";
  34. }
  35.  
  36. // Called when the game starts or when spawned
  37. void ATPSCharacter::BeginPlay()
  38. {
  39.     Super::BeginPlay();
  40.    
  41.     // Set default FOV
  42.     DefaultFOV = CameraComponent->FieldOfView;
  43.  
  44.     // Spawn weapon
  45.     FActorSpawnParameters SpawnParams;
  46.     SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
  47.  
  48.     CurrentWeapon = GetWorld()->SpawnActor<ATPSWeapon>(StarterWeapon, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
  49.     if (CurrentWeapon)
  50.     {
  51.         CurrentWeapon->SetOwner(this);
  52.         CurrentWeapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, WeaponSocketName);
  53.     }
  54.  
  55.     HealthComponent->OnHealthChanged.AddDynamic(this, &ATPSCharacter::OnHealthChanged);
  56. }
  57.  
  58. void ATPSCharacter::MoveForward(float Value)
  59. {
  60.     AddMovementInput(GetActorForwardVector() * Value);
  61. }
  62.  
  63. void ATPSCharacter::MoveRight(float Value)
  64. {
  65.     AddMovementInput(GetActorRightVector() * Value);
  66. }
  67.  
  68. void ATPSCharacter::BeginCrouch()
  69. {
  70.     Crouch();
  71. }
  72.  
  73. void ATPSCharacter::EndCrouch()
  74. {
  75.     UnCrouch();
  76. }
  77.  
  78. void ATPSCharacter::StartAiming()
  79. {
  80.     isPlayerAiming = true;
  81. }
  82.  
  83. void ATPSCharacter::StopAiming()
  84. {
  85.     isPlayerAiming = false;
  86. }
  87.  
  88. void ATPSCharacter::StartFire()
  89. {
  90.     if (CurrentWeapon)
  91.     {
  92.         CurrentWeapon->StartFire();
  93.     }
  94. }
  95.  
  96. void ATPSCharacter::StopFire()
  97. {
  98.     if (CurrentWeapon)
  99.     {
  100.         CurrentWeapon->StopFire();
  101.     }
  102. }
  103.  
  104. void ATPSCharacter::OnHealthChanged(UTPSHealthComponent* OwningHealthComp, float Health, float HealthDelta, const class UDamageType* DamageType, class AController* InstigatedBy, AActor* DamageCauser)
  105. {
  106.  
  107.     if (Health <= 0.0f && !IsDead)
  108.     {
  109.         // Dead
  110.         IsDead = true;
  111.         GetMovementComponent()->StopMovementImmediately();
  112.         GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
  113.  
  114.         DetachFromControllerPendingDestroy();
  115.  
  116.         SetLifeSpan(10.0f);
  117.     }  
  118. }
  119.  
  120. // Called every frame
  121. void ATPSCharacter::Tick(float DeltaTime)
  122. {
  123.     Super::Tick(DeltaTime);
  124.  
  125.     float TargetFOV = isPlayerAiming ? ZoomedFOV : DefaultFOV;
  126.     float NewFOV = FMath::FInterpTo(CameraComponent->FieldOfView, TargetFOV, DeltaTime, ZoomInterpSpeed);
  127.     CameraComponent->SetFieldOfView(NewFOV);
  128. }
  129.  
  130. // Called to bind functionality to input
  131. void ATPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  132. {
  133.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  134.  
  135.     PlayerInputComponent->BindAxis("MoveForward", this, &ATPSCharacter::MoveForward);
  136.     PlayerInputComponent->BindAxis("MoveRight", this, &ATPSCharacter::MoveRight);
  137.  
  138.     PlayerInputComponent->BindAxis("LookUp", this, &ATPSCharacter::AddControllerPitchInput);
  139.     PlayerInputComponent->BindAxis("LookRight", this, &ATPSCharacter::AddControllerYawInput);
  140.    
  141.     PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ATPSCharacter::BeginCrouch);
  142.     PlayerInputComponent->BindAction("Crouch", IE_Released, this, &ATPSCharacter::EndCrouch);
  143.  
  144.     PlayerInputComponent->BindAction("Aiming", IE_Pressed, this, &ATPSCharacter::StartAiming);
  145.     PlayerInputComponent->BindAction("Aiming", IE_Released, this, &ATPSCharacter::StopAiming);
  146.  
  147.     PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ATPSCharacter::StartFire);
  148.     PlayerInputComponent->BindAction("Fire", IE_Released, this, &ATPSCharacter::StopFire);
  149.  
  150. }
  151.  
  152. FVector ATPSCharacter::GetPawnViewLocation() const
  153. {
  154.     // Overrides the engine GetPawnViewLocation method
  155.     if (CameraComponent)
  156.     {
  157.         // Gets the location of the camera component
  158.         return CameraComponent->GetComponentLocation();
  159.     }
  160.     return Super::GetPawnViewLocation();
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement