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.

166 lines
4.9 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.25f);
9 months ago
UStatsSubsystem* StatsSystem = GetGameInstance()->GetSubsystem<UStatsSubsystem>();
if (StatsSystem->CurrentWave == 1)
{
StatsSystem->CurrentHP = StatsSystem->MaxHP;
}
}
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;
PCUnit->ApplyStats();
if (StatsSystem->CurrentWave == 1)
{
StatsSystem->CurrentHP = StatsSystem->MaxHP;
}
9 months ago
Cast<AAIController>(PCUnit->GetController())->MoveToLocation(LevelSetup.RunupPoint);
FTimerHandle RunupHandle;
World->GetTimerManager().SetTimer(
RunupHandle,
this,
&ASPGameState::SpawnEnemies,
2.0f);
}
void ASPGameState::DecrementEnemies()
{
AliveEnemies--;
if (AliveEnemies == 0)
{
ISaveable::Execute_SaveGame(Saveable);
AliveEnemies = -1;
PrimaryActorTick.bCanEverTick = false;
UStatsSubsystem* StatsSystem = GetGameInstance()->GetSubsystem<UStatsSubsystem>();
NextLevelName = "BattleMap";
StatsSystem->HighestWave = FMath::Max(StatsSystem->CurrentWave, StatsSystem->HighestWave);
StatsSystem->CurrentWave++;
StatsSystem->CurrentHP = Cast<AUnit>(PlayerCharacter)->HP;
FTimerHandle RunupHandle;
GetWorld()->GetTimerManager().SetTimer(
RunupHandle,
this,
&ASPGameState::TransitionLevel,
0.75f);
}
}
void ASPGameState::PlayerDead()
{
ISaveable::Execute_SaveGame(Saveable);
UStatsSubsystem* StatsSystem = GetGameInstance()->GetSubsystem<UStatsSubsystem>();
NextLevelName = "BattleMap";
StatsSystem->CurrentWave = 1;
FTimerHandle RunupHandle;
GetWorld()->GetTimerManager().SetTimer(
RunupHandle,
this,
&ASPGameState::TransitionLevel,
2.0f);
9 months ago
}
void ASPGameState::TransitionLevel()
{
UGameplayStatics::GetPlayerCameraManager(GetWorld(), 0)->StartCameraFade(0, 1.0f, .5f, FLinearColor::Black, false, true);
GetWorld()->GetTimerManager().SetTimer(
FadeHandle,
this,
&ASPGameState::LoadNextLevel,
0.5f);
9 months ago
}
void ASPGameState::SpawnEnemies()
{
int32 TotalEnemies = 0;
UWorld* World = GetWorld();
URosterSubsystem* RosterSubsystem = GetGameInstance()->GetSubsystem<URosterSubsystem>();
int32 CurrentWave = GetGameInstance()->GetSubsystem<UStatsSubsystem>()->CurrentWave;
if(CurrentWave % 10 == 0)
9 months ago
{
FActorSpawnParameters SpawnParams;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
UStatsSubsystem* StatsSubsystem = GetGameInstance()->GetSubsystem<UStatsSubsystem>();
if(CurrentWave == 10)
{
AActor* NewActor = World->SpawnActor<ACharacter>(StatsSubsystem->Bosses[0], LevelSetup.EnemySpawnPoints[0], FRotator::ZeroRotator, SpawnParams);
NewActor->Tags.Add("Enemy");
AliveEnemies = 1;
return;
}
} else
{
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++;
}
9 months ago
}
AliveEnemies = TotalEnemies;
}
void ASPGameState::FadeCamera()
{
UGameplayStatics::GetPlayerCameraManager(GetWorld(), 0)->StartCameraFade(1.0f, 0, .5f, FLinearColor::Black);
}
void ASPGameState::LoadNextLevel()
{
UGameplayStatics::OpenLevel(GetWorld(), NextLevelName);
}