blaster/Source/Blaster/PlayerController/BlasterPlayerController.cpp

187 lines
5.2 KiB
C++
Raw Normal View History

2022-05-05 15:57:57 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterPlayerController.h"
2022-05-08 19:16:19 +00:00
#include "Blaster/Character/BlasterCharacter.h"
2022-05-07 10:03:33 +00:00
#include "Blaster/HUD/BlasterHUD.h"
#include "Blaster/HUD/CharacterOverlay.h"
#include "Components/ProgressBar.h"
#include "Components/TextBlock.h"
void ABlasterPlayerController::BeginPlay()
{
Super::BeginPlay();
BlasterHUD = Cast<ABlasterHUD>(GetHUD());
}
2022-05-10 08:40:23 +00:00
void ABlasterPlayerController::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
SetHUDTime();
2022-05-10 09:14:54 +00:00
CheckTimeSync(DeltaSeconds);
2022-05-10 08:40:23 +00:00
}
2022-05-08 19:16:19 +00:00
void ABlasterPlayerController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
if (const ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(InPawn))
{
SetHUDHealth(BlasterCharacter->GetHealth(), BlasterCharacter->GetMaxHealth());
}
}
2022-05-10 09:14:54 +00:00
void ABlasterPlayerController::ReceivedPlayer()
{
Super::ReceivedPlayer();
if (IsLocalController())
{
ServerRequestServerTime(GetWorld()->GetTimeSeconds());
}
}
void ABlasterPlayerController::CheckTimeSync(float DeltaSeconds)
{
TimeSyncRunningTime += DeltaSeconds;
if (IsLocalController() && TimeSyncRunningTime > TimeSyncFrequency)
{
ServerRequestServerTime(GetWorld()->GetTimeSeconds());
TimeSyncRunningTime = 0.f;
}
}
float ABlasterPlayerController::GetServerTime()
{
if (HasAuthority()) return GetWorld()->GetTimeSeconds();
return GetWorld()->GetTimeSeconds() + ClientServerDelta;
}
void ABlasterPlayerController::ServerRequestServerTime_Implementation(float TimeOfClientRequest)
{
const float ServerTimeOfReceipt = GetWorld()->GetTimeSeconds();
ClientReportServerTime(TimeOfClientRequest, ServerTimeOfReceipt);
}
void ABlasterPlayerController::ClientReportServerTime_Implementation(float TimeOfClientRequest, float TimeServerReceivedClientRequest)
{
const float RoundTripTime = GetWorld()->GetTimeSeconds() - TimeOfClientRequest;
const float CurrentServerTime = TimeServerReceivedClientRequest + 0.5f * RoundTripTime;
ClientServerDelta = CurrentServerTime - GetWorld()->GetTimeSeconds();
}
2022-05-10 08:40:23 +00:00
void ABlasterPlayerController::SetHUDTime()
{
2022-05-10 09:14:54 +00:00
const uint32 SecondsLeft = FMath::CeilToInt(MatchTime - GetServerTime());
2022-05-10 08:40:23 +00:00
if (CountdownInt != SecondsLeft)
{
2022-05-10 09:14:54 +00:00
SetHUDMatchCountdown(MatchTime - GetServerTime());
2022-05-10 08:40:23 +00:00
}
2022-05-10 09:14:54 +00:00
2022-05-10 08:40:23 +00:00
CountdownInt = SecondsLeft;
}
2022-05-07 10:03:33 +00:00
void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->HealthBar &&
BlasterHUD->CharacterOverlay->HealthText;
2022-05-10 09:14:54 +00:00
2022-05-07 10:03:33 +00:00
if (bHUDValid)
{
const float HealthPercent = Health / MaxHealth;
BlasterHUD->CharacterOverlay->HealthBar->SetPercent(HealthPercent);
const FString HealthText = FString::Printf(TEXT("%d/%d"), FMath::CeilToInt(Health), FMath::CeilToInt(MaxHealth));
BlasterHUD->CharacterOverlay->HealthText->SetText(FText::FromString(HealthText));
}
}
2022-05-09 14:33:48 +00:00
void ABlasterPlayerController::SetHUDScore(float Score)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->ScoreValue;
if (bHUDValid)
{
2022-05-09 15:16:41 +00:00
const FString ScoreAmount = FString::Printf(TEXT("%d"), FMath::FloorToInt(Score));
BlasterHUD->CharacterOverlay->ScoreValue->SetText(FText::FromString(ScoreAmount));
}
}
void ABlasterPlayerController::SetHUDDefeats(int32 Defeats)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->DefeatsValue;
if (bHUDValid)
{
const FString DefeatsAmount = FString::Printf(TEXT("%d"), Defeats);
BlasterHUD->CharacterOverlay->DefeatsValue->SetText(FText::FromString(DefeatsAmount));
2022-05-09 14:33:48 +00:00
}
}
2022-05-09 16:39:41 +00:00
void ABlasterPlayerController::SetHUDWeaponAmmo(int32 Ammo)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->WeaponAmmoValue;
if (bHUDValid)
{
const FString WeaponAmmoAmount = FString::Printf(TEXT("%d"), Ammo);
BlasterHUD->CharacterOverlay->WeaponAmmoValue->SetText(FText::FromString(WeaponAmmoAmount));
}
}
2022-05-09 17:35:28 +00:00
void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->CarriedAmmoValue;
if (bHUDValid)
{
const FString CarriedAmmoAmount = FString::Printf(TEXT("%d"), Ammo);
BlasterHUD->CharacterOverlay->CarriedAmmoValue->SetText(FText::FromString(CarriedAmmoAmount));
}
}
2022-05-10 08:40:23 +00:00
void ABlasterPlayerController::SetHUDMatchCountdown(float CountdownTime)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->MatchCountdownText;
if (bHUDValid)
{
int32 Minutes = FMath::FloorToInt(CountdownTime / 60);
int32 Seconds = CountdownTime - Minutes * 60;
2022-05-10 09:14:54 +00:00
2022-05-10 08:40:23 +00:00
const FString CountdownText = FString::Printf(TEXT("%02d:%02d"), Minutes, Seconds);
BlasterHUD->CharacterOverlay->MatchCountdownText->SetText(FText::FromString(CountdownText));
}
}