Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "gesture3.h"
  4. #include "GestureActor.h"
  5.  
  6.  
  7. // Sets default values
  8. AGestureActor::AGestureActor()
  9. {
  10. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  11. PrimaryActorTick.bCanEverTick = true;
  12.  
  13. }
  14.  
  15. FVector2D AGestureActor::GetMousePosition() {
  16.  
  17. APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
  18. if (PlayerController != nullptr)
  19. {
  20. // Get the coordinates of the mouse from our controller
  21. float LocationX;
  22. float LocationY;
  23. PlayerController->GetMousePosition(LocationX, LocationY);
  24. // Do a trace and see if there the position intersects something in the world
  25. FVector2D MousePosition(LocationX, LocationY);
  26. return MousePosition;
  27. }
  28. return FVector2D(0, 0);
  29. }
  30.  
  31.  
  32. void AGestureActor::SaveGestures()
  33. {
  34. GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("SAVING"));
  35. UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
  36. SaveGameInstance->MemorisedGestures = MemorisedGestures;
  37. UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveGameInstance->SaveSlotName, SaveGameInstance->UserIndex);
  38. }
  39.  
  40.  
  41. void AGestureActor::LoadGestures()
  42. {
  43. UMySaveGame* LoadGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
  44. if (UGameplayStatics::DoesSaveGameExist(LoadGameInstance->SaveSlotName, LoadGameInstance->UserIndex))
  45. {
  46. GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("LOADING?"));
  47. LoadGameInstance = Cast<UMySaveGame>(UGameplayStatics::LoadGameFromSlot(LoadGameInstance->SaveSlotName, LoadGameInstance->UserIndex));
  48. MemorisedGestures = LoadGameInstance->MemorisedGestures;
  49. //for (auto& It : LoadGameInstance->MemorisedGestures)
  50. //{
  51. // GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::White, FString::Printf(TEXT(" IT LEN %d"), It.Gestures.Num()));
  52. //}
  53. }
  54.  
  55.  
  56. }
  57.  
  58. float AGestureActor::GetTime() {
  59. float Partial,Out;
  60. int32 Seconds;
  61. int32 Time = 0;
  62. UGameplayStatics::GetAccurateRealTime(GetWorld(), Seconds, Partial);
  63. Time += Seconds;
  64. Time %= 86400;
  65. Out = Time+Partial;
  66. return Out;
  67. }
  68. // Called when the game starts or when spawned
  69. void AGestureActor::BeginPlay()
  70. {
  71. Super::BeginPlay();
  72.  
  73. UWorld* const World = GetWorld();
  74. if (World != NULL && EmitterTemplate != NULL) {
  75. //static ConstructorHelpers::FObjectFinder<UParticleSystem> eT(TEXT("ParticleSystem'/Game/StarterContent/Particles/Steam_2.Steam_2'"));
  76. //emitterTemplate = eT.Object;
  77. MouseTrail = UGameplayStatics::SpawnEmitterAtLocation(World,EmitterTemplate,FVector(0,0,0),FRotator(0,0,0),false);
  78. GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("HURRDURR BEGINPLAY?"));
  79. }
  80.  
  81. this->LoadGestures();
  82.  
  83. }
  84.  
  85. FVector AGestureActor::GetMouseWorldPosition() {
  86. FVector MousePos;
  87. FVector MouseDir;
  88. FVector NewPos;
  89. APlayerController * playerController = (APlayerController*)GetWorld()->GetFirstPlayerController();
  90. playerController->DeprojectMousePositionToWorld(MousePos, MouseDir);
  91. // GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, FString::Printf(TEXT(" float %f float %f float %f"), mousePos.X, mousePos.Y, mousePos.Z));
  92. int zOffset = 500;
  93. NewPos = UKismetMathLibrary::GetForwardVector(MouseDir.Rotation())*zOffset + MousePos;
  94. return NewPos;
  95. }
  96.  
  97. // Called every frame
  98. void AGestureActor::Tick( float DeltaTime )
  99. {
  100. Super::Tick( DeltaTime );
  101.  
  102.  
  103. if (MouseTrail != NULL) {
  104. FVector NewPos = this->GetMouseWorldPosition();
  105.  
  106. //newPos = FRotationMatrix(mouseDir.Rotation()).GetScaledAxis(EAxis::X)*zOffset + mousePos;
  107. MouseTrail->SetWorldLocation(NewPos, false);
  108. float Time = this->GetTime();
  109. FVector2D MP = this->GetMousePosition();
  110. if (LearnGesture == true) { LearnLastFrame = true; }
  111. if (LearnGesture == true && (Buf.Gestures.Num() == 0 || FVector2D::Distance(Buf.Gestures.Last(), MP) >= MinLearnDist)) {
  112. Buf.Gestures.Push(MP);
  113. GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("Learn!"));
  114. }
  115. if (Trail.Num() == 0 || FVector2D::Distance(MP,FVector2D(Trail.Last().X,Trail.Last().Y)) >= MinDist) {
  116.  
  117. //GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("ADDING"));
  118. Trail.Push(FVector(MP.X,MP.Y, Time));
  119. TrailWorld.Push(NewPos);
  120. while (Trail.Num() != 0 && FMath::Abs(Trail[0].Z - Time) >= MaxTimeDiff) {
  121. Trail.RemoveAt(0, 1);
  122. TrailWorld.RemoveAt(0, 1);
  123. }
  124. }
  125.  
  126.  
  127.  
  128. if (LearnGesture == false) {
  129. if (LearnLastFrame == true && Buf.Gestures.Num() != 0) {
  130. LearnLastFrame = false;
  131. MemorisedGestures.Push(Buf);
  132. Buf.Gestures.Empty();
  133. Trail.Empty();
  134. TrailWorld.Empty();
  135. this->SaveGestures();
  136. }
  137.  
  138.  
  139. for (int32 GestureIndex = 0; GestureIndex != MemorisedGestures.Num(); ++GestureIndex)
  140. {
  141. int32 Matched = 0;
  142. int32 Len = MemorisedGestures[GestureIndex].Gestures.Num();
  143. if (Len > Trail.Num()) { continue; }
  144. for (int32 Index = 0; Index != Len; ++Index)
  145. {
  146. if (FVector2D::Distance(MemorisedGestures[GestureIndex].Gestures[Len - Index - 1], FVector2D(Trail[Trail.Num() - 1 - Index].X, Trail[Trail.Num() - 1 - Index].Y)) <= MatchDist) {
  147. Matched++;
  148. }
  149. }
  150. float Percent = (float)(Matched * 100) / (Len+0.01);
  151. //GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::White, FString::Printf(TEXT(" MATCH? %f"), Percent));
  152.  
  153. if (Percent >= PercentMatch) {
  154. Explode = true;
  155. GestureId = GestureIndex;
  156. GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, FString::Printf(TEXT("GESTURE ID %d"), GestureId));
  157. break;
  158. }
  159. }
  160. }
  161.  
  162.  
  163. if (Explode == true) {
  164. Explode = false;
  165. for (int32 Index = 0; Index != TrailWorld.Num(); ++Index)
  166. {
  167. if (Index >= (TrailWorld.Num() - MemorisedGestures[GestureId].Gestures.Num())) {
  168. UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ActivatedEffect, TrailWorld[Index], FRotator(0, 0, 0), true);
  169. }
  170. }
  171.  
  172. TrailWorld.Empty();
  173. Trail.Empty();
  174. }
  175. // GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, FString::Printf(TEXT("float: %f"), time));
  176. }
  177.  
  178.  
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement