Created
October 3, 2018 13:51
-
-
Save LeviVisser/4c5643f67424c71ce1a53b910d489546 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ Script made by Levi Visser | |
#include "BaseGameInstance.h" | |
// Singleton-like accessor. I only use this as a workaround for PIE | |
// The GameInstance lifecycle is different in PIE | |
// In proper builds, this wouldn't be necessary | |
ULevelGenerationManager* UBaseGameInstance::LevelGenerationManager() | |
{ | |
return | |
IsValid(LevelGenerationManagerInstance) ? | |
LevelGenerationManagerInstance : | |
LevelGenerationManagerInstance = NewObject<ULevelGenerationManager>( | |
this, | |
FName("LevelGenerationManager")); | |
} | |
UUnlockManager* UBaseGameInstance::UnlockManager() | |
{ | |
return | |
IsValid(UnlockManagerInstance) ? | |
UnlockManagerInstance : | |
UnlockManagerInstance = NewObject<UUnlockManager>( | |
this, | |
FName("UnlockManager")); | |
} | |
UMyScoreManager* UBaseGameInstance::ScoreManager() | |
{ | |
return | |
IsValid(ScoreManagerInstance) ? | |
ScoreManagerInstance : | |
ScoreManagerInstance = NewObject<UMyScoreManager>( | |
this, | |
FName("ScoreManager")); | |
} | |
// Cleanup. This acts as a quasi-destructor | |
void UBaseGameInstance::Shutdown() | |
{ | |
// PIE may cause this to be invalid. During the game, it will always be valid | |
if (IsValid(UnlockManagerInstance)) | |
{ | |
UnlockManagerInstance->Worlds.Empty(); | |
UnlockManagerInstance->UnlockedWorlds.Empty(); | |
} | |
} | |
/// | |
/// BeginPlay method from the game mode, To show how the singleton like getters are used | |
/// | |
void AEndlesRunnerGameMode::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
BaseGameInstance = (UBaseGameInstance*)GetWorld()->GetGameInstance(); | |
_playerController = (ABasePlayerController*)UGameplayStatics::GetPlayerController(GetWorld(), 0); | |
if (BaseGameInstance) | |
{ | |
if (UGameplayStatics::GetPlatformName() == "Windows" || UGameplayStatics::GetPlatformName() == "Mac") | |
{ | |
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Some debug message!")); | |
BaseGameInstance->UnlockManager()->Init(GetWorld(), BaseGameInstance->Worlds); | |
SetCurrentWorldData(BaseGameInstance->UnlockManager()->GetWorldFromName(ELevelNames::VE_SIFI)); | |
BaseGameInstance->LevelGenerationManager()->Init(GetCurrentWorldData(), GetWorld()); | |
} | |
if (UGameplayStatics::GetPlatformName() == "IOS") | |
{ | |
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Some debug message!")); | |
UnlockManager = UUnlockManager::Instance(); | |
ScoreManager = UMyScoreManager::Instance(); | |
LevelGenerationManager = ULevelGenerationManager::Instance(); | |
UnlockManager->Init(GetWorld(), BaseGameInstance->Worlds); | |
SetCurrentWorldData(UnlockManager->GetWorldFromName(ELevelNames::VE_SIFI)); | |
LevelGenerationManager->Init(GetCurrentWorldData(), GetWorld()); | |
} | |
} | |
} | |
//Script made by Levi Visser | |
#include "LevelGenerationManager.h" | |
#include "Math/UnrealMathUtility.h" | |
#include "Engine/StaticMeshActor.h" | |
ULevelGenerationManager* ULevelGenerationManager::instance; | |
ULevelGenerationManager* ULevelGenerationManager::Instance() | |
{ | |
if (!instance) | |
{ | |
instance = NewObject<ULevelGenerationManager>(); | |
} | |
return instance; | |
} | |
void ULevelGenerationManager::Init(FWorldData _currentWorld, UWorld* _world) | |
{ | |
CleanUp(); | |
SetWorld(_world); | |
SetCurrentGameWorld(_currentWorld); | |
AddTilesToCorrespondingArray(); | |
} | |
void ULevelGenerationManager::CleanUp() | |
{ | |
InnerCityTiles.Empty(); | |
InDoorTiles.Empty(); | |
NatureTiles.Empty(); | |
OuterCityTiles.Empty(); | |
} | |
void ULevelGenerationManager::SetNewZone() | |
{ | |
int _rand = FMath::RandRange(1, 4); | |
previousZone = currentZone; | |
switch (_rand) | |
{ | |
case 1: | |
if (previousZone == EZones::VE_INNERCITY) | |
{ | |
SetNewZone(); | |
} | |
else | |
{ | |
currentZone = EZones::VE_INNERCITY; | |
zoneTilesToSpawn = FMath::RandRange(3, 6); | |
} | |
break; | |
case 2: | |
if (previousZone == EZones::VE_OUTERCITY) | |
{ | |
SetNewZone(); | |
} | |
else | |
{ | |
currentZone = EZones::VE_OUTERCITY; | |
zoneTilesToSpawn = FMath::RandRange(3, 6); | |
} | |
break; | |
case 3: | |
if (previousZone == EZones::VE_NATURE) | |
{ | |
SetNewZone(); | |
} | |
else { | |
currentZone = EZones::VE_NATURE; | |
zoneTilesToSpawn = FMath::RandRange(3, 6); | |
} | |
break; | |
case 4: | |
if (previousZone == EZones::VE_INDOOR) | |
{ | |
SetNewZone(); | |
} | |
else { | |
currentZone = EZones::VE_INDOOR; | |
zoneTilesToSpawn = 1; | |
} | |
break; | |
default: | |
break; | |
} | |
zoneTilesSpawned = 0; | |
} | |
TSubclassOf <AFloorTile> ULevelGenerationManager::GetTileToSpawn() | |
{ | |
int arrayLenght; | |
int value; | |
if (zoneTilesSpawned >= zoneTilesToSpawn) | |
{ | |
SetNewZone(); | |
} | |
switch (currentZone) | |
{ | |
case EZones::VE_INNERCITY: | |
arrayLenght = InnerCityTiles.Num(); | |
value = FMath::RandRange(1, arrayLenght); | |
return InnerCityTiles[value - 1]; | |
case EZones::VE_OUTERCITY: | |
arrayLenght = OuterCityTiles.Num(); | |
value = FMath::RandRange(1, arrayLenght); | |
return OuterCityTiles[value - 1]; | |
case EZones::VE_NATURE: | |
arrayLenght = NatureTiles.Num(); | |
value = FMath::RandRange(1, arrayLenght); | |
return NatureTiles[value - 1]; | |
case EZones::VE_INDOOR: | |
arrayLenght = InDoorTiles.Num(); | |
value = FMath::RandRange(1, arrayLenght); | |
return InDoorTiles[value - 1]; | |
default: | |
return nullptr; | |
} | |
} | |
FVector ULevelGenerationManager::GetNextAttchPointLocation() | |
{ | |
return NextAttachPointLocation; | |
} | |
void ULevelGenerationManager::AddTile(FRotator _rotation, FVector _location) | |
{ | |
FActorSpawnParameters spawnInfo; | |
AFloorTile* spawnedFloorTile = world->SpawnActor<AFloorTile>(GetTileToSpawn(), _location, _rotation, spawnInfo); | |
zoneTilesSpawned += 1; | |
NextAttachPointLocation = spawnedFloorTile->FindComponentByClass<UArrowComponent>()->GetComponentLocation(); | |
spawnedTiles.Add(spawnedFloorTile); | |
if (spawnedTiles.IsValidIndex(amountOfTilesSpawned - 1)) | |
{ | |
PreviouslySpawnedTile = spawnedTiles[amountOfTilesSpawned - 1]; | |
} | |
else { PreviouslySpawnedTile = nullptr; } | |
amountOfTilesSpawned += 1; | |
spawnedFloorTile->SpawnObstacles(); | |
} | |
void ULevelGenerationManager::AddTile(FRotator _rotation, FVector _location, TSubclassOf<AFloorTile> _tile) | |
{ | |
FActorSpawnParameters spawnInfo; | |
AFloorTile* spawnedFloorTile = GetWorld()->SpawnActor<AFloorTile>(_tile, _location, _rotation, spawnInfo); | |
zoneTilesSpawned += 1; | |
NextAttachPointLocation = spawnedFloorTile->FindComponentByClass<UArrowComponent>()->GetComponentLocation(); | |
spawnedTiles.Add(spawnedFloorTile); | |
if (spawnedTiles.IsValidIndex(amountOfTilesSpawned - 1)) | |
{ | |
PreviouslySpawnedTile = spawnedTiles[amountOfTilesSpawned - 1]; | |
} | |
else { PreviouslySpawnedTile = nullptr; } | |
amountOfTilesSpawned += 1; | |
spawnedFloorTile->SpawnObstacles(); | |
} | |
void ULevelGenerationManager::SetWorld(UWorld* _world) | |
{ | |
if (_world) | |
{ | |
world = _world; | |
} | |
else | |
{ | |
return; | |
} | |
} | |
void ULevelGenerationManager::RemoveTile(AFloorTile* _tileToRemove) | |
{ | |
spawnedTiles.Remove(_tileToRemove); | |
_tileToRemove->Destroy(); | |
} | |
void ULevelGenerationManager::SetCurrentGameWorld(FWorldData _worldData) | |
{ | |
currentWorldData = _worldData; | |
} | |
void ULevelGenerationManager::GenerateFirstTiles() | |
{ | |
FindStartTile(); | |
for (int i = 0; i < 8; i++) | |
{ | |
AddTile(FRotator(0.0f, 0.0f, 0.0f), GetNextAttchPointLocation()); | |
} | |
} | |
void ULevelGenerationManager::FindStartTile() | |
{ | |
for (TActorIterator<AFloorTile> ActorItr(world); ActorItr; ++ActorItr) | |
{ | |
AFloorTile* floorTile = *ActorItr; | |
NextAttachPointLocation = floorTile->FindComponentByClass<UArrowComponent>()->GetComponentLocation(); | |
currentZone = floorTile->_correspondingZone; | |
zoneTilesToSpawn = FMath::RandRange(3, 6); | |
} | |
} | |
void ULevelGenerationManager::AddTilesToCorrespondingArray() | |
{ | |
for (TSubclassOf<AFloorTile> tile : currentWorldData.Tiles) | |
{ | |
AFloorTile* tile1 = tile->GetDefaultObject<AFloorTile>(); | |
switch (tile1->_correspondingZone) | |
{ | |
case EZones::VE_INNERCITY: | |
InnerCityTiles.Add(tile); | |
break; | |
case EZones::VE_OUTERCITY: | |
OuterCityTiles.Add(tile); | |
break; | |
case EZones::VE_NATURE: | |
NatureTiles.Add(tile); | |
break; | |
case EZones::VE_INDOOR: | |
InDoorTiles.Add(tile); | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment