-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShooterCharacter.cpp
More file actions
110 lines (87 loc) · 4.21 KB
/
ShooterCharacter.cpp
File metadata and controls
110 lines (87 loc) · 4.21 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
// Fill out your copyright notice in the Description page of Project Settings.
#include "ShooterCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Gun.h"
#include"Engine/DamageEvents.h"
#include "Components/CapsuleComponent.h"
#include "Simple_ShooterGameModeBase.h"
// Sets default values
AShooterCharacter::AShooterCharacter()
{
// 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;
Health = MaxHealth;
GetCharacterMovement()->MaxWalkSpeed = 700.f;
}
// Called when the game starts or when spawned
void AShooterCharacter::BeginPlay()
{
Super::BeginPlay();
Gun=GetWorld()->SpawnActor<AGun>(GunClass); //for spawning guns unthe actors hand
GetMesh()->HideBoneByName(TEXT("weapon_r"), EPhysBodyOp::PBO_None);//for hiding the previous gun from the mesh
Gun->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("weaponSocket"));//creating a new socket then now dynamically adding the gun to the weapon socket
Gun->SetOwner(this);//that this gun owner is the object pointer of the shooter character class and we will use it in damage function to get owner instigator
}
bool AShooterCharacter::IsDead() const
{
return Health <= 0 ;
//return false;
}
float AShooterCharacter::GetHealthPercentage() const
{
return Health/MaxHealth;
}
// Called every frame
void AShooterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AShooterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(TEXT("MoveForward"),this, &AShooterCharacter::MoveForward);
PlayerInputComponent->BindAxis(TEXT("LookUp"), this, &APawn::AddControllerPitchInput);//PlayerInputComponent->BindAxis(TEXT("LookUp"), this, AShooterCharacter::LookUp);//this addcontrollerpitch input is define in the pawn class which is a parent class of the character so we can directly call from this also
PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AShooterCharacter::MoveRight);
PlayerInputComponent->BindAxis(TEXT("LookRight"), this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis(TEXT("LookUpRate"), this, &AShooterCharacter::LookUpRate);
PlayerInputComponent->BindAxis(TEXT("LookRightRate"), this, &AShooterCharacter::LookRightRate);
PlayerInputComponent->BindAction(TEXT("Jump"),EInputEvent::IE_Pressed,this,&ACharacter::Jump);//this jump is define in the character class
PlayerInputComponent->BindAction(TEXT("Shoot"), EInputEvent::IE_Pressed, this, &AShooterCharacter::Shoot);
}
void AShooterCharacter::MoveForward(float AxisValue) {
AddMovementInput(GetActorForwardVector() * AxisValue);
}
/*
void AShooterCharacter::LookUp(float AxisValue) {
AddControllerPitchInput(AxisValue);
}*/
void AShooterCharacter::MoveRight(float AxisValue) {
AddMovementInput(GetActorRightVector() * AxisValue);
}
void AShooterCharacter::LookUpRate(float AxisValue) {
AddControllerPitchInput(AxisValue * RotationRate * GetWorld()->GetDeltaSeconds());
}
void AShooterCharacter::LookRightRate(float AxisValue) {
AddControllerYawInput(AxisValue * RotationRate * GetWorld()->GetDeltaSeconds());
}
void AShooterCharacter::Shoot()
{
Gun->PullTrigger();
}
float AShooterCharacter::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser)
{
float DamageToApply=Super::TakeDamage(DamageAmount, DamageEvent,EventInstigator, DamageCauser);
DamageToApply = FMath::Min(Health, DamageToApply);
Health -= DamageToApply;
UE_LOG(LogTemp, Warning, TEXT("Health is = %f"), Health);
if (IsDead()) {
ASimple_ShooterGameModeBase* GameMode = GetWorld()->GetAuthGameMode<ASimple_ShooterGameModeBase>();//to get the hold of the base base game mode class
if (GameMode != nullptr) {
GameMode->PawnKilled(this);
}
DetachFromControllerPendingDestroy();//from detaching from the player controller
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
return DamageToApply;
}