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 "gesture3.h"
- #include "GestureActor.h"
- // Sets default values
- AGestureActor::AGestureActor()
- {
- // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- }
- FVector2D AGestureActor::GetMousePosition() {
- APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
- if (PlayerController != nullptr)
- {
- // Get the coordinates of the mouse from our controller
- float LocationX;
- float LocationY;
- PlayerController->GetMousePosition(LocationX, LocationY);
- // Do a trace and see if there the position intersects something in the world
- FVector2D MousePosition(LocationX, LocationY);
- return MousePosition;
- }
- return FVector2D(0, 0);
- }
- void AGestureActor::SaveGestures()
- {
- GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("SAVING"));
- UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
- SaveGameInstance->MemorisedGestures = MemorisedGestures;
- UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveGameInstance->SaveSlotName, SaveGameInstance->UserIndex);
- }
- void AGestureActor::LoadGestures()
- {
- UMySaveGame* LoadGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
- if (UGameplayStatics::DoesSaveGameExist(LoadGameInstance->SaveSlotName, LoadGameInstance->UserIndex))
- {
- GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("LOADING?"));
- LoadGameInstance = Cast<UMySaveGame>(UGameplayStatics::LoadGameFromSlot(LoadGameInstance->SaveSlotName, LoadGameInstance->UserIndex));
- MemorisedGestures = LoadGameInstance->MemorisedGestures;
- //for (auto& It : LoadGameInstance->MemorisedGestures)
- //{
- // GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::White, FString::Printf(TEXT(" IT LEN %d"), It.Gestures.Num()));
- //}
- }
- }
- float AGestureActor::GetTime() {
- float Partial,Out;
- int32 Seconds;
- int32 Time = 0;
- UGameplayStatics::GetAccurateRealTime(GetWorld(), Seconds, Partial);
- Time += Seconds;
- Time %= 86400;
- Out = Time+Partial;
- return Out;
- }
- // Called when the game starts or when spawned
- void AGestureActor::BeginPlay()
- {
- Super::BeginPlay();
- UWorld* const World = GetWorld();
- if (World != NULL && EmitterTemplate != NULL) {
- //static ConstructorHelpers::FObjectFinder<UParticleSystem> eT(TEXT("ParticleSystem'/Game/StarterContent/Particles/Steam_2.Steam_2'"));
- //emitterTemplate = eT.Object;
- MouseTrail = UGameplayStatics::SpawnEmitterAtLocation(World,EmitterTemplate,FVector(0,0,0),FRotator(0,0,0),false);
- GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("HURRDURR BEGINPLAY?"));
- }
- this->LoadGestures();
- }
- FVector AGestureActor::GetMouseWorldPosition() {
- FVector MousePos;
- FVector MouseDir;
- FVector NewPos;
- APlayerController * playerController = (APlayerController*)GetWorld()->GetFirstPlayerController();
- playerController->DeprojectMousePositionToWorld(MousePos, MouseDir);
- // GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, FString::Printf(TEXT(" float %f float %f float %f"), mousePos.X, mousePos.Y, mousePos.Z));
- int zOffset = 500;
- NewPos = UKismetMathLibrary::GetForwardVector(MouseDir.Rotation())*zOffset + MousePos;
- return NewPos;
- }
- // Called every frame
- void AGestureActor::Tick( float DeltaTime )
- {
- Super::Tick( DeltaTime );
- if (MouseTrail != NULL) {
- FVector NewPos = this->GetMouseWorldPosition();
- //newPos = FRotationMatrix(mouseDir.Rotation()).GetScaledAxis(EAxis::X)*zOffset + mousePos;
- MouseTrail->SetWorldLocation(NewPos, false);
- float Time = this->GetTime();
- FVector2D MP = this->GetMousePosition();
- if (LearnGesture == true) { LearnLastFrame = true; }
- if (LearnGesture == true && (Buf.Gestures.Num() == 0 || FVector2D::Distance(Buf.Gestures.Last(), MP) >= MinLearnDist)) {
- Buf.Gestures.Push(MP);
- GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("Learn!"));
- }
- if (Trail.Num() == 0 || FVector2D::Distance(MP,FVector2D(Trail.Last().X,Trail.Last().Y)) >= MinDist) {
- //GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("ADDING"));
- Trail.Push(FVector(MP.X,MP.Y, Time));
- TrailWorld.Push(NewPos);
- while (Trail.Num() != 0 && FMath::Abs(Trail[0].Z - Time) >= MaxTimeDiff) {
- Trail.RemoveAt(0, 1);
- TrailWorld.RemoveAt(0, 1);
- }
- }
- if (LearnGesture == false) {
- if (LearnLastFrame == true && Buf.Gestures.Num() != 0) {
- LearnLastFrame = false;
- MemorisedGestures.Push(Buf);
- Buf.Gestures.Empty();
- Trail.Empty();
- TrailWorld.Empty();
- this->SaveGestures();
- }
- for (int32 GestureIndex = 0; GestureIndex != MemorisedGestures.Num(); ++GestureIndex)
- {
- int32 Matched = 0;
- int32 Len = MemorisedGestures[GestureIndex].Gestures.Num();
- if (Len > Trail.Num()) { continue; }
- for (int32 Index = 0; Index != Len; ++Index)
- {
- if (FVector2D::Distance(MemorisedGestures[GestureIndex].Gestures[Len - Index - 1], FVector2D(Trail[Trail.Num() - 1 - Index].X, Trail[Trail.Num() - 1 - Index].Y)) <= MatchDist) {
- Matched++;
- }
- }
- float Percent = (float)(Matched * 100) / (Len+0.01);
- //GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::White, FString::Printf(TEXT(" MATCH? %f"), Percent));
- if (Percent >= PercentMatch) {
- Explode = true;
- GestureId = GestureIndex;
- GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, FString::Printf(TEXT("GESTURE ID %d"), GestureId));
- break;
- }
- }
- }
- if (Explode == true) {
- Explode = false;
- for (int32 Index = 0; Index != TrailWorld.Num(); ++Index)
- {
- if (Index >= (TrailWorld.Num() - MemorisedGestures[GestureId].Gestures.Num())) {
- UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ActivatedEffect, TrailWorld[Index], FRotator(0, 0, 0), true);
- }
- }
- TrailWorld.Empty();
- Trail.Empty();
- }
- // GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, FString::Printf(TEXT("float: %f"), time));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement