Update Game Mode for Game End Timer

This commit is contained in:
Philip W 2024-01-09 12:51:55 +00:00
parent 2515ce4e9c
commit 1a3a8ec0d5
17 changed files with 153 additions and 20 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
CorruptedMemory/Content/Levels/Main.umap (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

BIN
CorruptedMemory/Content/UI/M_Outline.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
CorruptedMemory/Content/UI/M_OutlineBase.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -139,6 +139,16 @@ void UCMGameInstance::StartGame()
}
}
void UCMGameInstance::CloseServer(const FString& LobbyID)
{
const auto Request = FHttpModule::Get().CreateRequest();
Request->SetURL(LobbyAPI + "/lobbies/" + LobbyID);
Request->SetVerb("DELETE");
Request->SetHeader("Authorization", "Bearer " + JwtToken);
Request->SetHeader("Accept", "application/json");
Request->ProcessRequest();
}
void UCMGameInstance::Init()
{
Super::Init();

View File

@ -115,6 +115,8 @@ public:
void UnReady();
UFUNCTION(BlueprintCallable, Category = "Lobby")
void StartGame();
UFUNCTION(BlueprintCallable, Category = "Server")
void CloseServer(const FString& LobbyID);
virtual void Init() override;

View File

@ -11,6 +11,12 @@ void ACM_PlayerController::ServerAddGameplayTagToShelf_Implementation(AActor* Sh
Cast<ACorruptedMemoryGameMode>(GetWorld()->GetAuthGameMode())->AddGameplayTagToShelf(Shelving, GameplayTag);
}
void ACM_PlayerController::ServerRemoveGameplayTagToShelf_Implementation(AActor* Shelving, const FString& GameplayTag)
{
if (!GetWorld()->GetAuthGameMode()) return;
Cast<ACorruptedMemoryGameMode>(GetWorld()->GetAuthGameMode())->RemoveGameplayTagToShelf(Shelving, GameplayTag);
}
void ACM_PlayerController::BeginPlay()
{
Super::BeginPlay();

View File

@ -17,6 +17,8 @@ class CORRUPTEDMEMORY_API ACM_PlayerController : public APlayerController
public:
UFUNCTION(Server, Reliable)
void ServerAddGameplayTagToShelf(AActor* Shelving, const FString& GameplayTag);
UFUNCTION(Server, Reliable)
void ServerRemoveGameplayTagToShelf(AActor* Shelving, const FString& GameplayTag);
protected:
virtual void BeginPlay() override;

View File

@ -14,6 +14,18 @@ ACM_Shelving::ACM_Shelving()
bReplicates = true;
}
void ACM_Shelving::GameplayTagAdded()
{
if (GameplayTags.Contains("Corrupted"))
{
Corrupted();
}
if (GameplayTags.Contains("Scanned"))
{
Scanned();
}
}
// Called when the game starts or when spawned
void ACM_Shelving::BeginPlay()
{

View File

@ -15,8 +15,15 @@ public:
// Sets default values for this actor's properties
ACM_Shelving();
UPROPERTY(Replicated, BlueprintReadOnly, VisibleAnywhere)
UPROPERTY(ReplicatedUsing=GameplayTagAdded, BlueprintReadOnly, VisibleAnywhere)
TArray<FString> GameplayTags;
UFUNCTION(BlueprintImplementableEvent)
void Corrupted();
UFUNCTION(BlueprintImplementableEvent)
void Scanned();
UFUNCTION()
void GameplayTagAdded();
protected:
// Called when the game starts or when spawned

View File

@ -40,6 +40,7 @@ ACorruptedMemoryCharacter::ACorruptedMemoryCharacter()
// //Mesh1P->SetRelativeRotation(FRotator(0.9f, -19.19f, 5.2f));
// Mesh1P->SetRelativeLocation(FVector(-30.f, 0.f, -150.f));
bReplicates = true;
PrimaryActorTick.bCanEverTick = true;
}
void ACorruptedMemoryCharacter::BeginPlay()
@ -85,10 +86,17 @@ void ACorruptedMemoryCharacter::SetupPlayerInputComponent(class UInputComponent*
//Scanning
EnhancedInputComponent->BindAction(ScanAction, ETriggerEvent::Started, this, &ACorruptedMemoryCharacter::ScanStart);
EnhancedInputComponent->BindAction(ScanAction, ETriggerEvent::Canceled, this, &ACorruptedMemoryCharacter::ScanCancel);
EnhancedInputComponent->BindAction(ScanAction, ETriggerEvent::Completed, this, &ACorruptedMemoryCharacter::ScanCancel);
}
}
void ACorruptedMemoryCharacter::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
//Get the current scan percentage based on the timer elapsed and scan time
ScanPercentage = GetWorldTimerManager().GetTimerElapsed(ScanTimerHandle) / ScanTime;
}
void ACorruptedMemoryCharacter::LogServerMessage_Implementation(const FString& Message)
{
@ -114,6 +122,16 @@ void ACorruptedMemoryCharacter::Client_GameStart_Implementation()
OnGameStart.Broadcast();
}
void ACorruptedMemoryCharacter::Client_GameEnd_Implementation()
{
OnGameEnd.Broadcast();
FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, [this]()
{
Cast<UCMGameInstance>(GetWorld()->GetGameInstance())->CloseServer(Cast<UCMGameInstance>(GetWorld()->GetGameInstance())->JoinedLobbyID);
}, 10, false);
}
void ACorruptedMemoryCharacter::Move(const FInputActionValue& Value)
{
// input is a Vector2D
@ -154,17 +172,37 @@ void ACorruptedMemoryCharacter::ScanStart(const FInputActionValue& Value)
GetWorld()->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECC_Visibility, TraceParams);
if (!IsValid(HitResult.GetActor())) return;
if (HitResult.GetActor()->Tags.Contains("StoreShelf"))
if (HitResult.GetActor()->Tags.Contains("StoreShelf") && Cast<ACM_Shelving>(HitResult.GetActor())->GameplayTags.Contains("Corrupted"))
{
//LastScannedShelf = Cast<ACM_Shelving>(HitResult.GetActor());
FString AccountName = Cast<UCMGameInstance>(GetWorld()->GetGameInstance())->AccountUsername;
if (Cast<ACM_Shelving>(HitResult.GetActor())->GameplayTags.Contains(AccountName)) return;
Cast<ACM_PlayerController>(GetWorld()->GetFirstPlayerController())->ServerAddGameplayTagToShelf(HitResult.GetActor(), AccountName);
ServerSuccessfulScan(AccountName);
//if (Cast<ACM_Shelving>(HitResult.GetActor())->GameplayTags.Contains(AccountName)) return;
//Cast<ACM_PlayerController>(GetWorld()->GetFirstPlayerController())->ServerAddGameplayTagToShelf(HitResult.GetActor(), AccountName);
if (GetWorldTimerManager().TimerExists(ScanTimerHandle))
{
GetWorldTimerManager().ClearTimer(ScanTimerHandle);
ScanPercentage = 0.f;
}
GetWorld()->GetTimerManager().SetTimer(ScanTimerHandle, [this, AccountName, HitResult]()
{
if (Cast<ACM_Shelving>(HitResult.GetActor())->GameplayTags.Contains("Scanned")) return;
Cast<ACM_PlayerController>(GetWorld()->GetFirstPlayerController())->ServerAddGameplayTagToShelf(HitResult.GetActor(), "Scanned");
ServerSuccessfulScan(AccountName);
LastScannedShelf = nullptr;
}, 3.f, false);
}
}
void ACorruptedMemoryCharacter::ScanCancel(const FInputActionValue& Value)
{
const FString AccountName = Cast<UCMGameInstance>(GetWorld()->GetGameInstance())->AccountUsername;
GetWorldTimerManager().ClearTimer(ScanTimerHandle);
ScanPercentage = 0.f;
// if (IsValid(LastScannedShelf))
// {
// LastScannedShelf = nullptr;
// Cast<ACM_PlayerController>(GetWorld()->GetFirstPlayerController())->ServerRemoveGameplayTagToShelf(LastScannedShelf, AccountName);
// }
}
void ACorruptedMemoryCharacter::SetHasRifle(bool bNewHasRifle)

View File

@ -79,8 +79,8 @@ public:
void ServerPlayerJoin(const FString& PlayerName, const int32& PlayerIndex, const FUniqueNetIdRepl& UNetID);
UFUNCTION(Server, Reliable)
void ServerSuccessfulScan(const FString& Username);
UFUNCTION(Client, Reliable)
void LogServerMessage(const FString& Message);
@ -91,6 +91,11 @@ public:
UFUNCTION(Client, Reliable)
void Client_GameStart();
UFUNCTION(Client, Reliable)
void Client_GameEnd();
UPROPERTY(BlueprintReadOnly)
float ScanPercentage = 0.f;
protected:
/** Called for movement input */
@ -106,14 +111,22 @@ protected:
// APawn interface
virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;
// End of APawn interface
UPROPERTY()
FTimerHandle ScanTimerHandle;
ACM_Shelving* LastScannedShelf = nullptr;
public:
/** Returns Mesh1P subobject **/
// USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; }
/** Returns FirstPersonCameraComponent subobject **/
UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; }
virtual void Tick(float DeltaSeconds) override;
UPROPERTY(EditDefaultsOnly)
float ScanTime = 3.f;
};
inline void ACorruptedMemoryCharacter::ServerSuccessfulScan_Implementation(const FString& Username)
{
if (const ACorruptedMemoryGameMode* GameMode = Cast<ACorruptedMemoryGameMode>(GetWorld()->GetAuthGameMode()))

View File

@ -32,17 +32,33 @@ void ACorruptedMemoryGameMode::PostLogin(APlayerController* NewPlayer)
FParse::Value(FCommandLine::Get(), TEXT("maxPlayers="), MaxPlayers);
if (GetWorld()->GetGameState()->PlayerArray.Num() == FCString::Atoi(*MaxPlayers))
{
auto World = GetWorld();
FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, [World]()
GetWorld()->GetTimerManager().SetTimer(TimerHandle, [this]()
{
for (const auto PlayerState : World->GetGameState()->PlayerArray)
for (const auto PlayerState : GetWorld()->GetGameState()->PlayerArray)
{
Cast<ACorruptedMemoryCharacter>(PlayerState->GetPlayerController()->GetCharacter())->Client_GameStart();
Cast<ACorruptedMemoryCharacter>(PlayerState->GetPlayerController()->GetCharacter())->Client_EnableUserInput();
PlayerState->GetPlayerController()->EnableInput(PlayerState->GetPlayerController());
PlayerState->GetPlayerController()->SetInputMode(FInputModeGameOnly());
}
FTimerHandle TimerHandle2;
GetWorld()->GetTimerManager().SetTimer(TimerHandle2, [this]()
{
for (const auto PlayerState : GetWorld()->GetGameState()->PlayerArray)
{
Cast<ACorruptedMemoryCharacter>(PlayerState->GetPlayerController()->GetCharacter())->Client_GameEnd();
Cast<ACorruptedMemoryCharacter>(PlayerState->GetPlayerController()->GetCharacter())->Client_DisableUserInput();
PlayerState->GetPlayerController()->DisableInput(PlayerState->GetPlayerController());
PlayerState->GetPlayerController()->SetInputMode(FInputModeUIOnly());
}
FTimerHandle TimerHandle3;
GetWorld()->GetTimerManager().SetTimer(TimerHandle3, [this]()
{
FGenericPlatformMisc::RequestExit(false);
}, 10, false);
}, 125, false);
}, 5.f, false);
}
}
@ -60,6 +76,12 @@ void ACorruptedMemoryGameMode::AddGameplayTagToShelf(AActor* Shelving, const FSt
(*FoundShelf)->GameplayTags.Add(GameplayTag);
}
void ACorruptedMemoryGameMode::RemoveGameplayTagToShelf(AActor* Shelving, const FString& GameplayTag)
{
ACM_Shelving** FoundShelf = Shelves.FindByPredicate([Shelving](const ACM_Shelving* Shelf) { return Shelf == Shelving; });
(*FoundShelf)->GameplayTags.Remove(GameplayTag);
}
void ACorruptedMemoryGameMode::BeginPlay()
{
Super::BeginPlay();
@ -77,14 +99,28 @@ void ACorruptedMemoryGameMode::BeginPlay()
SpawnPoints.Add(PlayerStartActor->GetActorTransform());
}
//Gets all store shelves in level
TArray<AActor*> StoreShelves;
UGameplayStatics::GetAllActorsWithTag(GetWorld(), "StoreShelf", StoreShelves);
for (AActor* StoreShelf : StoreShelves)
{
Shelves.Add(Cast<ACM_Shelving>(StoreShelf));
}
Shelves[0]->GameplayTags.Add("Corrupted");
constexpr int StartCorruptedAmount = 35;
for (int i = 0; i < StartCorruptedAmount; i++)
{
ACM_Shelving* ShelfToCorrupt = Shelves[FMath::RandRange(0, Shelves.Num() - 1)];
ShelfToCorrupt->GameplayTags.Add("Corrupted");
}
FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, [this]()
{
ACM_Shelving* ShelfToCorrupt = Shelves[FMath::RandRange(0, Shelves.Num() - 1)];
if (ShelfToCorrupt->GameplayTags.Contains("Corrupted")) return;
if (ShelfToCorrupt->GameplayTags.Contains("Complete")) return;
ShelfToCorrupt->GameplayTags.Add("Corrupted");
}, 10.f, true);
}
void ACorruptedMemoryGameMode::ServerSpawnPlayer_Implementation(APlayerController* PlayerController)

View File

@ -26,6 +26,7 @@ public:
void SuccessfulScan(const FString& Username) const;
void AddGameplayTagToShelf(AActor* Shelving, const FString& GameplayTag);
void RemoveGameplayTagToShelf(AActor* Shelving, const FString& GameplayTag);
protected:
virtual void BeginPlay() override;