-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerActor.cpp
More file actions
357 lines (294 loc) · 9.7 KB
/
Copy pathPlayerActor.cpp
File metadata and controls
357 lines (294 loc) · 9.7 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
352
353
354
355
356
357
// Fill out your copyright notice in the Description page of Project Settings.
#include "PlayerActor.h"
#include "Components/InputComponent.h"
#include "GameFramework/Actor.h"
#include "Components/CapsuleComponent.h"
#include "Engine/World.h"
#include "Kismet/KismetMathLibrary.h"
#include "TimerManager.h"
#include "SwitchLever.h"
#include "Enemy.h"
#include "Bullet.h"
#include "DoorAccessable.h"
#include "MyHUD.h"
#include "UObject/ConstructorHelpers.h"
#include "Engine.h"
#include "MovingPlatforms.h"
// Sets default values
APlayerActor::APlayerActor()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Initialise Properties
AttackTimeout = 1.5f; // Cooldown per attack
TimeSinceLastStrike = 0; // Cooldown counter
canAttack = true; // Can the player attack
count = 0;
// create trigger capsule
TriggerCapsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Trigger Capsule"));
TriggerCapsule->InitCapsuleSize(55.f, 96.0f);;
TriggerCapsule->SetCollisionProfileName(TEXT("Trigger"));
TriggerCapsule->SetupAttachment(RootComponent);
// bind trigger events
TriggerCapsule->OnComponentBeginOverlap.AddDynamic(this, &APlayerActor::OnOverlapBegin);
TriggerCapsule->OnComponentEndOverlap.AddDynamic(this, &APlayerActor::OnOverlapEnd);
// set current light switch to null
CurrentLightSwitch = nullptr;
PlatformActivation = nullptr;
// Set attacking boolean to false
AttackStarted = false;
// Retrieve Time manager for delay functionality
// Get the world, get the timer manager, set a new timer with this handle 'TimerHandle' and set it to
// 5 Seconds, after 5 seconds trigger this function 'onTimerEnd'.
// GetWorldTimerManager().SetTimer(MemberTimerHandle, this, &APlayerActor::RepeatingFunction, 2.0f, false, 5.0f);
}
// Called when the game starts or when spawned
void APlayerActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APlayerActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Apply knockback vector
AddMovementInput(Knockback, 100.f);
// half it each frame
Knockback *= 0.5f;
// Restart Game if player has died
if (dead == true)
{
// Turns off player controller for the player;
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
APlayerActor::DisableInput(PlayerController);
// Restart game
UGameplayStatics::OpenLevel(this, FName(*GetWorld()->GetName()), false);
}
// Cooldown between enemy attacks
if (TimeSinceLastStrike <= 0 && canAttack) // returns null or 0
{
// If the cooldown is over, then attack again
// this resets the hit parameter
UE_LOG(LogTemp, Warning, TEXT("Can attack is FALSE"));
}
TimeSinceLastStrike += DeltaTime;
if (TimeSinceLastStrike > AttackTimeout && !canAttack)
{
canAttack = true;
TimeSinceLastStrike = 0;
UE_LOG(LogTemp, Warning, TEXT("Can attack is TRUE"));
}
}
// Called to bind functionality to input
void APlayerActor::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(InputComponent);
InputComponent->BindAxis("Forward", this, &APlayerActor::MoveForward);
InputComponent->BindAxis("Strafe", this, &APlayerActor::MoveRight);
InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
InputComponent->BindAction("Action", IE_Pressed, this, &APlayerActor::OnAction); // Interact with Items
InputComponent->BindAction("Inventory", IE_Pressed, this, &APlayerActor::ToggleInventory);
InputComponent->BindAxis("Yaw", this, &APlayerActor::Yaw);
InputComponent->BindAxis("Pitch", this, &APlayerActor::Pitch);
// Attack Input
InputComponent->BindAction("Attack", IE_Pressed, this, &APlayerActor::OnAttack);
}
void APlayerActor::OnAttack()
{
AttackStarted = true;
// if canAttack is true the player can attack
if (canAttack)
{
FVector fwd = GetActorForwardVector();
FVector nozzle = GetMesh()->GetBoneLocation("ArmR3");
nozzle += fwd * 55;
ABullet* bullet = GetWorld()->SpawnActor<ABullet>(BPFireball, nozzle,
RootComponent->GetComponentRotation());
canAttack = false;
if (bullet)
{
bullet->Firer;
bullet->ProxSphere->AddImpulse(fwd*BulletLaunchImpulse);
}
}
//GEngine->AddOnScreenDebugMessage(0, 5.f, FColor::Green, "Attacked %s");
}
void APlayerActor::MoveForward(float amount)
{
if (inventoryShowing)
{
// If the inventory is currently being displayed exit this method, consequenstally
// disabling the players movement controls intialized after.
return;
}
// Don't enter the body of this function if Controller is
// not set up yet, or if the amount to move is equal to 0
if (Controller && amount)
{
bisWalking = true;
FVector fwd = GetActorForwardVector();
// we call AddMovementInput to actually move the player
// by `amount` in the `fwd` direction
AddMovementInput(fwd, amount);
}
if (amount <= 0)
{
bisWalking = false;
}
}
void APlayerActor::MoveRight(float amount)
{
if (inventoryShowing)
{
return;
}
if (Controller && amount)
{
FVector right = GetActorRightVector();
AddMovementInput(right, amount);
}
}
void APlayerActor::Yaw(float amount)
{
if (inventoryShowing)
{
// if the button is down click drag
APlayerController* PController = GetWorld()->GetFirstPlayerController();
AMyHUD* hud = Cast<AMyHUD>(PController->GetHUD());
hud->MouseMoved();
return;
}
AddControllerYawInput(200.f * amount * GetWorld()->GetDeltaSeconds());
}
void APlayerActor::Pitch(float amount)
{
if (inventoryShowing)
{
// if the button is down click drag
APlayerController* PController = GetWorld()->GetFirstPlayerController();
AMyHUD* hud = Cast<AMyHUD>(PController->GetHUD());
hud->MouseMoved();
return;
}
AddControllerPitchInput(200.f * amount * GetWorld()->GetDeltaSeconds());
}
void APlayerActor::OnAction()
{
// Toggles when the LightSwitch isn't equal to note
//if (CurrentLightSwitch)
//{
// CurrentLightSwitch->ToggleLight();
//}
//
if (PlatformActivation)
{
UE_LOG(LogTemp, Warning, TEXT("IN FUNCTION"));
PlatformActivation->TogglePlatform();
}
}
// overlap on begin function
void APlayerActor::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
/* If player overlaps with actor, check for that actors class then add a pointer to that class definition
if (OtherActor && (OtherActor != this) && OtherComp && OtherActor->GetClass()->IsChildOf(ASwitchLever::StaticClass()))
{
CurrentLightSwitch = Cast<ASwitchLever>(OtherActor);
}
*/
if (OtherActor && (OtherActor != this) && OtherComp && OtherActor->GetClass()->IsChildOf(AMovingPlatforms::StaticClass()))
{
PlatformActivation = Cast<AMovingPlatforms>(OtherActor);
}
}
// overlap on end function
void APlayerActor::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
/*
if (OtherActor && (OtherActor != this) && OtherComp)
{
CurrentLightSwitch = nullptr;
}
*/
if (OtherActor && (OtherActor != this) && OtherComp)
{
PlatformActivation = nullptr;
}
}
float APlayerActor::TakeDamage(float Damage, struct FDamageEvent const& DamageEvent,
AController* EventInstigator, AActor* DamageCauser)
{
Super::TakeDamage(Damage, DamageEvent, EventInstigator, DamageCauser);
Hp -= Damage;
//GEngine->AddOnScreenDebugMessage(0, 5.f, FColor::Yellow, "Damage Taken");
// Add some knockback that gets applied over a few frames
Knockback = GetActorLocation() - DamageCauser->GetActorLocation();
Knockback.Normalize();
Knockback *= Damage * 1000;
// If the player goes below 0 hp, he will die
if (Hp <= 0)
{
Hp = 0; // clamp
//GEngine->AddOnScreenDebugMessage(0, 5.f, FColor::Yellow, "Game Over");
// Player is dead
dead = true;
}
return Damage;
}
void APlayerActor::Pickup(APickupItem *item)
{
// This will add items to the players backup of type 'APickupItem'
// If the player already has that item, the code increases the quanity,
// however if they do not have that item it will add the new item with a
// name, quanity and icon
if (Backpack.Contains(item->Name))
{
Backpack[item->Name] += item->Quantity;
}
else
{
Backpack.Add(item->Name, item->Quantity);
Icons.Add(item->Name, item->Icon);
}
}
void APlayerActor::ToggleInventory()
{
APlayerController* PController = GetWorld()->GetFirstPlayerController();
AMyHUD* hud = Cast<AMyHUD>(PController->GetHUD());
if (inventoryShowing)
{
hud->clearWidgets();
inventoryShowing = false;
PController->bShowMouseCursor = false;
return;
}
else
{
inventoryShowing = true;
PController->bShowMouseCursor = true;
for (TMap<FString, int>::TIterator it = Backpack.CreateIterator(); it; ++it)
{
//Combine string name of the item, with qty eg Cow x 5
FString fs = it->Key + FString::Printf(TEXT("x%d"), it->Value);
//GEngine->AddOnScreenDebugMessage(count++, 30.f, FColor::Red, fs).
UTexture2D* tex;
if (Icons.Find(it->Key))
{
tex = Icons[it->Key];
Widget w(Icon(fs, tex), fs);
hud->addWidget(w);
}
}
}
}
void APlayerActor::keyCaptured()
{
//AActor *player = Cast<ADoorAccessable>(OtherActor);
//GEngine->AddOnScreenDebugMessage(0, 5.f, FColor::Orange, "Key Captured");
}
/*
void APlayerActor::RepeatingFunction()
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Hello Timer"));
}
*/