-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProceduralBuildingsPlayerController.cpp
104 lines (88 loc) · 3.09 KB
/
ProceduralBuildingsPlayerController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright Epic Games, Inc. All Rights Reserved.
#include "ProceduralBuildingsPlayerController.h"
#include "GameFramework/Pawn.h"
#include "Blueprint/AIBlueprintHelperLibrary.h"
#include "NiagaraSystem.h"
#include "NiagaraFunctionLibrary.h"
#include "ProceduralBuildingsCharacter.h"
#include "Engine/World.h"
AProceduralBuildingsPlayerController::AProceduralBuildingsPlayerController()
{
bShowMouseCursor = true;
DefaultMouseCursor = EMouseCursor::Default;
}
void AProceduralBuildingsPlayerController::PlayerTick(float DeltaTime)
{
Super::PlayerTick(DeltaTime);
if(bInputPressed)
{
FollowTime += DeltaTime;
// Look for the touch location
FVector HitLocation = FVector::ZeroVector;
FHitResult Hit;
if(bIsTouch)
{
GetHitResultUnderFinger(ETouchIndex::Touch1, ECC_Visibility, true, Hit);
}
else
{
GetHitResultUnderCursor(ECC_Visibility, true, Hit);
}
HitLocation = Hit.Location;
// Direct the Pawn towards that location
APawn* const MyPawn = GetPawn();
if(MyPawn)
{
FVector WorldDirection = (HitLocation - MyPawn->GetActorLocation()).GetSafeNormal();
MyPawn->AddMovementInput(WorldDirection, 1.f, false);
}
}
else
{
FollowTime = 0.f;
}
}
void AProceduralBuildingsPlayerController::SetupInputComponent()
{
// set up gameplay key bindings
Super::SetupInputComponent();
InputComponent->BindAction("SetDestination", IE_Pressed, this, &AProceduralBuildingsPlayerController::OnSetDestinationPressed);
InputComponent->BindAction("SetDestination", IE_Released, this, &AProceduralBuildingsPlayerController::OnSetDestinationReleased);
// support touch devices
InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AProceduralBuildingsPlayerController::OnTouchPressed);
InputComponent->BindTouch(EInputEvent::IE_Released, this, &AProceduralBuildingsPlayerController::OnTouchReleased);
}
void AProceduralBuildingsPlayerController::OnSetDestinationPressed()
{
// We flag that the input is being pressed
bInputPressed = true;
// Just in case the character was moving because of a previous short press we stop it
StopMovement();
}
void AProceduralBuildingsPlayerController::OnSetDestinationReleased()
{
// Player is no longer pressing the input
bInputPressed = false;
// If it was a short press
if(FollowTime <= ShortPressThreshold)
{
// We look for the location in the world where the player has pressed the input
FVector HitLocation = FVector::ZeroVector;
FHitResult Hit;
GetHitResultUnderCursor(ECC_Visibility, true, Hit);
HitLocation = Hit.Location;
// We move there and spawn some particles
UAIBlueprintHelperLibrary::SimpleMoveToLocation(this, HitLocation);
UNiagaraFunctionLibrary::SpawnSystemAtLocation(this, FXCursor, HitLocation, FRotator::ZeroRotator, FVector(1.f, 1.f, 1.f), true, true, ENCPoolMethod::None, true);
}
}
void AProceduralBuildingsPlayerController::OnTouchPressed(const ETouchIndex::Type FingerIndex, const FVector Location)
{
bIsTouch = true;
OnSetDestinationPressed();
}
void AProceduralBuildingsPlayerController::OnTouchReleased(const ETouchIndex::Type FingerIndex, const FVector Location)
{
bIsTouch = false;
OnSetDestinationReleased();
}