// Fill out your copyright notice in the Description page of Project Settings. #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" namespace MatchState { const FName Cooldown = FName("Cooldown"); } ABlasterGameMode::ABlasterGameMode() { bDelayedStart = true; } void ABlasterGameMode::BeginPlay() { Super::BeginPlay(); LevelStartingTime = GetWorld()->GetTimeSeconds(); } void ABlasterGameMode::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); if (MatchState == MatchState::WaitingToStart) { CountDownTime = WarmupTime - GetWorld()->GetTimeSeconds() + LevelStartingTime; if (CountDownTime <= 0.f) { StartMatch(); } } else if (MatchState == MatchState::InProgress) { CountDownTime = WarmupTime + MatchTime - GetWorld()->GetTimeSeconds() + LevelStartingTime; if (CountDownTime <= 0.f) { SetMatchState(MatchState::Cooldown); } } else if (MatchState == MatchState::Cooldown) { CountDownTime = CooldownTime + WarmupTime + MatchTime - GetWorld()->GetTimeSeconds() + LevelStartingTime; } } void ABlasterGameMode::OnMatchStateSet() { Super::OnMatchStateSet(); for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator) { ABlasterPlayerController* PlayerController = Cast(*Iterator); if (PlayerController) { PlayerController->OnMatchStateSet(MatchState); } } } 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 (VictimPlayerState) { VictimPlayerState->IncreaseDefeats(1); } if (EliminatedCharacter) { EliminatedCharacter->Eliminated(); } } void ABlasterGameMode::RequestRespawn(ACharacter* EliminatedCharacter, AController* EliminatedController) { if (EliminatedCharacter) { EliminatedCharacter->Reset(); EliminatedCharacter->Destroy(); } if (EliminatedController) { TArray PlayerStarts; UGameplayStatics::GetAllActorsOfClass(this, APlayerStart::StaticClass(), PlayerStarts); const int32 Selection = FMath::RandRange(0, PlayerStarts.Num() - 1); RestartPlayerAtPlayerStart(EliminatedController, PlayerStarts[Selection]); } }