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.
215 lines
8.0 KiB
215 lines
8.0 KiB
2 years ago
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||
|
|
||
2 years ago
|
#include "BattleGameMode.h"
|
||
2 years ago
|
#include "Kismet/GameplayStatics.h"
|
||
2 years ago
|
#include "BattlePawn.h"
|
||
|
#include "BattleGameState.h"
|
||
|
#include "BattlePlayerController.h"
|
||
|
#include "BattlePlayerState.h"
|
||
|
#include "Trooper/Trooper.h"
|
||
|
|
||
|
ABattleGameMode::ABattleGameMode()
|
||
2 years ago
|
: Super() {
|
||
|
UE_LOG(LogTemp, Warning, TEXT("GameMode Constructor"));
|
||
2 years ago
|
GameStateClass = ABattleGameState::StaticClass();
|
||
|
PlayerControllerClass = ABattlePlayerController::StaticClass();
|
||
|
PlayerStateClass = ABattlePlayerState::StaticClass();
|
||
|
DefaultPawnClass = ABattlePawn::StaticClass();
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
void ABattleGameMode::BeginPlay() {
|
||
2 years ago
|
Super::BeginPlay();
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
auto ABattleGameMode::GetMyGameState() const {
|
||
|
return GetGameState<ABattleGameState>();
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
|
||
2 years ago
|
void ABattleGameMode::InitializeBattleField_Implementation() {
|
||
2 years ago
|
UE_LOG(LogTemp, Warning, TEXT("InitializeBattleField"));
|
||
|
FVector Location(2000.0f, -1000.0f, 0.0f);
|
||
|
FRotator Rotation(0.0f, 180.0f, 0.0f);
|
||
|
|
||
|
uint8 TrooperCount = 0;
|
||
|
|
||
|
TArray<const TCHAR *> bpPaths{
|
||
|
TEXT(
|
||
|
"Blueprint'/Game/Troopers/TrooperSkeletonMelee.TrooperSkeletonMelee_C'"
|
||
|
),
|
||
|
TEXT("Blueprint'/Game/Troopers/TrooperWizard.TrooperWizard_C'")
|
||
|
};
|
||
2 years ago
|
// TArray<UClass *> LoadedBpAssets;
|
||
2 years ago
|
for (int i = 0; i < bpPaths.Num(); ++i) {
|
||
|
TSoftClassPtr<ATrooper> ActorBpClass = TSoftClassPtr<ATrooper>(
|
||
|
FSoftObjectPath(bpPaths[i])
|
||
|
);
|
||
|
LoadedBpAssets.Push(ActorBpClass.LoadSynchronous());
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
for (int i = 0; i < 5; ++i) {
|
||
|
FTransform SpawnLocationAndRotation(Rotation);
|
||
|
SpawnLocationAndRotation.SetLocation(Location);
|
||
|
AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
|
||
|
LoadedBpAssets[i % 2], SpawnLocationAndRotation);
|
||
2 years ago
|
// AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
|
||
|
// ATrooper::StaticClass(), SpawnLocationAndRotation);
|
||
2 years ago
|
Cast<ATrooper>(Spawned)->Initialize(
|
||
2 years ago
|
0, Location, TrooperCount++);
|
||
|
Spawned->FinishSpawning(SpawnLocationAndRotation);
|
||
|
Spawned->SetActorLocation(Location);
|
||
2 years ago
|
GetMyGameState()->AddTrooper(Cast<ATrooper>(Spawned));
|
||
2 years ago
|
Location += {0.f, 500.f, 0.0f};
|
||
|
}
|
||
2 years ago
|
if (bIsMultiplayer) {
|
||
|
Location = {-2000.0f, -1000.0f, 0.0f};
|
||
|
Rotation = {0.0f, 0.0f, 0.0f};
|
||
|
for (int i = 0; i < 5; ++i) {
|
||
|
FTransform SpawnLocationAndRotation(Rotation);
|
||
|
SpawnLocationAndRotation.SetLocation(Location);
|
||
|
AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
|
||
|
LoadedBpAssets[i % 2], SpawnLocationAndRotation);
|
||
|
// AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
|
||
|
// ATrooper::StaticClass(), SpawnLocationAndRotation);
|
||
|
Cast<ATrooper>(Spawned)->Initialize(
|
||
|
1, Location, TrooperCount++);
|
||
|
Spawned->FinishSpawning(SpawnLocationAndRotation);
|
||
|
Spawned->SetActorLocation(Location);
|
||
|
GetMyGameState()->AddTrooper(Cast<ATrooper>(Spawned));
|
||
|
Location += {0.f, 500.f, 0.0f};
|
||
|
}
|
||
|
} else {
|
||
|
// Cast<ASinglePlayerGS>(GetMyGameState())->GetEnemyAIController()->
|
||
2 years ago
|
// SetTrooperAssetsAndSpawn(
|
||
|
// LoadedBpAssets,
|
||
|
// TrooperCount);
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
AActor *ABattleGameMode::ChoosePlayerStart_Implementation(AController *Player) {
|
||
2 years ago
|
UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart %d"),
|
||
|
GetNumPlayers());
|
||
|
InitializeSpawnPointsIfNeeded(Player);
|
||
|
const auto CurrentPlayerStart = *SpawnPoints.Find(GetNumPlayers());
|
||
|
UE_LOG(LogTemp, Warning, TEXT("GameMode ChoosePlayerStart end %d"),
|
||
|
CurrentPlayerStart->GetPlayerIndex());
|
||
|
return CurrentPlayerStart;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
void ABattleGameMode::InitializeSpawnPointsIfNeeded(AController *Player) {
|
||
2 years ago
|
UE_LOG(LogTemp, Warning, TEXT("InitializeSpawnPointsIfNeeded"));
|
||
|
if (SpawnPoints.Num() != 0) {
|
||
|
UE_LOG(LogTemp, Warning, TEXT("InitializeSpawnPointsIfNeeded Exit %d"),
|
||
|
SpawnPoints.Num());
|
||
|
return;
|
||
|
}
|
||
|
UE_LOG(LogTemp, Warning, TEXT("Rebuilding spawnpoints"));
|
||
|
const auto World = GetWorld();
|
||
2 years ago
|
for (TActorIterator<ABattlePlayerStart> PlayerStartIterator(GetWorld());
|
||
2 years ago
|
PlayerStartIterator; ++PlayerStartIterator) {
|
||
|
const auto PlayerStart = *PlayerStartIterator;
|
||
|
const UClass *PawnClass = GetDefaultPawnClassForController(Player);
|
||
|
const APawn *PawnToFit = PawnClass
|
||
|
? PawnClass->GetDefaultObject<APawn>()
|
||
|
: nullptr;
|
||
|
const FVector ActorLocation = PlayerStart->GetActorLocation();
|
||
|
const FRotator ActorRotation = PlayerStart->GetActorRotation();
|
||
|
UE_LOG(LogTemp, Warning, TEXT("PlayerStart iterator %d"),
|
||
|
PlayerStartIterator->GetPlayerIndex());
|
||
|
if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation,
|
||
|
ActorRotation)) {
|
||
|
SpawnPoints.Add(PlayerStartIterator->GetPlayerIndex(),
|
||
|
*PlayerStartIterator);
|
||
|
UE_LOG(LogTemp, Warning, TEXT("PlayerStart unoccupied iterator %d"),
|
||
|
PlayerStartIterator->GetPlayerIndex());
|
||
|
}
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
void ABattleGameMode::PostLogin(APlayerController *NewPlayer) {
|
||
2 years ago
|
Super::PostLogin(NewPlayer);
|
||
2 years ago
|
NewPlayer->SetShowMouseCursor(true);
|
||
2 years ago
|
UE_LOG(LogTemp, Warning, TEXT("PostLogin"));
|
||
2 years ago
|
// PlayerControllers.Add(dynamic_cast<AMyPlayerController *>(NewPlayer));
|
||
2 years ago
|
// const auto World = GetWorld();
|
||
|
const auto CurrentNumberOfPlayers = GetNumPlayers();
|
||
|
|
||
|
// 0-indexation
|
||
2 years ago
|
Cast<ABattlePlayerController>(NewPlayer)->SetPlayerIndex(
|
||
2 years ago
|
CurrentNumberOfPlayers - 1);
|
||
|
UE_LOG(LogTemp, Warning, TEXT("%d"), CurrentNumberOfPlayers);
|
||
|
if (CurrentNumberOfPlayers == 2) {
|
||
|
UE_LOG(LogTemp, Warning, TEXT("Game Start"));
|
||
|
// start the game
|
||
2 years ago
|
// dynamic_cast<AMyGameState *>(
|
||
|
// GetWorld()->GetGameState())->StartGame();
|
||
|
// InitializeBattleField();
|
||
2 years ago
|
StartGame();
|
||
2 years ago
|
// GetMyGameState()->PlayerInTurn()->SetEnemySelection();
|
||
|
// GetMyGameState()->PlayerNotInTurn()->SetEnemySelection();
|
||
2 years ago
|
} else {
|
||
|
// delay the game
|
||
|
UE_LOG(LogTemp, Warning, TEXT("Game Delay"));
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
void ABattleGameMode::StartGame_Implementation() {
|
||
2 years ago
|
InitializeBattleField();
|
||
2 years ago
|
// PlayerNotInTurn()->SetEnemySelection(Troopers);
|
||
|
// PlayerInTurn()->SetEnemySelection(Troopers);
|
||
2 years ago
|
|
||
2 years ago
|
// PlayerInTurn()->StartTurn();
|
||
|
GetMyGameState()->StartGame();
|
||
2 years ago
|
}
|
||
|
|
||
|
|
||
2 years ago
|
// void AMyGameMode::StartGame() {
|
||
|
// InitializeBattleField();
|
||
|
// // PlayerNotInTurn()->SetEnemySelection(Troopers);
|
||
|
// // PlayerInTurn()->SetEnemySelection(Troopers);
|
||
|
//
|
||
|
// // PlayerInTurn()->StartTurn();
|
||
|
// GetMyGameState()->StartGame();
|
||
|
// }
|
||
|
|
||
|
|
||
2 years ago
|
// AMyPlayerController *AMyGameMode::PlayerInTurn() const {
|
||
|
// return GetMyPlayerController(CurrentPlayerTurn);
|
||
|
// }
|
||
|
|
||
|
// AMyPlayerController *AMyGameMode::PlayerNotInTurn() const {
|
||
|
// // uint8 PlayerControllerIndexNotInTurn;
|
||
|
// // if (CurrentPlayerTurn == 0) {
|
||
|
// // PlayerControllerIndexNotInTurn = 1;
|
||
|
// // } else {
|
||
|
// // PlayerControllerIndexNotInTurn = 0;
|
||
|
// // }
|
||
|
// // return GetMyPlayerController(PlayerControllerIndexNotInTurn);
|
||
|
// return GetMyPlayerController(!CurrentPlayerTurn);
|
||
|
// }
|
||
|
|
||
|
// void AMyGameMode::CycleTurns() {
|
||
|
// if (!this)
|
||
|
// return;
|
||
|
// // PlayerInTurn()->EndTurn();
|
||
|
// for (const auto Trooper : Troopers) {
|
||
|
// if (Trooper != nullptr) {
|
||
|
// Trooper->ResetActionPoints();
|
||
|
// }
|
||
|
// }
|
||
|
// CurrentPlayerTurn = !CurrentPlayerTurn;
|
||
|
// // if (CurrentPlayerTurn == 0) {
|
||
|
// // CurrentPlayerTurn = 1;
|
||
|
// // } else {
|
||
|
// // CurrentPlayerTurn = 0;
|
||
|
// // }
|
||
|
// PlayerInTurn()->StartTurn();
|
||
|
// }
|
||
|
|
||
|
|
||
|
// AMyPlayerController *AMyGameMode::GetMyPlayerController(
|
||
|
// uint8 const PlayerIndex) const {
|
||
|
// return Cast<AMyPlayerController>(
|
||
|
// UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex));
|
||
|
// }
|