Adding a state and scene utils

This commit is contained in:
Zoe Roux
2021-05-24 14:59:28 +02:00
parent 6f7e7ffaf9
commit 54fe800549
8 changed files with 136 additions and 32 deletions
+4
View File
@@ -7,7 +7,11 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal)
add_executable(bomberman
sources/main.cpp
sources/Models/GameState.hpp
sources/Runner/Runner.cpp
sources/Runner/Runner.hpp
)
target_include_directories(bomberman PUBLIC sources)
find_package(raylib QUIET)
if (NOT raylib_FOUND)
+6
View File
@@ -50,5 +50,11 @@ namespace WAL
~Callback() = default;
//! @brief A default assignment operator
Callback &operator=(const Callback &) = default;
//! @brief Implicitly transform a function into a callback.
Callback(std::function<void (Types...)> callback) // NOLINT(google-explicit-constructor)
{
this->addCallback(callback);
}
};
}
-19
View File
@@ -12,25 +12,6 @@ namespace WAL
{
std::chrono::nanoseconds Wal::timestep = 8ms;
void Wal::run()
{
auto lastTick = std::chrono::steady_clock::now();
std::chrono::nanoseconds fBehind(0);
while (!this->_shouldClose) {
auto now = std::chrono::steady_clock::now();
std::chrono::nanoseconds dtime = now - lastTick;
fBehind += dtime;
lastTick = now;
while (fBehind > Wal::timestep) {
fBehind -= Wal::timestep;
this->_fixedUpdate();
}
this->_update(dtime);
}
}
void Wal::_update(std::chrono::nanoseconds dtime)
{
auto &entities = this->_scene.getEntities();
+36 -2
View File
@@ -8,10 +8,11 @@
#include <vector>
#include <memory>
#include <typeinfo>
#include <Exception/WalError.hpp>
#include "Exception/WalError.hpp"
#include "Scene/Scene.hpp"
#include "Entity/Entity.hpp"
#include "System/System.hpp"
#include "Models/Callback.hpp"
namespace WAL
{
@@ -101,7 +102,40 @@ namespace WAL
}
//! @brief Start the game loop
void run();
//! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here)
//! @param state An initial game state. If not specified, it will be defaulted.
//! @tparam T A type used to track your game state. It must be default constructable.
template<typename T>
void run(const std::function<void (Wal &, T &)> &callback, T state = T())
{
Callback<Wal &, T &> update(callback);
return this->run(update, state);
}
//! @brief Start the game loop
//! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here)
//! @param state An initial game state. If not specified, it will be defaulted.
//! @tparam T A type used to track your game state. It must be default constructable.
template<typename T>
void run(const Callback<Wal &, T &> &callback, T state = T())
{
auto lastTick = std::chrono::steady_clock::now();
std::chrono::nanoseconds fBehind(0);
while (!this->_shouldClose) {
auto now = std::chrono::steady_clock::now();
std::chrono::nanoseconds dtime = now - lastTick;
fBehind += dtime;
lastTick = now;
while (fBehind > Wal::timestep) {
fBehind -= Wal::timestep;
this->_fixedUpdate();
}
this->_update(dtime);
callback(*this, state);
}
}
//! @brief A default constructor
Wal() = default;
+31
View File
@@ -0,0 +1,31 @@
//
// Created by Zoe Roux on 5/24/21.
//
#pragma once
#include <unordered_map>
#include <Scene/Scene.hpp>
namespace Bomberman
{
//! @brief A class representing the current game state. This allow one to retain information between update calls.
class GameState
{
//! @brief The list of scenes available.
enum SceneID
{
MainMenu,
GameScene
};
//! @brief The currently loaded scene
SceneID currentScene = MainMenu;
//! @brief The list of loaded scenes.
std::unordered_map<SceneID, WAL::Scene> _loadedScenes = {};
};
}
+32
View File
@@ -0,0 +1,32 @@
//
// Created by Zoe Roux on 5/24/21.
//
#include <Wal.hpp>
#include <iostream>
#include "Runner.hpp"
#include "Models/GameState.hpp"
namespace Bomberman
{
void updateState(WAL::Wal &engine, GameState &state)
{
// You can change the scene here or update the game state based on entities values.
// If you want to keep a scene loaded but not running, store it in the state.loadedScenes.
// If you don't need the scene anymore, remember to remove it from the loadedScene array.
}
int run()
{
WAL::Wal wal;
try {
wal.run<GameState>(updateState);
return 0;
} catch (const std::exception &ex) {
std::cerr << ex.what() << std::endl;
return 1;
}
}
}
+12
View File
@@ -0,0 +1,12 @@
//
// Created by Zoe Roux on 5/24/21.
//
#pragma once
namespace Bomberman
{
//! @brief Start the game and run a Bomberman.
//! @return 0 on success, another value on error.
int run();
}
+15 -11
View File
@@ -7,17 +7,21 @@
#include <iostream>
#include <Wal.hpp>
#include "Runner/Runner.hpp"
int main()
void usage(const std::string &bin)
{
WAL::Wal wal;
try {
wal.run();
return 0;
} catch (const std::exception &ex) {
std::cerr << ex.what() << std::endl;
return 84;
}
std::cout << "Bomberman." << std::endl
<< "\tUsage: " << bin << " [options]" << std::endl
<< "Options:" << std::endl
<< "\t-h:\tPrint this help message" << std::endl;
}
int main(int argc, char **argv)
{
if (argc == 2 && std::string(argv[1]) == "-h") {
usage(argv[0]);
return 1;
}
return Bomberman::run();
}