-
Notifications
You must be signed in to change notification settings - Fork 0
/
SterlingResortsCharacter.cpp
327 lines (288 loc) · 10.7 KB
/
SterlingResortsCharacter.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "SterlingResorts.h"
#include "SterlingResortsCharacter.h"
#include "SterlingResortsProjectile.h"
#include "Animation/AnimInstance.h"
#include "GameFramework/InputSettings.h"
DEFINE_LOG_CATEGORY_STATIC(LogFPChar, Warning, All);
//////////////////////////////////////////////////////////////////////////
// ASterlingResortsCharacter
ASterlingResortsCharacter::ASterlingResortsCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
// set our turn rates for input
BaseTurnRate = 45.f;
BaseLookUpRate = 45.f;
// Create a CameraComponent
FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
FirstPersonCameraComponent->AttachParent = GetCapsuleComponent();
FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 64.f); // Position the camera
FirstPersonCameraComponent->bUsePawnControlRotation = true;
// Default offset from the character location for projectiles to spawn
GunOffset = FVector(100.0f, 30.0f, 10.0f);
// Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)
Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
Mesh1P->SetOnlyOwnerSee(true); // only the owning player will see this mesh
Mesh1P->AttachParent = FirstPersonCameraComponent;
Mesh1P->RelativeLocation = FVector(0.f, 0.f, -150.f);
Mesh1P->bCastDynamicShadow = false;
Mesh1P->CastShadow = false;
CollectionSphere = CreateAbstractDefaultSubobject<USphereComponent>(TEXT("CollectionSphere"));
CollectionSphere->AttachTo(RootComponent);
CollectionSphere->SetSphereRadius(200.0f);
TargetWayPoint = NULL;
bIsMovingForward = true;
bIsCharacterMoving = false;
// Note: The ProjectileClass and the skeletal mesh/anim blueprints for Mesh1P are set in the
// derived blueprint asset named MyCharacter (to avoid direct content references in C++)
}
//////////////////////////////////////////////////////////////////////////
// Input
void ASterlingResortsCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
// set up gameplay key bindings
check(InputComponent);
InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
//InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &ASterlingResortsCharacter::TouchStarted);
if( EnableTouchscreenMovement(InputComponent) == false )
{
InputComponent->BindAction("Fire", IE_Pressed, this, &ASterlingResortsCharacter::OnFire);
}
InputComponent->BindAxis("MoveForward", this, &ASterlingResortsCharacter::MoveForward);
InputComponent->BindAxis("MoveRight", this, &ASterlingResortsCharacter::MoveRight);
// We have 2 versions of the rotation bindings to handle different kinds of devices differently
// "turn" handles devices that provide an absolute delta, such as a mouse.
// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
InputComponent->BindAxis("TurnRate", this, &ASterlingResortsCharacter::TurnAtRate);
InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
InputComponent->BindAxis("LookUpRate", this, &ASterlingResortsCharacter::LookUpAtRate);
}
//void ASterlingResortsCharacter::OnFire()
//{
// if (TargetWayPoint != NULL)
// {
// UNavigationSystem::SimpleMoveToActor(GetWorld()->GetFirstPlayerController(), TargetWayPoint);
// }
//
// // try and fire a projectile
// if (ProjectileClass != NULL)
// {
// const FRotator SpawnRotation = GetControlRotation();
// // MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position
// const FVector SpawnLocation = GetActorLocation() + SpawnRotation.RotateVector(GunOffset);
//
// UWorld* const World = GetWorld();
// if (World != NULL)
// {
// // spawn the projectile at the muzzle
// World->SpawnActor<ASterlingResortsProjectile>(ProjectileClass, SpawnLocation, SpawnRotation);
// }
// }
//
// // try and play the sound if specified
// if (FireSound != NULL)
// {
// //UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
// }
//
// // try and play a firing animation if specified
// if(FireAnimation != NULL)
// {
// // Get the animation object for the arms mesh
// UAnimInstance* AnimInstance = Mesh1P->GetAnimInstance();
// if(AnimInstance != NULL)
// {
// AnimInstance->Montage_Play(FireAnimation, 1.f);
// }
// }
//
//}
/*
* Hitting fire will basically move the character towards the TargetWaypoint
* if there is one. We are a non-violent lot, we are.
*/
void ASterlingResortsCharacter::OnFire()
{
if (!bIsCharacterMoving)
{
if (TargetWayPoint != NULL)
{
UNavigationSystem::SimpleMoveToActor(GetWorld()->GetFirstPlayerController(), TargetWayPoint);
bIsCharacterMoving = true;
}
}
else {
GetCharacterMovement()->StopActiveMovement();
bIsCharacterMoving = false;
}
}
void ASterlingResortsCharacter::BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
{
if( TouchItem.bIsPressed == true )
{
return;
}
TouchItem.bIsPressed = true;
TouchItem.FingerIndex = FingerIndex;
TouchItem.Location = Location;
TouchItem.bMoved = false;
}
void ASterlingResortsCharacter::EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
{
if (TouchItem.bIsPressed == false)
{
return;
}
if( ( FingerIndex == TouchItem.FingerIndex ) && (TouchItem.bMoved == false) )
{
OnFire();
}
TouchItem.bIsPressed = false;
}
//void ASterlingResortsCharacter::TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location)
//{
// if ((TouchItem.bIsPressed == true) && ( TouchItem.FingerIndex==FingerIndex))
// {
// if (TouchItem.bIsPressed)
// {
// if (GetWorld() != nullptr)
// {
// UGameViewportClient* ViewportClient = GetWorld()->GetGameViewport();
// if (ViewportClient != nullptr)
// {
// FVector MoveDelta = Location - TouchItem.Location;
// FVector2D ScreenSize;
// ViewportClient->GetViewportSize(ScreenSize);
// FVector2D ScaledDelta = FVector2D( MoveDelta.X, MoveDelta.Y) / ScreenSize;
// if (ScaledDelta.X != 0.0f)
// {
// TouchItem.bMoved = true;
// float Value = ScaledDelta.X * BaseTurnRate;
// AddControllerYawInput(Value);
// }
// if (ScaledDelta.Y != 0.0f)
// {
// TouchItem.bMoved = true;
// float Value = ScaledDelta.Y* BaseTurnRate;
// AddControllerPitchInput(Value);
// }
// TouchItem.Location = Location;
// }
// TouchItem.Location = Location;
// }
// }
// }
//}
/*
* Putting my own functionality in the TouchUpdate; this way we can swipe up/down/left/right from the center
* of the screen and move the character in that direction.
*/
void ASterlingResortsCharacter::TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location)
{
if ((TouchItem.bIsPressed == true) && (TouchItem.FingerIndex == FingerIndex))
{
if (TouchItem.bIsPressed)
{
if (GetWorld() != nullptr)
{
UGameViewportClient* ViewportClient = GetWorld()->GetGameViewport();
if (ViewportClient != nullptr)
{
FVector2D ScreenSize;
ViewportClient->GetViewportSize(ScreenSize);
FVector2D ScreenCenter = ScreenSize / 2.0f;
FVector MoveDelta = Location - TouchItem.Location;
FVector2D ScaledDelta = FVector2D(MoveDelta.X, MoveDelta.Y) / ScreenSize;
//FVector2D ScaledDelta = FVector2D(MoveDelta.X - ScreenCenter.X, MoveDelta.Y - ScreenCenter.Y) / ScreenCenter;
if (ScaledDelta.X != 0.0f)
{
TouchItem.bMoved = true;
MoveRight(ScaledDelta.X * BaseTurnRate * -1.0f);
}
if (ScaledDelta.Y != 0.0f)
{
TouchItem.bMoved = true;
MoveForward(ScaledDelta.Y * BaseTurnRate * -1.0f);
}
}
}
}
}
}
void ASterlingResortsCharacter::MoveForward(float Value)
{
if (Value != 0.0f)
{
// add movement in that direction
AddMovementInput(GetActorForwardVector(), Value);
}
}
void ASterlingResortsCharacter::MoveRight(float Value)
{
if (Value != 0.0f)
{
// add movement in that direction
AddMovementInput(GetActorRightVector(), Value);
}
}
void ASterlingResortsCharacter::TurnAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
}
void ASterlingResortsCharacter::LookUpAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
}
bool ASterlingResortsCharacter::EnableTouchscreenMovement(class UInputComponent* InputComponent)
{
bool bResult = false;
if(FPlatformMisc::GetUseVirtualJoysticks() || GetDefault<UInputSettings>()->bUseMouseForTouch )
{
bResult = true;
InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &ASterlingResortsCharacter::BeginTouch);
InputComponent->BindTouch(EInputEvent::IE_Released, this, &ASterlingResortsCharacter::EndTouch);
InputComponent->BindTouch(EInputEvent::IE_Repeat, this, &ASterlingResortsCharacter::TouchUpdate);
}
return bResult;
}
// Called when the game starts or when spawned
void ASterlingResortsCharacter::BeginPlay()
{
Super::BeginPlay();
GetCharacterMovement()->MaxWalkSpeed = 62.5f;
// So the idea here, so far, is to gather all the waypoints in the collection
// radius, find the closest one to our character and set that as the TargetWayPoint
TArray<AActor*> AllWayPoints;
CollectionSphere->GetOverlappingActors(AllWayPoints, AWayPoint::StaticClass());
// For now, lazy bum that I am, I will try and set the TargetWaypoint to the first one from this array
if (AllWayPoints.Num() != 0)
{
TargetWayPoint = Cast<AWayPoint>(AllWayPoints[0]);
}
}
void ASterlingResortsCharacter::SendCharacterTo(AWayPoint* TargetWayPoint)
{
// Stop the Character from moving by default
GetCharacterMovement()->StopActiveMovement();
bIsCharacterMoving = false;
// Set the location of the character to the location of the WayPoint
FVector TargetLocation = TargetWayPoint->GetActorLocation();
TargetLocation.Z += GetCapsuleComponent()->GetUnscaledCapsuleHalfHeight();
SetActorLocation(TargetLocation);
// Now the tricky part, turn the character to look in the forward
// direction of the WayPoint.
/*
FVector twpForwardVector = TargetWayPoint->GetActorForwardVector();
FVector afcForwadVector = GetActorForwardVector();
float DotProd = FVector::DotProduct(twpForwardVector, afcForwadVector);
float Degrees = FMath::RadiansToDegrees(FMath::Acos(DotProd));
FRotator TargetRotation = FRotator(0.0f, Degrees, 0.0f);
SetActorRotation(TargetRotation);
*/
}