-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShooterCharacter.cpp
119 lines (87 loc) · 3.81 KB
/
ShooterCharacter.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
// Fill out your copyright notice in the Description page of Project Settings.
#include "ShooterCharacter.h"
#include "Gun.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;
}
// Called when the game starts or when spawned
void AShooterCharacter::BeginPlay()
{
Super::BeginPlay();
//set health equal to max health
health = maxHealth;
//at begin play.. spawn the gun actor
gun = GetWorld()->SpawnActor<AGun>(gunClass);
//hide the original gun attached to the character asset by hiding the bone attached to it
//GetMesh() is the mesh of the shooter character (this class)
GetMesh()->HideBoneByName(TEXT("weapon_r"), EPhysBodyOp::PBO_None);
// we need to attach our "runtime-spawned gun" to the this shooter character's mesh component (at runtime)
gun->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("weaponSocket"));
//set the owner of this spawned gun to the shooter character (needed in applying damage AND multiplayers)
gun->SetOwner(this);
}
// 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);
//bind the input action mapping for the player.. 'this' mean we want to apply this move funtion pn this character object
//looking up, right and jumping are already implemented in classes Pawn and Character..
PlayerInputComponent->BindAxis(TEXT("MoveVertical"), this, &AShooterCharacter::moveVertical);
PlayerInputComponent->BindAxis(TEXT("LookVertical"), this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAxis(TEXT("MoveHorizontal"), this, &AShooterCharacter::moveHorizontal);
PlayerInputComponent->BindAxis(TEXT("LookHorizontal"), this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAction(TEXT("Jump"), EInputEvent::IE_Pressed, this, &ACharacter::Jump);
//bind the shoot function
PlayerInputComponent->BindAction(TEXT("shoot"), EInputEvent::IE_Pressed, this, &AShooterCharacter::shoot);
}
float AShooterCharacter::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{
float damageToApply = Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
//what if damage to apply is greater than health
damageToApply = FMath::Min(health, damageToApply);
health -= damageToApply;
if (isDead())
{
//get hold of the game mode
ASimple_ShooterGameModeBase* gameMode = GetWorld()->GetAuthGameMode<ASimple_ShooterGameModeBase>();
if (gameMode != nullptr)
{
gameMode->pawnKilled(this);
}
DetachFromControllerPendingDestroy(); //disable the controller once the character is dead
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision); //disable the capsule component on the character
}
return damageToApply; //the amount of damage done.. zero if health is depleted
}
bool AShooterCharacter::isDead() const
{
return health <= 0;
}
void AShooterCharacter::moveVertical(float inputValue)
{
//this function is inherited from the character class, no need to implement any movement logic of our own..
//input is a valur between 1,-1.. if -ve, then we move backwards.
AddMovementInput(GetActorForwardVector() * inputValue);
}
void AShooterCharacter::moveHorizontal(float inputValue)
{
AddMovementInput(GetActorRightVector() * inputValue);
}
float AShooterCharacter::getHealthPercentage() const
{
return health / maxHealth;
}
void AShooterCharacter::shoot()
{
gun->pullTrigger();
}