rewrote using GameState and PlayerState, but end turn still doesn't work

pull/6/head
m4xxx1m 2 years ago
parent 24279b7255
commit 2be6c07339

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -25,8 +25,8 @@ void AManageSquadPlayerController::OnLeftMouseClick() {
UE_LOG(LogTemp, Warning, TEXT("Got hit result")); UE_LOG(LogTemp, Warning, TEXT("Got hit result"));
// auto const NewlySelectedLocation = HitResult.Location; // auto const NewlySelectedLocation = HitResult.Location;
AManageSquadTrooper *NewlySelectedTrooper = dynamic_cast AManageSquadTrooper *NewlySelectedTrooper = Cast
<AManageSquadTrooper *>( <AManageSquadTrooper>(
HitResult.GetActor()); HitResult.GetActor());
if (NewlySelectedTrooper == nullptr || !NewlySelectedTrooper-> if (NewlySelectedTrooper == nullptr || !NewlySelectedTrooper->
@ -46,11 +46,11 @@ void AManageSquadPlayerController::OnLeftMouseClick() {
if (SelectedTrooper) { if (SelectedTrooper) {
UE_LOG(LogTemp, Warning, TEXT("Trooper replacement")); UE_LOG(LogTemp, Warning, TEXT("Trooper replacement"));
SelectedTrooper->ChangeSkeletalMesh(NewlySelectedTrooper); SelectedTrooper->ChangeSkeletalMesh(NewlySelectedTrooper);
dynamic_cast<AManageSquadGameState *>( Cast<AManageSquadGameState>(
GetWorld()->GetGameState())->ChangeSquad( GetWorld()->GetGameState())->ChangeSquad(
SelectedTrooper->GetIndex(), SelectedTrooper->GetIndex(),
NewlySelectedTrooper->GetIndex() NewlySelectedTrooper->GetIndex()
); );
} }
break; break;
case ETrooperType::TROOPER_IN_SQUAD: case ETrooperType::TROOPER_IN_SQUAD:

@ -4,12 +4,15 @@
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "MyPawn.h" #include "MyPawn.h"
#include "MyGameState.h" #include "MyGameState.h"
#include "MyPlayerController.h"
#include "MyPlayerState.h"
AMyGameMode::AMyGameMode() AMyGameMode::AMyGameMode()
: Super() { : Super() {
UE_LOG(LogTemp, Warning, TEXT("GameMode Constructor")); UE_LOG(LogTemp, Warning, TEXT("GameMode Constructor"));
GameStateClass = AMyGameState::StaticClass(); GameStateClass = AMyGameState::StaticClass();
PlayerControllerClass = AMyPlayerController::StaticClass(); PlayerControllerClass = AMyPlayerController::StaticClass();
PlayerStateClass = AMyPlayerState::StaticClass();
DefaultPawnClass = AMyPawn::StaticClass(); DefaultPawnClass = AMyPawn::StaticClass();
} }
@ -17,6 +20,10 @@ void AMyGameMode::BeginPlay() {
Super::BeginPlay(); Super::BeginPlay();
} }
auto AMyGameMode::GetMyGameState() const {
return GetGameState<AMyGameState>();
}
void AMyGameMode::InitializeBattleField() const { void AMyGameMode::InitializeBattleField() const {
UE_LOG(LogTemp, Warning, TEXT("InitializeBattleField")); UE_LOG(LogTemp, Warning, TEXT("InitializeBattleField"));
@ -46,11 +53,11 @@ void AMyGameMode::InitializeBattleField() const {
LoadedBpAssets[i % 2], SpawnLocationAndRotation); LoadedBpAssets[i % 2], SpawnLocationAndRotation);
// AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>( // AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
// ATrooper::StaticClass(), SpawnLocationAndRotation); // ATrooper::StaticClass(), SpawnLocationAndRotation);
dynamic_cast<ATrooper *>(Spawned)->Initialize( Cast<ATrooper>(Spawned)->Initialize(
0, Location, TrooperCount++); 0, Location, TrooperCount++);
Spawned->FinishSpawning(SpawnLocationAndRotation); Spawned->FinishSpawning(SpawnLocationAndRotation);
Spawned->SetActorLocation(Location); Spawned->SetActorLocation(Location);
Troopers.Add(dynamic_cast<ATrooper *>(Spawned)); GetMyGameState()->AddTrooper(Cast<ATrooper>(Spawned));
Location += {0.f, 500.f, 0.0f}; Location += {0.f, 500.f, 0.0f};
} }
Location = {-2000.0f, -1000.0f, 0.0f}; Location = {-2000.0f, -1000.0f, 0.0f};
@ -62,11 +69,11 @@ void AMyGameMode::InitializeBattleField() const {
LoadedBpAssets[i % 2], SpawnLocationAndRotation); LoadedBpAssets[i % 2], SpawnLocationAndRotation);
// AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>( // AActor *Spawned = GetWorld()->SpawnActorDeferred<ATrooper>(
// ATrooper::StaticClass(), SpawnLocationAndRotation); // ATrooper::StaticClass(), SpawnLocationAndRotation);
dynamic_cast<ATrooper *>(Spawned)->Initialize( Cast<ATrooper>(Spawned)->Initialize(
1, Location, TrooperCount++); 1, Location, TrooperCount++);
Spawned->FinishSpawning(SpawnLocationAndRotation); Spawned->FinishSpawning(SpawnLocationAndRotation);
Spawned->SetActorLocation(Location); Spawned->SetActorLocation(Location);
Troopers.Add(dynamic_cast<ATrooper *>(Spawned)); GetMyGameState()->AddTrooper(Cast<ATrooper>(Spawned));
Location += {0.f, 500.f, 0.0f}; Location += {0.f, 500.f, 0.0f};
} }
} }
@ -120,7 +127,7 @@ void AMyGameMode::PostLogin(APlayerController *NewPlayer) {
const auto CurrentNumberOfPlayers = GetNumPlayers(); const auto CurrentNumberOfPlayers = GetNumPlayers();
// 0-indexation // 0-indexation
dynamic_cast<AMyPlayerController *>(NewPlayer)->SetPlayerIndex( Cast<AMyPlayerController>(NewPlayer)->SetPlayerIndex(
CurrentNumberOfPlayers - 1); CurrentNumberOfPlayers - 1);
UE_LOG(LogTemp, Warning, TEXT("%d"), CurrentNumberOfPlayers); UE_LOG(LogTemp, Warning, TEXT("%d"), CurrentNumberOfPlayers);
if (CurrentNumberOfPlayers == 2) { if (CurrentNumberOfPlayers == 2) {
@ -141,48 +148,49 @@ void AMyGameMode::StartGame() {
InitializeBattleField(); InitializeBattleField();
// PlayerNotInTurn()->SetEnemySelection(Troopers); // PlayerNotInTurn()->SetEnemySelection(Troopers);
// PlayerInTurn()->SetEnemySelection(Troopers); // PlayerInTurn()->SetEnemySelection(Troopers);
// PlayerControllers[0]->SetEnemySelection(Troopers);
// PlayerControllers[1]->SetEnemySelection(Troopers); // PlayerInTurn()->StartTurn();
PlayerInTurn()->StartTurn(); GetMyGameState()->StartGame();
}
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::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));
// }
AMyPlayerController *AMyGameMode::GetMyPlayerController(
uint8 const PlayerIndex) const {
return dynamic_cast<AMyPlayerController *>(
UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex));
}

@ -4,7 +4,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "EngineUtils.h" #include "EngineUtils.h"
#include "MyPlayerController.h" // #include "MyPlayerController.h"
#include "MyPlayerStart.h" #include "MyPlayerStart.h"
#include "GameFramework/GameMode.h" #include "GameFramework/GameMode.h"
#include "MyGameMode.generated.h" #include "MyGameMode.generated.h"
@ -23,9 +23,9 @@ public:
virtual void BeginPlay() override; virtual void BeginPlay() override;
//
UFUNCTION(BlueprintCallable) // UFUNCTION(BlueprintCallable)
void CycleTurns(); // void CycleTurns();
private: private:
void InitializeSpawnPointsIfNeeded(AController *Player); void InitializeSpawnPointsIfNeeded(AController *Player);
@ -34,22 +34,24 @@ private:
UPROPERTY() UPROPERTY()
TMap<uint8, AMyPlayerStart *> SpawnPoints{}; TMap<uint8, AMyPlayerStart *> SpawnPoints{};
UPROPERTY() // UPROPERTY()
mutable TArray<ATrooper *> Troopers; // mutable TArray<ATrooper *> Troopers;
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void StartGame(); void StartGame();
UFUNCTION(BlueprintPure) // UFUNCTION(BlueprintPure)
AMyPlayerController *PlayerInTurn() const; // AMyPlayerController *PlayerInTurn() const;
//
// UFUNCTION(BlueprintPure)
// AMyPlayerController *PlayerNotInTurn() const;
UFUNCTION(BlueprintPure) // UPROPERTY()
AMyPlayerController *PlayerNotInTurn() const; // uint8 CurrentPlayerTurn{0};
UPROPERTY() // UFUNCTION()
uint8 CurrentPlayerTurn{0}; // AMyPlayerController *GetMyPlayerController(uint8 const PlayerIndex) const;
UFUNCTION() auto GetMyGameState() const;
AMyPlayerController *GetMyPlayerController(uint8 const PlayerIndex) const;
}; };

@ -2,86 +2,65 @@
#include "MyGameState.h" #include "MyGameState.h"
// #include "MyPlayerController.h" #include "MyPlayerState.h"
// #include "Trooper.h" #include "Trooper.h"
// #include "Kismet/GameplayStatics.h" #include "Net/UnrealNetwork.h"
// void AMyGameState::InitializeBattleField() const { auto AMyGameState::GetMyPlayerState(uint8 PlayerIndex) const {
// UE_LOG(LogTemp, Warning, TEXT("InitializeBattleField")); return Cast<AMyPlayerState>(PlayerArray[PlayerIndex]);
// 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'")
// };
// TArray<UClass *> LoadedBpAssets;
// for (int i = 0; i < bpPaths.Num(); ++i) {
// TSoftClassPtr<ATrooper> ActorBpClass = TSoftClassPtr<ATrooper>(
// FSoftObjectPath(bpPaths[i])
// );
// LoadedBpAssets.Push(ActorBpClass.LoadSynchronous());
// }
//
// 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);
// dynamic_cast<ATrooper *>(Spawned)->Initialize(
// 0, Location, TrooperCount++);
// Spawned->FinishSpawning(SpawnLocationAndRotation);
// Spawned->SetActorLocation(Location);
// Location += {0.f, 500.f, 0.0f};
// }
// 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);
// dynamic_cast<ATrooper *>(Spawned)->Initialize(
// 1, Location, TrooperCount++);
// Spawned->FinishSpawning(SpawnLocationAndRotation);
// Spawned->SetActorLocation(Location);
// Location += {0.f, 500.f, 0.0f};
// }
// }
// AMyPlayerController *AMyGameState::GetMyPlayerController( void AMyGameState::BeginPlay() {
// uint8 const PlayerIndex) const { Super::BeginPlay();
// return dynamic_cast<AMyPlayerController *>( }
// UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex));
// } void AMyGameState::AddTrooper(ATrooper *Trooper) {
// Troopers.Add(Trooper);
// void AMyGameState::StartGame() { }
// // InitializeBattleField();
// PlayerInTurn()->StartTurn(); void AMyGameState::StartGame() const {
// } // PlayerNotInTurn()->SetEnemySelection(Troopers);
// // PlayerInTurn()->SetEnemySelection(Troopers);
// void AMyGameState::CycleTurns() { PlayerInTurn()->StartTurn();
// PlayerInTurn()->EndTurn(); }
// if (CurrentPlayerTurn == 0) {
// CurrentPlayerTurn = 1; void AMyGameState::CycleTurns_Implementation(uint8 CurrentPlayerIndex) {
// } else { if (CurrentPlayerTurn == CurrentPlayerIndex) {
// CurrentPlayerTurn = 0; PlayerInTurn()->EndTurn();
for (const auto Trooper : Troopers) {
if (Trooper != nullptr) {
Trooper->ResetActionPoints();
}
}
CurrentPlayerTurn = !CurrentPlayerTurn;
PlayerInTurn()->StartTurn();
}
}
// void AMyGameState::CycleTurns(uint8 CurrentPlayerIndex) {
// if (CurrentPlayerTurn == CurrentPlayerIndex) {
// PlayerInTurn()->EndTurn();
// for (const auto Trooper : Troopers) {
// if (Trooper != nullptr) {
// Trooper->ResetActionPoints();
// }
// }
// CurrentPlayerTurn = !CurrentPlayerTurn;
// PlayerInTurn()->StartTurn();
// } // }
// PlayerInTurn()->StartTurn();
// }
//
// AMyPlayerController *AMyGameState::PlayerInTurn() const {
// return GetMyPlayerController(CurrentPlayerTurn);
// }
//
// AMyPlayerController *AMyGameState::PlayerNotInTurn() const {
// return GetMyPlayerController(CurrentPlayerTurn == 0 ? 1 : 0);
// } // }
AMyPlayerState *AMyGameState::PlayerInTurn() const {
return GetMyPlayerState(CurrentPlayerTurn);
}
AMyPlayerState *AMyGameState::PlayerNotInTurn() const {
return GetMyPlayerState(!CurrentPlayerTurn);
}
void AMyGameState::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const {
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyGameState, Troopers);
DOREPLIFETIME(AMyGameState, CurrentPlayerTurn);
}

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "MyPlayerState.h"
#include "GameFramework/GameState.h" #include "GameFramework/GameState.h"
#include "MyGameState.generated.h" #include "MyGameState.generated.h"
@ -12,26 +13,32 @@
UCLASS() UCLASS()
class TURNBASEDTUTORIAL_API AMyGameState : public AGameState { class TURNBASEDTUTORIAL_API AMyGameState : public AGameState {
GENERATED_BODY() GENERATED_BODY()
// public:
// UFUNCTION(BlueprintCallable) public:
// void CycleTurns(); virtual void BeginPlay() override;
//
// UFUNCTION(BlueprintCallable) UFUNCTION()
// void StartGame(); void AddTrooper(ATrooper *Trooper);
//
// private: UFUNCTION()
// // void InitializeBattleField() const; void StartGame() const;
//
// UFUNCTION() UFUNCTION(BlueprintCallable, Server, Reliable)
// AMyPlayerController *GetMyPlayerController(uint8 const PlayerIndex) const; void CycleTurns(uint8 CurrentPlayerIndex);
//
// UFUNCTION(BlueprintPure)
// UFUNCTION(BlueprintPure) AMyPlayerState *PlayerInTurn() const;
// AMyPlayerController *PlayerInTurn() const;
// UFUNCTION(BlueprintPure)
// UFUNCTION(BlueprintPure) AMyPlayerState *PlayerNotInTurn() const;
// AMyPlayerController *PlayerNotInTurn() const;
// auto GetMyPlayerState(uint8 PlayerIndex) const;
// UPROPERTY()
// uint8 CurrentPlayerTurn{0}; private:
UPROPERTY(Replicated)
TArray<ATrooper *> Troopers;
UPROPERTY(Replicated)
uint8 CurrentPlayerTurn{0};
}; };

@ -3,10 +3,12 @@
#include "MyPlayerController.h" #include "MyPlayerController.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "MyGameMode.h" #include "MyGameMode.h"
#include "MyGameState.h"
#include "MyPlayerState.h"
#include "Net/UnrealNetwork.h" #include "Net/UnrealNetwork.h"
AMyPlayerController::AMyPlayerController() AMyPlayerController::AMyPlayerController()
: Super(), bIsMyTurn(false), SelectedTrooper(nullptr) { : Super()/*, bIsMyTurn(false), SelectedTrooper(nullptr)*/ {
UE_LOG(LogTemp, Warning, TEXT("Player controller created")); UE_LOG(LogTemp, Warning, TEXT("Player controller created"));
SetShowMouseCursor(true); SetShowMouseCursor(true);
} }
@ -17,58 +19,72 @@ void AMyPlayerController::SetupInputComponent() {
&AMyPlayerController::OnLeftMouseClick); &AMyPlayerController::OnLeftMouseClick);
} }
void AMyPlayerController::SetMyTurn(bool bMyTurn) { // void AMyPlayerController::SetMyTurn(bool bMyTurn) {
bIsMyTurn = bMyTurn; // bIsMyTurn = bMyTurn;
if (bIsMyTurn) { // if (bIsMyTurn) {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, // GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green,
FString::Printf( // FString::Printf(
TEXT("CURRENT TURN: %d"), // TEXT("CURRENT TURN: %d"),
PlayerIndex)); // PlayerIndex));
} // }
// OnMyTurnChanged.ExecuteIfBound(bIsMyTurn); // OnMyTurnChanged.ExecuteIfBound(bIsMyTurn);
// }
// void AMyPlayerController::StartTurn_Implementation() {
// SetMyTurn(true);
// UE_LOG(LogTemp, Warning, TEXT("Your turn, %d"), PlayerIndex);
// }
auto AMyPlayerController::GetMyGameState() const {
return Cast<AMyGameState>(UGameplayStatics::GetGameState(GetWorld()));
} }
void AMyPlayerController::StartTurn_Implementation() { auto AMyPlayerController::GetMyPlayerState() const {
SetMyTurn(true); return GetPlayerState<AMyPlayerState>();
UE_LOG(LogTemp, Warning, TEXT("Your turn, %d"), PlayerIndex);
} }
auto AMyPlayerController::GetMyGameMode() const { auto AMyPlayerController::GetMyGameMode() const {
return dynamic_cast<AMyGameMode *>( return Cast<AMyGameMode>(
UGameplayStatics::GetGameMode(GetWorld())); UGameplayStatics::GetGameMode(GetWorld()));
} }
void AMyPlayerController::EndTurn_Implementation() {
if (bIsMyTurn) { // void AMyPlayerController::EndTurn_Implementation() {
UE_LOG(LogTemp, Warning, TEXT("End Turn from player %d"), PlayerIndex); // GetMyPlayerState()->EndTurn();
SetMyTurn(false); // }
if (SelectedTrooper) {
SelectedTrooper->SetSelection(false);
SelectedTrooper = nullptr; // if (bIsMyTurn) {
} // UE_LOG(LogTemp, Warning, TEXT("End Turn from player %d"), PlayerIndex);
UE_LOG(LogTemp, Warning, TEXT("Not your turn, %d"), PlayerIndex); // SetMyTurn(false);
AMyGameMode *gameMode = GetMyGameMode(); // if (SelectedTrooper) {
gameMode->CycleTurns(); // SelectedTrooper->SetSelection(false);
} // SelectedTrooper = nullptr;
} // }
// UE_LOG(LogTemp, Warning, TEXT("Not your turn, %d"), PlayerIndex);
// AMyGameMode *gameMode = GetMyGameMode();
// gameMode->CycleTurns();
// }
// }
// AMyGameState *AMyPlayerController::GetMyGameState() const { // AMyGameState *AMyPlayerController::GetMyGameState() const {
// return dynamic_cast<AMyGameState *>( // return dynamic_cast<AMyGameState *>(
// GetWorld()->GetGameState()); // GetWorld()->GetGameState());
// } // }
void AMyPlayerController::MoveTrooper_Implementation( // void AMyPlayerController::MoveTrooper_Implementation(
ATrooper *Trooper, // ATrooper *Trooper,
FVector Location) { // FVector Location) {
if (Trooper->CheckMoveCorrectness(Location)) { // if (Trooper->CheckMoveCorrectness(Location)) {
Trooper->MoveTrooper(Location); // Trooper->MoveTrooper(Location);
// GetMyGameMode()->CycleTurns(); // // GetMyGameMode()->CycleTurns();
} else { // } else {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red, // GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red,
FString::Printf( // FString::Printf(
TEXT("Out of move radius!"))); // TEXT("Out of move radius!")));
} // }
} // }
// void AMyPlayerController::AttackTrooper_Implementation( // void AMyPlayerController::AttackTrooper_Implementation(
// ATrooper *Attacker, // ATrooper *Attacker,
@ -90,32 +106,38 @@ void AMyPlayerController::MoveTrooper_Implementation(
// } // }
void AMyPlayerController::Attack_Implementation(ATrooper *Attacker, // void AMyPlayerController::Attack_Implementation(ATrooper *Attacker,
FVector Location, // FVector Location,
int ActionIndex) { // int ActionIndex) {
if (Attacker && CurrentAction >= 1 && CurrentAction <= 2 && // if (Attacker && CurrentAction >= 1 && CurrentAction <= 2 &&
Attacker->CheckAttackCorrectness(Location, CurrentAction)) { // Attacker->CheckAttackCorrectness(Location, CurrentAction)) {
Attacker->Attack(CurrentAction); // Attacker->Attack(CurrentAction);
} else { // } else {
UE_LOG(LogTemp, Warning, // UE_LOG(LogTemp, Warning,
TEXT("Out of radius or not enough Action Points")); // TEXT("Out of radius or not enough Action Points"));
} // }
} // }
void AMyPlayerController::SetPlayerIndex(uint8 NewPlayerIndex) { void AMyPlayerController::SetPlayerIndex(uint8 NewPlayerIndex) {
PlayerIndex = NewPlayerIndex; PlayerIndex = NewPlayerIndex;
GetMyPlayerState()->SetPlayerIndex(NewPlayerIndex);
// GetMyPlayerState()->PlayerIndex = NewPlayerIndex;
} }
float AMyPlayerController::SetCurrentActionAndReturnRadius(int action) { float AMyPlayerController::SetCurrentActionAndReturnRadius(int action) {
CurrentAction = action; return GetMyPlayerState()->SetCurrentActionAndReturnRadius(action);
UE_LOG(LogTemp, Warning, TEXT("SetCurrentAction: %d on Player Controller "
"with index %d"), CurrentAction, PlayerIndex); // CurrentAction = action;
if (SelectedTrooper) { // UE_LOG(LogTemp, Warning, TEXT("SetCurrentAction: %d on Player Controller "
return SelectedTrooper->GetActionRadius(CurrentAction); // "with index %d"), CurrentAction, PlayerIndex);
} // if (SelectedTrooper) {
return 0.0f; // return SelectedTrooper->GetActionRadius(CurrentAction);
// }
// return 0.0f;
} }
void AMyPlayerController::SetEnemySelection_Implementation( void AMyPlayerController::SetEnemySelection_Implementation(
const TArray<ATrooper *> &Troopers) const { const TArray<ATrooper *> &Troopers) const {
for (const auto Trooper : Troopers) { for (const auto Trooper : Troopers) {
@ -149,18 +171,19 @@ void AMyPlayerController::SetEnemySelection_Implementation(
void AMyPlayerController::GetLifetimeReplicatedProps( void AMyPlayerController::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const { TArray<FLifetimeProperty> &OutLifetimeProps) const {
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyPlayerController, PlayerIndex); DOREPLIFETIME(AMyPlayerController, PlayerIndex);
DOREPLIFETIME(AMyPlayerController, CurrentAction); // DOREPLIFETIME(AMyPlayerController, CurrentAction);
DOREPLIFETIME(AMyPlayerController, bIsMyTurn); // DOREPLIFETIME(AMyPlayerController, bIsMyTurn);
DOREPLIFETIME(AMyPlayerController, SelectedTrooper); // DOREPLIFETIME(AMyPlayerController, SelectedTrooper);
} }
void AMyPlayerController::OnLeftMouseClick() { void AMyPlayerController::OnLeftMouseClick() {
if (!bIsMyTurn) { if (!GetMyPlayerState()->IsMyTurn()) {
return; return;
} }
UE_LOG(LogTemp, Warning, TEXT("Mouse clicked")); UE_LOG(LogTemp, Warning, TEXT("Mouse clicked"));
UE_LOG(LogTemp, Warning, TEXT("Current action: %d"), CurrentAction); // UE_LOG(LogTemp, Warning, TEXT("Current action: %d"), CurrentAction);
FHitResult HitResult; FHitResult HitResult;
bool const IsHitResult = GetHitResultUnderCursorForObjects( bool const IsHitResult = GetHitResultUnderCursorForObjects(
TArray<TEnumAsByte<EObjectTypeQuery>>{ObjectTypeQuery1}, false, TArray<TEnumAsByte<EObjectTypeQuery>>{ObjectTypeQuery1}, false,
@ -171,56 +194,57 @@ void AMyPlayerController::OnLeftMouseClick() {
if (!IsHitResult) if (!IsHitResult)
return; return;
UE_LOG(LogTemp, Warning, TEXT("Got hit result")); UE_LOG(LogTemp, Warning, TEXT("Got hit result"));
auto const NewlySelectedLocation = HitResult.Location; GetMyPlayerState()->OnPlayerAction(HitResult);
ATrooper *NewlySelectedTrooper = dynamic_cast<ATrooper *>( // auto const NewlySelectedLocation = HitResult.Location;
HitResult.GetActor()); // ATrooper *NewlySelectedTrooper = Cast<ATrooper>(
// HitResult.GetActor());
// skip re-selection //
if (SelectedTrooper == NewlySelectedTrooper) { // // skip re-selection
UE_LOG(LogTemp, Warning, TEXT("Skip reselection")); // if (SelectedTrooper == NewlySelectedTrooper) {
return; // UE_LOG(LogTemp, Warning, TEXT("Skip reselection"));
} // return;
// }
if (NewlySelectedTrooper == nullptr || !NewlySelectedTrooper-> //
IsValidLowLevel() || NewlySelectedTrooper->GetPlayerIndex() != // if (NewlySelectedTrooper == nullptr || !NewlySelectedTrooper->
PlayerIndex) { // IsValidLowLevel() || NewlySelectedTrooper->GetPlayerIndex() !=
// we selected something that is not a trooper (or trooper in shitty state...) // PlayerIndex) {
// probably we should move to it if we can... // // we selected something that is not a trooper (or trooper in shitty state...)
// // probably we should move to it if we can...
// UE_LOG(LogTemp, Warning, TEXT("Not a trooper")); //
// // UE_LOG(LogTemp, Warning, TEXT("Not a trooper"));
// if initial trooper is valid... //
if (SelectedTrooper != nullptr && SelectedTrooper->IsValidLowLevel()) { // // if initial trooper is valid...
switch (CurrentAction) { // if (SelectedTrooper != nullptr && SelectedTrooper->IsValidLowLevel()) {
case 0: // switch (CurrentAction) {
UE_LOG(LogTemp, Warning, TEXT("Do move")); // case 0:
// move this mf // UE_LOG(LogTemp, Warning, TEXT("Do move"));
MoveTrooper(SelectedTrooper, NewlySelectedLocation); // // move this mf
// and reset the selection.... // MoveTrooper(SelectedTrooper, NewlySelectedLocation);
SelectedTrooper->SetSelection(false); // // and reset the selection....
SelectedTrooper = nullptr; // SelectedTrooper->SetSelection(false);
break; // SelectedTrooper = nullptr;
default: // break;
// ATTACK! ATTACK! // default:
UE_LOG(LogTemp, Warning, TEXT("Do attack")); // // ATTACK! ATTACK!
Attack(SelectedTrooper, NewlySelectedLocation, // UE_LOG(LogTemp, Warning, TEXT("Do attack"));
CurrentAction); // Attack(SelectedTrooper, NewlySelectedLocation,
SelectedTrooper->SetSelection(false); // CurrentAction);
SelectedTrooper = nullptr; // SelectedTrooper->SetSelection(false);
break; // SelectedTrooper = nullptr;
} // break;
} // }
} else if (NewlySelectedTrooper != nullptr && NewlySelectedTrooper-> // }
IsValidLowLevel() && NewlySelectedTrooper->GetPlayerIndex() == // } else if (NewlySelectedTrooper != nullptr && NewlySelectedTrooper->
PlayerIndex) { // IsValidLowLevel() && NewlySelectedTrooper->GetPlayerIndex() ==
UE_LOG(LogTemp, Warning, TEXT("Do reselect")); // PlayerIndex) {
// our move, selection // UE_LOG(LogTemp, Warning, TEXT("Do reselect"));
if (SelectedTrooper) { // // our move, selection
SelectedTrooper->SetSelection(false); // if (SelectedTrooper) {
} // SelectedTrooper->SetSelection(false);
SelectedTrooper = NewlySelectedTrooper; // }
SelectedTrooper->SetSelection(true); // SelectedTrooper = NewlySelectedTrooper;
} // SelectedTrooper->SetSelection(true);
// }
// //
// if (NewlySelectedTrooper == nullptr || !NewlySelectedTrooper-> // if (NewlySelectedTrooper == nullptr || !NewlySelectedTrooper->

@ -2,7 +2,6 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
// #include "MyGameState.h"
#include "Trooper.h" #include "Trooper.h"
#include "GameFramework/PlayerController.h" #include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h" #include "MyPlayerController.generated.h"
@ -15,25 +14,25 @@ class TURNBASEDTUTORIAL_API AMyPlayerController : public APlayerController {
public: public:
// FOnMyTurnChangedDelegate OnMyTurnChanged; // FOnMyTurnChangedDelegate OnMyTurnChanged;
virtual void SetupInputComponent() override; virtual void SetupInputComponent() override;
AMyPlayerController(); AMyPlayerController();
UFUNCTION(Client, Reliable) // UFUNCTION(Client, Reliable)
void StartTurn(); // void StartTurn();
UFUNCTION(Client, Reliable, BlueprintCallable)
void EndTurn();
UFUNCTION(Server, Reliable)
void MoveTrooper(ATrooper *Trooper, FVector Location);
// UFUNCTION(Client, Reliable, BlueprintCallable)
// void EndTurn();
//
// UFUNCTION(Server, Reliable) // UFUNCTION(Server, Reliable)
// void AttackTrooper(ATrooper *Attacker, ATrooper *Victim); // void MoveTrooper(ATrooper *Trooper, FVector Location);
//
UFUNCTION(Server, Reliable) // // // UFUNCTION(Server, Reliable)
void Attack(ATrooper *Attacker, FVector Location, int ActionIndex); // // // void AttackTrooper(ATrooper *Attacker, ATrooper *Victim);
//
// UFUNCTION(Server, Reliable)
// void Attack(ATrooper *Attacker, FVector Location, int ActionIndex);
UFUNCTION() UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex); void SetPlayerIndex(uint8 NewPlayerIndex);
@ -45,23 +44,26 @@ public:
void SetEnemySelection(const TArray<ATrooper *> &Troopers) const; void SetEnemySelection(const TArray<ATrooper *> &Troopers) const;
private: private:
UPROPERTY(Replicated) // UPROPERTY(Replicated)
bool bIsMyTurn; // bool bIsMyTurn;
//
UPROPERTY(Replicated) // UPROPERTY(Replicated)
int CurrentAction = 0; // int CurrentAction = 0;
//
UPROPERTY(Replicated) UPROPERTY(Replicated)
uint8 PlayerIndex; uint8 PlayerIndex;
//
// UPROPERTY(Replicated)
// ATrooper *SelectedTrooper;
UPROPERTY(Replicated) UFUNCTION()
ATrooper *SelectedTrooper;
void OnLeftMouseClick(); void OnLeftMouseClick();
void SetMyTurn(bool bMyTurn); // void SetMyTurn(bool bMyTurn);
auto GetMyGameMode() const; auto GetMyGameMode() const;
// AMyGameState *GetMyGameState() const; auto GetMyGameState() const;
auto GetMyPlayerState() const;
}; };

@ -3,7 +3,6 @@
#include "MyPlayerStart.h" #include "MyPlayerStart.h"
uint8 AMyPlayerStart::GetPlayerIndex() const uint8 AMyPlayerStart::GetPlayerIndex() const {
{ return PlayerIndex;
return PlayerIndex;
} }

@ -10,15 +10,14 @@
* *
*/ */
UCLASS() UCLASS()
class TURNBASEDTUTORIAL_API AMyPlayerStart : public APlayerStart class TURNBASEDTUTORIAL_API AMyPlayerStart : public APlayerStart {
{ GENERATED_BODY()
GENERATED_BODY()
public:
public:
uint8 GetPlayerIndex() const; uint8 GetPlayerIndex() const;
protected: protected:
private: private:
UPROPERTY(EditAnywhere, Category="Spawn Info") UPROPERTY(EditAnywhere, Category="Spawn Info")
uint8 PlayerIndex=0; uint8 PlayerIndex = 0;
}; };

@ -0,0 +1,149 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPlayerState.h"
#include "MyGameState.h"
#include "Trooper.h"
#include "Net/UnrealNetwork.h"
AMyPlayerState::AMyPlayerState()
: Super(), bIsMyTurn(false), SelectedTrooper(nullptr) {
}
void AMyPlayerState::BeginPlay() {
Super::BeginPlay();
}
void AMyPlayerState::SetPlayerIndex(uint8 NewPlayerIndex) {
PlayerIndex = NewPlayerIndex;
}
bool AMyPlayerState::IsMyTurn() const {
return bIsMyTurn;
}
void AMyPlayerState::SetMyTurn(bool bMyTurn) {
bIsMyTurn = bMyTurn;
if (bIsMyTurn) {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green,
FString::Printf(
TEXT("CURRENT TURN: %d"),
PlayerIndex));
}
}
void AMyPlayerState::StartTurn_Implementation() {
SetMyTurn(true);
UE_LOG(LogTemp, Warning, TEXT("Your turn, %d"), PlayerIndex);
}
void AMyPlayerState::EndTurn_Implementation() {
if (bIsMyTurn) {
UE_LOG(LogTemp, Warning, TEXT("End Turn from player %d"), PlayerIndex);
SetMyTurn(false);
if (SelectedTrooper) {
SelectedTrooper->SetSelection(false);
SelectedTrooper = nullptr;
}
UE_LOG(LogTemp, Warning, TEXT("Not your turn, %d"), PlayerIndex);
// AMyGameMode *gameMode = GetMyGameMode();
// gameMode->CycleTurns();
// Cast<AMyGameState>(GetWorld()->GetGameState())->CycleTurns();
}
}
void AMyPlayerState::MoveTrooper_Implementation(
ATrooper *Trooper,
FVector Location) {
if (Trooper->CheckMoveCorrectness(Location)) {
Trooper->MoveTrooper(Location);
// GetMyGameMode()->CycleTurns();
} else {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red,
FString::Printf(
TEXT("Out of move radius!")));
}
}
void AMyPlayerState::Attack_Implementation(ATrooper *Attacker,
FVector Location,
int ActionIndex) {
if (Attacker && CurrentAction >= 1 && CurrentAction <= 2 &&
Attacker->CheckAttackCorrectness(Location, CurrentAction)) {
Attacker->Attack(CurrentAction);
} else {
UE_LOG(LogTemp, Warning,
TEXT("Out of radius or not enough Action Points"));
}
}
void AMyPlayerState::OnPlayerAction(const FHitResult &HitResult) {
auto const NewlySelectedLocation = HitResult.Location;
ATrooper *NewlySelectedTrooper = Cast<ATrooper>(
HitResult.GetActor());
// skip re-selection
if (SelectedTrooper == NewlySelectedTrooper) {
UE_LOG(LogTemp, Warning, TEXT("Skip reselection"));
return;
}
if (NewlySelectedTrooper == nullptr || !NewlySelectedTrooper->
IsValidLowLevel() || NewlySelectedTrooper->GetPlayerIndex() !=
PlayerIndex) {
if (SelectedTrooper != nullptr && SelectedTrooper->IsValidLowLevel()) {
switch (CurrentAction) {
case 0:
UE_LOG(LogTemp, Warning, TEXT("Do move"));
// move this mf
MoveTrooper(SelectedTrooper, NewlySelectedLocation);
// and reset the selection....
SelectedTrooper->SetSelection(false);
SelectedTrooper = nullptr;
break;
default:
// ATTACK! ATTACK!
UE_LOG(LogTemp, Warning, TEXT("Do attack"));
Attack(SelectedTrooper, NewlySelectedLocation,
CurrentAction);
SelectedTrooper->SetSelection(false);
SelectedTrooper = nullptr;
break;
}
}
} else if (NewlySelectedTrooper != nullptr && NewlySelectedTrooper->
IsValidLowLevel() && NewlySelectedTrooper->GetPlayerIndex() ==
PlayerIndex) {
UE_LOG(LogTemp, Warning, TEXT("Do reselect"));
// our move, selection
if (SelectedTrooper) {
SelectedTrooper->SetSelection(false);
}
SelectedTrooper = NewlySelectedTrooper;
SelectedTrooper->SetSelection(true);
}
}
float AMyPlayerState::SetCurrentActionAndReturnRadius(int Action) {
CurrentAction = Action;
UE_LOG(LogTemp, Warning, TEXT("SetCurrentAction: %d on Player Controller "
"with index %d"), CurrentAction, PlayerIndex);
if (SelectedTrooper) {
return SelectedTrooper->GetActionRadius(CurrentAction);
}
return 0.0f;
}
uint8 AMyPlayerState::GetPlayerIndex() {
return PlayerIndex;
}
void AMyPlayerState::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const {
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyPlayerState, PlayerIndex);
DOREPLIFETIME(AMyPlayerState, CurrentAction);
DOREPLIFETIME(AMyPlayerState, bIsMyTurn);
DOREPLIFETIME(AMyPlayerState, SelectedTrooper);
}

@ -0,0 +1,63 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerState.h"
#include "MyPlayerState.generated.h"
/**
*
*/
UCLASS()
class TURNBASEDTUTORIAL_API AMyPlayerState : public APlayerState {
GENERATED_BODY()
public:
AMyPlayerState();
virtual void BeginPlay() override;
UFUNCTION(Client, Reliable)
void StartTurn();
UFUNCTION(Client, Reliable, BlueprintCallable)
void EndTurn();
UFUNCTION(Server, Reliable)
void MoveTrooper(ATrooper *Trooper, FVector Location);
UFUNCTION(Server, Reliable)
void Attack(ATrooper *Attacker, FVector Location, int ActionIndex);
UFUNCTION()
bool IsMyTurn() const;
UFUNCTION()
void OnPlayerAction(const FHitResult &HitResult);
UFUNCTION(BlueprintCallable)
float SetCurrentActionAndReturnRadius(int Action);
UFUNCTION(BlueprintCallable)
uint8 GetPlayerIndex();
UFUNCTION()
void SetPlayerIndex(uint8 NewPlayerIndex);
private:
UPROPERTY(Replicated)
uint8 PlayerIndex;
UFUNCTION()
void SetMyTurn(bool bMyTurn);
UPROPERTY(Replicated)
bool bIsMyTurn;
UPROPERTY(Replicated)
int CurrentAction = 0;
UPROPERTY(Replicated)
ATrooper *SelectedTrooper;
};

@ -75,10 +75,6 @@ protected:
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
UAbility *SpecialAbility; UAbility *SpecialAbility;
const float MoveRadius = 1500.f;
float AttackRadius = 1000.f;
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
float Speed = 300.0f; float Speed = 300.0f;

Loading…
Cancel
Save