diff --git a/Content/Blueprints/GameModes/BP_BlasterGameMode.uasset b/Content/Blueprints/GameModes/BP_BlasterGameMode.uasset index 6c0fa37..4731c5a 100644 Binary files a/Content/Blueprints/GameModes/BP_BlasterGameMode.uasset and b/Content/Blueprints/GameModes/BP_BlasterGameMode.uasset differ diff --git a/Content/Blueprints/HUD/WBP_CharacterOverlay.uasset b/Content/Blueprints/HUD/WBP_CharacterOverlay.uasset index 89c2c16..1d7f645 100644 Binary files a/Content/Blueprints/HUD/WBP_CharacterOverlay.uasset and b/Content/Blueprints/HUD/WBP_CharacterOverlay.uasset differ diff --git a/Content/Blueprints/PlayerState/BP_BlasterPlayerState.uasset b/Content/Blueprints/PlayerState/BP_BlasterPlayerState.uasset new file mode 100644 index 0000000..b556491 Binary files /dev/null and b/Content/Blueprints/PlayerState/BP_BlasterPlayerState.uasset differ diff --git a/Source/Blaster/Character/BlasterCharacter.cpp b/Source/Blaster/Character/BlasterCharacter.cpp index 493f295..2011136 100644 --- a/Source/Blaster/Character/BlasterCharacter.cpp +++ b/Source/Blaster/Character/BlasterCharacter.cpp @@ -7,6 +7,7 @@ #include "Blaster/Components/CombatComponent.h" #include "Blaster/GameMode/BlasterGameMode.h" #include "Blaster/PlayerController/BlasterPlayerController.h" +#include "Blaster/PlayerState/BlasterPlayerState.h" #include "Blaster/Weapon/Weapon.h" #include "Camera/CameraComponent.h" #include "Components/CapsuleComponent.h" @@ -238,6 +239,20 @@ void ABlasterCharacter::Tick(float DeltaTime) } HideCameraIfCharacterClose(); + PollInit(); +} + +void ABlasterCharacter::PollInit() +{ + if (PlayerState == nullptr) + { + PlayerState = GetPlayerState(); + if (PlayerState) + { + // Initialize Score now we have the PlayerState + PlayerState->IncreaseScore(0.f); + } + } } void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) diff --git a/Source/Blaster/Character/BlasterCharacter.h b/Source/Blaster/Character/BlasterCharacter.h index 9ef8b30..fdf116b 100644 --- a/Source/Blaster/Character/BlasterCharacter.h +++ b/Source/Blaster/Character/BlasterCharacter.h @@ -53,8 +53,12 @@ protected: UFUNCTION() void ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* InstigatorController, AActor* DamageCauser); void UpdateHUDHealth(); + // Poll for any relevant classes and initialize them + void PollInit(); private: + class ABlasterPlayerState* PlayerState; + UPROPERTY(VisibleAnywhere, Category="Camera") class USpringArmComponent* CameraBoom; diff --git a/Source/Blaster/GameMode/BlasterGameMode.cpp b/Source/Blaster/GameMode/BlasterGameMode.cpp index 0659984..0f719e1 100644 --- a/Source/Blaster/GameMode/BlasterGameMode.cpp +++ b/Source/Blaster/GameMode/BlasterGameMode.cpp @@ -4,12 +4,22 @@ #include "BlasterGameMode.h" #include "Blaster/Character/BlasterCharacter.h" +#include "Blaster/PlayerController/BlasterPlayerController.h" +#include "Blaster/PlayerState/BlasterPlayerState.h" #include "GameFramework/PlayerStart.h" #include "Kismet/GameplayStatics.h" void ABlasterGameMode::PlayerEliminated(ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController, ABlasterPlayerController* AttackerController) { + ABlasterPlayerState* AttackerPlayerState = AttackerController ? Cast(AttackerController->PlayerState) : nullptr; + ABlasterPlayerState* VictimPlayerState = VictimController ? Cast(VictimController->PlayerState) : nullptr; + + if (AttackerPlayerState && AttackerPlayerState != VictimPlayerState) + { + AttackerPlayerState->IncreaseScore(1.f); + } + if (EliminatedCharacter) { EliminatedCharacter->Eliminated(); diff --git a/Source/Blaster/HUD/CharacterOverlay.h b/Source/Blaster/HUD/CharacterOverlay.h index f7f517f..61544a7 100644 --- a/Source/Blaster/HUD/CharacterOverlay.h +++ b/Source/Blaster/HUD/CharacterOverlay.h @@ -22,4 +22,7 @@ public: UPROPERTY(meta = (BindWidget)) class UTextBlock* HealthText; + UPROPERTY(meta = (BindWidget)) + UTextBlock* ScoreValue; + }; diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.cpp b/Source/Blaster/PlayerController/BlasterPlayerController.cpp index f4580bf..fe7aeab 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.cpp +++ b/Source/Blaster/PlayerController/BlasterPlayerController.cpp @@ -44,3 +44,18 @@ void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth) BlasterHUD->CharacterOverlay->HealthText->SetText(FText::FromString(HealthText)); } } + +void ABlasterPlayerController::SetHUDScore(float Score) +{ + BlasterHUD = BlasterHUD == nullptr ? Cast(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)); + } +} diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.h b/Source/Blaster/PlayerController/BlasterPlayerController.h index 37a51f9..0ab21fa 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.h +++ b/Source/Blaster/PlayerController/BlasterPlayerController.h @@ -17,6 +17,7 @@ class BLASTER_API ABlasterPlayerController : public APlayerController public: void SetHUDHealth(float Health, float MaxHealth); + void SetHUDScore(float Score); virtual void OnPossess(APawn* InPawn) override; protected: diff --git a/Source/Blaster/PlayerState/BlasterPlayerState.cpp b/Source/Blaster/PlayerState/BlasterPlayerState.cpp new file mode 100644 index 0000000..2622399 --- /dev/null +++ b/Source/Blaster/PlayerState/BlasterPlayerState.cpp @@ -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(GetPawn()) : Character; +} + +ABlasterPlayerController* ABlasterPlayerState::GetController() const +{ + if (Character) + { + return Controller == nullptr ? Cast(Character->Controller) : Controller; + } + + return nullptr; +} + diff --git a/Source/Blaster/PlayerState/BlasterPlayerState.h b/Source/Blaster/PlayerState/BlasterPlayerState.h new file mode 100644 index 0000000..ae99c8b --- /dev/null +++ b/Source/Blaster/PlayerState/BlasterPlayerState.h @@ -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; +};