-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQLWeaponGravityGun.cpp
More file actions
351 lines (315 loc) · 14.1 KB
/
QLWeaponGravityGun.cpp
File metadata and controls
351 lines (315 loc) · 14.1 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//----------------------------------------
// Quarter Life
//
// MIT license
//
// (\-/)
// (='.'=)
// (")-(")o
//----------------------------------------
#include "QL.h"
#include "QLWeaponGravityGun.h"
#include "QLCharacter.h"
//------------------------------------------------------------
//------------------------------------------------------------
AQLWeaponGravityGun::AQLWeaponGravityGun()
{
Name = "GravityGun";
bIsGravityGunCompatibleActorHeld = false;
bIsAltFireHeldDown = false;
RunningTimeAltFirePressed = 0.0f;
FixedIntervalAltFirePressed = 0.2f;
RunningTimeAltFireHeldDown = 0.0f;
FixedIntervalAltFireHeldDown = 0.2f;
ggcActor = nullptr;
CrosshairTextureList.Add("Regular", CreateCrosshairTexture(TEXT("/Game/Textures/Crosshair/gravity_gun_crosshair")));
InitializeCurrentCrosshairTexture("Regular");
// sound
SoundComponentList.Add("None", CreateSoundComponent(RootComponent, TEXT("/Game/Sounds/bottle"), TEXT("SoundNongComp")));
SoundComponentList.Add("Hold", CreateSoundComponent(RootComponent, TEXT("/Game/Sounds/zoom_in"), TEXT("SoundHoldComp")));
SoundComponentList.Add("Fire", CreateSoundComponent(RootComponent, TEXT("/Game/Sounds/gravity_gun_fire"), TEXT("SoundFireComp")));
}
//------------------------------------------------------------
// If the actor that is compatible with gravity gun is within range but not being
// held by the character, or if that actor is being held by the character,
// the apply impulse to it.
//------------------------------------------------------------
void AQLWeaponGravityGun::Fire()
{
// the actor is being held by the player
if (bIsGravityGunCompatibleActorHeld)
{
// ggcActor is supposed to be non-null, but we check it just in case
if (ggcActor)
{
UBoxComponent* comp = ggcActor->BoxComponent;
// by design, the gravity gun compatible actor must have a component
// here the component is checked for the sake of safety
if (comp)
{
// character releases the actor
bIsGravityGunCompatibleActorHeld = false;
// reenable collision between character and component
// enum members are shown in engine source code: EngineTypes.h
comp->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Block);
// physical handle releases the component
WeaponOwner->PhysicsHandle->ReleaseComponent();
// avoid comp stuck in sleep mode when at rest
comp->WakeRigidBody();
// unset logical ownership
WeaponOwner->RemoveFromInventory(ggcActor);
// apply repulsive force to the component
APlayerCameraManager* cm = UGameplayStatics::GetPlayerCameraManager(WeaponOwner->GetWorld(), 0);
FVector Impulse = repulsionInstantSpeed * comp->GetMass() * cm->GetActorForwardVector();
comp->AddImpulse(Impulse);
// apply sound
PlaySoundComponent("Fire");
}
}
}
// the actor is not being held by the player
else
{
Hit = WeaponOwner->RayTraceFromCharacterPOV();
// if hit occurs
if (Hit.bBlockingHit)
{
// check if the hit actor compatible with (i.e. responsive to) gravity gun
// if it is not, the result of cast is nullptr
// this is a convenient runtime polymorphic type check
AActor* hitActor = Hit.GetActor();
ggcActor = Cast<AQLGravityGunCompatibleActor>(hitActor);
if (ggcActor)
{
UBoxComponent* comp = ggcActor->BoxComponent;
// by design, the gravity gun compatible actor must have a component
// here the component is checked for the sake of safety
if (comp)
{
// if the compatible actor is considered next to the actor
if (WeaponOwner->IsObjectNextToCharacter(ggcActor))
{
// apply repulsive force to the component
APlayerCameraManager* cm = UGameplayStatics::GetPlayerCameraManager(WeaponOwner->GetWorld(), 0);
FVector Impulse = repulsionInstantSpeed * comp->GetMass() * cm->GetActorForwardVector();
comp->AddImpulse(Impulse);
// apply sound
PlaySoundComponent("Fire");
}
}
}
}
}
}
//------------------------------------------------------------
// If the actor that is compatible with gravity gun is far away,
// AltFire() will exert attractive force on it, until it becomes
// next to the player, when the player starts to hold it.
// When the actor is being held by the player, AltFire() will cause
// it to be released.
//
// Note that:
// bool bIsAltFireHeldDown; // key/button is held down for some time
// bool bIsAltFirePressed; // key/button is pressed, regardless if
// it is subsequently released or held down
//------------------------------------------------------------
void AQLWeaponGravityGun::AltFire()
{
bIsAltFirePressed = true;
// the actor is being held by the player
if (bIsGravityGunCompatibleActorHeld)
{
// only release the actor when the player is making a single click instead of
// holding down alt fire
if (!bIsAltFireHeldDown)
{
// ggcActor is supposed to be non-null, but we check it just in case
if (ggcActor)
{
UBoxComponent* comp = ggcActor->BoxComponent;
// by design, the gravity gun compatible actor must have a component
// here the component is checked for the sake of safety
if (comp)
{
// character releases the actor
bIsGravityGunCompatibleActorHeld = false;
// reenable collision between character and component
// enum members are shown in engine source code: EngineTypes.h
comp->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Block);
// physical handle releases the component
WeaponOwner->PhysicsHandle->ReleaseComponent();
// avoid comp stuck in sleep mode when at rest
comp->WakeRigidBody();
// unset logical ownership
WeaponOwner->RemoveFromInventory(ggcActor);
}
}
}
}
// the actor is not being held by the player
else
{
Hit = WeaponOwner->RayTraceFromCharacterPOV();
// if hit occurs
if (Hit.bBlockingHit)
{
// check if the hit actor compatible with (i.e. responsive to) gravity gun
// if it is not, the result of cast is nullptr
// note: this is a convenient runtime polymorphic type check!!
AActor* hitActor = Hit.GetActor();
ggcActor = Cast<AQLGravityGunCompatibleActor>(hitActor);
if (ggcActor)
{
UBoxComponent* comp = ggcActor->BoxComponent;
// by design, the gravity gun compatible actor must have a component
// here the component is checked for the sake of safety
if (comp)
{
// if the compatible actor is considered next to the actor
if (WeaponOwner->IsObjectNextToCharacter(ggcActor))
{
// character holds the actor
bIsGravityGunCompatibleActorHeld = true;
// get rotation delta between character and component
FRotator ggcActorRotation = comp->GetComponentRotation();
APlayerCameraManager* cm = UGameplayStatics::GetPlayerCameraManager(WeaponOwner->GetWorld(), 0);
FRotator cameraRotation = cm->GetCameraRotation();
DeltaRotation = ggcActorRotation - cameraRotation;
DeltaRotation.Normalize();
// physical handle grabs the component
WeaponOwner->PhysicsHandle->GrabComponent(comp, Hit.BoneName, Hit.Location, true);
// set logical ownership
WeaponOwner->AddToInventory(ggcActor);
// disable collision between character and component
// enum members are shown in engine source code: EngineTypes.h
comp->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
// apply sound
PlaySoundComponent("Hold");
// consequently player then holds the actor for every tick
// refer to Tick()
}
else
{
// apply attractive force to the component
APlayerCameraManager* cm = UGameplayStatics::GetPlayerCameraManager(WeaponOwner->GetWorld(), 0);
FVector Impulse = -attractionInstantSpeed * comp->GetMass() * cm->GetActorForwardVector();
comp->AddImpulse(Impulse);
}
}
}
// the ggcActor is not gravity gun compatible
else
{
// apply sound
PlaySoundComponent("None");
}
}
else
{
// apply sound
PlaySoundComponent("None");
}
}
}
//------------------------------------------------------------
//------------------------------------------------------------
void AQLWeaponGravityGun::AltFireReleased()
{
bIsAltFireHeldDown = false;
bIsAltFirePressed = false;
}
//------------------------------------------------------------
//------------------------------------------------------------
void AQLWeaponGravityGun::AltFireRepeat()
{
AltFire();
}
//------------------------------------------------------------
// In Tick() several things are done:
// --- if alt fire is pressed, check if it is being held down
// --- if alt fire is being held down, call AltFireRepeat() at a constant interval
// --- if gravity gun compatible actor is being held, move it along with the character
//------------------------------------------------------------
void AQLWeaponGravityGun::Tick(float DeltaSeconds)
{
// if the gravity gun has been owned by character
if (GetWeaponOwner())
{
if (bIsAltFirePressed)
{
RunningTimeAltFirePressed += DeltaSeconds;
if (RunningTimeAltFirePressed >= FixedIntervalAltFirePressed)
{
bIsAltFireHeldDown = true;
RunningTimeAltFirePressed = 0.0f;
}
}
if (bIsAltFireHeldDown)
{
// If AltFireRepeat() is called for every tick, the attracted object will gain
// momentum in a riduculous rapid fashion. As a workaround, AltFireRepeat() is
// called every FixedInterval.
RunningTimeAltFireHeldDown += DeltaSeconds;
if (RunningTimeAltFireHeldDown >= FixedIntervalAltFireHeldDown)
{
AltFireRepeat();
RunningTimeAltFireHeldDown = 0.0f;
}
}
if (bIsGravityGunCompatibleActorHeld)
{
if (ggcActor)
{
// note:
// --> ggcActor->SetActorLocation(newLocation);
// ggcActor->SetActorRotation(newRotation);
// will force actor to be in front of the character, in which case
// the actor may be embedded into the ground when the character is looking down
// --> the actor may deviate from the camera center when rotating it fast.
// this is because full physics simulation is still enabled and the actor cannot be
// moved instantly with the camera.
APlayerCameraManager* cm = UGameplayStatics::GetPlayerCameraManager(GetWorld(), 0);
FVector newLocation = cm->GetCameraLocation() + cm->GetActorForwardVector() * distanceFromCharacterToActorWhenHold;
FRotator newRotation = cm->GetCameraRotation() + DeltaRotation;
newRotation.Normalize();
WeaponOwner->PhysicsHandle->SetTargetLocation(newLocation);
WeaponOwner->PhysicsHandle->SetTargetRotation(newRotation);
}
}
}
}
//------------------------------------------------------------
//------------------------------------------------------------
void AQLWeaponGravityGun::ResetWeapon()
{
// if the weapon is being owned
if (WeaponOwner)
{
// if ggcActor exists and it is being held
if (ggcActor && bIsGravityGunCompatibleActorHeld)
{
UBoxComponent* comp = ggcActor->BoxComponent;
// by design, the gravity gun compatible actor must have a component
// here the component is checked for the sake of safety
if (comp)
{
// reenable collision between character and component
// enum members are shown in engine source code: EngineTypes.h
comp->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Block);
// physical handle releases the component
WeaponOwner->PhysicsHandle->ReleaseComponent();
// avoid comp stuck in sleep mode when at rest
comp->WakeRigidBody();
// unset logical ownership
WeaponOwner->RemoveFromInventory(ggcActor);
}
}
bIsGravityGunCompatibleActorHeld = false;
bIsAltFireHeldDown = false;
RunningTimeAltFirePressed = 0.0f;
FixedIntervalAltFirePressed = 0.2f;
RunningTimeAltFireHeldDown = 0.0f;
FixedIntervalAltFireHeldDown = 0.2f;
ggcActor = nullptr;
}
}