diff --git a/Content/BattleField/BP_BattleUI.uasset b/Content/BattleField/BP_BattleUI.uasset index 8c09052..818a0f2 100644 Binary files a/Content/BattleField/BP_BattleUI.uasset and b/Content/BattleField/BP_BattleUI.uasset differ diff --git a/Content/MainMenu/BP_SessionListEntry.uasset b/Content/MainMenu/BP_SessionListEntry.uasset index 36dc2c5..bc879b1 100644 Binary files a/Content/MainMenu/BP_SessionListEntry.uasset and b/Content/MainMenu/BP_SessionListEntry.uasset differ diff --git a/Content/MainMenu/BP_SessionListMenu.uasset b/Content/MainMenu/BP_SessionListMenu.uasset index 0f1b13f..60a0eb8 100644 Binary files a/Content/MainMenu/BP_SessionListMenu.uasset and b/Content/MainMenu/BP_SessionListMenu.uasset differ diff --git a/Content/MainMenu/MainMenuLevel.umap b/Content/MainMenu/MainMenuLevel.umap index 1e37f74..c6ab3b1 100644 Binary files a/Content/MainMenu/MainMenuLevel.umap and b/Content/MainMenu/MainMenuLevel.umap differ diff --git a/Content/ManageSquadMenu/BP_ManageSquadWidget.uasset b/Content/ManageSquadMenu/BP_ManageSquadWidget.uasset new file mode 100644 index 0000000..2fbcd2c Binary files /dev/null and b/Content/ManageSquadMenu/BP_ManageSquadWidget.uasset differ diff --git a/Source/TurnBasedTutorial/BattleMode/BattlePlayerController.cpp b/Source/TurnBasedTutorial/BattleMode/BattlePlayerController.cpp index 25857d9..45f8989 100644 --- a/Source/TurnBasedTutorial/BattleMode/BattlePlayerController.cpp +++ b/Source/TurnBasedTutorial/BattleMode/BattlePlayerController.cpp @@ -17,9 +17,7 @@ ABattlePlayerController::ABattlePlayerController() void ABattlePlayerController::BeginPlay() { Super::BeginPlay(); - UUserWidget *CreatedWidget = CreateWidget( - GetWorld(), WidgetClass); - CreatedWidget->AddToViewport(); + CreateBattleWidget(); } void ABattlePlayerController::SetupInputComponent() { @@ -64,8 +62,9 @@ void ABattlePlayerController::EndTurn_Implementation() { // GetMyGameState()->CycleTurns(); // } // GetMyPlayerState()->CycleTurns(); - if (GetMyGameState()->IsInTurn(PlayerIndex)) + if (GetMyGameState()->IsInTurn(PlayerIndex)) { GetMyGameState()->CycleTurns(); + } } // void AMyPlayerController::EndTurn_Implementation() { @@ -147,6 +146,20 @@ uint8 ABattlePlayerController::GetPlayerIndex() const { return PlayerIndex; } +void ABattlePlayerController::CreateBattleWidget_Implementation() { + BattleWidget = CreateWidget( + GetWorld(), WidgetClass); + BattleWidget->AddToViewport(); + // GetPlayerState()->SetBattleWidget(BattleWidget); + +} + +void ABattlePlayerController::SetWidgetTurn_Implementation(bool bIsMyTurn) { + if (BattleWidget) { + BattleWidget->SetWhoseTurnText(bIsMyTurn); + } +} + void ABattlePlayerController::StartPlayingMusic_Implementation( USoundBase *BackgroundSound) const { UGameplayStatics::PlaySound2D(GetWorld(), BackgroundSound); diff --git a/Source/TurnBasedTutorial/BattleMode/BattlePlayerController.h b/Source/TurnBasedTutorial/BattleMode/BattlePlayerController.h index e0ac385..02ed29c 100644 --- a/Source/TurnBasedTutorial/BattleMode/BattlePlayerController.h +++ b/Source/TurnBasedTutorial/BattleMode/BattlePlayerController.h @@ -2,6 +2,7 @@ #pragma once #include "CoreMinimal.h" +#include "BattleUI.h" #include "GameFramework/PlayerController.h" #include "BattlePlayerController.generated.h" @@ -53,7 +54,16 @@ public: UFUNCTION(Client, Reliable) void StartPlayingMusic(USoundBase *BackgroundSound) const; + UFUNCTION(Client, Reliable) + void SetWidgetTurn(bool bIsMyTurn); + private: + UFUNCTION(Client, Reliable) + void CreateBattleWidget(); + + UPROPERTY() + UBattleUI *BattleWidget; + UPROPERTY(EditAnywhere) TSubclassOf WidgetClass; diff --git a/Source/TurnBasedTutorial/BattleMode/BattlePlayerState.cpp b/Source/TurnBasedTutorial/BattleMode/BattlePlayerState.cpp index 93fcfaa..d5e459f 100644 --- a/Source/TurnBasedTutorial/BattleMode/BattlePlayerState.cpp +++ b/Source/TurnBasedTutorial/BattleMode/BattlePlayerState.cpp @@ -39,6 +39,11 @@ void ABattlePlayerState::SetPlayerIndex(uint8 NewPlayerIndex) { PlayerIndex = NewPlayerIndex; } +// void ABattlePlayerState:: +// SetBattleWidget_Implementation(UBattleUI *BattleWidget) { +// BattleUI = BattleWidget; +// } + void ABattlePlayerState::GameOver_Implementation(int PlayerLoseIndex) { UGameOverWidget *CreatedWidget = CreateWidget( GetWorld(), GameOverWidgetClass); @@ -116,12 +121,14 @@ bool ABattlePlayerState::IsMyTurn() const { void ABattlePlayerState::SetMyTurn(bool bMyTurn) { bIsMyTurn = bMyTurn; - if (bIsMyTurn) { - GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, - FString::Printf( - TEXT("CURRENT TURN: %d"), - PlayerIndex)); - } + Cast(GetWorld()->GetFirstPlayerController())-> + SetWidgetTurn(bIsMyTurn); + // if (bIsMyTurn) { + // GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, + // FString::Printf( + // TEXT("CURRENT TURN: %d"), + // PlayerIndex)); + // } } void ABattlePlayerState::StartTurn_Implementation() { @@ -139,6 +146,7 @@ void ABattlePlayerState::EndTurn_Implementation() { SelectedTrooper = nullptr; } UE_LOG(LogTemp, Warning, TEXT("Not your turn, %d"), PlayerIndex); + // AMyGameMode *gameMode = GetMyGameMode(); // gameMode->CycleTurns(); // Cast(GetWorld()->GetGameState())->CycleTurns(); diff --git a/Source/TurnBasedTutorial/BattleMode/BattlePlayerState.h b/Source/TurnBasedTutorial/BattleMode/BattlePlayerState.h index a68163b..b325b16 100644 --- a/Source/TurnBasedTutorial/BattleMode/BattlePlayerState.h +++ b/Source/TurnBasedTutorial/BattleMode/BattlePlayerState.h @@ -4,6 +4,7 @@ #include "Trooper/Trooper.h" #include "CoreMinimal.h" +#include "BattlePlayerController.h" #include "GameFramework/PlayerState.h" #include "BattlePlayerState.generated.h" @@ -58,8 +59,14 @@ public: UFUNCTION(Client, Reliable) void GameOver(int PlayerLoseIndex); + + // UFUNCTION(Client, Reliable) + // void SetBattleWidget(UBattleUI *BattleWidget); protected: + // UPROPERTY() + // UBattleUI *BattleUI; + UPROPERTY(EditAnywhere, BlueprintReadWrite) TSubclassOf GameOverWidgetClass; diff --git a/Source/TurnBasedTutorial/BattleMode/BattleUI.cpp b/Source/TurnBasedTutorial/BattleMode/BattleUI.cpp new file mode 100644 index 0000000..ef28c7b --- /dev/null +++ b/Source/TurnBasedTutorial/BattleMode/BattleUI.cpp @@ -0,0 +1,63 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#include "Components/Button.h" +#include "Components/TextBlock.h" +#include "BattleUI.h" +#include "BattlePlayerController.h" +#include "BattlePlayerState.h" + + +void UBattleUI::NativeConstruct() { + Super::NativeConstruct(); + EndTurnButton->OnClicked.AddDynamic(this, &ThisClass::OnEndTurnClicked); + ButtonAction_0->OnClicked.AddDynamic(this, &ThisClass::OnActionSwitched_0); + ButtonAction_1->OnClicked.AddDynamic(this, &ThisClass::OnActionSwitched_1); + ButtonAction_2->OnClicked.AddDynamic(this, &ThisClass::OnActionSwitched_2); +} + +void UBattleUI::SetWidgetText_Implementation(const FString &Text) { + InformationText->SetText(FText::FromString(Text)); +} + +void UBattleUI::SetWhoseTurnText_Implementation(bool IsThisPlayerTurn) { + if (IsThisPlayerTurn) { + SetWidgetText(TEXT("Your turn!")); + } else { + SetWidgetText(TEXT("Opponent's turn")); + } +} + +void UBattleUI::OnEndTurnClicked() { + Cast(GetWorld()->GetFirstPlayerController())-> + EndTurn(); +} + +void UBattleUI::OnActionSwitched_0() { + ButtonAction_0->SetIsEnabled(false); + ButtonAction_1->SetIsEnabled(true); + ButtonAction_2->SetIsEnabled(true); + ActionType = 0; + OnActionSwitched(); +} + +void UBattleUI::OnActionSwitched_1() { + ButtonAction_0->SetIsEnabled(true); + ButtonAction_1->SetIsEnabled(false); + ButtonAction_2->SetIsEnabled(true); + ActionType = 1; + OnActionSwitched(); +} + +void UBattleUI::OnActionSwitched_2() { + ButtonAction_0->SetIsEnabled(true); + ButtonAction_1->SetIsEnabled(true); + ButtonAction_2->SetIsEnabled(false); + ActionType = 2; + OnActionSwitched(); +} + +void UBattleUI::OnActionSwitched() const { + Cast(GetWorld()->GetFirstPlayerController())-> + GetPlayerState()->SetCurrentAction(ActionType); +} + diff --git a/Source/TurnBasedTutorial/BattleMode/BattleUI.h b/Source/TurnBasedTutorial/BattleMode/BattleUI.h new file mode 100644 index 0000000..4015b91 --- /dev/null +++ b/Source/TurnBasedTutorial/BattleMode/BattleUI.h @@ -0,0 +1,58 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Blueprint/UserWidget.h" +#include "BattleUI.generated.h" + +/** + * + */ +UCLASS() +class TURNBASEDTUTORIAL_API UBattleUI : public UUserWidget { + GENERATED_BODY() + +public: + virtual void NativeConstruct() override; + + UFUNCTION(Client, Reliable) + void SetWhoseTurnText(bool IsThisPlayerTurn); + + UFUNCTION(Client, Reliable) + void SetWidgetText(const FString &Text); + +protected: + int ActionType = 0; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget)) + class UButton *EndTurnButton; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget)) + class UButton *ButtonAction_0; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget)) + class UButton *ButtonAction_1; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget)) + class UButton *ButtonAction_2; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget)) + class UTextBlock *InformationText; + + UFUNCTION() + void OnEndTurnClicked(); + + UFUNCTION() + void OnActionSwitched_0(); + + UFUNCTION() + void OnActionSwitched_1(); + + UFUNCTION() + void OnActionSwitched_2(); + + UFUNCTION() + void OnActionSwitched() const; + +}; diff --git a/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.cpp b/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.cpp index 0baf22f..22fb0dc 100644 --- a/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.cpp +++ b/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.cpp @@ -12,18 +12,19 @@ void UMainMenuWidget::NativeConstruct() { this, &ThisClass::UMainMenuWidget::OnHostOnlineGameButtonClicked); GetMyGameSubsystem()->OnCreateSessionCompleteEvent.AddDynamic( - this, &ThisClass::StartSessionWhenCreatingSessonComplete); + this, &ThisClass::StartSessionWhenCreatingSessionComplete); } void UMainMenuWidget::OnHostOnlineGameButtonClicked() { // Ensure we have left any session GetMyGameSubsystem()->DestroySession(); - - GetMyGameSubsystem()->CreateSession( - "Lobby " + FString::FromInt(FMath::RandRange(1, 1e6)), 2, true); + + const FString SessionName = "Lobby " + FString::FromInt( + FMath::RandRange(1, 1e6)); + GetMyGameSubsystem()->CreateSession(SessionName, 2, true); } -void UMainMenuWidget::StartSessionWhenCreatingSessonComplete(bool bSuccess) { +void UMainMenuWidget::StartSessionWhenCreatingSessionComplete(bool bSuccess) { if (!bSuccess) { return; } diff --git a/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.h b/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.h index 992d040..e3b81d6 100644 --- a/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.h +++ b/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.h @@ -26,7 +26,7 @@ protected: void OnHostOnlineGameButtonClicked(); UFUNCTION() - void StartSessionWhenCreatingSessonComplete(bool bSuccess); + void StartSessionWhenCreatingSessionComplete(bool bSuccess); private: USessionsGameInstanceSubsystem *GetMyGameSubsystem() const; diff --git a/Source/TurnBasedTutorial/MainMenu/SessionListEntryWidget.cpp b/Source/TurnBasedTutorial/MainMenu/SessionListEntryWidget.cpp index 849328b..b136433 100644 --- a/Source/TurnBasedTutorial/MainMenu/SessionListEntryWidget.cpp +++ b/Source/TurnBasedTutorial/MainMenu/SessionListEntryWidget.cpp @@ -4,10 +4,18 @@ #include "SessionListEntryWidget.h" #include "OnlineSessionSettings.h" #include "Components/TextBlock.h" +#include "Components/Button.h" +#include "Kismet/GameplayStatics.h" + +void USessionListEntryWidget::NativeConstruct() { + Super::NativeConstruct(); + JoinSessionButton->OnClicked.AddDynamic( + this, &ThisClass::OnJoinButton); +} void USessionListEntryWidget::Update(int SessionIndex, - const FOnlineSessionSearchResult & - Session) { + const FOnlineSessionSearchResult & + Session) { SessionId = SessionIndex; IndexText->SetText(FText::AsNumber(SessionIndex + 1)); @@ -22,3 +30,16 @@ void USessionListEntryWidget::Update(int SessionIndex, PlayersCountText->SetText(FText::AsNumber(CurPlayerCount)); PingText->SetText(FText::AsNumber(Session.PingInMs)); } + +void USessionListEntryWidget::OnJoinButton() { + GetMyGameSubsystem()->JoinSession(SessionId); +} + +USessionsGameInstanceSubsystem * +USessionListEntryWidget::GetMyGameSubsystem() const { + const UGameInstance *GameInstance = UGameplayStatics::GetGameInstance( + GetWorld()); + USessionsGameInstanceSubsystem *GameInstanceSubsystem = GameInstance-> + GetSubsystem(); + return GameInstanceSubsystem; +} diff --git a/Source/TurnBasedTutorial/MainMenu/SessionListEntryWidget.h b/Source/TurnBasedTutorial/MainMenu/SessionListEntryWidget.h index 8c74028..0b3686f 100644 --- a/Source/TurnBasedTutorial/MainMenu/SessionListEntryWidget.h +++ b/Source/TurnBasedTutorial/MainMenu/SessionListEntryWidget.h @@ -3,6 +3,7 @@ #pragma once #include "CoreMinimal.h" +#include "../SessionsGameInstanceSubsystem.h" #include "Blueprint/UserWidget.h" #include "SessionListEntryWidget.generated.h" @@ -14,6 +15,8 @@ class TURNBASEDTUTORIAL_API USessionListEntryWidget : public UUserWidget { GENERATED_BODY() public: + virtual void NativeConstruct() override; + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget)) class UTextBlock *IndexText; @@ -26,8 +29,18 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget)) class UTextBlock *PingText; + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget)) + class UButton *JoinSessionButton; + void Update(int SessionIndex, const FOnlineSessionSearchResult &Session); + +protected: int SessionId; FString SessionName; + + USessionsGameInstanceSubsystem *GetMyGameSubsystem() const; + + UFUNCTION() + void OnJoinButton(); }; diff --git a/Source/TurnBasedTutorial/MainMenu/SessionListWidget.cpp b/Source/TurnBasedTutorial/MainMenu/SessionListWidget.cpp index af32814..e4bd9e9 100644 --- a/Source/TurnBasedTutorial/MainMenu/SessionListWidget.cpp +++ b/Source/TurnBasedTutorial/MainMenu/SessionListWidget.cpp @@ -13,8 +13,8 @@ void USessionListWidget::NativeConstruct() { RefreshListButton->OnClicked.AddDynamic( this, &ThisClass::OnRefreshListButtonClicked); - ConnectToSelectedSessionButton->OnClicked.AddDynamic( - this, &ThisClass::ConnectToFirstSession); + // ConnectToSelectedSessionButton->OnClicked.AddDynamic( + // this, &ThisClass::ConnectToFirstSession); const auto MyGameInstanceSubsystem = GetMyGameSubsystem(); MyGameInstanceSubsystem->OnFindSessionsCompleteEvent.AddUObject( @@ -54,9 +54,9 @@ void USessionListWidget::OnRefreshListButtonClicked() { GetMyGameSubsystem()->FindSessions(10, true); } -void USessionListWidget::ConnectToFirstSession() { - GetMyGameSubsystem()->JoinSession(0); -} +// void USessionListWidget::ConnectToFirstSession() { + // GetMyGameSubsystem()->JoinSession(0); +// } void USessionListWidget::OnJoinSessionSuccess( diff --git a/Source/TurnBasedTutorial/MainMenu/SessionListWidget.h b/Source/TurnBasedTutorial/MainMenu/SessionListWidget.h index e0b43ef..f9cdf4e 100644 --- a/Source/TurnBasedTutorial/MainMenu/SessionListWidget.h +++ b/Source/TurnBasedTutorial/MainMenu/SessionListWidget.h @@ -17,8 +17,8 @@ class TURNBASEDTUTORIAL_API USessionListWidget : public UUserWidget { protected: virtual void NativeConstruct() override; - UPROPERTY(meta = (BindWidget)) - class UButton *ConnectToSelectedSessionButton; + // UPROPERTY(meta = (BindWidget)) + // class UButton *ConnectToSelectedSessionButton; UPROPERTY(meta = (BindWidget)) class UButton *GoBackToMainMenuButton; @@ -43,6 +43,6 @@ private: UFUNCTION() void OnRefreshListButtonClicked(); - UFUNCTION() - void ConnectToFirstSession(); + // UFUNCTION() + // void ConnectToFirstSession(); }; diff --git a/Source/TurnBasedTutorial/ManageSquad/ManageSquadPlayerController.cpp b/Source/TurnBasedTutorial/ManageSquad/ManageSquadPlayerController.cpp index 72d4c77..1fada52 100644 --- a/Source/TurnBasedTutorial/ManageSquad/ManageSquadPlayerController.cpp +++ b/Source/TurnBasedTutorial/ManageSquad/ManageSquadPlayerController.cpp @@ -3,6 +3,7 @@ #include "ManageSquadPlayerController.h" #include "ManageSquadGameState.h" +#include "ManageSquadWidget.h" AManageSquadPlayerController::AManageSquadPlayerController() { SetShowMouseCursor(true); @@ -14,6 +15,19 @@ void AManageSquadPlayerController::SetupInputComponent() { &AManageSquadPlayerController::OnLeftMouseClick); } +void AManageSquadPlayerController::BeginPlay() { + Super::BeginPlay(); + const TSoftClassPtr WidgetClass = TSoftClassPtr< + UUserWidget>(FSoftObjectPath( + "WidgetBlueprint'/Game/ManageSquadMenu/BP_ManageSquadWidget.BP_ManageSquadWidget_C'" + )); + UUserWidget *CreatedWidget = CreateWidget( + GetWorld(), WidgetClass.LoadSynchronous()); + if (CreatedWidget) { + CreatedWidget->AddToViewport(); + } +} + void AManageSquadPlayerController::OnLeftMouseClick() { UE_LOG(LogTemp, Warning, TEXT("Mouse clicked")); FHitResult HitResult; diff --git a/Source/TurnBasedTutorial/ManageSquad/ManageSquadPlayerController.h b/Source/TurnBasedTutorial/ManageSquad/ManageSquadPlayerController.h index 12749f0..0c5e104 100644 --- a/Source/TurnBasedTutorial/ManageSquad/ManageSquadPlayerController.h +++ b/Source/TurnBasedTutorial/ManageSquad/ManageSquadPlayerController.h @@ -20,6 +20,8 @@ public: virtual void SetupInputComponent() override; + virtual void BeginPlay() override; + private: UPROPERTY() AManageSquadTrooper *SelectedTrooper; diff --git a/Source/TurnBasedTutorial/ManageSquad/ManageSquadWidget.cpp b/Source/TurnBasedTutorial/ManageSquad/ManageSquadWidget.cpp new file mode 100644 index 0000000..f68b47d --- /dev/null +++ b/Source/TurnBasedTutorial/ManageSquad/ManageSquadWidget.cpp @@ -0,0 +1,17 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#include "ManageSquadWidget.h" +#include "Components/Button.h" +#include "Kismet/GameplayStatics.h" + + +void UManageSquadWidget::NativeConstruct() { + Super::NativeConstruct(); + BackButton->OnClicked.AddDynamic( + this, &ThisClass::UManageSquadWidget::OnBackButtonClicked); +} + +void UManageSquadWidget::OnBackButtonClicked() { + UGameplayStatics::OpenLevel(GetWorld(), "MainMenuLevel"); + RemoveFromParent(); +} diff --git a/Source/TurnBasedTutorial/ManageSquad/ManageSquadWidget.h b/Source/TurnBasedTutorial/ManageSquad/ManageSquadWidget.h new file mode 100644 index 0000000..6fb284c --- /dev/null +++ b/Source/TurnBasedTutorial/ManageSquad/ManageSquadWidget.h @@ -0,0 +1,25 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Blueprint/UserWidget.h" +#include "ManageSquadWidget.generated.h" + +/** + * + */ +UCLASS() +class TURNBASEDTUTORIAL_API UManageSquadWidget : public UUserWidget { + GENERATED_BODY() + +public: + virtual void NativeConstruct() override; + +protected: + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget)) + class UButton *BackButton; + + UFUNCTION() + void OnBackButtonClicked(); +};