-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathJetpackCharacter.cpp
More file actions
155 lines (123 loc) · 4.53 KB
/
Copy pathJetpackCharacter.cpp
File metadata and controls
155 lines (123 loc) · 4.53 KB
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
// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.
#include "ShooterGame.h"
AJetpackCharacter::AJetpackCharacter(const class FPostConstructInitializeProperties& PCIP)
:Super(PCIP.SetDefaultSubobjectClass<UJetpackCharacterMovement>(ACharacter::CharacterMovementComponentName))
{
JetpackRecovery =2;
bIsUsingJetpack = false;
bIsJetpackEnabled = true;
WallJumpTraces=20;
WalljumpHorizontalStrenght = 1200;
WalljumpUpwardsStrenght = 1500;
WallJumpTraceDistance=200;
}
//////////////////////////////////////////////////////////////////////////
// Replication
void AJetpackCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
// everyone except local owner: flag change is locally instigated
DOREPLIFETIME_CONDITION(AJetpackCharacter, bIsUsingJetpack, COND_SkipOwner);
}
void AJetpackCharacter::WallJump()
{
AShooterPlayerController* MyPC = Cast<AShooterPlayerController>(Controller);
if (MyPC && MyPC->IsGameInputAllowed() && !CharacterMovement->IsMovingOnGround())
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Walljump"));
// Do a ring of traces
FVector TraceStart = GetActorLocation();
FVector Front = GetActorRotation().Vector();
FVector Side = FVector::CrossProduct(Front,FVector::UpVector);
DrawDebugLine(GetWorld(),TraceStart,TraceStart+Front*WallJumpTraceDistance,FColor::Blue,true,3);
DrawDebugLine(GetWorld(),TraceStart,TraceStart+Side*WallJumpTraceDistance,FColor::Red,true,3);
float MinDistance = 9999999;
FVector HitLocation = FVector::ZeroVector;
FVector HitNormal;
for(int i = 0; i< WallJumpTraces;i++)
{
float traceangle = 360/WallJumpTraces * i; // angle of the trace
FVector TraceDir = Front*FMath::Sin(traceangle) + Side*FMath::Cos(traceangle);
FVector TraceEnd = TraceStart+TraceDir*WallJumpTraceDistance;
DrawDebugLine(GetWorld(),TraceStart,TraceEnd,FColor::Black,true,3);
static FName TraceTag = FName(TEXT("WeaponTrace"));
// Perform trace to retrieve hit info
FCollisionQueryParams TraceParams(TraceTag, true, Instigator);
TraceParams.bTraceAsyncScene = true;
TraceParams.bReturnPhysicalMaterial = true;
FHitResult Hit(ForceInit);
GetWorld()->LineTraceSingle(Hit, TraceStart, TraceEnd, COLLISION_WEAPON, TraceParams);
if(Hit.bBlockingHit)
{
if( (Hit.Location-TraceStart).Size() < MinDistance)
{
HitLocation = Hit.Location;
HitNormal = Hit.Normal;
MinDistance = (Hit.Location-TraceStart).Size();
}
}
}
if(HitLocation != FVector::ZeroVector)
{
DrawDebugSphere(GetWorld(),HitLocation,20,20,FColor::Yellow,true,5);
if(Role < ROLE_Authority)
{
ServerAddForce(HitNormal * WalljumpHorizontalStrenght + FVector::UpVector * WalljumpUpwardsStrenght);
}
LaunchCharacter(HitNormal * WalljumpHorizontalStrenght + FVector::UpVector * WalljumpUpwardsStrenght,false,true);
}
}
}
void AJetpackCharacter::ServerAddForce_Implementation(FVector force)
{
LaunchCharacter(force,false,true);
}
void AJetpackCharacter::StopJetpack()
{
if (Role < ROLE_Authority) // in a client
{
ServerSetJetpack(false); // call server to change variable
}
bIsUsingJetpack = false;
}
void AJetpackCharacter::StartJetpack()
{
if(bIsJetpackEnabled) // check for if it is enabled, so you cant start flying again while the jetpack disabled due to the player depleting it
{
if (Role < ROLE_Authority) // we are in a client
{
ServerSetJetpack(true); // call server to change the variable
}
bIsUsingJetpack = true;
}
}
void AJetpackCharacter::ServerSetJetpack_Implementation(bool bNewJetpack)
{
// This is the server version, so the bIsUsingJetpack variable is replicated properly across the network
bIsUsingJetpack = bNewJetpack;
}
void AJetpackCharacter::DisableJetpack()
{
// Debug Message
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("JetpackDisabled"));
bIsJetpackEnabled = false;
GetWorldTimerManager().SetTimer(this, &AJetpackCharacter::EnableJetpack, JetpackRecovery, false);
}
void AJetpackCharacter::EnableJetpack()
{
// Debug Message
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("JetpackEnabled"));
bIsJetpackEnabled = true;
}
bool AJetpackCharacter::CanJump() const
{
return !bIsCrouched && CharacterMovement && CharacterMovement->IsMovingOnGround() && CharacterMovement->CanEverJump() && !CharacterMovement->bWantsToCrouch;
}
bool AJetpackCharacter::ServerSetJetpack_Validate(bool bNewJetpack)
{
return true;
}
bool AJetpackCharacter::ServerAddForce_Validate(FVector force)
{
return true;
}