Skip to content

Commit

Permalink
Added cheat console (desktop only) (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeef authored Dec 26, 2023
1 parent 8126ba9 commit ea11078
Show file tree
Hide file tree
Showing 47 changed files with 1,250 additions and 50 deletions.
2 changes: 2 additions & 0 deletions cmake/DependenciesDarwin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ macro(add_darwin_dependencies)
target_compile_definitions(Dependencies INTERFACE
SPELUNKY_PSP_PLATFORM_DARWIN
SPELUNKY_PSP_PLATFORM_DESKTOP
SPELUNKY_PSP_WITH_IMGUI
)
set(SPELUNKY_PSP_WITH_IMGUI TRUE)
endmacro()

macro(spelunky_psp_post_build_darwin)
Expand Down
3 changes: 3 additions & 0 deletions cmake/DependenciesLinux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ macro(add_linux_dependencies)

add_library(Dependencies INTERFACE)
target_link_libraries(Dependencies INTERFACE SDL_2_XX)
# TODO: Separate target, i.e PlatformDefinitions
target_compile_definitions(Dependencies INTERFACE
SPELUNKY_PSP_PLATFORM_LINUX
SPELUNKY_PSP_PLATFORM_DESKTOP
SPELUNKY_PSP_WITH_IMGUI
)
set(SPELUNKY_PSP_WITH_IMGUI TRUE)
endmacro()

macro(spelunky_psp_post_build_linux)
Expand Down
4 changes: 3 additions & 1 deletion cmake/DependenciesWindows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ macro(add_windows_dependencies)
target_compile_definitions(Dependencies INTERFACE
SPELUNKY_PSP_PLATFORM_WINDOWS
SPELUNKY_PSP_PLATFORM_DESKTOP
)
SPELUNKY_PSP_WITH_IMGUI
)
set(SPELUNKY_PSP_WITH_IMGUI TRUE)
endmacro()

macro(spelunky_psp_post_build_windows)
Expand Down
19 changes: 19 additions & 0 deletions src/game-loop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_library(GameLoop STATIC
src/game-loop/GameLoopStartedState.cpp
src/game-loop/GameLoopLevelSummaryState.cpp
src/game-loop/GameLoopScoresState.cpp
src/game-loop/GameLoopSandboxState.cpp

interface/game-loop/GameLoop.hpp
interface/game-loop/GameLoopBaseState.hpp
Expand All @@ -17,6 +18,7 @@ add_library(GameLoop STATIC
interface/game-loop/GameLoopStartedState.hpp
interface/game-loop/GameLoopLevelSummaryState.hpp
interface/game-loop/GameLoopScoresState.hpp
interface/game-loop/GameLoopSandboxState.hpp

# Main dude:

Expand Down Expand Up @@ -324,6 +326,10 @@ add_library(GameLoop STATIC

# Other:

src/other/CheatConsoleInterpreter.cpp
src/other/NpcType.cpp
src/other/ItemType.cpp
src/game-loop/GameLoopState.cpp
src/populator/Populator.cpp
src/populator/Shop.cpp
include/populator/Populator.hpp
Expand All @@ -332,13 +338,24 @@ 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/game-loop/GameLoopState.hpp
interface/other/Inventory.hpp
interface/other/InventoryEvent.hpp
interface/other/ItemType.hpp
include/other/LootCollectedEvent.hpp
include/other/NpcType.hpp
include/other/ParticleGenerator.hpp
include/components/generic/ImguiComponent.hpp
include/prefabs/ui/CheatConsole.hpp
include/populator/ItemFactory.hpp
include/populator/NpcFactory.hpp
include/populator/LootFactory.hpp
include/other/CheatConsoleInterpreter.h
)

target_include_directories(GameLoop
Expand Down Expand Up @@ -366,4 +383,6 @@ target_link_libraries(GameLoop
Audio
glad
GraphicsUtils
$<$<BOOL:${SPELUNKY_PSP_WITH_IMGUI}>:imgui>
Dependencies
)
8 changes: 8 additions & 0 deletions src/game-loop/include/components/generic/ImguiComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <functional>

struct ImguiComponent
{
std::function<void()> render_callback;
};
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);
};
27 changes: 27 additions & 0 deletions src/game-loop/include/prefabs/ui/CheatConsole.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "entt/entt.hpp"
#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(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;
GameLoopState _requested_state{GameLoopState::CURRENT};
};

struct CheatConsole
{
static entt::entity create(const std::shared_ptr<Viewport>& viewport);
};
}
2 changes: 2 additions & 0 deletions src/game-loop/include/system/RenderingSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class RenderingSystem final : public System

private:

void update_opengl(std::uint32_t delta_time_ms);
void update_imgui();
void use_camera(CameraType camera_type);
void use_model_view_camera();
void use_screen_space_camera();
Expand Down
6 changes: 6 additions & 0 deletions src/game-loop/interface/game-loop/GameLoop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "game-loop/GameLoopPlayingState.hpp"
#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 @@ -36,6 +38,8 @@ class GameLoop
public:
GameLoop(const std::shared_ptr<Viewport>&);
std::function<bool(uint32_t delta_time_ms)>& get();
GameLoopBaseState* get_game_loop_state_ptr(GameLoopState);

private:

friend class GameLoopBaseState;
Expand All @@ -44,6 +48,7 @@ class GameLoop
friend class GameLoopStartedState;
friend class GameLoopLevelSummaryState;
friend class GameLoopScoresState;
friend class GameLoopSandboxState;

struct
{
Expand All @@ -52,6 +57,7 @@ class GameLoop
GameLoopStartedState started;
GameLoopLevelSummaryState level_summary;
GameLoopScoresState scores;
GameLoopSandboxState sandbox;
GameLoopBaseState* current;
} _states;

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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GameLoopMainMenuState : public GameLoopBaseState
void enter(GameLoop&) override;
void exit(GameLoop&) override;
private:
entt::entity _cheat_console = entt::null;
entt::entity _main_dude = entt::null;
entt::entity _pause_overlay = entt::null;
entt::entity _death_overlay = entt::null;
Expand Down
1 change: 1 addition & 0 deletions src/game-loop/interface/game-loop/GameLoopPlayingState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ class GameLoopPlayingState : public GameLoopBaseState, public Observer<MainDudeE
entt::entity _hud = entt::null;
entt::entity _pause_overlay = entt::null;
entt::entity _death_overlay = entt::null;
entt::entity _cheat_console = entt::null;
};
22 changes: 22 additions & 0 deletions src/game-loop/interface/game-loop/GameLoopSandboxState.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <memory>
#include <entt/entt.hpp>
#include "game-loop/GameLoopBaseState.hpp"

class GameLoop;
class PauseOverlayComponent;
class ScoresOverlayComponent;

class GameLoopSandboxState : public GameLoopBaseState
{
public:
GameLoopBaseState* update(GameLoop&, uint32_t delta_time_ms) override;
void enter(GameLoop&) override;
void exit(GameLoop&) override;
private:
entt::entity _main_dude = entt::null;
entt::entity _pause_overlay = entt::null;
entt::entity _death_overlay = entt::null;
entt::entity _cheat_console = entt::null;
};
1 change: 1 addition & 0 deletions src/game-loop/interface/game-loop/GameLoopScoresState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class GameLoopScoresState : public GameLoopBaseState
entt::entity _main_dude = entt::null;
entt::entity _pause_overlay = entt::null;
entt::entity _death_overlay = entt::null;
entt::entity _cheat_console = entt::null;
};
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);
20 changes: 20 additions & 0 deletions src/game-loop/src/game-loop/GameLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,23 @@ GameLoop::GameLoop(const std::shared_ptr<Viewport>& viewport)
return _exit;
};
}

GameLoopBaseState *GameLoop::get_game_loop_state_ptr(GameLoopState state) {
switch (state) {
case GameLoopState::MAIN_MENU:
return &_states.main_menu;
case GameLoopState::PLAYING:
return &_states.playing;
case GameLoopState::STARTED:
return &_states.started;
case GameLoopState::LEVEL_SUMMARY:
return &_states.level_summary;
case GameLoopState::SCORES:
return &_states.scores;
case GameLoopState::SANDBOX:
return &_states.sandbox;
case GameLoopState::CURRENT:
return _states.current;
default: assert(false);
}
}
Loading

0 comments on commit ea11078

Please sign in to comment.