Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cheat console (desktop only) #147

Merged
merged 8 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 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 @@ -339,6 +341,9 @@ add_library(GameLoop STATIC
include/other/LootCollectedEvent.hpp
include/other/NpcType.hpp
include/other/ParticleGenerator.hpp
include/components/generic/ImguiComponent.hpp
include/prefabs/ui/CheatConsole.hpp
src/prefabs/ui/CheatConsole.cpp
)

target_include_directories(GameLoop
Expand Down Expand Up @@ -366,4 +371,5 @@ target_link_libraries(GameLoop
Audio
glad
GraphicsUtils
imgui
dbeef marked this conversation as resolved.
Show resolved Hide resolved
)
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;
};
18 changes: 18 additions & 0 deletions src/game-loop/include/prefabs/ui/CheatConsole.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "entt/entt.hpp"
#include "viewport/Viewport.hpp"
#include "game-loop/GameLoop.hpp"

namespace prefabs
{
struct CheatConsoleComponent
{
GameLoop::State state{GameLoop::State::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
14 changes: 14 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,7 @@
#include "game-loop/GameLoopPlayingState.hpp"
#include "game-loop/GameLoopStartedState.hpp"
#include "game-loop/GameLoopScoresState.hpp"
#include "game-loop/GameLoopSandboxState.hpp"

#include <entt/entt.hpp>

Expand All @@ -34,6 +35,17 @@ 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();
private:
Expand All @@ -44,6 +56,7 @@ class GameLoop
friend class GameLoopStartedState;
friend class GameLoopLevelSummaryState;
friend class GameLoopScoresState;
friend class GameLoopSandboxState;

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

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;
};
30 changes: 30 additions & 0 deletions src/game-loop/src/game-loop/GameLoopMainMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "prefabs/items/Flare.hpp"
#include "prefabs/main-dude/MainDude.hpp"
#include "prefabs/ui/PauseOverlay.hpp"
#include "prefabs/ui/CheatConsole.hpp"

#include <cmath>

Expand Down Expand Up @@ -104,6 +105,33 @@ GameLoopBaseState *GameLoopMainMenuState::update(GameLoop& game_loop, uint32_t d
}
}


auto& cheat_console = registry.get<prefabs::CheatConsoleComponent>(_cheat_console);
dbeef marked this conversation as resolved.
Show resolved Hide resolved
switch (cheat_console.state)
{
case GameLoop::State::SCORES:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.scores;
}
case GameLoop::State::MAIN_MENU:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.main_menu;
}
case GameLoop::State::PLAYING:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.playing;
}
case GameLoop::State::SANDBOX:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.sandbox;
}
default: {}
}

if (position.y_center <= EXIT_LEVEL_Y)
{
log_info("Quitting using rope.");
Expand Down Expand Up @@ -134,6 +162,7 @@ void GameLoopMainMenuState::enter(GameLoop& game_loop)
auto& model_view_camera = rendering_system->get_model_view_camera();
model_view_camera.set_x_not_rounded(game_loop._viewport->get_width_world_units() / 4.0f);
model_view_camera.set_y_not_rounded(game_loop._viewport->get_height_world_units() / 4.0f);
model_view_camera.update_gl_modelview_matrix();

prefabs::StartSign::create(5.55, 9.0);
prefabs::ScoresSign::create(9.55, 9.0);
Expand All @@ -155,6 +184,7 @@ void GameLoopMainMenuState::enter(GameLoop& game_loop)

_pause_overlay = prefabs::PauseOverlay::create(game_loop._viewport, PauseOverlayComponent::Type::MAIN_MENU);
_main_dude = prefabs::MainDude::create(17.5, 9.5);
_cheat_console = prefabs::CheatConsole::create(game_loop._viewport);

game_loop._level_summary_tracker->reset();

Expand Down
23 changes: 23 additions & 0 deletions src/game-loop/src/game-loop/GameLoopPlayingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "Level.hpp"
#include "audio/Audio.hpp"
#include "populator/Populator.hpp"
#include "prefabs/ui/CheatConsole.hpp"

GameLoopBaseState *GameLoopPlayingState::update(GameLoop& game_loop, uint32_t delta_time_ms)
{
Expand Down Expand Up @@ -112,6 +113,27 @@ GameLoopBaseState *GameLoopPlayingState::update(GameLoop& game_loop, uint32_t de

game_loop._level_summary_tracker->update(delta_time_ms);

auto& cheat_console = registry.get<prefabs::CheatConsoleComponent>(_cheat_console);
dbeef marked this conversation as resolved.
Show resolved Hide resolved
switch (cheat_console.state)
{
case GameLoop::State::SCORES:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.scores;
}
case GameLoop::State::MAIN_MENU:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.main_menu;
}
case GameLoop::State::PLAYING:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.playing;
}
default: {}
}

return this;
}

Expand Down Expand Up @@ -147,6 +169,7 @@ void GameLoopPlayingState::enter(GameLoop& game_loop)
_pause_overlay = prefabs::PauseOverlay::create(game_loop._viewport, PauseOverlayComponent::Type::PLAYING);
_death_overlay = prefabs::DeathOverlay::create(game_loop._viewport);
_hud = prefabs::HudOverlay::create(game_loop._viewport);
_cheat_console = prefabs::CheatConsole::create(game_loop._viewport);

game_loop._rendering_system->sort();

Expand Down
141 changes: 141 additions & 0 deletions src/game-loop/src/game-loop/GameLoopSandboxState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include "game-loop/GameLoopSandboxState.hpp"
#include "game-loop/GameLoop.hpp"

#include "EntityRegistry.hpp"

#include "prefabs/main-dude/MainDude.hpp"
#include "prefabs/ui/ScoresOverlay.hpp"
#include "prefabs/ui/PauseOverlay.hpp"
#include "prefabs/props/ResetSign.hpp"

#include "components/specialized/PauseOverlayComponent.hpp"
#include "components/specialized/MainDudeComponent.hpp"

#include "system/RenderingSystem.hpp"
#include "system/ScriptingSystem.hpp"
#include "system/PhysicsSystem.hpp"
#include "system/ShoppingSystem.hpp"
#include "system/AnimationSystem.hpp"
#include "system/InputSystem.hpp"
#include "system/DisposingSystem.hpp"
#include "system/ParticleSystem.hpp"
#include "system/ItemSystem.hpp"

#include "populator/Populator.hpp"
#include "logger/log.h"
#include "ModelViewCamera.hpp"
#include "ScreenSpaceCamera.hpp"
#include "CameraType.hpp"
#include "Level.hpp"
#include "other/Inventory.hpp"
#include "prefabs/ui/CheatConsole.hpp"

GameLoopBaseState *GameLoopSandboxState::update(GameLoop& game_loop, uint32_t delta_time_ms)
{
auto& registry = EntityRegistry::instance().get_registry();

auto& rendering_system = game_loop._rendering_system;
auto& scripting_system = game_loop._scripting_system;
auto& physics_system = game_loop._physics_system;
auto& animation_system = game_loop._animation_system;
auto& input_system = game_loop._input_system;
auto& disposing_system = game_loop._disposing_system;
auto& particle_system = game_loop._particle_system;
auto& item_system = game_loop._item_system;

// Adjust camera to follow main dude:
auto& position = registry.get<PositionComponent>(_main_dude);
auto& model_view_camera = game_loop._rendering_system->get_model_view_camera();

model_view_camera.adjust_to_bounding_box(position.x_center, position.y_center);
model_view_camera.adjust_to_level_boundaries(Consts ::LEVEL_WIDTH_TILES, Consts::LEVEL_HEIGHT_TILES);
model_view_camera.update_gl_modelview_matrix();

rendering_system->update(delta_time_ms);
input_system->update(delta_time_ms);

auto& pause = registry.get<PauseOverlayComponent>(_pause_overlay);

if (pause.is_paused())
{
if (pause.is_quit_requested())
{
log_info("Quit requested.");
game_loop._exit = true;
}

pause.update(registry);
}
else
{
physics_system->update(delta_time_ms);
animation_system->update(delta_time_ms);
scripting_system->update(delta_time_ms);
disposing_system->update(delta_time_ms);
particle_system->update(delta_time_ms);
item_system->update(delta_time_ms);
}

auto& dude = registry.get<MainDudeComponent>(_main_dude);

if (dude.entered_door())
{
return &game_loop._states.main_menu;
}


auto& cheat_console = registry.get<prefabs::CheatConsoleComponent>(_cheat_console);
switch (cheat_console.state)
{
case GameLoop::State::SCORES:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.scores;
}
case GameLoop::State::MAIN_MENU:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.main_menu;
}
case GameLoop::State::PLAYING:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.playing;
}
default: {}
}

return this;
}

void GameLoopSandboxState::enter(GameLoop& game_loop)
{
log_info("Entered GameLoopSandboxState");

auto& registry = EntityRegistry::instance().get_registry();

auto& rendering_system = game_loop._rendering_system;

Level::instance().get_tile_batch().clean();
Level::instance().get_tile_batch().generate_frame();
Level::instance().get_tile_batch().generate_cave_background();
Level::instance().get_tile_batch().batch_vertices();
Level::instance().get_tile_batch().add_render_entity(registry);

auto& inventory = Inventory::instance();
inventory.clear_items();
game_loop._shopping_system = std::make_shared<ShoppingSystem>();

float pos_x = Consts::LEVEL_WIDTH_TILES / 2;
float pos_y = Consts::LEVEL_HEIGHT_TILES / 2;

_main_dude = prefabs::MainDude::create(pos_x, pos_y);
_pause_overlay = prefabs::PauseOverlay::create(game_loop._viewport, PauseOverlayComponent::Type::SCORES);
_cheat_console = prefabs::CheatConsole::create(game_loop._viewport);
}

void GameLoopSandboxState::exit(GameLoop& game_loop)
{
auto& registry = EntityRegistry::instance().get_registry();
registry.clear();
}
Loading
Loading