218 - Preventing Friendly Fire

This commit is contained in:
Kingsmedia 2022-05-30 19:31:37 +02:00
parent 19b84ff30f
commit 5d3c2aacf7
7 changed files with 37 additions and 12 deletions

View File

@ -249,7 +249,7 @@ void ABlasterCharacter::MulticastEliminated_Implementation(bool bPlayerLeftGame)
void ABlasterCharacter::EliminationTimerFinished() void ABlasterCharacter::EliminationTimerFinished()
{ {
ABlasterGameMode* BlasterGameMode = GetWorld()->GetAuthGameMode<ABlasterGameMode>(); BlasterGameMode = BlasterGameMode == nullptr ? GetWorld()->GetAuthGameMode<ABlasterGameMode>() : BlasterGameMode;
if (BlasterGameMode && !bLeftGame) if (BlasterGameMode && !bLeftGame)
{ {
BlasterGameMode->RequestRespawn(this, Controller); BlasterGameMode->RequestRespawn(this, Controller);
@ -262,7 +262,7 @@ void ABlasterCharacter::EliminationTimerFinished()
void ABlasterCharacter::ServerLeaveGame_Implementation() void ABlasterCharacter::ServerLeaveGame_Implementation()
{ {
ABlasterGameMode* BlasterGameMode = GetWorld()->GetAuthGameMode<ABlasterGameMode>(); BlasterGameMode = BlasterGameMode == nullptr ? GetWorld()->GetAuthGameMode<ABlasterGameMode>() : BlasterGameMode;
BlasterPlayerState = BlasterPlayerState == nullptr ? GetPlayerState<ABlasterPlayerState>() : BlasterPlayerState; BlasterPlayerState = BlasterPlayerState == nullptr ? GetPlayerState<ABlasterPlayerState>() : BlasterPlayerState;
if (BlasterGameMode && BlasterPlayerState) if (BlasterGameMode && BlasterPlayerState)
{ {
@ -279,7 +279,7 @@ void ABlasterCharacter::Destroyed()
EliminationBotComponent->DestroyComponent(); EliminationBotComponent->DestroyComponent();
} }
ABlasterGameMode* BlasterGameMode = Cast<ABlasterGameMode>(UGameplayStatics::GetGameMode(this)); BlasterGameMode = (BlasterGameMode == nullptr) ? GetWorld()->GetAuthGameMode<ABlasterGameMode>() : BlasterGameMode;
bool bMatchNotInProgress = BlasterGameMode && BlasterGameMode->GetMatchState() != MatchState::InProgress; bool bMatchNotInProgress = BlasterGameMode && BlasterGameMode->GetMatchState() != MatchState::InProgress;
if (Combat && Combat->EquippedWeapon && bMatchNotInProgress) if (Combat && Combat->EquippedWeapon && bMatchNotInProgress)
{ {
@ -538,8 +538,10 @@ void ABlasterCharacter::GrenadeButtonPressed()
void ABlasterCharacter::ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* InstigatorController, AActor* DamageCauser) void ABlasterCharacter::ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* InstigatorController, AActor* DamageCauser)
{ {
if (bEliminated) return; BlasterGameMode = (BlasterGameMode == nullptr) ? GetWorld()->GetAuthGameMode<ABlasterGameMode>() : BlasterGameMode;
if (bEliminated || BlasterGameMode == nullptr) return;
Damage = BlasterGameMode->CalculateDamage(InstigatorController, Controller, Damage);
float DamageToHealth = Damage; float DamageToHealth = Damage;
if (Shield > 0.f) if (Shield > 0.f)
{ {
@ -559,10 +561,7 @@ void ABlasterCharacter::ReceiveDamage(AActor* DamagedActor, float Damage, const
UpdateHUDHealth(); UpdateHUDHealth();
UpdateHUDShield(); UpdateHUDShield();
if (Health > 0.f) PlayHitReactMontage();
{
PlayHitReactMontage();
}
if (Health == 0.f) if (Health == 0.f)
{ {
@ -571,7 +570,7 @@ void ABlasterCharacter::ReceiveDamage(AActor* DamagedActor, float Damage, const
Eliminated(false); Eliminated(false);
return; return;
} }
ABlasterGameMode* BlasterGameMode = GetWorld()->GetAuthGameMode<ABlasterGameMode>(); BlasterGameMode = (BlasterGameMode == nullptr) ? GetWorld()->GetAuthGameMode<ABlasterGameMode>() : BlasterGameMode;
if (BlasterGameMode) if (BlasterGameMode)
{ {
BlasterPlayerController = BlasterPlayerController == nullptr ? Cast<ABlasterPlayerController>(Controller) : BlasterPlayerController; BlasterPlayerController = BlasterPlayerController == nullptr ? Cast<ABlasterPlayerController>(Controller) : BlasterPlayerController;
@ -903,7 +902,7 @@ void ABlasterCharacter::UpdateHUDAmmo()
void ABlasterCharacter::SpawnDefaultWeapon() void ABlasterCharacter::SpawnDefaultWeapon()
{ {
const ABlasterGameMode* BlasterGameMode = Cast<ABlasterGameMode>(UGameplayStatics::GetGameMode(this)); BlasterGameMode = (BlasterGameMode == nullptr) ? GetWorld()->GetAuthGameMode<ABlasterGameMode>() : BlasterGameMode;
UWorld* World = GetWorld(); UWorld* World = GetWorld();
if (BlasterGameMode && World && !bEliminated && DefaultWeaponClass) if (BlasterGameMode && World && !bEliminated && DefaultWeaponClass)
{ {

View File

@ -322,6 +322,9 @@ private:
// Grenade // Grenade
UPROPERTY(VisibleAnywhere) UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* AttachedGrenade; UStaticMeshComponent* AttachedGrenade;
UPROPERTY()
class ABlasterGameMode* BlasterGameMode;
public: public:
void SetOverlappingWeapon(AWeapon* Weapon); void SetOverlappingWeapon(AWeapon* Weapon);

View File

@ -72,6 +72,12 @@ void ABlasterGameMode::OnMatchStateSet()
} }
} }
float ABlasterGameMode::CalculateDamage(AController* Attacker, AController* Victim, float BaseDamage)
{
return BaseDamage;
}
void ABlasterGameMode::PlayerEliminated(class ABlasterCharacter* EliminatedCharacter, class ABlasterPlayerController* VictimController, ABlasterPlayerController* AttackerController) void ABlasterGameMode::PlayerEliminated(class ABlasterCharacter* EliminatedCharacter, class ABlasterPlayerController* VictimController, ABlasterPlayerController* AttackerController)
{ {
if (AttackerController == nullptr || AttackerController->PlayerState == nullptr) return; if (AttackerController == nullptr || AttackerController->PlayerState == nullptr) return;

View File

@ -28,6 +28,7 @@ public:
class ABlasterPlayerController* AttackerController); class ABlasterPlayerController* AttackerController);
virtual void RequestRespawn(ACharacter* EliminatedCharacter, AController* EliminatedController); virtual void RequestRespawn(ACharacter* EliminatedCharacter, AController* EliminatedController);
void PlayerLeftGame(ABlasterPlayerState* PlayerLeaving); void PlayerLeftGame(ABlasterPlayerState* PlayerLeaving);
virtual float CalculateDamage(AController* Attacker, AController* Victim, float BaseDamage);
UPROPERTY(EditDefaultsOnly) UPROPERTY(EditDefaultsOnly)
float WarmupTime = 10.f; float WarmupTime = 10.f;

View File

@ -65,3 +65,15 @@ void ATeamsGameMode::AssignToTeam(APlayerState* PlayerState)
} }
} }
} }
float ATeamsGameMode::CalculateDamage(AController* Attacker, AController* Victim, float BaseDamage)
{
ABlasterPlayerState* AttackerPState = Attacker->GetPlayerState<ABlasterPlayerState>();
ABlasterPlayerState* VictimPState = Victim->GetPlayerState<ABlasterPlayerState>();
if (AttackerPState == nullptr || VictimPState == nullptr) return BaseDamage;
if (VictimPState == AttackerPState) return BaseDamage;
if (AttackerPState->GetTeam() == VictimPState->GetTeam() && !FriendlyFire) return 0.f;
return BaseDamage;
}

View File

@ -17,13 +17,17 @@ class BLASTER_API ATeamsGameMode : public ABlasterGameMode
public: public:
virtual void PostLogin(APlayerController* NewPlayer) override; virtual void PostLogin(APlayerController* NewPlayer) override;
virtual void Logout(AController* Exiting) override; virtual void Logout(AController* Exiting) override;
virtual float CalculateDamage(AController* Attacker, AController* Victim, float BaseDamage) override;
protected: protected:
virtual void HandleMatchHasStarted() override; virtual void HandleMatchHasStarted() override;
private: private:
UPROPERTY() UPROPERTY()
class ABlasterGameState* BGameState; class ABlasterGameState* BGameState;
UPROPERTY(EditAnywhere)
bool FriendlyFire = false;
void AssignToTeam(APlayerState* PlayerState); void AssignToTeam(APlayerState* PlayerState);