Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeef committed Dec 26, 2023
1 parent 99e95d8 commit cc7ad8f
Show file tree
Hide file tree
Showing 26 changed files with 602 additions and 126 deletions.
10 changes: 8 additions & 2 deletions src/game-loop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ add_library(GameLoop STATIC

src/other/ParticleGenerator.cpp
src/other/Inventory.cpp
src/prefabs/ui/CheatConsole.cpp
src/populator/ItemFactory.cpp
src/populator/LootFactory.cpp
src/populator/NpcFactory.cpp
interface/other/PhysicsComponentType.hpp
interface/other/Inventory.hpp
interface/other/InventoryEvent.hpp
Expand All @@ -343,8 +347,10 @@ add_library(GameLoop STATIC
include/other/ParticleGenerator.hpp
include/components/generic/ImguiComponent.hpp
include/prefabs/ui/CheatConsole.hpp
src/prefabs/ui/CheatConsole.cpp
)
include/populator/ItemFactory.hpp
include/populator/NpcFactory.hpp
include/populator/LootFactory.hpp
src/other/NpcType.cpp src/other/ItemType.cpp include/other/CheatConsoleInterpreter.h src/other/CheatConsoleInterpreter.cpp interface/game-loop/GameLoopState.hpp src/game-loop/GameLoopState.cpp)

target_include_directories(GameLoop
PRIVATE include interface
Expand Down
31 changes: 31 additions & 0 deletions src/game-loop/include/other/CheatConsoleInterpreter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <vector>
#include <string>
#include <functional>
#include <map>

#include "other/ItemType.hpp"
#include "other/NpcType.hpp"
#include "game-loop/GameLoopState.hpp"
#include "LootType.hpp"

class CheatConsoleInterpreter
{
public:
using Command = std::vector<std::string>;
using CommandHandlerResult = std::pair<bool, std::string>;
using CommandHandler = std::function<CommandHandlerResult(const Command&)>;
CheatConsoleInterpreter();

const CommandHandler& get_spawn_command_handler() const;
const CommandHandler& get_enter_command_handler() const;
private:
std::map<std::string, ItemType> _string_to_item_type_map;
std::map<std::string, NpcType> _string_to_npc_type_map;
std::map<std::string, LootType> _string_to_loot_type_map;
std::map<std::string, GameLoopState> _string_to_game_loop_state_map;

CommandHandler _spawn_command_handler;
CommandHandler _enter_command_handler;
};
14 changes: 11 additions & 3 deletions src/game-loop/include/other/NpcType.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#pragma once

enum class NpcType
#include <cassert>
#include <cstdint>

using NpcType_t = std::uint16_t;

enum class NpcType : NpcType_t
{
NONE,
NONE = 0,
SNAKE,
BAT,
CAVEMAN,
Expand All @@ -11,5 +16,8 @@ enum class NpcType
SHOPKEEPER,
DAMSEL,
BLUE_FROG,
RED_FROG
RED_FROG,
_SIZE
};

const char* to_string(NpcType npc_type);
10 changes: 10 additions & 0 deletions src/game-loop/include/populator/ItemFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "other/ItemType.hpp"
#include <entt/entt.hpp>

class ItemFactory {
public:
static entt::entity make(ItemType);
static entt::entity make(ItemType, float pos_x, float pos_y);
};
10 changes: 10 additions & 0 deletions src/game-loop/include/populator/LootFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <entt/entt.hpp>
#include "LootType.hpp"

class LootFactory {
public:
static entt::entity make(LootType);
static entt::entity make(LootType, float pos_x, float pos_y);
};
10 changes: 10 additions & 0 deletions src/game-loop/include/populator/NpcFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "other/NpcType.hpp"
#include <entt/entt.hpp>

class NpcFactory {
public:
static entt::entity make(NpcType);
static entt::entity make(NpcType, float pos_x, float pos_y);
};
9 changes: 6 additions & 3 deletions src/game-loop/include/prefabs/ui/CheatConsole.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
#include "viewport/Viewport.hpp"
#include "game-loop/GameLoop.hpp"

#include <vector>
#include <functional>

namespace prefabs
{
class CheatConsoleComponent
{
public:
bool is_state_change_requested() const { return _state_change_requested; }
void request_state_change(GameLoop::State requested_state) { _requested_state = requested_state; _state_change_requested = true; }
GameLoop::State get_requested_state() const { return _requested_state;}
void request_state_change(GameLoopState requested_state) { _requested_state = requested_state; _state_change_requested = true; }
GameLoopState get_requested_state() const { return _requested_state;}
private:
bool _state_change_requested = false;
GameLoop::State _requested_state{GameLoop::State::CURRENT};
GameLoopState _requested_state{GameLoopState::CURRENT};
};

struct CheatConsole
Expand Down
14 changes: 2 additions & 12 deletions src/game-loop/interface/game-loop/GameLoop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "game-loop/GameLoopStartedState.hpp"
#include "game-loop/GameLoopScoresState.hpp"
#include "game-loop/GameLoopSandboxState.hpp"
#include "game-loop/GameLoopState.hpp"

#include <entt/entt.hpp>

Expand All @@ -35,20 +36,9 @@ class ShoppingSystem;
class GameLoop
{
public:
enum class State
{
MAIN_MENU = 0,
PLAYING,
STARTED,
LEVEL_SUMMARY,
SCORES,
SANDBOX,
CURRENT
};

GameLoop(const std::shared_ptr<Viewport>&);
std::function<bool(uint32_t delta_time_ms)>& get();
GameLoopBaseState* get_game_loop_state_ptr(State);
GameLoopBaseState* get_game_loop_state_ptr(GameLoopState);

private:

Expand Down
1 change: 1 addition & 0 deletions src/game-loop/interface/game-loop/GameLoopBaseState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class GameLoop;

// TODO: Rename to IGameLoopState to not confuse with GameLoopState enum
class GameLoopBaseState
{
public:
Expand Down
18 changes: 18 additions & 0 deletions src/game-loop/interface/game-loop/GameLoopState.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <cstdint>

using GameLoopState_t = std::uint16_t;
enum class GameLoopState : GameLoopState_t
{
MAIN_MENU = 0,
PLAYING,
STARTED,
LEVEL_SUMMARY,
SCORES,
SANDBOX,
CURRENT,
_SIZE
};

const char* to_string(GameLoopState);
10 changes: 8 additions & 2 deletions src/game-loop/interface/other/ItemType.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#pragma once

enum class ItemType
#include <cassert>
#include <cstdint>

using ItemType_t = std::uint16_t;
enum class ItemType : ItemType_t
{
ARROW,
ARROW = 0,
BOMB,
CAPE,
CHEST,
Expand Down Expand Up @@ -30,3 +34,5 @@ enum class ItemType
FLARE,
_SIZE
};

const char* to_string(ItemType item_type);
16 changes: 8 additions & 8 deletions src/game-loop/src/game-loop/GameLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ GameLoop::GameLoop(const std::shared_ptr<Viewport>& viewport)
};
}

GameLoopBaseState *GameLoop::get_game_loop_state_ptr(GameLoop::State state) {
GameLoopBaseState *GameLoop::get_game_loop_state_ptr(GameLoopState state) {
switch (state) {
case State::MAIN_MENU:
case GameLoopState::MAIN_MENU:
return &_states.main_menu;
case State::PLAYING:
case GameLoopState::PLAYING:
return &_states.playing;
case State::STARTED:
case GameLoopState::STARTED:
return &_states.started;
case State::LEVEL_SUMMARY:
case GameLoopState::LEVEL_SUMMARY:
return &_states.level_summary;
case State::SCORES:
case GameLoopState::SCORES:
return &_states.scores;
case State::SANDBOX:
case GameLoopState::SANDBOX:
return &_states.sandbox;
case State::CURRENT:
case GameLoopState::CURRENT:
return _states.current;
default: assert(false);
}
Expand Down
18 changes: 18 additions & 0 deletions src/game-loop/src/game-loop/GameLoopState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "game-loop/GameLoopState.hpp"
#include <cassert>

const char *to_string(GameLoopState game_loop_state) {
#define TO_STRING(x) case GameLoopState::x: return #x;
switch (game_loop_state) {
TO_STRING(MAIN_MENU);
TO_STRING(PLAYING);
TO_STRING(STARTED);
TO_STRING(LEVEL_SUMMARY);
TO_STRING(SCORES);
TO_STRING(SANDBOX);
TO_STRING(CURRENT);
TO_STRING(_SIZE);
}
assert(false);
return "Failed to match passed GameLoopState";
}
115 changes: 115 additions & 0 deletions src/game-loop/src/other/CheatConsoleInterpreter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "other/CheatConsoleInterpreter.h"
#include "game-loop/GameLoop.hpp"
#include "prefabs/ui/CheatConsole.hpp"
#include "patterns/Singleton.hpp"
#include "components/generic/PositionComponent.hpp"
#include "populator/NpcFactory.hpp"
#include "populator/ItemFactory.hpp"
#include "populator/LootFactory.hpp"
#include "EntityRegistry.hpp"
#include "components/specialized/MainDudeComponent.hpp"

template<class Enumerator, class Enumerator_t>
std::map<std::string, Enumerator> populate_string_to_enum_map()
{
std::map<std::string, Enumerator> out;
Enumerator_t enumerator_index = 0;
while (enumerator_index < static_cast<Enumerator_t>(Enumerator::_SIZE)) {
const auto enumerator = static_cast<Enumerator>(enumerator_index);
out.emplace(to_string(enumerator), enumerator);
enumerator_index++;
}
return out;
}

CheatConsoleInterpreter::CheatConsoleInterpreter()
{
_string_to_item_type_map = populate_string_to_enum_map<ItemType, ItemType_t>();
_string_to_npc_type_map = populate_string_to_enum_map<NpcType, NpcType_t>();
_string_to_loot_type_map = populate_string_to_enum_map<LootType, LootType_t>();
_string_to_game_loop_state_map = populate_string_to_enum_map<GameLoopState, GameLoopState_t>();

_spawn_command_handler = CommandHandler([&](const Command& command){
if (command.size() != 2 && (command.at(0) == "HELP" || command.at(0) == "SPAWN")) {
return std::make_pair(false, "spawn <NpcType/ItemType>");
}

if (command.at(0) != "SPAWN") {
return std::make_pair(false, "");
}

const auto& type = command.at(1);
const auto npc_type_match = _string_to_npc_type_map.find(type);
const auto item_type_match = _string_to_item_type_map.find(type);
const auto loot_type_match = _string_to_loot_type_map.find(type);

auto& registry = EntityRegistry::instance().get_registry();
auto dudes = registry.view<MainDudeComponent>();
assert(dudes.size() == 1);
auto dude = dudes.front();
auto &dude_position = registry.get<PositionComponent>(dude);

const float offset_x = -2;
const float offset_y = -2;

if (npc_type_match != _string_to_npc_type_map.end())
{
const NpcType npc_type = npc_type_match->second;
NpcFactory::make(npc_type, dude_position.x_center + offset_x, dude_position.y_center + offset_y);
return std::make_pair(true, "spawning");
}

if (item_type_match != _string_to_item_type_map.end())
{
const ItemType item_type = item_type_match->second;
ItemFactory::make(item_type, dude_position.x_center + offset_x, dude_position.y_center + offset_y);
return std::make_pair(true, "spawning");
}

if (loot_type_match != _string_to_loot_type_map.end())
{
const LootType loot_type = loot_type_match->second;
LootFactory::make(loot_type, dude_position.x_center + offset_x, dude_position.y_center + offset_y);
return std::make_pair(true, "spawning");
}

return std::make_pair(true, "failed to match <NpcType/ItemType>");
});

_enter_command_handler = CommandHandler ([this](const Command& command){
if (command.size() != 2 && (command.at(0) == "HELP" || command.at(0) == "ENTER")) {
return std::make_pair(false, "enter <GameLoop::State>");
}

if (command.at(0) != "ENTER") {
return std::make_pair(false, "");
}

auto& registry = EntityRegistry::instance().get_registry();
auto cheat_consoles = registry.view<prefabs::CheatConsoleComponent>();
assert(cheat_consoles.size() == 1);
auto cheat_console = cheat_consoles.front();
auto& cheat_console_component = registry.get<prefabs::CheatConsoleComponent>(cheat_console);

const auto& requested_game_loop_state = command.at(1);
const auto game_loop_state_match = _string_to_game_loop_state_map.find(requested_game_loop_state);

if (game_loop_state_match == _string_to_game_loop_state_map.end())
{
return std::make_pair(true, "failed to match <GameLoopState>");
}

cheat_console_component.request_state_change(game_loop_state_match->second);
return std::make_pair(true, "entering");
});
}

const CheatConsoleInterpreter::CommandHandler& CheatConsoleInterpreter::get_spawn_command_handler() const
{
return _spawn_command_handler;
}

const CheatConsoleInterpreter::CommandHandler& CheatConsoleInterpreter::get_enter_command_handler() const
{
return _enter_command_handler;
}
Loading

0 comments on commit cc7ad8f

Please sign in to comment.