Death implementation and catching end of match

pull/6/head
m4xxx1m 2 years ago
parent 98e8fe3827
commit c256136d69

@ -12,9 +12,13 @@ auto AMyGameState::GetMyPlayerState(uint8 PlayerIndex) const {
void AMyGameState::BeginPlay() { void AMyGameState::BeginPlay() {
Super::BeginPlay(); Super::BeginPlay();
LivingTroopers.SetNum(2);
} }
void AMyGameState::AddTrooper_Implementation(ATrooper *Trooper) { void AMyGameState::AddTrooper_Implementation(ATrooper *Trooper) {
if (Trooper->GetPlayerIndex() >= 0 && Trooper->GetPlayerIndex() <= LivingTroopers.Num()) {
LivingTroopers[Trooper->GetPlayerIndex()]++;
}
Troopers.Add(Trooper); Troopers.Add(Trooper);
} }
@ -69,6 +73,17 @@ bool AMyGameState::IsGameStarted() const {
return bGameStarted; return bGameStarted;
} }
void AMyGameState::DecreaseLivingTroopers(int PlayerIndex) {
if (bGameIsOver)
return;
LivingTroopers[PlayerIndex]--;
if (LivingTroopers[PlayerIndex] <= 0) {
UE_LOG(LogTemp, Warning, TEXT("Player %d lose!"), PlayerIndex);
bGameIsOver = true;
}
}
void AMyGameState::GetLifetimeReplicatedProps( void AMyGameState::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const { TArray<FLifetimeProperty> &OutLifetimeProps) const {
Super::GetLifetimeReplicatedProps(OutLifetimeProps); Super::GetLifetimeReplicatedProps(OutLifetimeProps);

@ -41,10 +41,19 @@ public:
UFUNCTION() UFUNCTION()
bool IsGameStarted() const; bool IsGameStarted() const;
UFUNCTION()
void DecreaseLivingTroopers(int PlayerIndex);
private: private:
UPROPERTY(Replicated)
bool bGameIsOver = false;
UPROPERTY(Replicated) UPROPERTY(Replicated)
TArray<ATrooper *> Troopers; TArray<ATrooper *> Troopers;
UPROPERTY(Replicated)
TArray<int> LivingTroopers;
UPROPERTY(Replicated) UPROPERTY(Replicated)
bool bGameStarted = false; bool bGameStarted = false;

@ -2,6 +2,7 @@
#include <Kismet/GameplayStatics.h> #include <Kismet/GameplayStatics.h>
#include "HealthBar.h" #include "HealthBar.h"
#include "MyGameState.h"
#include "MyPlayerController.h" #include "MyPlayerController.h"
#include "MyPlayerState.h" #include "MyPlayerState.h"
#include "MyProjectile.h" #include "MyProjectile.h"
@ -128,7 +129,7 @@ void ATrooper::Tick(float const DeltaTime) {
// } // }
// } // }
void ATrooper::MoveTrooper(FVector const NewPos) { void ATrooper::MoveTrooper_Implementation(FVector const NewPos) {
TargetLocation = NewPos; TargetLocation = NewPos;
bIsMoving = true; bIsMoving = true;
ActionPoints -= (NewPos - CurrentLocation).Size() * MoveCost; ActionPoints -= (NewPos - CurrentLocation).Size() * MoveCost;
@ -158,7 +159,7 @@ void ATrooper::GetLifetimeReplicatedProps(
DOREPLIFETIME(ATrooper, CurrentAbilityDestination); DOREPLIFETIME(ATrooper, CurrentAbilityDestination);
} }
uint8 ATrooper::GetPlayerIndex() const { int8 ATrooper::GetPlayerIndex() const {
return PlayerIndex; return PlayerIndex;
} }
@ -246,17 +247,18 @@ UAbility *ATrooper::GetAbility(int AbilityIndex) const {
} }
} }
bool ATrooper::TakeDamage(float Damage) { void ATrooper::TakeDamage_Implementation(float Damage) {
if (bIsTakingDamage) { if (bIsTakingDamage || bIsDead) {
return false; return;
} }
HitPoints = FMath::Max<float>(0, HitPoints - Damage); HitPoints = FMath::Max<float>(0, HitPoints - Damage);
if (HitPoints == 0) { if (HitPoints == 0) {
bIsDead = true; bIsDead = true;
return true; SetLifeSpan(DyingAnimationDuration);
} GetWorld()->GetGameState<AMyGameState>()->DecreaseLivingTroopers(PlayerIndex);
} else {
bIsTakingDamage = true; bIsTakingDamage = true;
return false; }
} }
TSubclassOf<AMyProjectile> ATrooper::GetProjectileClass( TSubclassOf<AMyProjectile> ATrooper::GetProjectileClass(
@ -312,7 +314,7 @@ int ATrooper::GetAnimationValue() {
return 0; return 0;
} }
void ATrooper::Attack(int AbilityIndex, FVector ToLocation) { void ATrooper::Attack_Implementation(int AbilityIndex, FVector ToLocation) {
bIsAttacking = true; bIsAttacking = true;
bIsWaitingForFire = true; bIsWaitingForFire = true;
ActionPoints -= GetAbility(AbilityIndex)->ActionCost; ActionPoints -= GetAbility(AbilityIndex)->ActionCost;

@ -18,9 +18,9 @@ public:
uint8 const NewId); uint8 const NewId);
UFUNCTION() UFUNCTION()
uint8 GetPlayerIndex() const; int8 GetPlayerIndex() const;
UFUNCTION() UFUNCTION(Server, Reliable)
void MoveTrooper(FVector const NewPos); void MoveTrooper(FVector const NewPos);
UFUNCTION() UFUNCTION()
@ -38,7 +38,7 @@ public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
int GetAnimationValue(); int GetAnimationValue();
UFUNCTION() UFUNCTION(Server, Reliable)
void Attack(int AbilityIndex, FVector ToLocation); void Attack(int AbilityIndex, FVector ToLocation);
UFUNCTION() UFUNCTION()
@ -65,8 +65,8 @@ public:
UFUNCTION() UFUNCTION()
UAbility *GetAbility(int AbilityIndex) const; UAbility *GetAbility(int AbilityIndex) const;
UFUNCTION() UFUNCTION(Server, Reliable)
bool TakeDamage(float Damage); void TakeDamage(float Damage);
protected: protected:
constexpr static float PIXELS_IN_RADIUS = 50; constexpr static float PIXELS_IN_RADIUS = 50;
@ -140,6 +140,8 @@ protected:
const float TakingDamageDuration = 1.46667f; const float TakingDamageDuration = 1.46667f;
const float DyingAnimationDuration = 2.83333f;
UPROPERTY(Replicated) UPROPERTY(Replicated)
bool bIsDead = false; bool bIsDead = false;

Loading…
Cancel
Save