107 - Blaster Player State

This commit is contained in:
Kingsmedia 2022-05-09 16:33:48 +02:00
parent e478e0d433
commit 4261cd8f0d
11 changed files with 127 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include "Blaster/Components/CombatComponent.h" #include "Blaster/Components/CombatComponent.h"
#include "Blaster/GameMode/BlasterGameMode.h" #include "Blaster/GameMode/BlasterGameMode.h"
#include "Blaster/PlayerController/BlasterPlayerController.h" #include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Blaster/PlayerState/BlasterPlayerState.h"
#include "Blaster/Weapon/Weapon.h" #include "Blaster/Weapon/Weapon.h"
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h" #include "Components/CapsuleComponent.h"
@ -238,6 +239,20 @@ void ABlasterCharacter::Tick(float DeltaTime)
} }
HideCameraIfCharacterClose(); HideCameraIfCharacterClose();
PollInit();
}
void ABlasterCharacter::PollInit()
{
if (PlayerState == nullptr)
{
PlayerState = GetPlayerState<ABlasterPlayerState>();
if (PlayerState)
{
// Initialize Score now we have the PlayerState
PlayerState->IncreaseScore(0.f);
}
}
} }
void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)

View File

@ -53,8 +53,12 @@ protected:
UFUNCTION() UFUNCTION()
void ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* InstigatorController, AActor* DamageCauser); void ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* InstigatorController, AActor* DamageCauser);
void UpdateHUDHealth(); void UpdateHUDHealth();
// Poll for any relevant classes and initialize them
void PollInit();
private: private:
class ABlasterPlayerState* PlayerState;
UPROPERTY(VisibleAnywhere, Category="Camera") UPROPERTY(VisibleAnywhere, Category="Camera")
class USpringArmComponent* CameraBoom; class USpringArmComponent* CameraBoom;

View File

@ -4,12 +4,22 @@
#include "BlasterGameMode.h" #include "BlasterGameMode.h"
#include "Blaster/Character/BlasterCharacter.h" #include "Blaster/Character/BlasterCharacter.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Blaster/PlayerState/BlasterPlayerState.h"
#include "GameFramework/PlayerStart.h" #include "GameFramework/PlayerStart.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
void ABlasterGameMode::PlayerEliminated(ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController, void ABlasterGameMode::PlayerEliminated(ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController,
ABlasterPlayerController* AttackerController) ABlasterPlayerController* AttackerController)
{ {
ABlasterPlayerState* AttackerPlayerState = AttackerController ? Cast<ABlasterPlayerState>(AttackerController->PlayerState) : nullptr;
ABlasterPlayerState* VictimPlayerState = VictimController ? Cast<ABlasterPlayerState>(VictimController->PlayerState) : nullptr;
if (AttackerPlayerState && AttackerPlayerState != VictimPlayerState)
{
AttackerPlayerState->IncreaseScore(1.f);
}
if (EliminatedCharacter) if (EliminatedCharacter)
{ {
EliminatedCharacter->Eliminated(); EliminatedCharacter->Eliminated();

View File

@ -22,4 +22,7 @@ public:
UPROPERTY(meta = (BindWidget)) UPROPERTY(meta = (BindWidget))
class UTextBlock* HealthText; class UTextBlock* HealthText;
UPROPERTY(meta = (BindWidget))
UTextBlock* ScoreValue;
}; };

View File

@ -44,3 +44,18 @@ void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth)
BlasterHUD->CharacterOverlay->HealthText->SetText(FText::FromString(HealthText)); BlasterHUD->CharacterOverlay->HealthText->SetText(FText::FromString(HealthText));
} }
} }
void ABlasterPlayerController::SetHUDScore(float Score)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->ScoreValue;
if (bHUDValid)
{
const FString ScoreValue = FString::Printf(TEXT("%d"), FMath::FloorToInt(Score));
BlasterHUD->CharacterOverlay->ScoreValue->SetText(FText::FromString(ScoreValue));
}
}

View File

@ -17,6 +17,7 @@ class BLASTER_API ABlasterPlayerController : public APlayerController
public: public:
void SetHUDHealth(float Health, float MaxHealth); void SetHUDHealth(float Health, float MaxHealth);
void SetHUDScore(float Score);
virtual void OnPossess(APawn* InPawn) override; virtual void OnPossess(APawn* InPawn) override;
protected: protected:

View File

@ -0,0 +1,53 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterPlayerState.h"
#include "Blaster/Character/BlasterCharacter.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
void ABlasterPlayerState::IncreaseScore(float ScoreAmount)
{
SetScore(GetScore() + ScoreAmount);
Character = GetCharacter();
if (Character)
{
Controller = GetController();
if (Controller)
{
Controller->SetHUDScore(GetScore());
}
}
}
void ABlasterPlayerState::OnRep_Score()
{
Super::OnRep_Score();
Character = GetCharacter();
if (Character)
{
Controller = GetController();
if (Controller)
{
Controller->SetHUDScore(GetScore());
}
}
}
ABlasterCharacter* ABlasterPlayerState::GetCharacter() const
{
return Character == nullptr ? Cast<ABlasterCharacter>(GetPawn()) : Character;
}
ABlasterPlayerController* ABlasterPlayerState::GetController() const
{
if (Character)
{
return Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
}
return nullptr;
}

View File

@ -0,0 +1,26 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerState.h"
#include "BlasterPlayerState.generated.h"
/**
*
*/
UCLASS()
class BLASTER_API ABlasterPlayerState : public APlayerState
{
GENERATED_BODY()
public:
virtual void OnRep_Score() override;
void IncreaseScore(float ScoreAmount);
private:
class ABlasterCharacter* Character;
class ABlasterPlayerController* Controller;
ABlasterCharacter* GetCharacter() const;
ABlasterPlayerController* GetController() const;
};