Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. // HeartRateActorDummy.h
  2.  
  3. #pragma once
  4.  
  5. #include "CoreMinimal.h"
  6. #include "GameFramework/Actor.h"
  7. #include "HeartRateRetriever.h"
  8. #include "HeartRateActorDummy.generated.h"
  9.  
  10. UCLASS()
  11. class ANGEL_GREYBOX_API AHeartRateActorDummy : public AActor
  12. {
  13.     GENERATED_BODY()
  14.    
  15. public:
  16.     // Sets default values for this actor's properties
  17.     AHeartRateActorDummy();
  18.  
  19.     virtual void BeginPlay() override;
  20.     virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);
  21.     virtual void BeginDestroy();
  22.     virtual void Tick(float DeltaTime) override;
  23.     void TimerWrapper();
  24.  
  25.     float ConvertHeartRate();
  26.  
  27.     HeartRateRetriever* HRR;
  28.  
  29.     FTimerHandle MemberTimerHandle;
  30.  
  31.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Game")
  32.         int heartRate;
  33.  
  34.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Game")
  35.         int baseHeartRate;
  36.  
  37.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Game")
  38.         int upperHeartRate;
  39.  
  40.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Game")
  41.         int lowerHeartRate;
  42.  
  43.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Game")
  44.         float GameplayHeartRate;
  45.  
  46.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Game")
  47.         int prevHeartRate;
  48.    
  49. };
  50.  
  51. // HeartRateActorDummy.cpp
  52.  
  53. #include "HeartRateActorDummy.h"
  54. #include "UObject/ConstructorHelpers.h"
  55. #include <EngineGlobals.h>
  56. #include <Runtime/Engine/Classes/Engine/Engine.h>
  57. #include "TimerManager.h"
  58.  
  59. AHeartRateActorDummy::AHeartRateActorDummy()
  60.     : Super()
  61. {
  62.    
  63.     PrimaryActorTick.bStartWithTickEnabled = true;
  64.     PrimaryActorTick.bCanEverTick = true;
  65.  
  66.     baseHeartRate = 60;
  67.     upperHeartRate = 120;
  68.     lowerHeartRate = 60;
  69.  
  70. }
  71.  
  72. void AHeartRateActorDummy::BeginPlay()
  73. {
  74.  
  75.     HRR = nullptr;
  76.     HRR = new HeartRateRetriever();
  77.  
  78.     GetWorldTimerManager().SetTimer(MemberTimerHandle, this, &AHeartRateActorDummy::TimerWrapper, 1.0f, true, 2.0f);
  79.  
  80. }
  81.  
  82. void AHeartRateActorDummy::EndPlay(const EEndPlayReason::Type EndPlayReason)
  83. {
  84.  
  85.     if (HRR)
  86.     {
  87.  
  88.         if (HRR)
  89.         {
  90.  
  91.             HRR->EnsureCompletion();
  92.             delete HRR;
  93.             HRR = nullptr;
  94.  
  95.         }
  96.  
  97.     }
  98.  
  99.     Super::EndPlay(EndPlayReason);
  100.  
  101. }
  102.  
  103. void AHeartRateActorDummy::BeginDestroy()
  104. {
  105.  
  106.     if (HRR)
  107.     {
  108.  
  109.         HRR->EnsureCompletion();
  110.         delete HRR;
  111.         HRR = nullptr;
  112.  
  113.     }
  114.  
  115.     Super::BeginDestroy();
  116.  
  117. }
  118.  
  119. void AHeartRateActorDummy::Tick(float DeltaTime)
  120. {
  121.  
  122.     Super::Tick(DeltaTime);
  123.  
  124. }
  125.  
  126. void AHeartRateActorDummy::TimerWrapper()
  127. {
  128.  
  129.     if (HRR)
  130.     {
  131.  
  132.         heartRate = HRR->GetHeartRate();
  133.  
  134.         // Update the heart rate bounds values if necessary
  135.         if (heartRate > upperHeartRate)
  136.         {
  137.  
  138.             upperHeartRate = heartRate;
  139.  
  140.         }
  141.         else if (heartRate < lowerHeartRate)
  142.         {
  143.  
  144.             lowerHeartRate = heartRate;
  145.  
  146.         }
  147.        
  148.         GameplayHeartRate = ConvertHeartRate();
  149.  
  150.         prevHeartRate = HRR->GetPrevHeartRate();
  151.  
  152.     }
  153.  
  154. }
  155.  
  156. // Function for converting heart rate into useful gameplay variable
  157. float AHeartRateActorDummy::ConvertHeartRate()
  158. {
  159.  
  160.     float tempHR = 0;
  161.  
  162.     tempHR = heartRate - baseHeartRate;
  163.  
  164.     // Make sure the heart rate is a positive value
  165.     if (tempHR < 0)
  166.     {
  167.  
  168.         tempHR = 0;
  169.  
  170.     }
  171.  
  172.     // Convert the heart rate into something useful for gameplay
  173.     // Expect a value between 1 and 4
  174.     tempHR = tempHR / 20;
  175.     tempHR = tempHR + 1;
  176.  
  177.     return tempHR;
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement