Advertisement
Dirt113

DHCharacter.cpp

Nov 2nd, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.18 KB | None | 0 0
  1. // Copyright 2015 Dirt Productions. All rights reserved.
  2.  
  3. #pragma once
  4.  
  5. #include "DistantHome.h"
  6. #include "Public/Player/DHCharacterMovementComponent.h"
  7. #include "Public/Player/DHCharacter.h"
  8. #include "Public/Gameplay/Utility/DHUtility.h"
  9.  
  10.  
  11. // Sets default values
  12. ADHCharacter::ADHCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer.SetDefaultSubobjectClass<UDHCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
  13. {
  14.     // Set player HP to defaults
  15.     PlayerData.HP.MaxHitPoints, PlayerData.HP.CurrentHitPoints = 100.0f;
  16.  
  17.     // Create first person camera
  18.     FirstPersonCamera = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("First Person Camera"));
  19.     FirstPersonCamera->AttachParent = GetCapsuleComponent();
  20.     // Position the camera above BaseEyeHeight
  21.     FirstPersonCamera->RelativeLocation = FVector(0.0f, 0.0f, 64.0f + BaseEyeHeight);
  22.     // Allow pawn to control rotation
  23.     FirstPersonCamera->bUsePawnControlRotation = true;
  24.  
  25.     // Create orbit view camera and attach to sprint arm
  26.     OrbitViewCamera = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("Orbit View Camera"));
  27.     OrbitSpringArm = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("Orbit Spring Arm"));
  28.     OrbitViewCamera->AttachParent = OrbitSpringArm;
  29.     OrbitSpringArm->AttachParent = GetCapsuleComponent();
  30.     // Position camera
  31.     OrbitSpringArm->TargetArmLength = 300.0f;
  32.     // Allow pawn to control rotation
  33.     OrbitSpringArm->bUsePawnControlRotation = true;
  34.  
  35.     // Update camera view
  36.     UpdateCameraView();
  37.  
  38.     // Set this character to call Tick() every frame
  39.     PrimaryActorTick.bCanEverTick = true;
  40.  
  41.     // Allow pawn to control rotation
  42.     GetCharacterMovement()->bUseControllerDesiredRotation = true;
  43.  
  44.     // Set rate of view (view speed) of character
  45.     ViewRate = 45.0f;
  46.  
  47.     // Set movement speeds
  48.     WalkingSpeed = 500.0f;
  49.     AdditiveSprintingSpeed = 400.0f;
  50. }
  51.  
  52. // Called when the game starts or when spawned
  53. void ADHCharacter::BeginPlay()
  54. {
  55.     Super::BeginPlay();
  56.  
  57.     bSprintOnNextTick = false;
  58.  
  59.     if (GEngine)
  60.     {
  61.         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("We are using DHCharacter!"));
  62.     }
  63.  
  64.     // Spawn weapons
  65.     /*
  66.     MakeWeapon(PlayerData.Loadout.PrimaryWeapon, EWeaponSlot::PrimaryWeapon);
  67.     MakeWeapon(PlayerData.Loadout.SecondaryWeapon, EWeaponSlot::SecondaryWeapon);
  68.     MakeWeapon(PlayerData.Loadout.MeleeWeapon, EWeaponSlot::MeleeWeapon);
  69.     MakeWeapon(PlayerData.Loadout.SpecialWeapon, EWeaponSlot::SpecialWeapon);
  70.     */
  71. }
  72.  
  73. // Called every frame
  74. void ADHCharacter::Tick(float DeltaTime)
  75. {
  76.     Super::Tick(DeltaTime);
  77.  
  78.     // Update is airborne
  79.     if (GetCharacterMovement()->IsFalling()) bIsAirborne = true;
  80.     // Update is moving
  81.     if (GetCharacterMovement()->IsMovingOnGround() || GetCharacterMovement()->IsFalling() || bIsAirborne) bIsMoving = true;
  82.     // Call OnStartSprinting() if bSprintOnNextTick == true
  83.     if (bSprintOnNextTick)
  84.     {
  85.         OnStartSprinting();
  86.         bSprintOnNextTick = false;
  87.     }
  88. }
  89.  
  90. // Called to bind functionality to input
  91. void ADHCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  92. {
  93.     Super::SetupPlayerInputComponent(InputComponent);
  94.  
  95.     // Bind axes
  96.     InputComponent->BindAxis("MoveForward", this, &ADHCharacter::MoveForward);
  97.     InputComponent->BindAxis("MoveRight", this, &ADHCharacter::MoveRight);
  98.  
  99.     InputComponent->BindAxis("Turn", this, &ADHCharacter::Turn);
  100.     InputComponent->BindAxis("LookUp", this, &ADHCharacter::LookUp);
  101.     InputComponent->BindAxis("TurnRate", this, &ADHCharacter::TurnAtRate);
  102.     InputComponent->BindAxis("LookUpRate", this, &ADHCharacter::LookUpAtRate);
  103.  
  104.     InputComponent->BindAxis("SwitchWeapon", this, &ADHCharacter::OnSwitchWeaponAxis);
  105.  
  106.     // Bind actions
  107.     InputComponent->BindAction("MoveBackward", IE_Released, this, &ADHCharacter::OnStopMovingBackward);
  108.  
  109.     InputComponent->BindAction("PrimaryWeapon", IE_Pressed, this, &ADHCharacter::OnSwitchToPrimary);
  110.     InputComponent->BindAction("SecondaryWeapon", IE_Pressed, this, &ADHCharacter::OnSwitchToSecondary);
  111.     InputComponent->BindAction("MeleeWeapon", IE_Pressed, this, &ADHCharacter::OnSwitchToMelee);
  112.     InputComponent->BindAction("SpecialWeapon", IE_Pressed, this, &ADHCharacter::OnSwitchToSpecial);
  113.  
  114.     InputComponent->BindAction("Jump", IE_Pressed, this, &ADHCharacter::OnStartJumping);
  115.     InputComponent->BindAction("Sprint", IE_Pressed, this, &ADHCharacter::OnStartSprinting);
  116.     InputComponent->BindAction("Sprint", IE_Released, this, &ADHCharacter::OnStopSprinting);
  117.     InputComponent->BindAction("Crouch", IE_Pressed, this, &ADHCharacter::OnStartCrouching);
  118.     InputComponent->BindAction("Crouch", IE_Released, this, &ADHCharacter::OnStopCrouching);
  119.  
  120.     InputComponent->BindAction("Fire", IE_Pressed, this, &ADHCharacter::OnStartFiring);
  121.     InputComponent->BindAction("Fire", IE_Released, this, &ADHCharacter::OnStopFiring);
  122.     InputComponent->BindAction("Reload", IE_Pressed, this, &ADHCharacter::OnStartReloading);
  123.     InputComponent->BindAction("Reload", IE_Released, this, &ADHCharacter::OnStopReloading);
  124. }
  125.  
  126. // Handles X axis movement
  127. void ADHCharacter::MoveForward(float Value)
  128. {
  129.     if (Controller != NULL && Value != 0.0f)
  130.     {
  131.         bIsMoving = true;
  132.         // Determine forward direction
  133.         FRotator Rotation = Controller->GetControlRotation();
  134.         // Zero out pitch and roll
  135.         Rotation.Pitch, Rotation.Roll = 0.0f;
  136.  
  137.         // Add forward movement
  138.         AddMovementInput(FRotationMatrix(Rotation).GetScaledAxis(EAxis::X), Value);
  139.  
  140.         if (Value > 0.0f) bIsMovingForward = true;
  141.         else if (Value < 0.0f)
  142.         {
  143.             bIsMovingForward = false;
  144.             OnStopSprinting();
  145.         }
  146.         else bIsMovingForward = false;
  147.     }
  148.     else bIsMoving, bIsMovingForward = false;
  149. }
  150.  
  151. // Handles Y axis movement
  152. void ADHCharacter::MoveRight(float Value)
  153. {
  154.     if (Controller != NULL && Value != 0.0f)
  155.     {
  156.         bIsMoving = true;
  157.         // Determine right direction and add right movement
  158.         AddMovementInput(FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y), Value);
  159.     }
  160.     else bIsMoving = false;
  161. }
  162.  
  163. // Handles mouse yaw
  164. void ADHCharacter::Turn(float Value)
  165. {
  166.     if (Value != 0.0f) AddControllerYawInput(Value * GetWorld()->DeltaTimeSeconds * 15.0f);
  167. }
  168.  
  169. // Handles mouse pitch
  170. void ADHCharacter::LookUp(float Value)
  171. {
  172.     if (Value != 0.0f) AddControllerPitchInput(Value * GetWorld()->DeltaTimeSeconds * 15.0f);
  173. }
  174.  
  175. // Handles stick yaw
  176. void ADHCharacter::TurnAtRate(float Value)
  177. {
  178.     if (Value != 0.0f) AddControllerYawInput(Value * GetWorld()->DeltaTimeSeconds * ViewRate);
  179. }
  180.  
  181. // Handles stick pitch
  182. void ADHCharacter::LookUpAtRate(float Value)
  183. {
  184.     if (Value != 0.0f) AddControllerPitchInput(Value * GetWorld()->DeltaTimeSeconds * ViewRate);
  185. }
  186.  
  187. // Called when MoveBackward released
  188. void ADHCharacter::OnStopMovingBackward()
  189. {
  190.     if (bWantsToSprint) bSprintOnNextTick = true;
  191. }
  192.  
  193. // Called when jump pressed
  194. void ADHCharacter::OnStartJumping()
  195. {
  196.     if (bIsCrouching) OnStopCrouching();
  197.     Jump();
  198. }
  199.  
  200. // Called when sprint pressed
  201. void ADHCharacter::OnStartSprinting()
  202. {
  203.     if (!bIsFiring) bIsSprinting = true;
  204. }
  205.  
  206. // Called when sprint released
  207. void ADHCharacter::OnStopSprinting()
  208. {
  209.     bIsSprinting = false;
  210. }
  211.  
  212. // Called when crouch pressed
  213. void ADHCharacter::OnStartCrouching()
  214. {
  215.     Crouch();
  216.     bIsCrouching = true;
  217. }
  218.  
  219. // Called when crouch released
  220. void ADHCharacter::OnStopCrouching()
  221. {
  222.     UnCrouch();
  223.     bIsCrouching = false;
  224. }
  225.  
  226. // Called when aim pressed
  227. void ADHCharacter::OnStartAiming()
  228. {
  229.     if (bIsSprinting) OnStopSprinting();
  230.     bIsAiming = true;
  231. }
  232.  
  233. // Called when aim released
  234. void ADHCharacter::OnStopAiming()
  235. {
  236.     if (bWantsToSprint) bSprintOnNextTick = true;
  237.     bIsAiming = false;
  238. }
  239.  
  240. // Called when fire pressed
  241. void ADHCharacter::OnStartFiring()
  242. {
  243.     // Set bWantsToFire to true
  244.     bWantsToFire = true;
  245.  
  246.     // Determines fire capability
  247.     if (CanFire(false, true))
  248.     {
  249.         // Stop sprinting if sprinitng
  250.         if (bIsSprinting) OnStopSprinting();
  251.  
  252.         // Fire once if already firing
  253.         if (bIsFirstFire) FireWeapon(PlayerData.CurrentWeapon);
  254.  
  255.         // Set bIsFiring to true and set timer for FireCurrentWeapon()
  256.         bIsFiring = true;
  257.        
  258.         GetWorldTimerManager().SetTimer(this, &ADHCharacter::FireCurrentWeapon, GetCurrentFireDelay(), true);
  259.     }
  260.     else return;
  261. }
  262.  
  263. // Called when fire released
  264. void ADHCharacter::OnStopFiring()
  265. {
  266.     // Set bWantsToFire, bIsFiring to false then clear timer for FireCurrentWeapon()
  267.     bWantsToFire = false;
  268.     bIsFiring = false;
  269.     GetWorldTimerManager().ClearTimer(this, &ADHCharacter::FireCurrentWeapon);
  270. }
  271.  
  272. /**
  273.  * Equips weapon from inventory
  274.  * @param WeaponToEquip : Desired EWeaponSlot Value
  275.  */
  276. void ADHCharacter::EquipWeapon(EWeaponSlot WeaponToEquip)
  277. {
  278.     EWeaponSlot PreviousWeapon = PlayerData.CurrentWeapon;
  279.     PlayerData.CurrentWeapon = WeaponToEquip;
  280.     OnWeaponChanged(PreviousWeapon);
  281. }
  282.  
  283. /**
  284.  * Called when weapon changed
  285.  * @param PreviousWeapon : Previous weapon as EWeaponSlot
  286.  */
  287. void ADHCharacter::OnWeaponChanged(EWeaponSlot PreviousWeapon)
  288. {
  289.     if (PreviousWeapon != PlayerData.CurrentWeapon)
  290.     {
  291.         return;
  292.     }
  293.     else return;
  294. }
  295.  
  296. /**
  297.  * Reloads weapon
  298.  * @param WeaponToReload : EWeaponSlot value of desired weapon
  299.  */
  300. void ADHCharacter::ReloadWeapon(EWeaponSlot WeaponToReload)
  301. {
  302.     if (WeaponToReload == PlayerData.CurrentWeapon)
  303.     {
  304.         // Set is sprinting and stop opposing movement
  305.         if (bIsSprinting) OnStopSprinting();
  306.         if (bIsAiming) OnStopAiming();
  307.         bIsReloading = true;
  308.        
  309.         // Set ammo data
  310.         if (WeaponToReload == EWeaponSlot::PrimaryWeapon) PlayerData.WeaponInfo.PrimaryWeapon.Reload();
  311.         else if (WeaponToReload == EWeaponSlot::SecondaryWeapon) PlayerData.WeaponInfo.SecondaryWeapon.Reload();
  312.         else if (WeaponToReload == EWeaponSlot::MeleeWeapon) PlayerData.WeaponInfo.MeleeWeapon.Reload();
  313.         else if (WeaponToReload == EWeaponSlot::SpecialWeapon) PlayerData.WeaponInfo.SpecialWeapon.Reload();
  314.         else return;
  315.  
  316.         // Sprint or aim depending on input
  317.         if (bWantsToSprint) OnStartSprinting();
  318.         if (bWantsToAim) OnStartAiming();
  319.     }
  320.     else return;
  321. }
  322.  
  323. // Called when reload pressed
  324. void ADHCharacter::OnStartReloading()
  325. {
  326.     bWantsToReload = true;
  327.     ReloadWeapon(PlayerData.CurrentWeapon);
  328. }
  329.  
  330. // Called when reload released
  331. void ADHCharacter::OnStopReloading()
  332. {
  333.     bWantsToReload = false;
  334. }
  335.  
  336. // Calculates bullet spread, fires line trace, then spawns projectile to calculated destination
  337. void ADHCharacter::FireWeapon(EWeaponSlot WeaponToFire)
  338. {
  339.     bool bRestoreWeapon = false;
  340.     EWeaponSlot PreviousWeapon = PlayerData.CurrentWeapon;
  341.  
  342.     if (CanFire(false, true))
  343.     {
  344.         if (WeaponToFire == EWeaponSlot::PrimaryWeapon) PlayerData.WeaponInfo.PrimaryWeapon.DecrementAmmo();
  345.         else if (WeaponToFire == EWeaponSlot::SecondaryWeapon) PlayerData.WeaponInfo.SecondaryWeapon.DecrementAmmo();
  346.         else if (WeaponToFire == EWeaponSlot::MeleeWeapon) PlayerData.WeaponInfo.MeleeWeapon.DecrementAmmo();
  347.         else if (WeaponToFire == EWeaponSlot::SpecialWeapon) PlayerData.WeaponInfo.SpecialWeapon.DecrementAmmo();
  348.         else return;
  349.  
  350.         if (WeaponToFire == PlayerData.CurrentWeapon) bRestoreWeapon = false;
  351.         else if (WeaponToFire != PlayerData.CurrentWeapon)
  352.         {
  353.             if (WeaponToFire != EWeaponSlot::NoWeapon)
  354.             {
  355.                 // Store current weapon
  356.                 PreviousWeapon = PlayerData.CurrentWeapon;
  357.  
  358.                 // Equip WeaponToFire
  359.                 EquipWeapon(WeaponToFire);
  360.  
  361.                 // Set to restore weapon
  362.                 bRestoreWeapon = true;
  363.             }
  364.             else return;
  365.         }
  366.  
  367.         // Get bullet spread from current weapon
  368.         float BulletSpread = Cast<ADHWeapon>(GetWeaponInstance(WeaponToFire))->GetBulletSpread();
  369.  
  370.         // Modify bullet spread based on character state
  371.         if (bIsMoving) BulletSpread = BulletSpread * 1.15f;
  372.         if (bIsCrouching) BulletSpread = BulletSpread * 0.95f;
  373.         if (bIsAiming) BulletSpread = BulletSpread * 0.95f;
  374.  
  375.         // Get perfect destination with 0 bullet spread
  376.         FVector Destination = FRotationMatrix(GetControlRotation()).GetScaledAxis(EAxis::X) * 30000.0f;
  377.  
  378.         // Add bullet spread to destination
  379.         float NegatedBulletSpread = BulletSpread * -1.0f;
  380.         Destination.X = Destination.X + FMath::FRandRange(NegatedBulletSpread, BulletSpread);
  381.         Destination.Y = Destination.Y + FMath::FRandRange(NegatedBulletSpread, BulletSpread);
  382.         Destination.Z = Destination.Z + FMath::FRandRange(NegatedBulletSpread, BulletSpread);
  383.  
  384.         if (bRestoreWeapon) EquipWeapon(PreviousWeapon);
  385.     }
  386.     else if (WeaponToFire == PlayerData.CurrentWeapon) ReloadWeapon(WeaponToFire);
  387. }
  388.  
  389. // Fires current weapon
  390. void ADHCharacter::FireCurrentWeapon()
  391. {
  392.     FireWeapon(PlayerData.CurrentWeapon);
  393. }
  394.  
  395. // Gets instance of weapon from EWeaponSlot
  396. ADHWeapon* ADHCharacter::GetWeaponInstance(EWeaponSlot SlotToGet)
  397. {
  398.     if (SlotToGet == EWeaponSlot::PrimaryWeapon)
  399.     {
  400.         return PlayerData.Inventory.PrimaryWeapon;
  401.     }
  402.     else if (SlotToGet == EWeaponSlot::SecondaryWeapon)
  403.     {
  404.         return PlayerData.Inventory.SecondaryWeapon;
  405.     }
  406.     else if (SlotToGet == EWeaponSlot::MeleeWeapon)
  407.     {
  408.         return PlayerData.Inventory.MeleeWeapon;
  409.     }
  410.     else if (SlotToGet == EWeaponSlot::SpecialWeapon)
  411.     {
  412.         return PlayerData.Inventory.SpecialWeapon;
  413.     }
  414.     else return NULL;
  415. }
  416.  
  417. /**
  418.  * Spawns new weapon then adds to inventory
  419.  * @param WeaponToSpawn : Weapon to spawn
  420.  * @param SlotToUse : Slot to add weapon to
  421.  */
  422. void ADHCharacter::MakeWeapon(ADHWeapon* WeaponToSpawn, EWeaponSlot SlotToUse)
  423. {
  424.     // Declare and configure ActorSpawnParams
  425.     FActorSpawnParameters WeaponSpawnParams;
  426.     WeaponSpawnParams.bNoCollisionFail = false;
  427.    
  428.     // Determine weapon type and set WeaponSpawnParams.Template accordingly
  429.     if (Cast<ADHProjectileWeapon>(WeaponToSpawn)) WeaponSpawnParams.Template = Cast<ADHProjectileWeapon>(WeaponToSpawn);
  430.     else return;
  431.  
  432.     // Spawn weapon if character has network authority, otherwise request spawn of weapon
  433.     if (HasAuthority())
  434.     {
  435.         if (SlotToUse == EWeaponSlot::PrimaryWeapon) PlayerData.Inventory.PrimaryWeapon = GetWorld()->SpawnActor<ADHWeapon>(ADHWeapon::StaticClass(), WeaponSpawnParams);
  436.         else if (SlotToUse == EWeaponSlot::SecondaryWeapon) PlayerData.Inventory.SecondaryWeapon = GetWorld()->SpawnActor<ADHWeapon>(ADHWeapon::StaticClass(), WeaponSpawnParams);
  437.         else if (SlotToUse == EWeaponSlot::MeleeWeapon) PlayerData.Inventory.MeleeWeapon = GetWorld()->SpawnActor<ADHWeapon>(ADHWeapon::StaticClass(), WeaponSpawnParams);
  438.         else if (SlotToUse == EWeaponSlot::SpecialWeapon) PlayerData.Inventory.SpecialWeapon = GetWorld()->SpawnActor<ADHWeapon>(ADHWeapon::StaticClass(), WeaponSpawnParams);
  439.         else return;
  440.     }
  441.     else
  442.     {
  443.  
  444.     }
  445.  
  446.     // Sets weapon data of SlotToUse
  447.     if (SlotToUse == EWeaponSlot::PrimaryWeapon)
  448.     {
  449.         const ADHWeapon* Weapon = Cast<ADHWeapon>(GetWeaponInstance(SlotToUse));
  450.         PlayerData.WeaponInfo.PrimaryWeapon = Weapon->WeaponInfo;
  451.     }
  452.     else if (SlotToUse == EWeaponSlot::SecondaryWeapon)
  453.     {
  454.         const ADHWeapon* Weapon = Cast<ADHWeapon>(GetWeaponInstance(SlotToUse));
  455.         PlayerData.WeaponInfo.SecondaryWeapon = Weapon->WeaponInfo;
  456.     }
  457.     else if (SlotToUse == EWeaponSlot::MeleeWeapon)
  458.     {
  459.         const ADHWeapon* Weapon = Cast<ADHWeapon>(GetWeaponInstance(SlotToUse));
  460.         PlayerData.WeaponInfo.MeleeWeapon = Weapon->WeaponInfo;
  461.     }
  462.     else if (SlotToUse == EWeaponSlot::SpecialWeapon)
  463.     {
  464.         const ADHWeapon* Weapon = Cast<ADHWeapon>(GetWeaponInstance(SlotToUse));
  465.         PlayerData.WeaponInfo.SpecialWeapon = Weapon->WeaponInfo;
  466.     }
  467. }
  468.  
  469. /**
  470.  * Gets fire delay of EWeaponSlot value
  471.  * @param WeaponToGet : EWeaponSlot value of desired weapon
  472.  */
  473. float ADHCharacter::GetWeaponFireDelay(EWeaponSlot WeaponToGet)
  474. {
  475.     float ReturnValue;
  476.     if (WeaponToGet == EWeaponSlot::PrimaryWeapon) ReturnValue = PlayerData.WeaponInfo.PrimaryWeapon.FireDelay;
  477.     else if (WeaponToGet == EWeaponSlot::SecondaryWeapon) ReturnValue = PlayerData.WeaponInfo.SecondaryWeapon.FireDelay;
  478.     else if (WeaponToGet == EWeaponSlot::MeleeWeapon) ReturnValue = PlayerData.WeaponInfo.MeleeWeapon.FireDelay;
  479.     else if (WeaponToGet == EWeaponSlot::SpecialWeapon) ReturnValue = PlayerData.WeaponInfo.SpecialWeapon.FireDelay;
  480.     else ReturnValue = NULL;
  481.  
  482.     return ReturnValue;
  483. }
  484.  
  485. // Gets fire delay of current weapon
  486. float ADHCharacter::GetCurrentFireDelay()
  487. {
  488.     return GetWeaponFireDelay(PlayerData.CurrentWeapon);
  489. }
  490.  
  491. // Handles SwitchWeapon axis input
  492. void ADHCharacter::OnSwitchWeaponAxis(float Value)
  493. {
  494.     FDHUtil Util;
  495.     float Modified = Util.Convert.EWeaponSlotToFloat(PlayerData.CurrentWeapon) + Value;
  496.     if (Value != 0.0f && !(Modified > 1.0f || Modified < 4.0f))
  497.     {
  498.         EWeaponSlot NewWeaponSlot = Util.Convert.FloatToEWeaponSlot(Modified);
  499.         if (NewWeaponSlot != PlayerData.CurrentWeapon) EquipWeapon(NewWeaponSlot);
  500.     }
  501. }
  502.  
  503. // Handles PrimaryWeapon switch input
  504. void ADHCharacter::OnSwitchToPrimary()
  505. {
  506.     if (PlayerData.CurrentWeapon != EWeaponSlot::PrimaryWeapon) EquipWeapon(EWeaponSlot::PrimaryWeapon);
  507. }
  508.  
  509. // Handles SecondaryWeapon switch input
  510. void ADHCharacter::OnSwitchToSecondary()
  511. {
  512.     if (PlayerData.CurrentWeapon != EWeaponSlot::SecondaryWeapon) EquipWeapon(EWeaponSlot::SecondaryWeapon);
  513. }
  514.  
  515. // Handles MeleeWeapon switch input
  516. void ADHCharacter::OnSwitchToMelee()
  517. {
  518.     if (PlayerData.CurrentWeapon != EWeaponSlot::MeleeWeapon) EquipWeapon(EWeaponSlot::MeleeWeapon);
  519. }
  520.  
  521. // Handles MeleeWeaponUse input
  522. void ADHCharacter::OnUseMelee()
  523. {
  524.     if (PlayerData.CurrentWeapon == EWeaponSlot::MeleeWeapon) FireCurrentWeapon();
  525.     else FireWeapon(EWeaponSlot::MeleeWeapon);
  526. }
  527.  
  528. // Handles SpecialWeapon switch input
  529. void ADHCharacter::OnSwitchToSpecial()
  530. {
  531.     if (PlayerData.CurrentWeapon != EWeaponSlot::SpecialWeapon) EquipWeapon(EWeaponSlot::SpecialWeapon);
  532. }
  533.  
  534. // Handles SpecialWeaponUse input
  535. void ADHCharacter::OnUseSpecial()
  536. {
  537.     if (PlayerData.CurrentWeapon == EWeaponSlot::SpecialWeapon) FireCurrentWeapon();
  538.     else FireWeapon(EWeaponSlot::SpecialWeapon);
  539. }
  540.  
  541.  
  542. // Update camera view
  543. void ADHCharacter::UpdateCameraView()
  544. {
  545.     const bool bLife = PlayerData.HP.IsAlive();
  546.     FirstPersonCamera->SetActive(bLife, false);
  547.     OrbitViewCamera->SetActive(!bLife, false);
  548. }
  549.  
  550. // Returns firing state
  551. bool ADHCharacter::IsFiring() const
  552. {
  553.     return bIsFiring;
  554. }
  555.  
  556. // Returns sprinting state
  557. bool ADHCharacter::IsSprinting() const
  558. {
  559.     return bIsSprinting;
  560. }
  561.  
  562. // Returns crouching state
  563. bool ADHCharacter::IsCrouching() const
  564. {
  565.     return bIsCrouching;
  566. }
  567.  
  568. // Returns aiming state
  569. bool ADHCharacter::IsAiming() const
  570. {
  571.     return bIsAiming;
  572. }
  573.  
  574. // Returns moving state
  575. bool ADHCharacter::IsMoving() const
  576. {
  577.     return bIsMoving;
  578. }
  579.  
  580. // Returns is alive state
  581. bool ADHCharacter::IsAlive() const
  582. {
  583.     return PlayerData.HP.IsAlive();
  584. }
  585.  
  586. /** Check if character meets the requirements to fire
  587.  * @param bCheckMovement : true to check movement, otherwise false
  588.  * @param bFalseIfFiring : true to account for current fire state, otherwise false
  589.  */
  590. bool ADHCharacter::CanFire(bool bCheckMovement, bool bFalseIfFiring) const
  591. {
  592.     // Return false if check bCheckMovement && bIsSprinting, or bFalseIfFiring && bIsFiring
  593.     if (!(bCheckMovement && bIsSprinting) && !(bFalseIfFiring && bIsFiring))
  594.     {
  595.         // Test each weapon for ammo, then return coresponding value
  596.         if (PlayerData.CurrentWeapon == EWeaponSlot::PrimaryWeapon) return PlayerData.WeaponInfo.PrimaryWeapon.CanFire();
  597.         else if (PlayerData.CurrentWeapon == EWeaponSlot::SecondaryWeapon) return PlayerData.WeaponInfo.SecondaryWeapon.CanFire();
  598.         else if (PlayerData.CurrentWeapon == EWeaponSlot::MeleeWeapon) return PlayerData.WeaponInfo.MeleeWeapon.CanFire();
  599.         else if (PlayerData.CurrentWeapon == EWeaponSlot::SpecialWeapon) return PlayerData.WeaponInfo.SpecialWeapon.CanFire();
  600.         else return false;
  601.     }
  602.     else return false;
  603. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement