diff --git a/Content/BP_MyGameMode.uasset b/Content/BP_MyGameMode.uasset index aec131a..d432830 100644 Binary files a/Content/BP_MyGameMode.uasset and b/Content/BP_MyGameMode.uasset differ diff --git a/Content/BP_MyPlayerController.uasset b/Content/BP_MyPlayerController.uasset index ca2c84f..42ec990 100644 Binary files a/Content/BP_MyPlayerController.uasset and b/Content/BP_MyPlayerController.uasset differ diff --git a/Content/BattleField/BattleFieldMap.umap b/Content/BattleField/BattleFieldMap.umap index d3fb3a4..2c00f7a 100644 Binary files a/Content/BattleField/BattleFieldMap.umap and b/Content/BattleField/BattleFieldMap.umap differ diff --git a/Source/TurnBasedTutorial/MyGameMode.cpp b/Source/TurnBasedTutorial/MyGameMode.cpp index 3f36c01..2103d43 100644 --- a/Source/TurnBasedTutorial/MyGameMode.cpp +++ b/Source/TurnBasedTutorial/MyGameMode.cpp @@ -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(Location, Rotation, SpawnInfo); - dynamic_cast(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(Location, Rotation, SpawnInfo); - dynamic_cast(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(Location, Rotation, SpawnInfo); + // dynamic_cast(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(Location, Rotation, SpawnInfo); + // dynamic_cast(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 PlayerStartIterator(GetWorld()); PlayerStartIterator; ++PlayerStartIterator) { @@ -70,6 +74,7 @@ void AMyGameMode::InitializeSpawnPointsIfNeeded(AController* Player) const APawn* PawnToFit = PawnClass ? PawnClass->GetDefaultObject() : 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(UGameplayStatics::GetPlayerController(GetWorld(), 0)); + return dynamic_cast(UGameplayStatics::GetPlayerController(GetWorld(), PlayerIndex)); } diff --git a/Source/TurnBasedTutorial/MyGameMode.h b/Source/TurnBasedTutorial/MyGameMode.h index 9d0eb1b..2304cbd 100644 --- a/Source/TurnBasedTutorial/MyGameMode.h +++ b/Source/TurnBasedTutorial/MyGameMode.h @@ -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 SpawnPoints; + TMap 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}; }; diff --git a/Source/TurnBasedTutorial/MyPlayerController.cpp b/Source/TurnBasedTutorial/MyPlayerController.cpp index 76ee933..8056434 100644 --- a/Source/TurnBasedTutorial/MyPlayerController.cpp +++ b/Source/TurnBasedTutorial/MyPlayerController.cpp @@ -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(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(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(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(); } diff --git a/Source/TurnBasedTutorial/MyPlayerController.h b/Source/TurnBasedTutorial/MyPlayerController.h index 357e164..2060f43 100644 --- a/Source/TurnBasedTutorial/MyPlayerController.h +++ b/Source/TurnBasedTutorial/MyPlayerController.h @@ -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; };