Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "SCharacter.h"
- #include "Components/CapsuleComponent.h"
- #include "Camera/CameraComponent.h"
- #include "GameFramework/SpringArmComponent.h"
- #include "GameFramework/PawnMovementComponent.h"
- #include "Engine/SkeletalMesh.h"
- #include "Net/UnrealNetwork.h"
- #include "GameFramework/CharacterMovementComponent.h"
- #include "SWeaponBase.h"
- //Done
- // Sets default values
- ASCharacter::ASCharacter()
- {
- // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- //Setting variables for the spring arm component
- SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
- SpringArmComp->bUsePawnControlRotation = true;
- SpringArmComp->SetupAttachment(RootComponent);
- //Setting the variables for the FPS hands mesh
- MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
- MeshComp->CastShadow = false;
- MeshComp->AttachToComponent(SpringArmComp, FAttachmentTransformRules::KeepRelativeTransform);
- //Setup variables for the camera
- CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
- CameraComp->SetupAttachment(MeshComp, "CameraSocket");
- //Is not aiming on beginplay
- IsAiming = false;
- //Can aim on beginplay
- CanAim = true;
- //Is not crouching on beginplay
- IsCrouching = false;
- //Crouch speed
- CrouchSpeed = 10.0f;
- //Crouch height
- CrouchedCapsuleHalfHeight = 44.0f;
- //Setting the default capsule height
- DefaultCapsuleHalfHeight = GetCapsuleComponent()->GetScaledCapsuleHalfHeight();
- //Setting default values for Sprint, walk and crouch speeds
- SprintSpeed = 800.0f;
- WalkSpeed = 600.0f;
- CrouchedSpeed = 150.f;
- //Allows the player to swap weapons
- CanWeaponSwap = true;
- }
- //Done
- void ASCharacter::PlayFireMontage()
- {
- //Selects a random numberr, either 1 or 2
- int Value = FMath::RandRange(1, 2);
- //Checks to see if the player is aiming
- if (IsAiming)
- {
- //Checking between the 'Value' value, and playing the respective animation.
- if (Value == 1)
- {
- MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->ADSFireMontage1, 1.0f);
- }
- if (Value == 2)
- {
- MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->ADSFireMontage2, 1.0f);
- }
- }
- else
- {
- //Checking between the 'Value' value, and playing the respective animation.
- if (Value == 1)
- {
- MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->FireMontage1, 1.0f);
- }
- if (Value == 2)
- {
- MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->FireMontage2, 1.0f);
- }
- }
- //Adds the recoil for the pitch
- AddControllerPitchInput(FMath::RandRange(CurrentWeapon->PitchMin, CurrentWeapon->PitchMax));
- //Adds the recoil for the yaw
- AddControllerYawInput(FMath::RandRange(CurrentWeapon->YawMin, CurrentWeapon->YawMax));
- }
- //Done
- // Called when the game starts or when spawned
- void ASCharacter::BeginPlay()
- {
- Super::BeginPlay();
- //Defining the spawn parameters for the weapon
- FActorSpawnParameters SpawnParams;
- SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
- //Spawning the weapon
- CurrentWeapon = GetWorld()->SpawnActor<ASWeaponBase>(BasePrimaryWeaponClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
- if (CurrentWeapon)
- {
- //Attaching the weapon to it's socket
- CurrentWeapon->SetOwner(this);
- CurrentWeapon->AttachToComponent(MeshComp, FAttachmentTransformRules::SnapToTargetNotIncludingScale, CurrentWeapon->WeaponAttachmentSocketName);
- WeaponSelectedInt = CurrentWeapon->WeaponSwapInt;
- }
- MP40_AmmoArray.Init(32, 8);
- P08_AmmoArray.Init(8, 8);
- K98s_Ammo = 5;
- K98s_TotalAmmo = 25;
- }
- //Done
- void ASCharacter::MoveForward(float Value)
- {
- //Moving forward/back
- AddMovementInput(GetActorForwardVector() * Value);
- }
- //Done
- void ASCharacter::MoveRight(float Value)
- {
- //Moving left/right
- AddMovementInput(GetActorRightVector() * Value);
- }
- //Done
- void ASCharacter::LookUp(float Value)
- {
- //Adds vertical controller input
- AddControllerPitchInput(Value);
- }
- //Done
- void ASCharacter::LookRight(float Value)
- {
- //Adds horizontal controller input
- AddControllerYawInput(Value);
- }
- //Done
- //Handles executing the crouch function
- void ASCharacter::ExecCrouch()
- {
- //Checks to see if the player is already crouching
- if (!IsCrouching)
- {
- //If the player is not crouching, checks to see if the player is sprinting
- if (IsSprinting)
- {
- //If the player is sprinting, disables sprinting
- IsSprinting = false;
- }
- //Toggles on crouch
- IsCrouching = true;
- //Sets the max movement speed to crouch
- GetCharacterMovement()->MaxWalkSpeed = CrouchedSpeed;
- //Allows the player to aim
- CanAim = true;
- }
- else
- {
- //Since the player is already crouching, toggles off crouch
- IsCrouching = false;
- //Sets the movement speed to walk
- GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
- //Allows the player to aim
- CanAim = true;
- }
- }
- //Done
- void ASCharacter::StartADS()
- {
- //Sets wants to aim to true for use in the tick function
- WantsToAim = true;
- }
- //Done
- void ASCharacter::StopADS()
- {
- //Sets wants to aim for false for use in the tick function
- WantsToAim = false;
- }
- //Done
- void ASCharacter::Sprint()
- {
- //Checks to see if the character is crouching
- if (IsCrouching == true)
- {
- //If is crouching, sets is crouching to false
- IsCrouching = false;
- }
- //Toggles on sprint
- IsSprinting = true;
- //Sets the movement speed to sprint
- GetCharacterMovement()->MaxWalkSpeed = SprintSpeed;
- //Does not allow the player to aim
- CanAim = false;
- }
- //Done
- void ASCharacter::StopSprint()
- {
- //Checks to see if the character has crouched
- if (IsCrouching == true)
- {
- //If it is true, nothing happens as all the transitions are handled within the crouch function
- }
- else
- {
- //If the character has just stopped springing, changes variables to fit walk
- //Sets is sprinting to false
- IsSprinting = false;
- //Sets the movement speed to walk
- GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
- //Allows the player to aim
- CanAim = true;
- }
- }
- //Done
- void ASCharacter::LeanLeft()
- {
- //Checks if the player is already leaning left
- if (!IsLeaningLeft)
- {
- //Sets the lean amount
- LeanAmount = 22.5f;
- //Sets that the player is leaning left
- IsLeaningLeft = true;
- //Sets that the player is not leaning right
- IsLeaningRight = false;
- //Sets the crouched speed of the player
- GetCharacterMovement()->MaxWalkSpeed = CrouchedSpeed;
- //Casts to BP to finish off the lean function
- BPLeanLeft();
- }
- //Executes if the player is already leaning left
- else
- {
- //Sets the lean amount to 0 (no lean)
- LeanAmount = 0.0f;
- //Sets that the player is not leaning left
- IsLeaningLeft = false;
- //Sets that the player is not leaning right
- IsLeaningRight = false;
- //Casts to BP to reset the lean
- BPResetLean();
- //Checks if the player is crouching
- if (IsCrouching)
- {
- //If the player is crouching, nothing changes (Crouch speed is the same as lean speed)
- }
- else
- {
- //If the player is not crouching, the speed is set to walk speed
- GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
- }
- }
- }
- //Done
- void ASCharacter::LeanRight()
- {
- //Checks if the player is already leaning right
- if (!IsLeaningRight)
- {
- //Sets the lean amount
- LeanAmount = -22.5f;
- //Sets that the player is not leaning left
- IsLeaningLeft = false;
- //Sets that the player is leaning right
- IsLeaningRight = true;
- //Sets the crouched speed of the player
- GetCharacterMovement()->MaxWalkSpeed = CrouchedSpeed;
- //Casts to BP to finish off the lean function
- BPLeanRight();
- }
- //Executes if the player is already leaning right
- else
- {
- //Sets the lean amount to 0 (no lean)
- LeanAmount = 0.0f;
- //Sets that the player is not leaning left
- IsLeaningLeft = false;
- //Sets that the player is not leaning right
- IsLeaningRight = false;
- //Casts to BP to reset the lean
- BPResetLean();
- //Checks if the player is crouching
- if (IsCrouching)
- {
- //If the player is crouching, nothing changes (Crouch speed is the same as lean speed)
- }
- else
- {
- //If the player is not crouching, the speed is set to walk speed
- GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
- }
- }
- }
- //Done
- void ASCharacter::StartFire()
- {
- //Starts fire on the current weapon
- CurrentWeapon->StartFire();
- }
- //Done
- void ASCharacter::StopFire()
- {
- //Stops fire on the current weapon
- CurrentWeapon->StopFire();
- }
- //Done
- void ASCharacter::Reload()
- {
- //Checks to see if the weapon is already reloading
- if (!CurrentWeapon->IsReloading)
- {
- //Executes the reload function of the currently equipped weapon
- CurrentWeapon->Reload();
- //Checks if the reloading montage is set
- if (CurrentWeapon->ArmsReloadingMontage && CurrentWeapon->EmptyArmsReloadingMontage)
- {
- //Determines whether the weapon is using clip reloading or not
- if (CurrentWeapon->ClipReloading)
- {
- //Checks if the weapon needs to be reloaded, or is already fully reloaded
- if (CurrentWeapon->AmmoTestArray != CurrentWeapon->AmmoArray)
- {
- //Checks to see if the current magazine is empty (and thus, the bolt needs to be pulled back to chamber a fresh round)
- if (CurrentWeapon->AmmoArray[0] <= 0)
- {
- //Plays the empty reloading animation
- AnimTime = MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->EmptyArmsReloadingMontage, 1.0f);
- }
- else
- {
- //Plays the normal reloading animation
- AnimTime = MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->ArmsReloadingMontage, 1.0f);
- }
- //Disables the player's ability to swap weapons
- CanWeaponSwap = false;
- }
- }
- else
- {
- //Checks if the weapon needs to be reloaded, or is already fully reloaded
- if (CurrentWeapon->CurrentAmmo != CurrentWeapon->StrClipSize && CurrentWeapon->TotalAmmo > 0)
- {
- if (CurrentWeapon->CurrentAmmo <= 0)
- {
- //Plays the empty reloading animation
- AnimTime = MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->EmptyArmsReloadingMontage, 1.0f);
- }
- else
- {
- //Plays the reloading animation
- AnimTime = MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->ArmsReloadingMontage, 1.0f);
- }
- //Disables the player's ability to swap weapons
- CanWeaponSwap = false;
- }
- }
- //allows the player to swap weapons once they've finished reloading
- GetWorldTimerManager().SetTimer(TimerHandle_ReloadAnimTime, this, &ASCharacter::AllowWeaponSwap, AnimTime, false, AnimTime);
- }
- }
- }
- void ASCharacter::AllowWeaponSwap()
- {
- CanWeaponSwap = true;
- }
- void ASCharacter::SetPrimaryWeapon()
- {
- if (CanWeaponSwap)
- {
- //Defining the spawn parameters for the weapon
- FActorSpawnParameters SpawnParams;
- SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
- CurrentWeapon->UpdateCharacterAmmo();
- CurrentWeapon->K2_DestroyActor();
- CurrentWeapon = GetWorld()->SpawnActor<ASWeaponBase>(BasePrimaryWeaponClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
- if (CurrentWeapon)
- {
- //Attaching the weapon to it's socket
- CurrentWeapon->SetOwner(this);
- CurrentWeapon->AttachToComponent(MeshComp, FAttachmentTransformRules::SnapToTargetNotIncludingScale, CurrentWeapon->WeaponAttachmentSocketName);
- }
- WeaponSelectedInt = CurrentWeapon->WeaponSwapInt;
- CurrentWeapon->MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->EquipMontage, 1.0f);
- CurrentWeapon->UpdateWeaponAmmo();
- }
- }
- void ASCharacter::SetSecondaryWeapon()
- {
- if (CanWeaponSwap)
- {
- //Defining the spawn parameters for the weapon
- FActorSpawnParameters SpawnParams;
- SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
- CurrentWeapon->UpdateCharacterAmmo();
- CurrentWeapon->MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->UnequipMontage, 1.0f);
- CurrentWeapon->K2_DestroyActor();
- CurrentWeapon = GetWorld()->SpawnActor<ASWeaponBase>(BaseSecondaryWeaponClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
- if (CurrentWeapon)
- {
- //Attaching the weapon to it's socket
- CurrentWeapon->SetOwner(this);
- CurrentWeapon->AttachToComponent(MeshComp, FAttachmentTransformRules::SnapToTargetNotIncludingScale, CurrentWeapon->WeaponAttachmentSocketName);
- }
- WeaponSelectedInt = CurrentWeapon->WeaponSwapInt;
- CurrentWeapon->MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->EquipMontage, 1.0f);
- CurrentWeapon->UpdateWeaponAmmo();
- }
- }
- //Done
- // Called every frame
- void ASCharacter::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- //Crouching
- //Sets the new Target Half Height based on whether the player is crouching or uncrouching
- float TargetHalfHeight = IsCrouching? CrouchedCapsuleHalfHeight : DefaultCapsuleHalfHeight;
- //Interpolates between the current height and the target height
- float NewHalfHeight = FMath::FInterpTo(GetCapsuleComponent()->GetScaledCapsuleHalfHeight(), TargetHalfHeight, DeltaTime, CrouchSpeed);
- //Sets the half height of the capsule component to the new interpolated half height
- GetCapsuleComponent()->SetCapsuleHalfHeight(NewHalfHeight);
- float TargetLeanAmountLeft = IsLeaningLeft ? 0.0f : LeanAmount;
- float NewLeanAmountLeft = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountLeft, DeltaTime, 8.0f);
- ViewRotation.Pitch = NewLeanAmountLeft;
- float TargetLeanAmountRight = IsLeaningRight ? 0.0f : LeanAmount;
- float NewLeanAmountRight = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountRight, DeltaTime, 8.0f);
- ViewRotation.Pitch = NewLeanAmountRight;
- //Aiming
- //Checks if the player wants to aim and is able to aim
- if (WantsToAim == true && CanAim == true)
- {
- //If both conditions are met, the player is allowed to aim down sights (IsAiming is set to true)
- IsAiming = true;
- CrouchedCapsuleHalfHeight = 50.0f;
- }
- else
- {
- //If one or neither condition is met, the player cannot aim down sights
- IsAiming = false;
- CrouchedCapsuleHalfHeight = 44.0f;
- }
- }
- //Done
- // Called to bind functionality to input
- void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- //Move forward/back + left/right inputs
- PlayerInputComponent->BindAxis("MoveForward", this, &ASCharacter::MoveForward);
- PlayerInputComponent->BindAxis("MoveRight", this, &ASCharacter::MoveRight);
- //Look up/down + left/right inputs
- PlayerInputComponent->BindAxis("LookUp", this, &ASCharacter::LookUp);
- PlayerInputComponent->BindAxis("Turn", this, &ASCharacter::LookRight);
- //Crouch input
- PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ASCharacter::ExecCrouch);
- //Jump input
- PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ASCharacter::Jump);
- //ADS input (start/stop)
- PlayerInputComponent->BindAction("ADS", IE_Pressed, this, &ASCharacter::StartADS);
- PlayerInputComponent->BindAction("ADS", IE_Released, this, &ASCharacter::StopADS);
- //Leaning input (Left + Right)
- PlayerInputComponent->BindAction("LeanLeft", IE_Pressed, this, &ASCharacter::LeanLeft);
- PlayerInputComponent->BindAction("LeanRight", IE_Pressed, this, &ASCharacter::LeanRight);
- //Sprint input (start/stop)
- PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &ASCharacter::Sprint);
- PlayerInputComponent->BindAction("Sprint", IE_Released, this, &ASCharacter::StopSprint);
- //Fire inout (start/stop)
- PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ASCharacter::StartFire);
- PlayerInputComponent->BindAction("Fire", IE_Released, this, &ASCharacter::StopFire);
- //Reload input
- PlayerInputComponent->BindAction("Reload", IE_Pressed, this, &ASCharacter::Reload);
- //Weapon swapping
- PlayerInputComponent->BindAction("SelectPrimary", IE_Pressed, this, &ASCharacter::SetPrimaryWeapon);
- PlayerInputComponent->BindAction("SelectSecondary", IE_Pressed, this, &ASCharacter::SetSecondaryWeapon);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement