Skip to content

Commit

Permalink
Implemented possibility to restart the game after it ended
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Lasaj committed Dec 5, 2020
1 parent 2378302 commit eeaaa4d
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Content/UI/UI_EndMenu.uasset
Git LFS file not shown
4 changes: 2 additions & 2 deletions Content/UI/UI_IngameScoreAndTurns.uasset
Git LFS file not shown
4 changes: 2 additions & 2 deletions Content/UI/UI_MainMenu.uasset
Git LFS file not shown
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ There is a game field consisting of hex fields containing blocks. The player nee

## Ideas for more features, expansions..

- [ ] Score (+ UI)
- [X] ~~Score (+ UI)~~
- [ ] Varying probabilities for the different token types (the rarer, the more points it's worth)
- [X] ~~Game ends after x moves~~
- [ ] Possibility to restart the game after ending of game
- [X] ~~Possibility to restart the game after ending of game~~
- [ ] Exception Handling
- [ ] Special blocks with special effects
- [ ] Combos
Expand Down
15 changes: 15 additions & 0 deletions Source/MatchThreeLines/MTLGameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ bool AMTLGameState::DestroyTokens(TArray<AGameToken*> SelectedTokens)
return true;
};

void AMTLGameState::ResetPlayingField()
{
for (TArray<AGameToken*>& Column : PlayingField)
{
for (AGameToken* Token : Column)
{
Token->Destroy();
}
Column.Empty();
}
PlayingField.Empty();

InitPlayingField();
}

// protected functions
void AMTLGameState::BeginPlay()
{
Expand Down
2 changes: 2 additions & 0 deletions Source/MatchThreeLines/MTLGameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class MATCHTHREELINES_API AMTLGameState : public AGameStateBase
*/
bool DestroyTokens(TArray<AGameToken*> SelectedTokens);

void ResetPlayingField();

protected:
virtual void BeginPlay() override;
};
17 changes: 17 additions & 0 deletions Source/MatchThreeLines/MTLPlayerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "MTLPlayerController.h"
#include "MTLPlayerState.h"
#include "GameToken.h"
#include "MTLGameState.h"

// private functions
void AMTLPlayerController::OnMouseClicked()
Expand Down Expand Up @@ -88,6 +89,22 @@ void AMTLPlayerController::StartOrResumeGame()
SetPause(false);
}

void AMTLPlayerController::ResetGame() const
{
AMTLGameState* GameState = GetWorld()->GetGameState<AMTLGameState>();
AMTLPlayerState* MTLPlayerState = GetPlayerState<AMTLPlayerState>();

if (GameState != nullptr && MTLPlayerState != nullptr)
{
GameState->ResetPlayingField();
MTLPlayerState->Init();
}
else
{
GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Red, TEXT("Couldn't get GameState or PlayerState!"));
}
}

// protected functions
void AMTLPlayerController::SetupInputComponent()
{
Expand Down
6 changes: 5 additions & 1 deletion Source/MatchThreeLines/MTLPlayerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ class MATCHTHREELINES_API AMTLPlayerController : public APlayerController
/** Wait for a tiny bit, then display the End menu */
void EndGame();

/** Display the in-game menu, then unpause the game */
/** Hide the main/pause menu, display the in-game menu, then unpause the game */
UFUNCTION(BlueprintCallable, Category = "MTL – UI")
void StartOrResumeGame();

/** Reset the playing field, score and remaining turns */
UFUNCTION(BlueprintCallable, Category = "MTL – UI")
void ResetGame() const;

protected:
/** Called when the game starts. */
virtual void BeginPlay() override;
Expand Down
41 changes: 23 additions & 18 deletions Source/MatchThreeLines/MTLPlayerState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ void AMTLPlayerState::AddTokenToSelected(AGameToken* Token)
}
}

void AMTLPlayerState::Init()
{
AGameStateBase* GameStateBase = GetWorld()->GetGameState();
if (GameStateBase != nullptr)
{
const AMTLGameMode* GameMode = GameStateBase->GetDefaultGameMode<AMTLGameMode>();
if (GameMode != nullptr)
{
SetScore(0.f);
AmountOfRemainingTurns = GameMode->GetMaxAmountOfTurns();
}
else
{
GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Red, TEXT("Couldn't get GameMode!"));
}
}
else
{
GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Red, TEXT("Couldn't get GameState!"));
}
}

void AMTLPlayerState::EndTurn()
{
SetIsSelecting(false);
Expand Down Expand Up @@ -98,22 +120,5 @@ void AMTLPlayerState::BeginPlay()
{
Super::BeginPlay();

AGameStateBase* GameStateBase = GetWorld()->GetGameState();
if (GameStateBase != nullptr)
{
const AMTLGameMode* GameMode = GameStateBase->GetDefaultGameMode<AMTLGameMode>();
if (GameMode != nullptr)
{
SetScore(0.f);
AmountOfRemainingTurns = GameMode->GetMaxAmountOfTurns();
}
else
{
GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Red, TEXT("Couldn't get GameMode!"));
}
}
else
{
GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Red, TEXT("Couldn't get GameState!"));
}
Init();
}
3 changes: 3 additions & 0 deletions Source/MatchThreeLines/MTLPlayerState.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class MATCHTHREELINES_API AMTLPlayerState : public APlayerState

void AddTokenToSelected(AGameToken* Token);

/** Reset all members to default/initial values */
void Init();

/** Destroys all selected GameTokens, if more than three have been selected; then decrements AmountOfRemainingTurns */
void EndTurn();

Expand Down

0 comments on commit eeaaa4d

Please sign in to comment.