-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyArmouredCar.cpp
332 lines (296 loc) · 14.7 KB
/
MyArmouredCar.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
328
329
330
331
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyArmouredCar.h"
#include "WheeledVehiclePawn.h"
//input
#include <EnhancedInputComponent.h>
#include <EnhancedInputSubsystems.h>
#include "EnhancedInput/Public/EnhancedInputComponent.h"
#include "MyInputConfigData.h"
#include "MyInputConfigData.h"
//components
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "UObject/ConstructorHelpers.h"
#include "ChaosVehicleMovementComponent.h"
#include "MyRewindComponent.h"
#include "Components/TimelineComponent.h"
#include "Blueprint/UserWidget.h" //crosshair
//Audio
#include "Components/AudioComponent.h"
//debug
#include "DrawDebugHelpers.h"
//tools
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
//Projectile to spawn
#include "MyProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
AMyArmouredCar::AMyArmouredCar() {
PrimaryActorTick.bCanEverTick = true;
bReplicates = true;
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));//Creating spring arm
SpringArmComp->SetupAttachment(GetMesh());//Setting up attachment to the mesh
ThirdPersonCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ThirdPersonCamera"));
ThirdPersonCamera->SetupAttachment(SpringArmComp);
RewindComponent = CreateDefaultSubobject<UMyRewindComponent>(TEXT("RewindComponent"));
FireTimelineComponent = CreateDefaultSubobject<UTimelineComponent>(TEXT("FireTimeline"));
TurretAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("TurretRotationAudioComponent"));
EngineAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("EngineAudioComponent"));
//const ConstructorHelpers::FObjectFinder<UCurveFloat> Curve(TEXT("/Script/Engine.CurveFloat'/Game/VehicleAssets/VehicleCurves/MyRecoilCurve.MyRecoilCurve'"));
}
void AMyArmouredCar::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
}
void AMyArmouredCar::BeginPlay()
{
Super::BeginPlay();
FOnTimelineFloat progressFunction;
progressFunction.BindUFunction(this, "setGunRecoil"); // The function EffectProgress gets called
if (RecoilCurve) {
FireTimelineComponent->AddInterpFloat(RecoilCurve, progressFunction, TEXT("RecoilTrack"));
}
else {
UE_LOG(LogTemp, Warning, TEXT("AMyArmouredCar::BeginPlay: RecoilCurve not set"));
}
}
void AMyArmouredCar::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
//Get player controller
APlayerController* PC = Cast<APlayerController>(GetController());
// Get the local player subsystem
UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer());
//Clear out existing mapping, and add our mapping
Subsystem->ClearAllMappings();
Subsystem->AddMappingContext(InputMapping, 0);
//Getting the EnhancedInputComponent
UEnhancedInputComponent* PEI = Cast<UEnhancedInputComponent>(PlayerInputComponent);
//Binding the actions
PEI->BindAction(InputActions->InputSteer, ETriggerEvent::Triggered, this, &AMyArmouredCar::Steer);
PEI->BindAction(InputActions->InputSteer, ETriggerEvent::Completed, this, &AMyArmouredCar::Steer);
PEI->BindAction(InputActions->InputLook, ETriggerEvent::Triggered, this, &AMyArmouredCar::Look);
PEI->BindAction(InputActions->InputBrake, ETriggerEvent::Triggered, this, &AMyArmouredCar::Brake);
PEI->BindAction(InputActions->InputBrake, ETriggerEvent::Completed, this, &AMyArmouredCar::Brake);
PEI->BindAction(InputActions->InputAccelerate, ETriggerEvent::Triggered , this, &AMyArmouredCar::Accelerate);
PEI->BindAction(InputActions->InputAccelerate, ETriggerEvent::Completed, this, &AMyArmouredCar::Accelerate);
PEI->BindAction(InputActions->Rangefind, ETriggerEvent::Triggered, this, &AMyArmouredCar::rewind);
PEI->BindAction(InputActions->InputFire, ETriggerEvent::Triggered, this, &AMyArmouredCar::fire);
}
// Called every frame
void AMyArmouredCar::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//Setting the turret to follow the camera each tick
FVector LookingAt = getLookingAT();
interpTurretRotation(DeltaTime, getTurretRotationFromCamera(LookingAt));
//setGunElevation(getGunEvevationFromCamera(LookingAt));
interpGunElevation(DeltaTime, getGunEvevationFromCamera(LookingAt));
//TurretRotation = ThirdPersonCamera->GetComponentRotation().Yaw - GetMesh()->GetComponentRotation().Yaw;
FString FloatAsString = FString::Printf(TEXT("%f"), TurretRotation);
//UE_LOG(LogTemp, Display, TEXT("%f"), TurretRotation);
myDrawDebugLine(GetMesh()->GetComponentLocation(), ThirdPersonCamera->GetComponentLocation(), FColor::Red);
if (!CanFire) {
float NewPercent = UKismetMathLibrary::FInterpTo_Constant(ReloadPercent, 1, DeltaTime, 1.f/ReloadDelay);
ReloadPercent = NewPercent;
}
}
void AMyArmouredCar::Look(const FInputActionValue& Value){
FVector2D LookValue = Value.Get<FVector2D>();
//UE_LOG(LogTemp, Log, TEXT("LOOK"));
if (Controller != nullptr) {
//UE_LOG(LogTemp, Log, TEXT("Controller"));
AddControllerYawInput(LookValue.X);
AddControllerPitchInput(LookValue.Y);
}
}
void AMyArmouredCar::Accelerate(const FInputActionValue& Value) {
//Accelerate input sets throttle. Allows for linear input binding to trigger
float Magnitude = Value.GetMagnitude();
//UE_LOG(LogTemp, Display, TEXT("ThrottleDetected"));
GetVehicleMovementComponent()->SetThrottleInput(Magnitude);
//vehicle will brake if trying to drive forward while moving backwards, this allows for greater deceleration to change direction.
if (GetVehicleMovementComponent()->GetForwardSpeed() < -0.1) {
GetVehicleMovementComponent()->SetBrakeInput(Magnitude);
}
if (GetVehicleMovementComponent()->GetForwardSpeed() > -0.1) {
GetVehicleMovementComponent()->SetBrakeInput(0);
}
}
void AMyArmouredCar::Steer(const FInputActionValue& Value) {
float Magnitude = Value.GetMagnitude();
GetVehicleMovementComponent()->SetSteeringInput(Magnitude);
}
void AMyArmouredCar::Brake(const FInputActionValue& Value) {
//Setting brake to the brake float value. Allows for linear input binding with xBox controller for example
GetVehicleMovementComponent()->SetBrakeInput(Value.GetMagnitude());
}
void AMyArmouredCar::fire(const FInputActionValue& Value) {
if (!CanFire) {
return;
}
CanFire = false;
ReloadPercent = 0;
//Spawning projectile
FActorSpawnParameters SpawnInfo;
AActor* SpawnedActor = GetWorld()->SpawnActor<AMyProjectile>(ProjectileClass, GetMesh()->GetSocketLocation(FName("BarrelSocket")), GetMesh()->GetSocketRotation(FName("BarrelSocket")), SpawnInfo);
UProjectileMovementComponent* ProjectileMovement = SpawnedActor->FindComponentByClass<UProjectileMovementComponent>();
//ProjectileMovement->AddImpulse(GetVelocity(), TEXT("Root"), true);
FireTimelineComponent->PlayFromStart();
//Delay before setting can fire to true
if (!GetWorld()->GetTimerManager().IsTimerActive(ReloadTimer)) {
GetWorld()->GetTimerManager().SetTimer(ReloadTimer, this, &AMyArmouredCar::setCanFireTrue, ReloadDelay, false, ReloadDelay);
}
if (ReloadDelay >= ReloadCompleteSoundDuration) {
GetWorld()->GetTimerManager().SetTimer(ReloadCompleteSoundTimer, this, &AMyArmouredCar::playReloadCompletSound, ReloadDelay, false, ReloadDelay - ReloadCompleteSoundDuration);
}
UGameplayStatics::PlaySoundAtLocation(GetWorld(), FireSound, GetMesh()->GetBoneLocation("Barrel"));
//Getting direction of gun to add impulse to vehicle
FQuat RotationQuat = GetMesh()->GetBoneQuaternion(FName("Gunshield"));
FVector Impulse = UKismetMathLibrary::Quat_RotateVector(RotationQuat, FVector(RecoilImpulse, 0.f, 0.f));
//recoil effect
GetMesh()->AddImpulse(-Impulse, "Root", false);
//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("FIRE"));
}
void AMyArmouredCar::setCanFireTrue()
{
CanFire = true;
}
void AMyArmouredCar::playReloadCompletSound()
{
UGameplayStatics::PlaySoundAtLocation(GetWorld(), ReloadCompleteSound, GetMesh()->GetBoneLocation("Turret"));
}
void AMyArmouredCar::setGunRecoil(float value) {
GunRecoil = value * MaxGunRecoil;
}
FVector AMyArmouredCar::getLookingAT() {
//Line trace visible geometry from the player camera and return hit location or the end of the linetrace if no geometry is hit.
FHitResult OutHitResult;
FVector Start = ThirdPersonCamera->GetComponentLocation(); //LineTrace from the camera
FVector End = Start + ThirdPersonCamera->GetForwardVector() * MaxAimDistance;
bool HitResult = GetWorld()->LineTraceSingleByChannel(
OutHitResult,
Start,
End,
ECollisionChannel::ECC_Visibility);
if (HitResult) {
myDrawDebugLine(Start, OutHitResult.ImpactPoint, FColor::Green);
return OutHitResult.ImpactPoint;
}
else {
return End;
}
}
FVector AMyArmouredCar::getAimingAT() {
//Line trace starting at the "gunshield" bone along the line of the gun barrel, returns first hit in the visibility channel
FHitResult OutHitResult;
//Start location of the line trace
FVector Start = GetMesh()->GetBoneLocation(FName("Barrel"));
//Getting rotation of the barrel bone to line trace along
FQuat RotationQuat = GetMesh()->GetBoneQuaternion(FName("Barrel"));
//rotating finding the end point of the line trace to determine where the barrel is facing
FVector End = Start + UKismetMathLibrary::Quat_RotateVector(RotationQuat, FVector(MaxAimDistance, 0.f, 0.f));
//line trace
bool HitResult = GetWorld()->LineTraceSingleByChannel(
OutHitResult,
Start,
End,
ECollisionChannel::ECC_Visibility);
if (HitResult) {
myDrawDebugLine(Start, OutHitResult.ImpactPoint, FColor::Red);
return OutHitResult.ImpactPoint;
}
else {
myDrawDebugLine(Start, End, FColor::Red);
return End;
}
}
void AMyArmouredCar::myDrawDebugLine(const FVector& Start, const FVector& End, FColor Colour = FColor::Red) {
if (!DrawDebugs) {
return;
}
DrawDebugLine(
GetWorld(),
Start,
End,
Colour,
false, // Persistent lines
0.1 // Lifetime
);
}
FVector AMyArmouredCar::getRelativeLookingAt(const FVector& LookingAt) {
//Getting the world rotation of the vehicle to rotate the looking at vector so that relative looking at is in vehicle rotation space
FQuat MeshAngle = GetMesh()->GetComponentQuat();
//Taking the world looking at and making it relative to the turret bone by subtracting the vehicle location and rotating by the inverse of the vehicle rotation.
FVector RelativeLookingAt = UKismetMathLibrary::Quat_RotateVector(MeshAngle.Inverse(), LookingAt - GetMesh()->GetBoneLocation(FName("Turret")));
return RelativeLookingAt;
}
float AMyArmouredCar::getTurretRotationFromCamera(const FVector& LookingAt) {
//Takes the looking at vector (location) from the getLookingAt function
//Returns the rotation of the turret bone so that it looks at the same spot as the player camera
//Getting the world rotation of the vehicle to rotate the looking at vector so that it has the same orientation as the vehicle
FQuat MeshAngle = GetMesh()->GetComponentQuat();
//Taking the world "LookingAt" and making it relative to the turret bone by subtracting the vehicle location and rotating by the inverse of the vehicle rotation.
FVector RelativeLookingAt = UKismetMathLibrary::Quat_RotateVector(MeshAngle.Inverse(), LookingAt - GetMesh()->GetBoneLocation(FName("Turret")));
//Calculating the angle between the turret bone and the looking at location in the plane of the vehicle
FRotator Rotation = FRotationMatrix::MakeFromX(RelativeLookingAt).Rotator();
//We only want the Yaw
float rotation = Rotation.Yaw;
return rotation;
}
float AMyArmouredCar::getGunEvevationFromCamera(const FVector& LookingAt) {
//Calculates and returns the rotation of the gunshield bone so that it looks at the same spot as the player camera
//Getting the world rotation of the vehicle to rotate the looking at vector so that it has the same orientation as the vehicle
FQuat MeshAngle = GetMesh()->GetComponentQuat();
//Taking the world looking at and making it relative to the turret bone by subtracting the vehicle location and rotating by the inverse of the vehicle rotation.
FVector RelativeLookingAt = UKismetMathLibrary::Quat_RotateVector(MeshAngle.Inverse(), LookingAt - GetMesh()->GetBoneLocation(FName("Gunshield")));
//float rotation = FMath::Atan(RelativeLookingAt.Y / RelativeLookingAt.X) * 360/(2*3.14159265358979);
FRotator Rotation = FRotationMatrix::MakeFromX(RelativeLookingAt).Rotator();
float elevation = Rotation.Pitch;
return elevation;
}
void AMyArmouredCar::interpGunElevation(float DeltaTime, const float& TargetRotation) {
//linear interpolation from the current gun elevation to a target gun elevation
float NewRotation = FMath::FInterpConstantTo(GunElevation, TargetRotation, DeltaTime, GunElevationRate);
setGunElevation(NewRotation);
}
void AMyArmouredCar::interpTurretRotation(float DeltaTime, const float& TargetRotation) {
//linear interpolation from the current turret rotation to a target rotation with some hacky maths to make it work properly
float LocalTarget;
//The shorter of the two rotations between two angles on a circle is always less than 180 degrees, in this instance, we just interpolate like normal
if (abs(TargetRotation - TurretRotation) <= 180.f) {
LocalTarget = TargetRotation;
}
//If the angle is greater than 180 degrees then we would be taking the longer path, hence we calculate a new local target that takes the shorter path
else {
//Here I am converting the angle to its compliment i.e the angle that gives the same position but going in the opposite direction around the circle. -90 degrees becomes 270 degrees.
LocalTarget = ((TargetRotation < 0) - (TargetRotation > 0)) * (360 - abs(TargetRotation));
//If the target rotation has the same sign as the turret rotation then we set both to their remainder with respect to 360, to keep the numbers in check (we also know that the interp distance is less than 180).
if ((TargetRotation > 0) - (TargetRotation < 0) == (TurretRotation > 0) - (TurretRotation < 0)) {
//Used to keep the numbers in check, without this, the interpolation would break after 1 full rotation
LocalTarget = TargetRotation;
setTurretRotation(remainder(TurretRotation, 360));
}
}
float NewRotation = FMath::FInterpConstantTo(TurretRotation, LocalTarget, DeltaTime, TurretRotationRate);
setTurretRotation(NewRotation);
//Handling turret rotation sound
if (LocalTarget != TurretRotation) {
TurretAudioComponent->SetPaused(false);
}
else {
TurretAudioComponent->SetPaused(true);
}
}
void AMyArmouredCar::setTurretRotation(const float& rotation) {
TurretRotation = rotation;
}
void AMyArmouredCar::setGunElevation(const float& elevation) {
//Sets the value of gun elevation exposed to the animation instance
//Only sets elevation if within max and min elevation default values.
if ((GunElevationLimit > elevation) && (elevation > -GunDepressionLimit)) {
GunElevation = elevation;
}
}
void AMyArmouredCar::rewind() {
RewindComponent->rewind();
}