Basic replication of turns implemented

pull/5/head
eyakm1 2 years ago
parent 80f821dfae
commit 77951cd5ee

Binary file not shown.

@ -2,7 +2,8 @@
#include "MyGameMode.h" #include "MyGameMode.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "MyPawn.h"
#include "MyGameState.h"
AMyGameMode::AMyGameMode() : Super() AMyGameMode::AMyGameMode() : Super()
{ {
@ -15,35 +16,35 @@ AMyGameMode::AMyGameMode() : Super()
void AMyGameMode::BeginPlay() void AMyGameMode::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
ATrooper::InitNumberOfTroopersForId(); // ATrooper::InitNumberOfTroopersForId();
UE_LOG(LogTemp, Warning, TEXT("GameMode BeginPlay")); // UE_LOG(LogTemp, Warning, TEXT("GameMode BeginPlay"));
if (GetWorld()->GetMapName().Contains("BattleFieldMap")) // if (GetWorld()->GetMapName().Contains("BattleFieldMap"))
{ // {
UE_LOG(LogTemp, Warning, TEXT("Player Logined")); // UE_LOG(LogTemp, Warning, TEXT("Player Logined"));
InitializeBattleField(); // InitializeBattleField();
GetPlayerController()->StartTurn(); // GetMyPlayerController()->StartTurn();
} // }
} }
void AMyGameMode::InitializeBattleField() const void AMyGameMode::InitializeBattleField() const
{ {
FVector Location(2000.0f, -1000.0f, 0.0f); // FVector Location(2000.0f, -1000.0f, 0.0f);
FRotator Rotation(0.0f, 180.0f, 0.0f); // FRotator Rotation(0.0f, 180.0f, 0.0f);
FActorSpawnParameters const SpawnInfo; // FActorSpawnParameters const SpawnInfo;
for (int i = 0; i < 5; ++i) // for (int i = 0; i < 5; ++i)
{ // {
AActor* Spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo); // AActor* Spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo);
dynamic_cast<ATrooper*>(Spawned)->InitTrooper(Location, true); // dynamic_cast<ATrooper*>(Spawned)->InitTrooper(Location, true);
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};
Rotation = {0.0f, 0.0f, 0.0f}; // Rotation = {0.0f, 0.0f, 0.0f};
for (int i = 0; i < 5; ++i) // for (int i = 0; i < 5; ++i)
{ // {
AActor* Spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo); // AActor* Spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo);
dynamic_cast<ATrooper*>(Spawned)->InitTrooper(Location, false); // dynamic_cast<ATrooper*>(Spawned)->InitTrooper(Location, false);
Location += {0.f, 500.f, 0.0f}; // Location += {0.f, 500.f, 0.0f};
} // }
} }
@ -58,10 +59,13 @@ AActor* AMyGameMode::ChoosePlayerStart_Implementation(AController* Player)
void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player) void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player)
{ {
UE_LOG(LogTemp, Warning, TEXT("InitializeSpawnPointsIfNeeded"));
if (SpawnPoints.Num() != 0) if (SpawnPoints.Num() != 0)
{ {
UE_LOG(LogTemp, Warning, TEXT("InitializeSpawnPointsIfNeeded Exit %d"), SpawnPoints.Num());
return; return;
} }
UE_LOG(LogTemp, Warning, TEXT("Rebuilding spawnpoints"));
const auto World = GetWorld(); const auto World = GetWorld();
for (TActorIterator<AMyPlayerStart> PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator) for (TActorIterator<AMyPlayerStart> PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator)
{ {
@ -70,6 +74,7 @@ void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player)
const APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject<APawn>() : nullptr; const APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject<APawn>() : nullptr;
const FVector ActorLocation = PlayerStart->GetActorLocation(); const FVector ActorLocation = PlayerStart->GetActorLocation();
const FRotator ActorRotation = PlayerStart->GetActorRotation(); const FRotator ActorRotation = PlayerStart->GetActorRotation();
UE_LOG(LogTemp, Warning, TEXT("PlayerStart iterator %d"), PlayerStartIterator->GetPlayerIndex());
if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation, ActorRotation)) if (!World->EncroachingBlockingGeometry(PawnToFit, ActorLocation, ActorRotation))
{ {
SpawnPoints.Add(PlayerStartIterator->GetPlayerIndex(), *PlayerStartIterator); SpawnPoints.Add(PlayerStartIterator->GetPlayerIndex(), *PlayerStartIterator);
@ -79,7 +84,68 @@ void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player)
} }
AMyPlayerController* AMyGameMode::GetPlayerController() const void AMyGameMode::PostLogin(APlayerController* NewPlayer)
{
Super::PostLogin(NewPlayer);
UE_LOG(LogTemp, Warning, TEXT("PostLogin"));
const auto World = GetWorld();
const auto CurrentNumberOfPlayers = GetNumPlayers();
UE_LOG(LogTemp, Warning, TEXT("%d"), CurrentNumberOfPlayers);
if (CurrentNumberOfPlayers == 2)
{
UE_LOG(LogTemp, Warning, TEXT("Game Start"));
// start the game
StartGame();
}
else
{
// delay the game
UE_LOG(LogTemp, Warning, TEXT("Game Delay"));
}
}
void AMyGameMode::StartGame()
{
PlayerInTurn()->StartTurn();
}
AMyPlayerController* AMyGameMode::PlayerInTurn() const
{
return GetMyPlayerController(CurrentPlayerTurn);
}
AMyPlayerController* AMyGameMode::PlayerNotInTurn() const
{
uint8 PlayerControllerIndexNotInTurn = 0;
if (CurrentPlayerTurn == 0)
{
PlayerControllerIndexNotInTurn = 1;
}
else
{
PlayerControllerIndexNotInTurn = 0;
}
return GetMyPlayerController(PlayerControllerIndexNotInTurn);
}
void AMyGameMode::CycleTurns()
{
PlayerInTurn()->EndTurn();
if (CurrentPlayerTurn == 0)
{
CurrentPlayerTurn = 1;
}
else
{
CurrentPlayerTurn = 0;
}
PlayerInTurn()->StartTurn();
}
AMyPlayerController* AMyGameMode::GetMyPlayerController(uint8 const PlayerIndex) const
{ {
return dynamic_cast<AMyPlayerController*>(UGameplayStatics::GetPlayerController(GetWorld(), 0)); return dynamic_cast<AMyPlayerController*>(UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex));
} }

@ -5,8 +5,6 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "EngineUtils.h" #include "EngineUtils.h"
#include "MyPlayerController.h" #include "MyPlayerController.h"
#include "MyGameState.h"
#include "MyPawn.h"
#include "MyPlayerStart.h" #include "MyPlayerStart.h"
#include "GameFramework/GameMode.h" #include "GameFramework/GameMode.h"
#include "MyGameMode.generated.h" #include "MyGameMode.generated.h"
@ -22,14 +20,31 @@ public:
virtual AActor* ChoosePlayerStart_Implementation(AController* Player) override; virtual AActor* ChoosePlayerStart_Implementation(AController* Player) override;
virtual void PostLogin(APlayerController* NewPlayer) override;
virtual void BeginPlay() override; virtual void BeginPlay() override;
UFUNCTION(BlueprintCallable)
void CycleTurns();
private: private:
void InitializeSpawnPointsIfNeeded(AController* Player); void InitializeSpawnPointsIfNeeded(AController* Player);
void InitializeBattleField() const; void InitializeBattleField() const;
TMap<uint8, AMyPlayerStart*> SpawnPoints; TMap<uint8, AMyPlayerStart*> SpawnPoints{};
AMyPlayerController* GetMyPlayerController(uint8 const PlayerIndex) const;
UFUNCTION(BlueprintCallable)
void StartGame();
UFUNCTION(BlueprintPure)
AMyPlayerController* PlayerInTurn() const;
UFUNCTION(BlueprintPure)
AMyPlayerController* PlayerNotInTurn() const;
AMyPlayerController* GetPlayerController() const; UPROPERTY()
uint8 CurrentPlayerTurn{0};
}; };

@ -1,8 +1,10 @@
// Fill out your copyright notice in the Description page of Project Settings. // Fill out your copyright notice in the Description page of Project Settings.
#include "MyPlayerController.h" #include "MyPlayerController.h"
#include "Kismet/GameplayStatics.h"
#include "MyGameMode.h"
AMyPlayerController::AMyPlayerController() : Super(), IsMyTurn(false), SelectedTrooper(nullptr) AMyPlayerController::AMyPlayerController() : Super(), bIsMyTurn(false), SelectedTrooper(nullptr)
{ {
UE_LOG(LogTemp, Warning, TEXT("Player controller created")); UE_LOG(LogTemp, Warning, TEXT("Player controller created"));
} }
@ -14,64 +16,92 @@ void AMyPlayerController::SetupInputComponent()
&AMyPlayerController::OnLeftMouseClick); &AMyPlayerController::OnLeftMouseClick);
} }
void AMyPlayerController::StartTurn() void AMyPlayerController::SetMyTurn(bool bMyTurn)
{ {
IsMyTurn = true; bIsMyTurn = bMyTurn;
if (bIsMyTurn)
{
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green,
FString::Printf(
TEXT("YOUR TURN!")));
}
// OnMyTurnChanged.ExecuteIfBound(bIsMyTurn);
}
void AMyPlayerController::StartTurn_Implementation()
{
SetMyTurn(true);
UE_LOG(LogTemp, Warning, TEXT("Your turn")); UE_LOG(LogTemp, Warning, TEXT("Your turn"));
} }
void AMyPlayerController::EndTurn() void AMyPlayerController::EndTurn_Implementation()
{ {
IsMyTurn = false; SetMyTurn(false);
UE_LOG(LogTemp, Warning, TEXT("Not your turn")); UE_LOG(LogTemp, Warning, TEXT("Not your turn"));
} }
void AMyPlayerController::SetTrooperIsMoving(bool isMoving) void AMyPlayerController::SetTrooperIsMoving(bool isMoving)
{ {
IsThereTrooperMoving = isMoving; bIsThereTrooperMoving = isMoving;
} }
void AMyPlayerController::OnLeftMouseClick() auto AMyPlayerController::GetMyGameMode() const
{
if (IsThereTrooperMoving)
{
return;
}
UE_LOG(LogTemp, Warning, TEXT("Mouse clicked"));
FHitResult HitResult;
bool const IsHitResult = GetHitResultUnderCursorByChannel(TraceTypeQuery1, false, HitResult);
if (IsHitResult)
{
AActor* Actor = HitResult.Actor.Get();
if (Actor->ActorHasTag(FName("Trooper")))
{ {
ATrooper* Trooper = dynamic_cast<ATrooper*>(Actor); return dynamic_cast<AMyGameMode*>(UGameplayStatics::GetGameMode(GetWorld()));
if (Trooper != nullptr && Trooper != SelectedTrooper)
{
if (Trooper->IsOnPlayersSide())
{
UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, on our side"),
Trooper->GetId());
SelectedTrooper = Trooper;
} }
else
void AMyPlayerController::MoveHero_Implementation()
{ {
UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, enemy"), GetMyGameMode()->CycleTurns();
Trooper->GetId());
UE_LOG(LogTemp, Warning, TEXT("Trooper #%d hit enemy trooper #%d"),
SelectedTrooper->GetId(), Trooper->GetId());
} }
}
}
else if (Actor->ActorHasTag(FName("Floor"))) void AMyPlayerController::OnLeftMouseClick()
{ {
UE_LOG(LogTemp, Warning, TEXT("Hitted floor: %f, %f, %f"), HitResult.Location.X, // if (bIsThereTrooperMoving)
HitResult.Location.Y, HitResult.Location.Z); // {
if (SelectedTrooper != nullptr) // return;
// }
// UE_LOG(LogTemp, Warning, TEXT("Mouse clicked"));
// FHitResult HitResult;
// bool const IsHitResult = GetHitResultUnderCursorByChannel(TraceTypeQuery1, false, HitResult);
// if (IsHitResult)
// {
// AActor* Actor = HitResult.Actor.Get();
// if (Actor->ActorHasTag(FName("Trooper")))
// {
// ATrooper* Trooper = dynamic_cast<ATrooper*>(Actor);
// if (Trooper != nullptr && Trooper != SelectedTrooper)
// {
// if (Trooper->IsOnPlayersSide())
// {
// UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, on our side"),
// Trooper->GetId());
// SelectedTrooper = Trooper;
// }
// else
// {
// UE_LOG(LogTemp, Warning, TEXT("Hitted trooper id: %d, enemy"),
// Trooper->GetId());
// UE_LOG(LogTemp, Warning, TEXT("Trooper #%d hit enemy trooper #%d"),
// SelectedTrooper->GetId(), Trooper->GetId());
// }
// }
// }
// else if (Actor->ActorHasTag(FName("Floor")))
// {
// UE_LOG(LogTemp, Warning, TEXT("Hitted floor: %f, %f, %f"), HitResult.Location.X,
// HitResult.Location.Y, HitResult.Location.Z);
// if (SelectedTrooper != nullptr)
// {
// SelectedTrooper->MoveTrooper(HitResult.Location);
// bIsThereTrooperMoving = true;
// }
// }
// }
if (!bIsMyTurn)
{ {
SelectedTrooper->MoveTrooper(HitResult.Location); return;
IsThereTrooperMoving = true;
}
}
} }
MoveHero();
} }

@ -9,28 +9,42 @@
/** /**
* *
*/ */
// DECLARE_DYNAMIC_DELEGATE_OneParam(FOnMyTurnChangedDelegate, bool, bMyTurn);
UCLASS() UCLASS()
class TURNBASEDTUTORIAL_API AMyPlayerController : public APlayerController class TURNBASEDTUTORIAL_API AMyPlayerController : public APlayerController
{ {
GENERATED_BODY() GENERATED_BODY()
public: public:
// FOnMyTurnChangedDelegate OnMyTurnChanged;
AMyPlayerController(); AMyPlayerController();
UFUNCTION(Client, Reliable)
void StartTurn(); void StartTurn();
UFUNCTION(Client, Reliable)
void EndTurn(); void EndTurn();
UFUNCTION(Server, Reliable)
void MoveHero();
virtual void SetupInputComponent() override; virtual void SetupInputComponent() override;
void SetTrooperIsMoving(bool isMoving); void SetTrooperIsMoving(bool isMoving);
private: private:
bool IsMyTurn; bool bIsMyTurn;
bool IsThereTrooperMoving = false; bool bIsThereTrooperMoving = false;
ATrooper* SelectedTrooper; ATrooper* SelectedTrooper;
void OnLeftMouseClick(); void OnLeftMouseClick();
void SetMyTurn(bool bMyTurn);
auto GetMyGameMode() const;
}; };

Loading…
Cancel
Save