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.
165 lines
4.9 KiB
165 lines
4.9 KiB
// 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);
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void ASPGameState::SpawnEnemies()
|
|
{
|
|
int32 TotalEnemies = 0;
|
|
UWorld* World = GetWorld();
|
|
URosterSubsystem* RosterSubsystem = GetGameInstance()->GetSubsystem<URosterSubsystem>();
|
|
int32 CurrentWave = GetGameInstance()->GetSubsystem<UStatsSubsystem>()->CurrentWave;
|
|
|
|
if(CurrentWave % 10 == 0)
|
|
{
|
|
FActorSpawnParameters SpawnParams;
|
|
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
|
|
UStatsSubsystem* StatsSubsystem = GetGameInstance()->GetSubsystem<UStatsSubsystem>();
|
|
AActor* NewActor = World->SpawnActor<ACharacter>(StatsSubsystem->Bosses[CurrentWave / 10 - 1], LevelSetup.EnemySpawnPoints[1], FRotator::ZeroRotator, SpawnParams);
|
|
|
|
if(NewActor)
|
|
{
|
|
NewActor->Tags.Add("Enemy");
|
|
AliveEnemies = 1;
|
|
}
|
|
return;
|
|
}
|
|
|
|
int32 TargetChallenge = FMath::FloorToInt((CurrentWave % 10) * 1.25f);
|
|
while(TargetChallenge > 0)
|
|
{
|
|
const URosterEntry* NewEnemy = RosterSubsystem->GetEnemy(
|
|
FMath::DivideAndRoundDown(CurrentWave, 10), 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);
|
|
}
|