Skip to content

Commit cc7ad8f

Browse files
committed
Refactor
1 parent 99e95d8 commit cc7ad8f

26 files changed

+602
-126
lines changed

src/game-loop/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ add_library(GameLoop STATIC
334334

335335
src/other/ParticleGenerator.cpp
336336
src/other/Inventory.cpp
337+
src/prefabs/ui/CheatConsole.cpp
338+
src/populator/ItemFactory.cpp
339+
src/populator/LootFactory.cpp
340+
src/populator/NpcFactory.cpp
337341
interface/other/PhysicsComponentType.hpp
338342
interface/other/Inventory.hpp
339343
interface/other/InventoryEvent.hpp
@@ -343,8 +347,10 @@ add_library(GameLoop STATIC
343347
include/other/ParticleGenerator.hpp
344348
include/components/generic/ImguiComponent.hpp
345349
include/prefabs/ui/CheatConsole.hpp
346-
src/prefabs/ui/CheatConsole.cpp
347-
)
350+
include/populator/ItemFactory.hpp
351+
include/populator/NpcFactory.hpp
352+
include/populator/LootFactory.hpp
353+
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)
348354

349355
target_include_directories(GameLoop
350356
PRIVATE include interface
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <string>
5+
#include <functional>
6+
#include <map>
7+
8+
#include "other/ItemType.hpp"
9+
#include "other/NpcType.hpp"
10+
#include "game-loop/GameLoopState.hpp"
11+
#include "LootType.hpp"
12+
13+
class CheatConsoleInterpreter
14+
{
15+
public:
16+
using Command = std::vector<std::string>;
17+
using CommandHandlerResult = std::pair<bool, std::string>;
18+
using CommandHandler = std::function<CommandHandlerResult(const Command&)>;
19+
CheatConsoleInterpreter();
20+
21+
const CommandHandler& get_spawn_command_handler() const;
22+
const CommandHandler& get_enter_command_handler() const;
23+
private:
24+
std::map<std::string, ItemType> _string_to_item_type_map;
25+
std::map<std::string, NpcType> _string_to_npc_type_map;
26+
std::map<std::string, LootType> _string_to_loot_type_map;
27+
std::map<std::string, GameLoopState> _string_to_game_loop_state_map;
28+
29+
CommandHandler _spawn_command_handler;
30+
CommandHandler _enter_command_handler;
31+
};
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#pragma once
22

3-
enum class NpcType
3+
#include <cassert>
4+
#include <cstdint>
5+
6+
using NpcType_t = std::uint16_t;
7+
8+
enum class NpcType : NpcType_t
49
{
5-
NONE,
10+
NONE = 0,
611
SNAKE,
712
BAT,
813
CAVEMAN,
@@ -11,5 +16,8 @@ enum class NpcType
1116
SHOPKEEPER,
1217
DAMSEL,
1318
BLUE_FROG,
14-
RED_FROG
19+
RED_FROG,
20+
_SIZE
1521
};
22+
23+
const char* to_string(NpcType npc_type);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "other/ItemType.hpp"
4+
#include <entt/entt.hpp>
5+
6+
class ItemFactory {
7+
public:
8+
static entt::entity make(ItemType);
9+
static entt::entity make(ItemType, float pos_x, float pos_y);
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <entt/entt.hpp>
4+
#include "LootType.hpp"
5+
6+
class LootFactory {
7+
public:
8+
static entt::entity make(LootType);
9+
static entt::entity make(LootType, float pos_x, float pos_y);
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "other/NpcType.hpp"
4+
#include <entt/entt.hpp>
5+
6+
class NpcFactory {
7+
public:
8+
static entt::entity make(NpcType);
9+
static entt::entity make(NpcType, float pos_x, float pos_y);
10+
};

src/game-loop/include/prefabs/ui/CheatConsole.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
#include "viewport/Viewport.hpp"
55
#include "game-loop/GameLoop.hpp"
66

7+
#include <vector>
8+
#include <functional>
9+
710
namespace prefabs
811
{
912
class CheatConsoleComponent
1013
{
1114
public:
1215
bool is_state_change_requested() const { return _state_change_requested; }
13-
void request_state_change(GameLoop::State requested_state) { _requested_state = requested_state; _state_change_requested = true; }
14-
GameLoop::State get_requested_state() const { return _requested_state;}
16+
void request_state_change(GameLoopState requested_state) { _requested_state = requested_state; _state_change_requested = true; }
17+
GameLoopState get_requested_state() const { return _requested_state;}
1518
private:
1619
bool _state_change_requested = false;
17-
GameLoop::State _requested_state{GameLoop::State::CURRENT};
20+
GameLoopState _requested_state{GameLoopState::CURRENT};
1821
};
1922

2023
struct CheatConsole

src/game-loop/interface/game-loop/GameLoop.hpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "game-loop/GameLoopStartedState.hpp"
1515
#include "game-loop/GameLoopScoresState.hpp"
1616
#include "game-loop/GameLoopSandboxState.hpp"
17+
#include "game-loop/GameLoopState.hpp"
1718

1819
#include <entt/entt.hpp>
1920

@@ -35,20 +36,9 @@ class ShoppingSystem;
3536
class GameLoop
3637
{
3738
public:
38-
enum class State
39-
{
40-
MAIN_MENU = 0,
41-
PLAYING,
42-
STARTED,
43-
LEVEL_SUMMARY,
44-
SCORES,
45-
SANDBOX,
46-
CURRENT
47-
};
48-
4939
GameLoop(const std::shared_ptr<Viewport>&);
5040
std::function<bool(uint32_t delta_time_ms)>& get();
51-
GameLoopBaseState* get_game_loop_state_ptr(State);
41+
GameLoopBaseState* get_game_loop_state_ptr(GameLoopState);
5242

5343
private:
5444

src/game-loop/interface/game-loop/GameLoopBaseState.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
class GameLoop;
66

7+
// TODO: Rename to IGameLoopState to not confuse with GameLoopState enum
78
class GameLoopBaseState
89
{
910
public:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
using GameLoopState_t = std::uint16_t;
6+
enum class GameLoopState : GameLoopState_t
7+
{
8+
MAIN_MENU = 0,
9+
PLAYING,
10+
STARTED,
11+
LEVEL_SUMMARY,
12+
SCORES,
13+
SANDBOX,
14+
CURRENT,
15+
_SIZE
16+
};
17+
18+
const char* to_string(GameLoopState);

0 commit comments

Comments
 (0)