// Fill out your copyright notice in the Description page of Project Settings. #include "SPGameState.h" #include "AIController.h" #include "RosterSubsystem.h" #include "StatsSubsystem.h" #include "GameFramework/Character.h" #include "Kismet/GameplayStatics.h" void ASPGameState::BeginPlay() { UGameplayStatics::GetPlayerCameraManager(GetWorld(),0)->SetManualCameraFade(1.0, FLinearColor::Black, false); GetWorld()->GetTimerManager().SetTimer( FadeHandle, this, &ASPGameState::FadeCamera, 0.5f); UStatsSubsystem* StatsSystem = GetGameInstance()->GetSubsystem(); if (StatsSystem->CurrentWave == 1) { StatsSystem->CurrentHP = StatsSystem->MaxHP; } } void ASPGameState::Tick(float DeltaSeconds) { if(AliveEnemies != -1) { if (AliveEnemies == 0 || Cast(PlayerCharacter)->IsDead()) { ISaveable::Execute_SaveGame(Saveable); AliveEnemies = -1; PrimaryActorTick.bCanEverTick = false; UStatsSubsystem* StatsSystem = GetGameInstance()->GetSubsystem(); NextLevelName = "BattleMap"; // All enemies dead if(!Cast(PlayerCharacter)->IsDead()) { StatsSystem->HighestWave = FMath::Max(StatsSystem->CurrentWave, StatsSystem->HighestWave); StatsSystem->CurrentWave++; StatsSystem->CurrentHP = Cast(PlayerCharacter)->HP; } else { StatsSystem->CurrentWave = 1; } FTimerHandle RunupHandle; GetWorld()->GetTimerManager().SetTimer( RunupHandle, this, &ASPGameState::TransitionLevel, 2.0f); } } } ASPGameState::ASPGameState() { PrimaryActorTick.bCanEverTick = true; } void ASPGameState::StartLevel(FLevelSetup InLevelSetup) { LevelSetup = InLevelSetup; UWorld* World = GetWorld(); if(!World) { UE_LOG(LogTemp, Error, TEXT("StartLevel was unable to get a handle via GetWorld")); return; } ISaveable::Execute_SaveGame(Saveable); PlayerCharacter = World->SpawnActor(LevelSetup.PlayerCharacterClass, LevelSetup.PlayerSpawnPoint, FRotator::ZeroRotator); UStatsSubsystem* StatsSystem = GetGameInstance()->GetSubsystem(); AUnit* PCUnit = Cast(PlayerCharacter); PCUnit->MaxHP = StatsSystem->MaxHP; StatsSystem->CurrentHP += StatsSystem->RecoveryPerTurn; StatsSystem->CurrentHP = FMath::Min(StatsSystem->CurrentHP, StatsSystem->MaxHP); PCUnit->HP = StatsSystem->CurrentHP; PCUnit->Attack = StatsSystem->AttackPower; PCUnit->Defense = StatsSystem->DefensePower; PCUnit->Cooldown = 1 / StatsSystem->AttackSpeed; PCUnit->CooldownTimer = PCUnit->Cooldown; PCUnit->Dexterity = StatsSystem->Dexterity; Cast(PCUnit->GetController())->MoveToLocation(LevelSetup.RunupPoint); FTimerHandle RunupHandle; World->GetTimerManager().SetTimer( RunupHandle, this, &ASPGameState::SpawnEnemies, 2.0f); } void ASPGameState::DecrementEnemies() { AliveEnemies--; } void ASPGameState::TransitionLevel() { UGameplayStatics::GetPlayerCameraManager(GetWorld(), 0)->StartCameraFade(0, 1.0f, .5f, FLinearColor::Black, false, true); GetWorld()->GetTimerManager().SetTimer( FadeHandle, this, &ASPGameState::LoadNextLevel, 1); } void ASPGameState::SpawnEnemies() { int32 TotalEnemies = 0; UWorld* World = GetWorld(); URosterSubsystem* RosterSubsystem = GetGameInstance()->GetSubsystem(); int32 CurrentWave = GetGameInstance()->GetSubsystem()->CurrentWave; int32 TargetChallenge = FMath::FloorToInt(CurrentWave * 1.25f); while(TargetChallenge > 0) { const URosterEntry* NewEnemy = RosterSubsystem->GetEnemy(TargetChallenge); FVector SpawnPoint = LevelSetup.EnemySpawnPoints[FMath::RandRange(0, LevelSetup.EnemySpawnPoints.Num() -1)]; FActorSpawnParameters SpawnParams; SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn; AActor* NewActor = World->SpawnActor(NewEnemy->EnemyUnit.Get(), SpawnPoint, FRotator::ZeroRotator, SpawnParams); NewActor->Tags.Add("Enemy"); TargetChallenge -= NewEnemy->DifficultyRating; TotalEnemies++; } AliveEnemies = TotalEnemies; } void ASPGameState::FadeCamera() { UGameplayStatics::GetPlayerCameraManager(GetWorld(), 0)->StartCameraFade(1.0f, 0, .5f, FLinearColor::Black); } void ASPGameState::LoadNextLevel() { UGameplayStatics::OpenLevel(GetWorld(), NextLevelName); }