You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.3 KiB

9 months ago
// 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<UStatsSubsystem>();
if (StatsSystem->CurrentWave == 1)
{
StatsSystem->CurrentHP = StatsSystem->MaxHP;
}
}
void ASPGameState::Tick(float DeltaSeconds)
{
if(AliveEnemies != -1)
{
if (AliveEnemies == 0 || Cast<AUnit>(PlayerCharacter)->IsDead())
{
ISaveable::Execute_SaveGame(Saveable);
AliveEnemies = -1;
PrimaryActorTick.bCanEverTick = false;
UStatsSubsystem* StatsSystem = GetGameInstance()->GetSubsystem<UStatsSubsystem>();
NextLevelName = "BattleMap";
// All enemies dead
if(!Cast<AUnit>(PlayerCharacter)->IsDead())
{
StatsSystem->HighestWave = FMath::Max(StatsSystem->CurrentWave, StatsSystem->HighestWave);
StatsSystem->CurrentWave++;
StatsSystem->CurrentHP = Cast<AUnit>(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<ACharacter>(LevelSetup.PlayerCharacterClass, LevelSetup.PlayerSpawnPoint, FRotator::ZeroRotator);
UStatsSubsystem* StatsSystem = GetGameInstance()->GetSubsystem<UStatsSubsystem>();
AUnit* PCUnit = Cast<AUnit>(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<AAIController>(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<URosterSubsystem>();
int32 CurrentWave = GetGameInstance()->GetSubsystem<UStatsSubsystem>()->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<ACharacter>(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);
}