Skip to content

Commit

Permalink
Restart game after someone wins
Browse files Browse the repository at this point in the history
  • Loading branch information
jrempel-skybox committed Jul 30, 2019
1 parent e51ef80 commit 5a6d873
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 5 deletions.
12 changes: 12 additions & 0 deletions sfml-cpp/GameInstance.cpp
Expand Up @@ -27,6 +27,13 @@ void GameInstance::Update()

activeLevel->Update(dt);
window.Update();

if(restartTriggered)
{
restartTriggered = false;
activeLevel = std::make_shared<Level>(this, Paths::GetLevelPath(config.GetStartLevelFileName()));
activeLevel->LoadData();
}
}

void GameInstance::Render()
Expand All @@ -38,3 +45,8 @@ bool GameInstance::IsRunning() const
{
return window.IsOpen();
}

void GameInstance::RestartGame()
{
restartTriggered = true;
}
4 changes: 3 additions & 1 deletion sfml-cpp/GameInstance.h
Expand Up @@ -11,11 +11,13 @@ class GameInstance
void Update();
void Render();
bool IsRunning() const;
const GameConfig GetGameConfig() const;
void RestartGame();
const GameConfig GetGameConfig() const { return config; }

private:
std::shared_ptr<Level> activeLevel;
GameWindow window;
sf::Clock deltaClock;
GameConfig config;
bool restartTriggered = false;
};
2 changes: 1 addition & 1 deletion sfml-cpp/Level.cpp
Expand Up @@ -5,7 +5,7 @@
#include <memory>
#include "RenderRule.h"

Level::Level(const GameInstance* pGameInstance, std::string path)
Level::Level(GameInstance* pGameInstance, std::string path)
:
pGameInstance(pGameInstance),
levelData(path)
Expand Down
6 changes: 3 additions & 3 deletions sfml-cpp/Level.h
Expand Up @@ -11,7 +11,7 @@ class GameInstance;
class Level : public std::enable_shared_from_this<Level>
{
public:
Level(const GameInstance* pGameInstance, std::string path);
Level(GameInstance* pGameInstance, std::string path);
void LoadData();

std::weak_ptr<GameObject> SpawnObject(std::string name, float x, float y);
Expand All @@ -35,11 +35,11 @@ class Level : public std::enable_shared_from_this<Level>
void SetCameraTarget(Vector2 target) { cameraTarget = target; }
Vector2 GetCameraTarget() const { return cameraTarget; }

const GameInstance* GetGameInstance() const { return pGameInstance; };
GameInstance* GetGameInstance() const { return pGameInstance; };

// VARIABLES
private:
const GameInstance* pGameInstance;
GameInstance* pGameInstance;

LevelData levelData;
Vector2 cameraTarget;
Expand Down
3 changes: 3 additions & 0 deletions sfml-cpp/PlayerComponent.cpp
Expand Up @@ -13,6 +13,7 @@
#include "SpriteComponent.h"
#include "AnimatedSpriteComponent.h"
#include "SwordKillBoxComponent.h"
#include "RestartGameTimerComponent.h"



Expand Down Expand Up @@ -85,6 +86,8 @@ void PlayerComponent::HitBySword()
auto level = go->GetLevel().lock();

level->SpawnObjectFromFile(Paths::GetObjectPath("UI_GameOverLabel.json"));
auto timerGo = level->SpawnObject("GameRestartTimer", 0,0).lock();
timerGo->AddComponent<RestartGameTimerComponent>();

level->DestroyObject(go);
}
Expand Down
22 changes: 22 additions & 0 deletions sfml-cpp/RestartGameTimerComponent.cpp
@@ -0,0 +1,22 @@
#include "RestartGameTimerComponent.h"
#include "GameObject.h"
#include "Level.h"
#include "GameInstance.h"

void RestartGameTimerComponent::Start()
{
EnableTick(true);
}

void RestartGameTimerComponent::Tick(float deltaTime)
{
timeLeft -= deltaTime;
if(timeLeft <= 0)
{
auto go = GetGameObject().lock();
auto level = go->GetLevel().lock();

level->GetGameInstance()->RestartGame();

}
}
13 changes: 13 additions & 0 deletions sfml-cpp/RestartGameTimerComponent.h
@@ -0,0 +1,13 @@
#pragma once
#include "GameComponent.h"

class RestartGameTimerComponent : public GameComponent
{

public:
void Start() override;
void Tick(float deltaTime) override;

private:
float timeLeft = 5.0f;
};
2 changes: 2 additions & 0 deletions sfml-cpp/sfml-cpp.vcxproj
Expand Up @@ -171,6 +171,7 @@ XCOPY "$(SolutionDir)SFML-2.5.1\bin\openal32.dll" "$(TargetDir)" /S /Y /R
<ClCompile Include="PlayerComponent.cpp" />
<ClCompile Include="RenderRule.cpp" />
<ClCompile Include="AnimatedSpriteComponent.cpp" />
<ClCompile Include="RestartGameTimerComponent.cpp" />
<ClCompile Include="SpriteComponent.cpp" />
<ClCompile Include="SwordKillBoxComponent.cpp" />
<ClCompile Include="TransformComponent.cpp" />
Expand All @@ -194,6 +195,7 @@ XCOPY "$(SolutionDir)SFML-2.5.1\bin\openal32.dll" "$(TargetDir)" /S /Y /R
<ClInclude Include="GameRenderer.h" />
<ClInclude Include="AnimatedSpriteComponent.h" />
<ClInclude Include="Rect.h" />
<ClInclude Include="RestartGameTimerComponent.h" />
<ClInclude Include="SwordKillBoxComponent.h" />
<ClInclude Include="UIFPSLabelComponent.h" />
<ClInclude Include="UILabelComponent.h" />
Expand Down
6 changes: 6 additions & 0 deletions sfml-cpp/sfml-cpp.vcxproj.filters
Expand Up @@ -99,6 +99,9 @@
<ClCompile Include="SwordKillBoxComponent.cpp">
<Filter>Source\Game</Filter>
</ClCompile>
<ClCompile Include="RestartGameTimerComponent.cpp">
<Filter>Source\Game</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="json.hpp">
Expand Down Expand Up @@ -188,5 +191,8 @@
<ClInclude Include="SwordKillBoxComponent.h">
<Filter>Source\Game</Filter>
</ClInclude>
<ClInclude Include="RestartGameTimerComponent.h">
<Filter>Source\Game</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 5a6d873

Please sign in to comment.