Merge pull request #63 from AnonymusRaccoon/ecs

State management, file system cleanup
This commit is contained in:
Clément Le Bihan
2021-05-26 11:25:28 +02:00
committed by GitHub
22 changed files with 220 additions and 135 deletions
+2 -2
View File
@@ -19,7 +19,7 @@ jobs:
run: |
mkdir build && cd build
cmake ..
cmake --build . -t wal_tests
cmake --build . -t unit_tests
- name: Run tests
run: |
./build/lib/wal/wal_tests
./build/unit_tests
+35 -3
View File
@@ -9,8 +9,40 @@ include_directories(bomberman lib/wal/sources)
add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal)
add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray)
add_executable(bomberman
sources/main.cpp
set(SOURCES
sources/Models/GameState.hpp
sources/Runner/Runner.cpp
sources/Runner/Runner.hpp
sources/Component/Position/PositionComponent.cpp
sources/Component/Position/PositionComponent.hpp
sources/Component/Movable/MovableComponent.cpp
sources/Component/Movable/MovableComponent.hpp
sources/System/Movable/MovableSystem.hpp
sources/System/Movable/MovableSystem.cpp
sources/Models/Vector3.hpp
)
target_link_libraries(bomberman wal ray)
add_executable(bomberman
sources/main.cpp
${SOURCES}
)
target_include_directories(bomberman PUBLIC sources)
target_link_libraries(bomberman PUBLIC wal ray)
add_executable(unit_tests EXCLUDE_FROM_ALL
${SOURCES}
tests/EntityTests.cpp
tests/MainTest.cpp
tests/EngineTests.cpp
tests/CallbackTest.cpp
)
target_include_directories(unit_tests PUBLIC sources)
target_link_libraries(unit_tests PUBLIC wal ray)
find_package(Catch2 QUIET)
if (NOT Catch2_FOUND)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/lib/catch2)
find_package(Catch2 REQUIRED)
endif()
target_link_libraries(unit_tests PRIVATE Catch2::Catch2)
+1 -25
View File
@@ -15,32 +15,8 @@ add_library(wal
sources/Exception/WalError.hpp
sources/Entity/Entity.cpp
sources/Component/Component.cpp
sources/Component/Position/PositionComponent.cpp
sources/Component/Position/PositionComponent.hpp
sources/Models/Vector3.hpp
sources/Component/Movable/MovableComponent.cpp
sources/Component/Movable/MovableComponent.hpp
sources/System/Movable/MovableSystem.cpp
sources/System/Movable/MovableSystem.hpp
sources/System/System.cpp
sources/Models/Callback.hpp
)
target_include_directories(wal PUBLIC sources)
add_executable(wal_tests EXCLUDE_FROM_ALL
tests/EntityTests.cpp
tests/MainTest.cpp
tests/EngineTests.cpp
tests/CallbackTest.cpp
)
target_link_libraries(wal_tests PRIVATE wal)
find_package(Catch2 QUIET)
if (NOT Catch2_FOUND)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../catch2)
find_package(Catch2 REQUIRED)
endif()
target_link_libraries(wal_tests PRIVATE Catch2::Catch2)
target_include_directories(wal PUBLIC sources)
+6
View File
@@ -52,5 +52,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);
}
};
} // namespace WAL
-43
View File
@@ -1,43 +0,0 @@
//
// Created by Zoe Roux on 2021-05-14.
//
#pragma once
#include <queue>
#include "Scene/Scene.hpp"
namespace WAL
{
//! @brief A class to manage scenes
class SceneManager
{
private:
std::deque<Scene> _scenes = {};
public:
//! @brief Add a scene to the container and move to it.
//! @return The manager instance used to call this function is returned. This allow method chaining.
SceneManager &addScene(Scene &&scene);
//! @brief Add a scene before the current scene. This could be useful for lobbies or scene where the next scene can be constructed.
//! @return The manager instance used to call this function is returned. This allow method chaining.
SceneManager &addBackScene(Scene &&scene);
//! @brief Get the current scene
Scene &getCurrent();
//! @brief Remove the current scene and switch to the previous scene on the stack.
//! @return The manager instance used to call this function is returned. This allow method chaining.
SceneManager &closeCurrent();
//! @brief A default constructor
SceneManager() = default;
//! @brief A scene manager is copy constructable
SceneManager(const SceneManager &) = default;
//! @brief A default destructor.
~SceneManager() = default;
//! @brief A scene manager is assignable
SceneManager &operator=(const SceneManager &) = default;
};
}
-19
View File
@@ -10,25 +10,6 @@ namespace WAL
{
std::chrono::nanoseconds Wal::timestep = std::chrono::milliseconds(8);
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
@@ -9,10 +9,11 @@
#include <string>
#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;
@@ -4,13 +4,13 @@
#include "MovableComponent.hpp"
namespace WAL
namespace BBM
{
MovableComponent::MovableComponent(Entity &entity)
MovableComponent::MovableComponent(WAL::Entity &entity)
: Component(entity)
{}
Component *MovableComponent::clone(Entity &entity) const
WAL::Component *MovableComponent::clone(WAL::Entity &entity) const
{
return new MovableComponent(entity);
}
@@ -7,10 +7,10 @@
#include "Models/Vector3.hpp"
#include "Entity/Entity.hpp"
namespace WAL
namespace BBM
{
//! @brief A component to place on entities that can move or be moved.
class MovableComponent : public Component
class MovableComponent : public WAL::Component
{
private:
//! @brief The acceleration of this entity.
@@ -23,10 +23,10 @@ namespace WAL
void addForce(Vector3f force);
//! @inherit
Component *clone(Entity &entity) const override;
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Create a new movable component.
explicit MovableComponent(Entity &entity);
explicit MovableComponent(WAL::Entity &entity);
//! @brief A movable component is copy constructable.
MovableComponent(const MovableComponent &) = default;
//! @brief A default destructor
@@ -4,24 +4,24 @@
#include "PositionComponent.hpp"
namespace WAL
namespace BBM
{
PositionComponent::PositionComponent(Entity &entity)
PositionComponent::PositionComponent(WAL::Entity &entity)
: Component(entity),
position()
{}
PositionComponent::PositionComponent(Entity &entity, Vector3f pos)
PositionComponent::PositionComponent(WAL::Entity &entity, Vector3f pos)
: Component(entity),
position(pos)
{}
PositionComponent::PositionComponent(Entity &entity, float x, float y, float z)
PositionComponent::PositionComponent(WAL::Entity &entity, float x, float y, float z)
: Component(entity),
position(x, y, z)
{}
Component *PositionComponent::clone(WAL::Entity &entity) const
WAL::Component *PositionComponent::clone(WAL::Entity &entity) const
{
return new PositionComponent(entity, this->position);
}
@@ -7,10 +7,10 @@
#include "Models/Vector3.hpp"
#include "Component/Component.hpp"
namespace WAL
namespace BBM
{
//! @brief A basic position component
class PositionComponent : public Component
class PositionComponent : public WAL::Component
{
public:
//! @brief Get the editable position of this entity
@@ -24,14 +24,14 @@ namespace WAL
float getZ() const;
//! @inherit
Component *clone(Entity &entity) const override;
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Create a new PositionComponent linked to a specific entity
explicit PositionComponent(Entity &entity);
explicit PositionComponent(WAL::Entity &entity);
//! @brief Create a new PositionComponent at a certain position
PositionComponent(Entity &entity, Vector3f pos);
PositionComponent(WAL::Entity &entity, Vector3f pos);
//! @brief Create a new PositionComponent at a certain position
PositionComponent(Entity &entity, float x, float y, float z);
PositionComponent(WAL::Entity &entity, float x, float y, float z);
//! @brief A position component is copy constructable
PositionComponent(const PositionComponent &) = default;
//! @brief A default destructor
+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 BBM
{
//! @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 = {};
};
}
@@ -8,7 +8,7 @@
#include <iostream>
#include <cmath>
namespace WAL
namespace BBM
{
//! @brief A Vector3 data type. (templated to allow any kind of vector3)
template<typename T>
@@ -160,7 +160,7 @@ namespace WAL
} // namespace WAL
template<typename T>
std::ostream &operator<<(std::ostream &s, const WAL::Vector3<T> &v)
std::ostream &operator<<(std::ostream &s, const BBM::Vector3<T> &v)
{
s << "Vector3<" << typeid(T).name() << ">("<< v.x << ", " << v.y << ", " << v.z << ")";
return s;
+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 BBM
{
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 BBM
{
//! @brief Start the game and run a Bomberman.
//! @return 0 on success, another value on error.
int run();
}
@@ -3,11 +3,11 @@
//
#include "Component/Position/PositionComponent.hpp"
#include "System/Movable/MovableSystem.hpp"
#include "MovableSystem.hpp"
#include "Component/Movable/MovableComponent.hpp"
#include "Wal.hpp"
namespace WAL
namespace BBM
{
MovableSystem::MovableSystem()
: System({
@@ -16,13 +16,13 @@ namespace WAL
})
{}
void MovableSystem::onFixedUpdate(Entity &entity)
void MovableSystem::onFixedUpdate(WAL::Entity &entity)
{
auto &movable = entity.getComponent<MovableComponent>();
auto &position = entity.getComponent<PositionComponent>();
position.position += movable._velocity * Wal::timestep.count();
movable._velocity = movable._acceleration * Wal::timestep.count();
position.position += movable._velocity * WAL::Wal::timestep.count();
movable._velocity = movable._acceleration * WAL::Wal::timestep.count();
movable._acceleration = Vector3f();
}
} // namespace WAL
@@ -7,14 +7,14 @@
#include "System/System.hpp"
namespace WAL
namespace BBM
{
//! @brief A system to handle movable entities. This system update velocity based on accelerations and positions based on velocity.
class MovableSystem : public System
class MovableSystem : public WAL::System
{
public:
//! @inherit
void onFixedUpdate(Entity &entity) override;
void onFixedUpdate(WAL::Entity &entity) override;
//! @brief A default constructor
MovableSystem();
+35 -13
View File
@@ -6,7 +6,10 @@
*/
#include "Wal.hpp"
#include <iostream>
#include "Runner/Runner.hpp"
// Dependencies of the demo
#include "Camera/Camera3D.hpp"
#include "Controllers/Keyboard.hpp"
#include "Drawables/2D/Text.hpp"
@@ -17,19 +20,19 @@
#include "Vector/Vector3.hpp"
#include "Window.hpp"
int main()
int demo()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
RAY::Window &window = RAY::Window::getInstance(screenWidth, screenHeight, "Bidibidibop", FLAG_WINDOW_RESIZABLE);
RAY::Camera::Camera3D camera(RAY::Vector3(10.0f, 10.0f, 10.0f),
RAY::Vector3(0.0f, 0.0f, 0.0f),
RAY::Vector3(0.0f, 1.0f, 0.0f),
45.0f, CAMERA_PERSPECTIVE
);
RAY::Vector3(0.0f, 0.0f, 0.0f),
RAY::Vector3(0.0f, 1.0f, 0.0f),
45.0f, CAMERA_PERSPECTIVE
);
RAY::Model model("assets/guy.iqm");
RAY::Texture texture("assets/guytex.png");
RAY::ModelAnimations animations("assets/guy.iqm");
@@ -63,17 +66,17 @@ int main()
//----------------------------------------------------------------------------------
window.setDrawingState(RAY::Window::DRAWING);
window.clear();
window.clear();
window.useCamera(camera);
window.useCamera(camera);
window.draw(model, position, RAY::Vector3(1.0f, 0.0f, 0.0f), -90.0f, RAY::Vector3( 1.0f, 1.0f, 1.0f ));
window.draw(model, position, RAY::Vector3(1.0f, 0.0f, 0.0f), -90.0f, RAY::Vector3( 1.0f, 1.0f, 1.0f ));
window.draw(grid);
window.draw(grid);
window.unuseCamera();
window.unuseCamera();
window.draw(instructionText);
window.draw(instructionText);
window.setDrawingState(RAY::Window::IDLE);
//----------------------------------------------------------------------------------
@@ -87,3 +90,22 @@ int main()
return 0;
}
void usage(const std::string &bin)
{
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 demo();
return BBM::run();
}
@@ -8,6 +8,7 @@
#include <catch2/catch.hpp>
using namespace WAL;
using namespace BBM;
TEST_CASE("Create system", "[Engine][System]")
{
@@ -7,6 +7,7 @@
#include <catch2/catch.hpp>
using namespace WAL;
using namespace BBM;
TEST_CASE("Component", "[Entity]")
{