Skip to content

Commit

Permalink
added skip level command
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Jun 8, 2024
1 parent de2b0d3 commit 18726d3
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 41 deletions.
28 changes: 14 additions & 14 deletions FluffyEngine/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ namespace Fluffy
{
enum class Button : unsigned int
{
NONE = 0x0000,
NONE = 0,

XINPUT_CONTROLLER_A = 0x1000,
XINPUT_CONTROLLER_B = 0x2000,
XINPUT_CONTROLLER_X = 0x4000,
XINPUT_CONTROLLER_Y = 0x8000,
GAMEPAD_A = XINPUT_GAMEPAD_A,
GAMEPAD_B = XINPUT_GAMEPAD_B,
GAMEPAD_X = XINPUT_GAMEPAD_X,
GAMEPAD_Y = XINPUT_GAMEPAD_Y,

XINPUT_GAMEPAD_DRAD_UP = 0x0001,
XINPUT_GAMEPAD_DRAD_DOWN = 0x0002,
XINPUT_GAMEPAD_DRAD_LEFT = 0x0004,
XINPUT_GAMEPAD_DRAD_RIGHT = 0x0008
GAMEPAD_DPAD_UP = XINPUT_GAMEPAD_DPAD_UP,
GAMEPAD_DPAD_DOWN = XINPUT_GAMEPAD_DPAD_DOWN,
GAMEPAD_DPAD_LEFT = XINPUT_GAMEPAD_DPAD_LEFT,
GAMEPAD_DPAD_RIGHT = XINPUT_GAMEPAD_DPAD_RIGHT,
};

struct ControllerInput
Expand Down Expand Up @@ -70,14 +70,14 @@ namespace Fluffy
void AddCommand(ControllerInput input, std::unique_ptr<class Command> command);

private:
int m_ControllerIndex;
int m_ControllerIndex{};

XINPUT_STATE m_InputState{};

unsigned int m_PressedButtons;
unsigned int m_ReleasedButtons;
unsigned int m_PreviousButtons;
unsigned int m_PressedButtons{};
unsigned int m_ReleasedButtons{};
unsigned int m_PreviousButtons{};

std::unordered_map<ControllerInput, std::unique_ptr<class Command>> m_ButtonBindings;
std::unordered_map<ControllerInput, std::unique_ptr<class Command>> m_ButtonBindings{};
};
}
2 changes: 2 additions & 0 deletions Galaga/Galaga.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
<ClCompile Include="ScoreComponent.cpp" />
<ClCompile Include="ShootCommand.cpp" />
<ClCompile Include="EnemyStates.cpp" />
<ClCompile Include="SkipLevelCommand.cpp" />
<ClCompile Include="SoundManager.cpp" />
<ClCompile Include="UIManager.cpp" />
</ItemGroup>
Expand All @@ -209,6 +210,7 @@
<ClInclude Include="ScoreComponent.h" />
<ClInclude Include="ShootCommand.h" />
<ClInclude Include="EnemyStates.h" />
<ClInclude Include="SkipLevelCommand.h" />
<ClInclude Include="SoundManager.h" />
<ClInclude Include="Structs.h" />
<ClInclude Include="UIManager.h" />
Expand Down
6 changes: 6 additions & 0 deletions Galaga/Galaga.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
<ClCompile Include="GameOverScreen.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SkipLevelCommand.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="AchievementComponent.h">
Expand Down Expand Up @@ -158,5 +161,8 @@
<ClInclude Include="GameOverScreen.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SkipLevelCommand.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
23 changes: 14 additions & 9 deletions Galaga/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ void GameManager::StartLevel(const int levelIndex)
BulletsManager::GetInstance().Initialize();
}

void GameManager::SkipToNextLevel()
{
if (++m_CurrentLevelIndex <= LEVELS_COUNT)
{
GameEvents::OnLevelCompleted.Invoke();
StartLevel(m_CurrentLevelIndex);
}
else
{
TriggerGameOver();
}
}

void GameManager::Update(const float deltaTime)
{
// if the level has started
Expand Down Expand Up @@ -107,15 +120,7 @@ void GameManager::OnNotify(const Fluffy::EventType& eventType, const Fluffy::IEv
case Fluffy::EventType::OnEnemyKilled:
if (CharactersManager::GetInstance().AreAllEnemiesDead())
{
if (++m_CurrentLevelIndex <= LEVELS_COUNT)
{
GameEvents::OnLevelCompleted.Invoke();
StartLevel(m_CurrentLevelIndex);
}
else
{
TriggerGameOver();
}
SkipToNextLevel();
}
break;

Expand Down
1 change: 1 addition & 0 deletions Galaga/GameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class GameManager final : public Fluffy::Component, public Fluffy::ComponentSing
GameManager& operator=(GameManager&&) = delete;

void StartLevel(const int levelIndex);
void SkipToNextLevel();
void Update(const float deltaTime) override;
void OnNotify(const Fluffy::EventType& eventType, const Fluffy::IEventParam* param) override;

Expand Down
16 changes: 10 additions & 6 deletions Galaga/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "HealthDisplayComponent.h"
#include "MoveCommand.h"
#include "ShootCommand.h"
#include "SkipLevelCommand.h"
#include "BulletsManager.h"
#include "CollidersHandler.h"
#include "CollisionLayers.h"
Expand Down Expand Up @@ -64,20 +65,23 @@ static void CreateScene()
const KeyboardInput k_left{ SDL_SCANCODE_A, InputState::Held };
const KeyboardInput k_right{ SDL_SCANCODE_D, InputState::Held };
const KeyboardInput k_Space{ SDL_SCANCODE_SPACE, InputState::Held };
const KeyboardInput k_F1{ SDL_SCANCODE_F1, InputState::Released };

keyboard->AddCommand(k_left, std::make_unique<MoveCommand>(0, glm::vec2(-1.0f, 0.0f), 200.0f));
keyboard->AddCommand(k_right, std::make_unique<MoveCommand>(0, glm::vec2(1.0f, 0.0f), 200.0f));
keyboard->AddCommand(k_Space, std::make_unique<ShootCommand>(0));
keyboard->AddCommand(k_F1, std::make_unique<SkipLevelCommand>());

std::unique_ptr<Controller> controller = std::make_unique<Controller>();

const ControllerInput left{ Button::XINPUT_GAMEPAD_DRAD_LEFT, InputState::Held };
const ControllerInput right{ Button::XINPUT_GAMEPAD_DRAD_RIGHT, InputState::Held };
const ControllerInput B{ Button::XINPUT_CONTROLLER_B, InputState::Held };
const ControllerInput c_left{ Button::GAMEPAD_DPAD_LEFT, InputState::Held };
const ControllerInput c_right{ Button::GAMEPAD_DPAD_RIGHT, InputState::Held };
const ControllerInput c_B{ Button::GAMEPAD_B, InputState::Held };

controller->AddCommand(left, std::make_unique<MoveCommand>(1, glm::vec2(-1.0f, 0.0f), 200.0f));
controller->AddCommand(right, std::make_unique<MoveCommand>(1, glm::vec2(1.0f, 0.0f), 200.0f));
controller->AddCommand(B, std::make_unique<ShootCommand>(1));
// since we don't have multiplayer right now, we're using both keyboard and gamepad for the same player index
controller->AddCommand(c_left, std::make_unique<MoveCommand>(0, glm::vec2(-1.0f, 0.0f), 200.0f));
controller->AddCommand(c_right, std::make_unique<MoveCommand>(0, glm::vec2(1.0f, 0.0f), 200.0f));
controller->AddCommand(c_B, std::make_unique<ShootCommand>(0));

auto& input = InputManager::GetInstance();
input.AddDevice(std::move(keyboard));
Expand Down
5 changes: 0 additions & 5 deletions Galaga/MoveCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
#include "Command.h"
#include "glm/glm.hpp"

namespace Fluffy
{
class Command;
}

class MoveCommand final : public Fluffy::Command
{
public:
Expand Down
7 changes: 0 additions & 7 deletions Galaga/ShootCommand.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#pragma once
#include "Command.h"
#include "glm/glm.hpp"

namespace Fluffy
{
class Command;
}

class ShootCommand final : public Fluffy::Command
{
Expand All @@ -23,4 +17,3 @@ class ShootCommand final : public Fluffy::Command
private:
const int m_PlayerIndex;
};

8 changes: 8 additions & 0 deletions Galaga/SkipLevelCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "SkipLevelCommand.h"
#include "GameManager.h"

void SkipLevelCommand::Execute()
{
if (GameManager::GetInstance() != nullptr)
GameManager::GetInstance()->SkipToNextLevel();
}
16 changes: 16 additions & 0 deletions Galaga/SkipLevelCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "Command.h"

class SkipLevelCommand final : public Fluffy::Command
{
public:
SkipLevelCommand() = default;
~SkipLevelCommand() = default;

SkipLevelCommand(const SkipLevelCommand& other) = default;
SkipLevelCommand& operator=(const SkipLevelCommand& other) = default;
SkipLevelCommand(SkipLevelCommand&& other) = default;
SkipLevelCommand& operator=(SkipLevelCommand&& other) = default;

void Execute() override;
};

0 comments on commit 18726d3

Please sign in to comment.