Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "TPSCharacter.h"
- #include "Engine/World.h"
- #include "Components/InputComponent.h"
- #include "Camera/CameraComponent.h"
- #include "GameFramework/SpringArmComponent.h"
- #include "GameFramework/PawnMovementComponent.h"
- #include "Components/CapsuleComponent.h"
- #include "components/SkeletalMeshComponent.h"
- #include "TPSHealthComponent.h"
- #include "TP_SHOOTER.h"
- #include "TPSWeapon.h"
- // Sets default values
- ATPSCharacter::ATPSCharacter()
- {
- PrimaryActorTick.bCanEverTick = true;
- SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
- SpringArmComponent->bUsePawnControlRotation = true;
- SpringArmComponent->SetupAttachment(RootComponent);
- GetMovementComponent()->GetNavAgentPropertiesRef().bCanCrouch = true;
- GetCapsuleComponent()->SetCollisionResponseToChannel(COLLISION_WEAPON, ECR_Ignore);
- HealthComponent = CreateDefaultSubobject<UTPSHealthComponent>(TEXT("HealthComponent"));
- CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
- CameraComponent->SetupAttachment(SpringArmComponent);
- ZoomedFOV = 65.0f;
- ZoomInterpSpeed = 20.0f;
- WeaponSocketName = "WeaponSocket";
- }
- // Called when the game starts or when spawned
- void ATPSCharacter::BeginPlay()
- {
- Super::BeginPlay();
- // Set default FOV
- DefaultFOV = CameraComponent->FieldOfView;
- // Spawn weapon
- FActorSpawnParameters SpawnParams;
- SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
- CurrentWeapon = GetWorld()->SpawnActor<ATPSWeapon>(StarterWeapon, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
- if (CurrentWeapon)
- {
- CurrentWeapon->SetOwner(this);
- CurrentWeapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, WeaponSocketName);
- }
- HealthComponent->OnHealthChanged.AddDynamic(this, &ATPSCharacter::OnHealthChanged);
- }
- void ATPSCharacter::MoveForward(float Value)
- {
- AddMovementInput(GetActorForwardVector() * Value);
- }
- void ATPSCharacter::MoveRight(float Value)
- {
- AddMovementInput(GetActorRightVector() * Value);
- }
- void ATPSCharacter::BeginCrouch()
- {
- Crouch();
- }
- void ATPSCharacter::EndCrouch()
- {
- UnCrouch();
- }
- void ATPSCharacter::StartAiming()
- {
- isPlayerAiming = true;
- }
- void ATPSCharacter::StopAiming()
- {
- isPlayerAiming = false;
- }
- void ATPSCharacter::StartFire()
- {
- if (CurrentWeapon)
- {
- CurrentWeapon->StartFire();
- }
- }
- void ATPSCharacter::StopFire()
- {
- if (CurrentWeapon)
- {
- CurrentWeapon->StopFire();
- }
- }
- void ATPSCharacter::OnHealthChanged(UTPSHealthComponent* OwningHealthComp, float Health, float HealthDelta, const class UDamageType* DamageType, class AController* InstigatedBy, AActor* DamageCauser)
- {
- if (Health <= 0.0f && !IsDead)
- {
- // Dead
- IsDead = true;
- GetMovementComponent()->StopMovementImmediately();
- GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
- DetachFromControllerPendingDestroy();
- SetLifeSpan(10.0f);
- }
- }
- // Called every frame
- void ATPSCharacter::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- float TargetFOV = isPlayerAiming ? ZoomedFOV : DefaultFOV;
- float NewFOV = FMath::FInterpTo(CameraComponent->FieldOfView, TargetFOV, DeltaTime, ZoomInterpSpeed);
- CameraComponent->SetFieldOfView(NewFOV);
- }
- // Called to bind functionality to input
- void ATPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- PlayerInputComponent->BindAxis("MoveForward", this, &ATPSCharacter::MoveForward);
- PlayerInputComponent->BindAxis("MoveRight", this, &ATPSCharacter::MoveRight);
- PlayerInputComponent->BindAxis("LookUp", this, &ATPSCharacter::AddControllerPitchInput);
- PlayerInputComponent->BindAxis("LookRight", this, &ATPSCharacter::AddControllerYawInput);
- PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ATPSCharacter::BeginCrouch);
- PlayerInputComponent->BindAction("Crouch", IE_Released, this, &ATPSCharacter::EndCrouch);
- PlayerInputComponent->BindAction("Aiming", IE_Pressed, this, &ATPSCharacter::StartAiming);
- PlayerInputComponent->BindAction("Aiming", IE_Released, this, &ATPSCharacter::StopAiming);
- PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ATPSCharacter::StartFire);
- PlayerInputComponent->BindAction("Fire", IE_Released, this, &ATPSCharacter::StopFire);
- }
- FVector ATPSCharacter::GetPawnViewLocation() const
- {
- // Overrides the engine GetPawnViewLocation method
- if (CameraComponent)
- {
- // Gets the location of the camera component
- return CameraComponent->GetComponentLocation();
- }
- return Super::GetPawnViewLocation();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement