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 "Kismet/GameplayStatics.h"
#include "MyPawn.h"
#include "MyGameState.h"
AMyGameMode::AMyGameMode() : Super()
{
@ -15,35 +16,35 @@ AMyGameMode::AMyGameMode() : Super()
void AMyGameMode::BeginPlay()
{
Super::BeginPlay();
ATrooper::InitNumberOfTroopersForId();
UE_LOG(LogTemp, Warning, TEXT("GameMode BeginPlay"));
if (GetWorld()->GetMapName().Contains("BattleFieldMap"))
{
UE_LOG(LogTemp, Warning, TEXT("Player Logined"));
InitializeBattleField();
GetPlayerController()->StartTurn();
}
// ATrooper::InitNumberOfTroopersForId();
// UE_LOG(LogTemp, Warning, TEXT("GameMode BeginPlay"));
// if (GetWorld()->GetMapName().Contains("BattleFieldMap"))
// {
// UE_LOG(LogTemp, Warning, TEXT("Player Logined"));
// InitializeBattleField();
// GetMyPlayerController()->StartTurn();
// }
}
void AMyGameMode::InitializeBattleField() const
{
FVector Location(2000.0f, -1000.0f, 0.0f);
FRotator Rotation(0.0f, 180.0f, 0.0f);
FActorSpawnParameters const SpawnInfo;
for (int i = 0; i < 5; ++i)
{
AActor* Spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo);
dynamic_cast<ATrooper*>(Spawned)->InitTrooper(Location, true);
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)
{
AActor* Spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo);
dynamic_cast<ATrooper*>(Spawned)->InitTrooper(Location, false);
Location += {0.f, 500.f, 0.0f};
}
// FVector Location(2000.0f, -1000.0f, 0.0f);
// FRotator Rotation(0.0f, 180.0f, 0.0f);
// FActorSpawnParameters const SpawnInfo;
// for (int i = 0; i < 5; ++i)
// {
// AActor* Spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo);
// dynamic_cast<ATrooper*>(Spawned)->InitTrooper(Location, true);
// 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)
// {
// AActor* Spawned = GetWorld()->SpawnActor<ATrooper>(Location, Rotation, SpawnInfo);
// dynamic_cast<ATrooper*>(Spawned)->InitTrooper(Location, false);
// Location += {0.f, 500.f, 0.0f};
// }
}
@ -58,10 +59,13 @@ AActor* AMyGameMode::ChoosePlayerStart_Implementation(AController* Player)
void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player)
{
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();
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 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);
@ -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 "EngineUtils.h"
#include "MyPlayerController.h"
#include "MyGameState.h"
#include "MyPawn.h"
#include "MyPlayerStart.h"
#include "GameFramework/GameMode.h"
#include "MyGameMode.generated.h"
@ -22,14 +20,31 @@ public:
virtual AActor* ChoosePlayerStart_Implementation(AController* Player) override;
virtual void PostLogin(APlayerController* NewPlayer) override;
virtual void BeginPlay() override;
UFUNCTION(BlueprintCallable)
void CycleTurns();
private:
void InitializeSpawnPointsIfNeeded(AController* Player);
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.
#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"));
}
@ -14,64 +16,92 @@ void AMyPlayerController::SetupInputComponent()
&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"));
}
void AMyPlayerController::EndTurn()
void AMyPlayerController::EndTurn_Implementation()
{
IsMyTurn = false;
SetMyTurn(false);
UE_LOG(LogTemp, Warning, TEXT("Not your turn"));
}
void AMyPlayerController::SetTrooperIsMoving(bool isMoving)
{
IsThereTrooperMoving = isMoving;
bIsThereTrooperMoving = isMoving;
}
auto AMyPlayerController::GetMyGameMode() const
{
return dynamic_cast<AMyGameMode*>(UGameplayStatics::GetGameMode(GetWorld()));
}
void AMyPlayerController::MoveHero_Implementation()
{
GetMyGameMode()->CycleTurns();
}
void AMyPlayerController::OnLeftMouseClick()
{
if (IsThereTrooperMoving)
// if (bIsThereTrooperMoving)
// {
// 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)
{
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);
IsThereTrooperMoving = true;
}
}
}
MoveHero();
}

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

Loading…
Cancel
Save