Skip to content

Commit

Permalink
Get game loop (with win condition) fully functional (#2)
Browse files Browse the repository at this point in the history
* Some initial fixes

* Some more cleanup

* Get dots working again

* Rename some files

* Finish renaming C++ files

* Rename some assets

* Rename primary map

* Clean up comments

* Reset level
  • Loading branch information
nucleartide authored Jun 18, 2023
1 parent 7b0a8ec commit 6d652af
Show file tree
Hide file tree
Showing 33 changed files with 291 additions and 166 deletions.
4 changes: 2 additions & 2 deletions Config/DefaultEngine.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@


[/Script/EngineSettings.GameMapsSettings]
GameDefaultMap=/Game/Maps/Prototype2.Prototype2
GameDefaultMap=/Game/Maps/Game.Game
GlobalDefaultGameMode=/Script/Engine.GameModeBase
EditorStartupMap=/Game/Maps/Prototype2.Prototype2
EditorStartupMap=/Game/Maps/Game.Game

[/Script/WindowsTargetPlatform.WindowsTargetSettings]
DefaultGraphicsRHI=DefaultGraphicsRHI_DX12
Expand Down
Binary file added Content/Blueprints/BP_ConsumableDotActor.uasset
Binary file not shown.
Binary file added Content/Blueprints/BP_GameOverWinWidget.uasset
Binary file not shown.
Binary file modified Content/Blueprints/BP_LevelGenerationActor.uasset
Binary file not shown.
Binary file removed Content/Blueprints/BP_PacmanDotActor.uasset
Binary file not shown.
Binary file modified Content/Blueprints/BP_PacmanPawn.uasset
Binary file not shown.
Binary file added Content/Blueprints/BP_PacmanPawnManager.uasset
Binary file not shown.
Binary file added Content/Blueprints/BP_PlayerManager.uasset
Binary file not shown.
Binary file modified Content/Blueprints/BP_UIActor.uasset
Binary file not shown.
Binary file added Content/Blueprints/BP_UIManager.uasset
Binary file not shown.
Binary file not shown.
Binary file removed Content/Blueprints/GameOverWin.uasset
Binary file not shown.
62 changes: 31 additions & 31 deletions Content/Levels/level2.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
WWWWWWWWWWWWoWWoWWWWWWWWWWWW
WooooooooooooWWooooooooooooW
WoWWWWoWWWWWoWWoWWWWWoWWWWoW
WoWWWWoWWWWWoWWoWWWWWoWWWWoW
WoWWWWoWWWWWoWWoWWWWWoWWWWoW
WooooooooooooooooooooooooooW
WoWWWWoWWoWWWWWWWWoWWoWWWWoW
WoWWWWoWWoWWWWWWWWoWWoWWWWoW
WooooooWWooooWWooooWWooooooW
WWWWWWoWWWWooWWooWWWWoWWWWWW
WxxxxWoWWWWooWWooWWWWoWxxxxW
WxxxxWoWWooooooooooWWoWxxxxW
WxxxxWoWWooBBBBBBooWWoWxxxxW
WWWWWWoWWooBBBBBBooWWoWWWWWW
oooooooooooBBBBBBooooooooooo
WWWWWWoWWooBBBBBBooWWoWWWWWW
WxxxxWoWWooBBBBBBooWWoWxxxxW
WxxxxWoWWooooooooooWWoWxxxxW
WxxxxWoWWoWWWWWWWWoWWoWxxxxW
WWWWWWoWWoWWWWWWWWoWWoWWWWWW
WooooooooooooWWooooooooooooW
WoWWWWoWWWWWoWWoWWWWWoWWWWoW
WoWWWWoWWWWWoWWoWWWWWoWWWWoW
WoooWWoWWooooooooooWWooooooW
WWWoWWoWWoWWWWWWWWoWWoWWoWWW
WWWoWWoWWoWWWWWWWWoWWoWWoWWW
WooooooWWooooWWooooWWooooooW
WoWWWWWWWWWWoWWoWWWWWWWWWWoW
WoWWWWWWWWWWoWWoWWWWWWWWWWoW
W oooooooooooooooooooooooW
WWWWWWWWWWWWoWWoWWWWWWWWWWWW
WWWWWWWWWWWW WW WWWWWWWWWWWW
W WW W
W WWWW WWWWW WW WWWWW WWWW W
W WWWW WWWWW WW WWWWW WWWW W
W WWWW WWWWW WW WWWWW WWWW W
W W
W WWWW WW WWWWWWWW WW WWWW W
W WWWW WW WWWWWWWW WW WWWW W
W WW WW WW W
WWWWWW WWWW WW WWWW WWWWWW
WxxxxW WWWW WW WWWW WxxxxW
WxxxxW WW WW WxxxxW
WxxxxW WW BBBBBB WW WxxxxW
WWWWWW WW BBBBBB WW WWWWWW
BBBBBB
WWWWWW WW BBBBBB WW WWWWWW
WxxxxW WW BBBBBB WW WxxxxW
WxxxxW WW WW WxxxxW
WxxxxW WW WWWWWWWW WW WxxxxW
WWWWWW WW WWWWWWWW WW WWWWWW
W WW W
W WWWW WWWWW WW WWWWW WWWW W
W WWWW WWWWW WW WWWWW WWWW W
W WW WW WW W
WWW WW WW WWWWWWWW WW WW WWW
WWW WW WW WWWWWWWW WW WW WWW
W WW WW WW W
W WWWWWWWWWW WW WWWWWWWWWW W
W WWWWWWWWWW WW WWWWWWWWWW W
W W
WWWWWWWWWWWW WW WWWWWWWWWWWW
Binary file added Content/Maps/Game.umap
Binary file not shown.
Binary file modified Content/Maps/Prototype2.umap
Binary file not shown.
3 changes: 2 additions & 1 deletion KiwiPizza.uproject
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"LoadingPhase": "Default",
"AdditionalDependencies": [
"Engine",
"CoreUObject"
"CoreUObject",
"UMG"
]
}
],
Expand Down
7 changes: 7 additions & 0 deletions Source/KiwiPizza/Private/ConsumableDotActor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "ConsumableDotActor.h"

void AConsumableDotActor::Destroyed()
{
Super::Destroyed();
OnDotConsumedDelegate.Broadcast();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#include "CoreMinimal.h"
#include "Engine/StaticMeshActor.h"
#include "PacmanDotActor.generated.h"
#include "ConsumableDotActor.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnDotConsumedSignature);

UCLASS()
class APacmanDotActor : public AStaticMeshActor
class AConsumableDotActor : public AStaticMeshActor
{
GENERATED_BODY()

Expand Down
15 changes: 15 additions & 0 deletions Source/KiwiPizza/Private/GameOverWinWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "GameOverWinWidget.h"
#include "Debug.h"
#include "Components/Button.h"

void UGameOverWinWidget::NativeConstruct()
{
Super::NativeConstruct();
RestartGameButton->OnReleased.AddUniqueDynamic(this, &UGameOverWinWidget::HandleRestartGame);
}

void UGameOverWinWidget::HandleRestartGame()
{
DEBUG_LOG(TEXT("Handling restart game..."));
OnRestartGameClickedDelegate.Broadcast();
}
26 changes: 26 additions & 0 deletions Source/KiwiPizza/Private/GameOverWinWidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "GameOverWinWidget.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnRestartGameClickedSignature);

UCLASS(BlueprintType, Blueprintable)
class UGameOverWinWidget : public UUserWidget
{
GENERATED_BODY()

protected:
UPROPERTY(meta = (BindWidget))
class UButton *RestartGameButton;

virtual void NativeConstruct() override;

UFUNCTION()
void HandleRestartGame();

public:
UPROPERTY(BlueprintCallable, BlueprintAssignable)
FOnRestartGameClickedSignature OnRestartGameClickedDelegate;
};
75 changes: 33 additions & 42 deletions Source/KiwiPizza/Private/LevelGenerationActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,33 @@
#include "GameFramework/Actor.h"
#include "Kismet/GameplayStatics.h"
#include "Debug.h"
#include "PacmanDotActor.h"
#include "ConsumableDotActor.h"
#include "PacmanGameMode.h"

// Called when the game starts or when spawned
void ALevelGenerationActor::BeginPlay()
{
Super::BeginPlay();
RegenerateDots();

//
// Attach event handler.
//

/*
// Fetch GameMode.
auto GameMode = GetWorld()->GetAuthGameMode();
check(GameMode);
// Fetch GameMode. This couples this class tightly to the game mode, but that's okay.
auto GameMode = GetWorld()->GetAuthGameMode();
check(GameMode);

// Cast to correct class.
auto PacmanGameMode = Cast<AKiwiPizzaGameModeBase>(GameMode);
check(PacmanGameMode);
// Cast to correct class.
auto PacmanGameMode = Cast<APacmanGameMode>(GameMode);
check(PacmanGameMode);

// Attach event handler.
PacmanGameMode->OnGameRestartedDelegate.AddUniqueDynamic(this, &ALevelGenerationActor::RegenerateDots);
*/
// Attach event handler.
PacmanGameMode->OnGameRestartedDelegate.AddUniqueDynamic(this, &ALevelGenerationActor::RegenerateDots);
}

void ALevelGenerationActor::RegenerateDots()
{
//
// Load level.
//
Expand Down Expand Up @@ -71,16 +74,6 @@ void ALevelGenerationActor::BeginPlay()
// Spawn the actor at the desired location.
auto SpawnedActor = World->SpawnActor<AStaticMeshActor>(WallTile, Location, FRotator::ZeroRotator, SpawnParams);
check(SpawnedActor);

// Set the tags of the actor.
// auto Tags = WallTile.GetDefaultObject()->Tags;
// for (auto Tag : Tags)
// SpawnedActor->Tags.Add(Tag);

// Set the static mesh of the actor
// auto StaticMesh = WallTile.GetDefaultObject()->GetStaticMeshComponent()->GetStaticMesh();
// SpawnedActor->GetStaticMeshComponent()->SetMobility(EComponentMobility::Movable);
// SpawnedActor->GetStaticMeshComponent()->SetStaticMesh(StaticMesh);
}
else if (Character == ' ')
{
Expand All @@ -97,15 +90,20 @@ void ALevelGenerationActor::BeginPlay()
FVector Location(LocationY, LocationX, 0.0f);

// Spawn the actor at the desired location
auto Actor = World->SpawnActor<AStaticMeshActor>(PacmanDot, Location, FRotator::ZeroRotator, Params);
FVector BogusLocation(10000.0f, 10000.0f, 10000.0f);
auto Actor = World->SpawnActor<AStaticMeshActor>(PacmanDot, BogusLocation, FRotator::ZeroRotator, Params); // Spawn in a spot away from the player to avoid spawn failures.
check(Actor);

// Once successful, set the actor location once again.
Actor->SetMobility(EComponentMobility::Movable);
Actor->SetActorLocation(Location);

// Cast.
auto PacmanDotActor = Cast<APacmanDotActor>(Actor);
check(PacmanDotActor);
auto ConsumableDotActor = Cast<AConsumableDotActor>(Actor);
check(ConsumableDotActor);

// Attach handler to when PacmanDotActor is destroyed.
PacmanDotActor->OnDotConsumedDelegate.AddUniqueDynamic(this, &ALevelGenerationActor::HandleDotConsumption);
// Attach handler to when ConsumableDotActor is destroyed.
ConsumableDotActor->OnDotConsumedDelegate.AddUniqueDynamic(this, &ALevelGenerationActor::HandleDotConsumption);

// Bump counter.
NumDotsGenerated++;
Expand All @@ -116,22 +114,6 @@ void ALevelGenerationActor::BeginPlay()
DEBUG_LOG(TEXT("%d dots generated."), NumDotsGenerated);
}

/*
// TODO: Save this for later.
void ALevelGenerationActor::ResetPlayerPosition()
{
auto PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
auto Pawn = PlayerController->GetPawn();
auto StartingLocation = PlayerStartingLocation->GetActorLocation();
Pawn->SetActorLocation(StartingLocation);
}
*/

void ALevelGenerationActor::RegenerateDots()
{
// TODO: Gonna need this.
}

void ALevelGenerationActor::HandleDotConsumption()
{
NumDotsGenerated--;
Expand All @@ -140,5 +122,14 @@ void ALevelGenerationActor::HandleDotConsumption()
if (NumDotsGenerated == 0)
{
OnLevelClearedDelegate.Broadcast();

// Fetch the game mode.
auto GameMode = GetWorld()->GetAuthGameMode();
check(GameMode);

auto PacmanGameMode = Cast<APacmanGameMode>(GameMode);
check(PacmanGameMode);

PacmanGameMode->SetGameState(PacmanGameState::GameOverWin);
}
}
2 changes: 0 additions & 2 deletions Source/KiwiPizza/Private/LevelLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,4 @@ class ULevelLoader : public UObject

public:
TArray<FString> StringList;
// static int GetLevelWidth(TSubclassOf<ULevelLoader> LevelLoader);
// static int GetLevelHeight(TSubclassOf<ULevelLoader> LevelLoader);
};
7 changes: 0 additions & 7 deletions Source/KiwiPizza/Private/PacmanDotActor.cpp

This file was deleted.

20 changes: 7 additions & 13 deletions Source/KiwiPizza/Private/PacmanGameMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
#include "PacmanPlayerController.h"
#include "Debug.h"

/*
// TODO: On depletion of all dots, invoke the OnDotsDepleted event
// TODO: this will alert the PacmanGameMode (which subscribes to the Levelgenerationactor)
// TODO: the pacman game mode will then emit a "game over win" event
void APacmanGameMode::Tick(float DeltaTime)
void APacmanGameMode::SetGameState(PacmanGameState NewState)
{
if (FoundActors.Num() == 0)
if (NewState == PacmanGameState::Playing)
{
// TODO: Update state on the GameModeBase.
if (GameState != GameOverWin)
{
GameState = GameOverWin;
OnPacmanGameStateChangedDelegate.Broadcast(GameOverWin);
}
OnGameRestartedDelegate.Broadcast();
}

// NOTE: THIS MUST COME AFTER THE RESTART, otherwise player will consume dots before level restarts.
GameState = NewState;
OnGameStateChangedDelegate.Broadcast(NewState);
}
*/
13 changes: 8 additions & 5 deletions Source/KiwiPizza/Private/PacmanGameMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,30 @@
#include "PacmanGameMode.generated.h"

UENUM(BlueprintType)
enum PacmanGameState
enum class PacmanGameState : uint8
{
Playing,
GameOverWin,
GameOverLose,
};

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnGameStateChangedSignature, PacmanGameState, GameState);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnGameRestartedSignature);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnGameStateChangedSignature, PacmanGameState, GameState);

UCLASS()
class KIWIPIZZA_API APacmanGameMode : public AGameModeBase
{
GENERATED_BODY()

public:
PacmanGameState GameState = Playing;
PacmanGameState GameState = PacmanGameState::Playing;

UPROPERTY(BlueprintCallable, BlueprintAssignable)
FOnGameStateChangedSignature OnGameStateChangedDelegate;
FOnGameRestartedSignature OnGameRestartedDelegate;

UPROPERTY(BlueprintCallable, BlueprintAssignable)
FOnGameRestartedSignature OnGameRestartedDelegate;
FOnGameStateChangedSignature OnGameStateChangedDelegate;

UFUNCTION()
void SetGameState(PacmanGameState NewState);
};
12 changes: 12 additions & 0 deletions Source/KiwiPizza/Private/PacmanPawn.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "PacmanPawn.h"
#include "PacmanGameMode.h"

APacmanPawn::APacmanPawn()
{
Expand Down Expand Up @@ -159,6 +160,17 @@ void APacmanPawn::WrapAroundWorld()

void APacmanPawn::NotifyActorBeginOverlap(AActor* Other)
{
auto GameMode = GetWorld()->GetAuthGameMode();
check(GameMode);

auto PacmanGameMode = Cast<APacmanGameMode>(GameMode);
check(PacmanGameMode);

if (PacmanGameMode->GameState != PacmanGameState::Playing)
{
return;
}

if (Other->Tags.Contains(FName("SmallDot")))
{
Other->Destroy();
Expand Down
Loading

0 comments on commit 6d652af

Please sign in to comment.