diff --git a/.github/workflows/build_web.yml b/.github/workflows/build_web.yml new file mode 100644 index 00000000..2b9a2f3e --- /dev/null +++ b/.github/workflows/build_web.yml @@ -0,0 +1,20 @@ +name: Web Build (Emscripten) +on: [push, pull_request] + +jobs: + Build: + if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + with: + submodules: true + - name: Exec + shell: bash + run: ./build_web.sh + - name: Check files creation + run: | + test -f build_web/bomberman.html + test -f build_web/bomberman.js + test -f build_web/bomberman.wasm + test -f build_web/bomberman.data diff --git a/.gitignore b/.gitignore index 42cd6b29..f93eff30 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,7 @@ cmake-build-debug ./bomberman .vscode build/* -docs/* \ No newline at end of file +docs/* +emsdk/ +build_web/* +wasm-python.py \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index f1f860c8..71343bba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,13 @@ include_directories(bomberman sources) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) +if (EMSCRIPTEN) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NO_DISABLE_EXCEPTION_CATCHING --shell-file ../sources/wasm/frontend.html --preload-file ../assets") + set(CMAKE_EXECUTABLE_SUFFIX ".html") +endif () + set(SOURCES sources/Models/GameState.hpp sources/Runner/Runner.cpp @@ -46,25 +53,25 @@ set(SOURCES sources/System/GridCentered/GridCenteredSystem.cpp sources/System/GridCentered/GridCenteredSystem.hpp sources/Models/Vector2.hpp - sources/Component/Renderer/Drawable3DComponent.hpp sources/Component/Renderer/Drawable2DComponent.hpp - sources/System/Renderer/Renderer3DSystem.hpp - sources/System/Renderer/Renderer2DSystem.hpp - sources/System/Renderer/RenderScreenSystem.hpp - sources/System/Renderer/RenderScreenSystem.cpp + sources/Component/Renderer/Drawable3DComponent.hpp + sources/System/Renderer/RenderSystem.hpp + sources/System/Renderer/RenderSystem.cpp sources/Component/Renderer/CameraComponent.cpp sources/Component/Renderer/CameraComponent.hpp - sources/System/Renderer/Render2DScreenSystem.cpp - sources/System/Renderer/Render2DScreenSystem.hpp + sources/Component/Animation/AnimationsComponent.cpp + sources/Component/Animation/AnimationsComponent.hpp + sources/System/Animation/AnimationsSystem.cpp + sources/System/Animation/AnimationsSystem.hpp sources/Component/Collision/CollisionComponent.cpp sources/Component/Collision/CollisionComponent.hpp sources/System/Collision/CollisionSystem.hpp sources/System/Collision/CollisionSystem.cpp ) - add_executable(bomberman sources/main.cpp - ${SOURCES}) + ${SOURCES} + ) target_include_directories(bomberman PUBLIC sources) target_link_libraries(bomberman PUBLIC wal ray) @@ -75,9 +82,9 @@ add_executable(unit_tests EXCLUDE_FROM_ALL tests/MainTest.cpp tests/EngineTests.cpp tests/CallbackTest.cpp - tests/CollisionTest.cpp tests/MoveTests.cpp -) + tests/CollisionTest.cpp + ) target_include_directories(unit_tests PUBLIC sources) target_link_libraries(unit_tests PUBLIC wal ray) diff --git a/README.md b/README.md index 18e93e7a..6bfa9002 100644 --- a/README.md +++ b/README.md @@ -40,12 +40,13 @@ mkdir build cd build cmake .. cmake --build . +cd .. ``` Enjoy ! ```bash -./bomberman +./build/bomberman ``` diff --git a/build_web.sh b/build_web.sh new file mode 100755 index 00000000..b5975bba --- /dev/null +++ b/build_web.sh @@ -0,0 +1,11 @@ +#! /bin/bash +EMSDK_PATH=./emsdk +if [[ ! -d ./emsdk ]]; then + git clone https://github.com/emscripten-core/emsdk.git +fi +./emsdk/emsdk install latest +./emsdk/emsdk activate latest +source ./emsdk/emsdk_env.sh +mkdir -p build_web +emcmake cmake -S . -B build_web -DPLATFORM=Web && +cmake --build build_web \ No newline at end of file diff --git a/lib/Ray/sources/Exceptions/RayError.hpp b/lib/Ray/sources/Exceptions/RayError.hpp index 52068b22..f700ac17 100644 --- a/lib/Ray/sources/Exceptions/RayError.hpp +++ b/lib/Ray/sources/Exceptions/RayError.hpp @@ -9,6 +9,7 @@ #define RAYERROR_HPP_ #include +#include namespace RAY::Exception { //! @brief base exception class for RAY lib diff --git a/lib/Ray/sources/Model/ModelAnimations.cpp b/lib/Ray/sources/Model/ModelAnimations.cpp index 0bd88c03..6d41832d 100644 --- a/lib/Ray/sources/Model/ModelAnimations.cpp +++ b/lib/Ray/sources/Model/ModelAnimations.cpp @@ -10,12 +10,13 @@ RAY::Cache<::ModelAnimation> RAY::ModelAnimations::_animationsCache(LoadModelAnimations, UnloadModelAnimations); RAY::ModelAnimations::ModelAnimations(const std::string &filePath): - _animationsPtr(_animationsCache.fetch(filePath, &this->_animationCount)) + _animationsPtr(_animationsCache.fetch(filePath, &this->_animationCount)), + _filePath(filePath) { ::ModelAnimation *ptr = this->_animationsPtr.get(); for (int i = 0; i < this->_animationCount; i++) - this->_animations.push_back(RAY::ModelAnimation(ptr[i])); + this->_animations.emplace_back(ptr[i]); } RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index) @@ -28,3 +29,22 @@ size_t RAY::ModelAnimations::getAnimationsCount() const return this->_animationCount; } +std::string RAY::ModelAnimations::getFilePath() const +{ + return this->_filePath; +} + +const RAY::ModelAnimation &RAY::ModelAnimations::at(int index) const +{ + return this->_animations.at(index); +} + +const RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index) const +{ + return this->_animations[index]; +} + +RAY::ModelAnimation &RAY::ModelAnimations::at(int index) +{ + return this->_animations.at(index); +} diff --git a/lib/Ray/sources/Model/ModelAnimations.hpp b/lib/Ray/sources/Model/ModelAnimations.hpp index ba46a0e4..2b99e1d7 100644 --- a/lib/Ray/sources/Model/ModelAnimations.hpp +++ b/lib/Ray/sources/Model/ModelAnimations.hpp @@ -21,21 +21,33 @@ namespace RAY { //! @param filePath Path to the file containing animations ModelAnimations(const std::string &filePath); - //! @brief Only single entity can hold these animations pointers - ModelAnimations(const ModelAnimations &) = delete; + //! @brief default copy ctor + ModelAnimations(const ModelAnimations &) = default; //! @brief Default constructor ~ModelAnimations() = default; - //! @brief Only single entity can hold these animations pointers - ModelAnimations &operator=(const ModelAnimations &) = delete; + //! @brief Default assignment operator + ModelAnimations &operator=(const ModelAnimations &) = default; //! @brief Castin Object to raw model animation pointer ModelAnimation &operator[](int index); + //! @brief std [] const + const ModelAnimation &operator[](int index) const; + + //! @brief std at const + const ModelAnimation &at(int index) const; + + //! @brief std at + ModelAnimation &at(int index); + //! @return the number of loaded animations size_t getAnimationsCount() const; + //! @brief Get the creation file + std::string getFilePath() const; + private: //! @brief Holds the pointer returned by the loading function std::shared_ptr<::ModelAnimation> _animationsPtr; @@ -46,6 +58,9 @@ namespace RAY { //! @brief the number of loaded animations int _animationCount; + //! @brief The file where the animations were loaded (used to create a copy of this class) + const std::string _filePath; + static Cache<::ModelAnimation> _animationsCache; }; } diff --git a/lib/Ray/sources/Utils/Cache.hpp b/lib/Ray/sources/Utils/Cache.hpp index f30fa3ff..7f041853 100644 --- a/lib/Ray/sources/Utils/Cache.hpp +++ b/lib/Ray/sources/Utils/Cache.hpp @@ -60,13 +60,17 @@ namespace RAY { {}; std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter) { - if (this->_cache.find(path) == this->_cache.end()) - this->_cache.emplace(path, std::shared_ptr<::ModelAnimation>( - this->_dataLoader(path.c_str(), counter), [this, counter](::ModelAnimation *p) { - this->_dataUnloader(p, *counter); - delete p; - })); - return _cache[path]; + if (this->_cache.find(path) != this->_cache.end()) + return this->_cache[path]; + + ::ModelAnimation *animations = this->_dataLoader(path.c_str(), counter); + unsigned int animCount = *counter; + + this->_cache.emplace(path, std::shared_ptr<::ModelAnimation>( + animations, [this, animCount](::ModelAnimation *p) { + this->_dataUnloader(p, animCount); + })); + return this->_cache[path]; }; private: //! @brief function to call to load data diff --git a/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index 11acdab7..2cef08f1 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -108,10 +108,14 @@ void RAY::Window::clear(RAY::Color color) ClearBackground(color); } -void RAY::Window::draw() +void RAY::Window::beginDrawing() +{ + BeginDrawing(); +} + +void RAY::Window::endDrawing() { EndDrawing(); - BeginDrawing(); } void RAY::Window::useCamera(RAY::Camera::Camera2D &camera) @@ -168,3 +172,8 @@ void RAY::Window::setIcon(RAY::Image &img) { SetWindowIcon(img); } + +bool RAY::Window::isReady() const +{ + return IsWindowReady(); +} \ No newline at end of file diff --git a/lib/Ray/sources/Window.hpp b/lib/Ray/sources/Window.hpp index 94c2ad80..2aa128ec 100644 --- a/lib/Ray/sources/Window.hpp +++ b/lib/Ray/sources/Window.hpp @@ -102,8 +102,10 @@ namespace RAY { NONE, }; - //! @brief Draw the content of the buffer on the screen. - void draw(); + //! @brief Setup canvas (framebuffer) to start drawing + void beginDrawing(); + //! @brief End canvas drawing and swap buffers (double buffering) + void endDrawing(); //! @brief Initialize 2D mode with custom camera (2D) void useCamera(Camera::Camera2D &camera); @@ -131,6 +133,9 @@ namespace RAY { //! @brief Draw a 3d mesh with material and transform void draw(const Mesh &mesh, const Material &material, const Matrix &transform); + //! @return true if the window's context has been correctly initialized + bool isReady() const; + private: //! @brief Creates window, and opens it if openNow is set to true diff --git a/lib/raylib/Findraylib.cmake b/lib/raylib/Findraylib.cmake index 4baf8bd8..d836183b 100644 --- a/lib/raylib/Findraylib.cmake +++ b/lib/raylib/Findraylib.cmake @@ -14,6 +14,9 @@ if (NOT raylib_FOUND) SET(FETCHCONTENT_QUIET NO) FetchContent_Populate(raylib) SET(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) + if (EMSCRIPTEN) + SET(PLATFORM Web) + endif() ADD_SUBDIRECTORY(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR}) SET(raylib_FOUND TRUE) endif() diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index e8cd8655..d216b9f3 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -17,6 +17,6 @@ add_library(wal sources/Component/Component.cpp sources/System/System.cpp sources/Models/Callback.hpp -) + sources/Models/TypeHolder.hpp) target_include_directories(wal PUBLIC sources) \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 8e157c6e..b64bfb82 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -10,6 +10,7 @@ #include #include "Component/Component.hpp" #include "Exception/WalError.hpp" +#include "Models/TypeHolder.hpp" namespace WAL { @@ -83,6 +84,18 @@ namespace WAL return *this; } + //! @brief Add a component to this entity. The component is constructed in place. + //! @throw DuplicateError is thrown if a component with the same type already exist. + //! @return This entity is returned + template + Entity &addComponent(Types &&...params) + { + if (this->hasComponent()) + throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists."); + this->_components.push_back(std::make_unique(*this, TypeHolder(), std::forward(params)...)); + return *this; + } + //! @brief Copy a component to this entity. //! @return This entity is returned. Entity &addComponent(const Component &component); diff --git a/lib/wal/sources/Models/TypeHolder.hpp b/lib/wal/sources/Models/TypeHolder.hpp new file mode 100644 index 00000000..45057ed1 --- /dev/null +++ b/lib/wal/sources/Models/TypeHolder.hpp @@ -0,0 +1,13 @@ +// +// Created by Zoe Roux on 2021-06-02. +// + + +#pragma once + +namespace WAL +{ + //! @brief A class only used to specify template arguments. + template + class TypeHolder {}; +} \ No newline at end of file diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index 875e3bd8..371c5774 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -41,8 +41,10 @@ namespace WAL { // TODO use an hashmap to cache results. const auto &dependency = system.getDependencies(); - return std::ranges::all_of(dependency.begin(), dependency.end(), [&entity](const auto &dependency) { - return entity.hasComponent(dependency); - }); + for (const auto &dependency : system.getDependencies()) { + if (!entity.hasComponent(dependency)) + return false; + } + return true; } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 50a87188..3785081a 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -15,6 +15,10 @@ #include "System/System.hpp" #include "Models/Callback.hpp" +#if defined(PLATFORM_WEB) +#include +#endif + namespace WAL { //! @brief The main WAL class, it is used to setup and run the ECS. @@ -109,7 +113,13 @@ namespace WAL void run(const std::function &callback, T state = T()) { Callback update(callback); + + #if defined(PLATFORM_WEB) + std::tuple iterationParams(this, &update, &state); + return emscripten_set_main_loop_arg((em_arg_callback_func)runIteration, (void *)&iterationParams, 0, 1); + #else return this->run(update, state); + #endif } //! @brief Start the game loop @@ -137,6 +147,30 @@ namespace WAL } } + #if defined(PLATFORM_WEB) + template + static void runIteration(void *param) + { + static auto iterationParams = reinterpret_cast *, T *> *>(param); + static const Callback callback = *((Callback *)std::get<1>(*iterationParams)); + static T *state = (T *)std::get<2>(*iterationParams); + static Wal *wal = (Wal *)std::get<0>(*iterationParams); + static auto lastTick = std::chrono::steady_clock::now(); + static std::chrono::nanoseconds fBehind(0); + + auto now = std::chrono::steady_clock::now(); + std::chrono::nanoseconds dtime = now - lastTick; + fBehind += dtime; + lastTick = now; + while (fBehind > Wal::timestep) { + fBehind -= Wal::timestep; + wal->_fixedUpdate(); + } + wal->_update(dtime); + callback(*wal, *state); + } + #endif + //! @brief A default constructor Wal() = default; //! @brief A WAL can't be copy constructed diff --git a/sources/Component/Animation/AnimationsComponent.cpp b/sources/Component/Animation/AnimationsComponent.cpp new file mode 100644 index 00000000..4461cc8e --- /dev/null +++ b/sources/Component/Animation/AnimationsComponent.cpp @@ -0,0 +1,71 @@ +// +// Created by cbihan on 01/06/2021. +// + +#include "AnimationsComponent.hpp" +#include "Entity/Entity.hpp" +#include "Model/ModelAnimations.hpp" + +namespace BBM +{ + AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex, bool play) + : WAL::Component(entity), + _modelAnimation(std::move(modelAnimation)), + _currentAnimIndex(animIndex), + _animDisabled(play) + { + this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(0); + } + + WAL::Component *AnimationsComponent::clone(WAL::Entity &entity) const + { + return new AnimationsComponent(entity, + RAY::ModelAnimations(this->_modelAnimation.getFilePath()), + this->_currentAnimIndex); + } + + size_t AnimationsComponent::getCurrentAnimFrameCounter() const + { + return this->_modelAnimation.at(this->_currentAnimIndex).getFrameCounter(); + } + + RAY::ModelAnimation AnimationsComponent::getCurrentModelAnim() + { + return this->_modelAnimation[this->_currentAnimIndex]; + } + + void AnimationsComponent::setCurrentAnimFrameCounter(size_t animFrameCounter) + { + this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(animFrameCounter); + } + + void AnimationsComponent::resetCurrentAnimFrameCounter() + { + this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(0); + } + + size_t AnimationsComponent::getCurrentAnimIndex() const + { + return this->_currentAnimIndex; + } + + void AnimationsComponent::setAnimIndex(int animIndex) + { + this->_currentAnimIndex = animIndex % static_cast(this->_modelAnimation.getAnimationsCount()); + } + + void AnimationsComponent::incCurrentAnimFrameCounter() + { + this->_modelAnimation[this->_currentAnimIndex].incrementFrameCounter(); + } + + void AnimationsComponent::setAnimDisabled(bool disable) + { + this->_animDisabled = disable; + } + + bool AnimationsComponent::isAnimDisabled() const + { + return this->_animDisabled; + } +} \ No newline at end of file diff --git a/sources/Component/Animation/AnimationsComponent.hpp b/sources/Component/Animation/AnimationsComponent.hpp new file mode 100644 index 00000000..463f241f --- /dev/null +++ b/sources/Component/Animation/AnimationsComponent.hpp @@ -0,0 +1,63 @@ +// +// Created by cbihan on 01/06/2021. +// + +#pragma once + +#include +#include +#include +#include + +namespace BBM +{ + class AnimationsComponent : public WAL::Component + { + private: + //! @brief To get the animation data + RAY::ModelAnimations _modelAnimation; + //! @brief The index of the + int _currentAnimIndex; + //! @brief Bool allowing to play pause an animation + bool _animDisabled; + public: + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief get the current animation index + size_t getCurrentAnimIndex() const; + + //! @brief Set the animation index to use + void setAnimIndex(int animIndex); + + //! @brief get animation frame counter + size_t getCurrentAnimFrameCounter() const; + + //! @brief get the current + RAY::ModelAnimation getCurrentModelAnim(); + + //! @brief set the anim frame counter + void setCurrentAnimFrameCounter(size_t animFrameCounter); + + //! @brief Set the internal anim counter to 0 + void resetCurrentAnimFrameCounter(); + + //! @brief Increment the internal anim counter + void incCurrentAnimFrameCounter(); + + //! @brief Allow to play pause animations + void setAnimDisabled(bool disable); + + //! @brief To know if the animation will be updated or not + bool isAnimDisabled() const; + + //! @brief ctor entity and the path of the animation file + explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex, bool play = true); + //! @brief copy ctor + AnimationsComponent(const AnimationsComponent &) = default; + //! @brief dtor + ~AnimationsComponent() override = default; + //! @brief assignment operator + AnimationsComponent &operator=(const AnimationsComponent &) = delete; + }; +} diff --git a/sources/Component/Renderer/Drawable2DComponent.hpp b/sources/Component/Renderer/Drawable2DComponent.hpp index 63aaaf2b..227eacd6 100644 --- a/sources/Component/Renderer/Drawable2DComponent.hpp +++ b/sources/Component/Renderer/Drawable2DComponent.hpp @@ -4,36 +4,37 @@ #pragma once +#include #include "Component/Component.hpp" #include "Drawables/ADrawable2D.hpp" +#include "Model/Model.hpp" namespace BBM { - template class Drawable2DComponent : public WAL::Component { public: //! @brief The type of the component - T member; + std::shared_ptr drawable; - //! ctor - Drawable2DComponent(WAL::Entity &entity, T member) + //! @brief ctor + Drawable2DComponent(WAL::Entity &entity, std::shared_ptr drawable) : WAL::Component(entity), - member(std::move(member)) + drawable(std::move(drawable)) {} //! ctor - template - explicit Drawable2DComponent(WAL::Entity &entity, Params &&...params) + template + explicit Drawable2DComponent(WAL::Entity &entity, WAL::TypeHolder, Params &&...params) : WAL::Component(entity), - member(std::forward(params)...) + drawable(new T(std::forward(params)...)) {} //! @brief Clone a component for another or the same entity. //! @param entity The entity that owns the ne component. WAL::Component *clone(WAL::Entity &entity) const override { - return new Drawable2DComponent(entity, this->member); + return new Drawable2DComponent(entity, this->drawable); } //! @brief Default copy ctor @@ -42,7 +43,5 @@ namespace BBM ~Drawable2DComponent() override = default; //! @brief Default assignment operator Drawable2DComponent &operator=(const Drawable2DComponent &) = delete; - - }; } \ No newline at end of file diff --git a/sources/Component/Renderer/Drawable3DComponent.hpp b/sources/Component/Renderer/Drawable3DComponent.hpp index e4f85321..5a04da07 100644 --- a/sources/Component/Renderer/Drawable3DComponent.hpp +++ b/sources/Component/Renderer/Drawable3DComponent.hpp @@ -4,37 +4,37 @@ #pragma once +#include #include "Component/Component.hpp" #include "Drawables/ADrawable3D.hpp" #include "Model/Model.hpp" namespace BBM { - template class Drawable3DComponent : public WAL::Component { public: //! @brief The type of the component - T member; + std::shared_ptr drawable; //! @brief ctor - Drawable3DComponent(WAL::Entity &entity, T member) + Drawable3DComponent(WAL::Entity &entity, std::shared_ptr drawable) : WAL::Component(entity), - member(std::move(member)) + drawable(std::move(drawable)) {} //! ctor - template - explicit Drawable3DComponent(WAL::Entity &entity, Params &&...params) + template + explicit Drawable3DComponent(WAL::Entity &entity, WAL::TypeHolder, Params &&...params) : WAL::Component(entity), - member(std::forward(params)...) + drawable(new T(std::forward(params)...)) {} //! @brief Clone a component for another or the same entity. //! @param entity The entity that owns the ne component. WAL::Component *clone(WAL::Entity &entity) const override { - return new Drawable3DComponent(entity, this->member); + return new Drawable3DComponent(entity, this->drawable); } //! @brief Default copy ctor diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 989e515f..af3a7f48 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -151,7 +151,7 @@ namespace BBM scene->addEntity("Hole Block") .addComponent(Vector3f(coords.x, coords.y - 1, coords.z)) .addComponent(holeObj, std::make_pair(MAP_DIFFUSE, holePng)); - /* .addComponent([](WAL::Entity &other, const WAL::Entity &entity) { + /*.addComponent([](WAL::Entity &other, const WAL::Entity &entity) { if (other.hasComponent()) { auto &health = other.getComponent(); health.takeDmg(health.getHealthPoint()); diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 46f9fc06..f08f25bd 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -14,7 +14,7 @@ #include #include #include "Component/Renderer/Drawable3DComponent.hpp" -#include "System/Renderer/Renderer3DSystem.hpp" +#include "System/Renderer/RenderSystem.hpp" #include "Scene/Scene.hpp" #include "Model/Model.hpp" #include "Component/Component.hpp" diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index db3896c9..cd5b30e1 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -4,15 +4,13 @@ #include #include -#include -#include -#include -#include +#include "System/Movable/MovableSystem.hpp" +#include "System/Renderer/RenderSystem.hpp" #include +#include #include #include #include -#include #include #include #include @@ -23,8 +21,13 @@ #include #include "Models/Vector2.hpp" #include "Component/Renderer/CameraComponent.hpp" +#include "Component/Renderer/Drawable2DComponent.hpp" +#include "Component/Renderer/Drawable3DComponent.hpp" #include "Runner.hpp" #include "Models/GameState.hpp" +#include +#include "Component/Animation/AnimationsComponent.hpp" +#include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" namespace RAY2D = RAY::Drawables::Drawables2D; @@ -55,13 +58,7 @@ namespace BBM { RAY::TraceLog::setLevel(LOG_WARNING); RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); - - wal.addSystem>(); - wal.addSystem>(); - - wal.addSystem(window) - .addSystem>(); - wal.addSystem(window); + wal.addSystem(wal, window); } std::shared_ptr loadGameScene() @@ -69,14 +66,15 @@ namespace BBM auto scene = std::make_shared(); scene->addEntity("player") .addComponent() - .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) + .addComponent("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() .addComponent() + .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 1) .addComponent(2) .addComponent(); scene->addEntity("cube") .addComponent(-5, 0, -5) - .addComponent>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) + .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) .addComponent() .addComponent() .addComponent([](WAL::Entity &, const WAL::Entity &){}, @@ -86,7 +84,7 @@ namespace BBM mov.resetVelocity(); } catch (std::exception &e) { }; }, 3); - + scene->addEntity("camera") .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp new file mode 100644 index 00000000..cce35719 --- /dev/null +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -0,0 +1,35 @@ +// +// Created by cbihan on 01/06/2021. +// + +#include +#include "AnimationsSystem.hpp" +#include "Component/Animation/AnimationsComponent.hpp" +#include "Model/Model.hpp" +#include "Component/Renderer/Drawable3DComponent.hpp" + +namespace BBM +{ + + AnimationsSystem::AnimationsSystem() + : WAL::System({ + typeid(Drawable3DComponent), + typeid(AnimationsComponent) + }) + { + } + + void AnimationsSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) + { + auto &model = entity.getComponent(); + auto &anim = entity.getComponent(); + + if (anim.isDisabled()) + return; + auto modelPtr = std::dynamic_pointer_cast(model.drawable); + if (modelPtr) { + modelPtr->setAnimation(anim.getCurrentModelAnim()); + anim.incCurrentAnimFrameCounter(); + } + } +} \ No newline at end of file diff --git a/sources/System/Animation/AnimationsSystem.hpp b/sources/System/Animation/AnimationsSystem.hpp new file mode 100644 index 00000000..fb08b2ef --- /dev/null +++ b/sources/System/Animation/AnimationsSystem.hpp @@ -0,0 +1,26 @@ +// +// Created by cbihan on 01/06/2021. +// + +#pragma once + +#include + +namespace BBM +{ + class AnimationsSystem : public WAL::System + { + public: + //! @inherit + void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override; + + //! @brief A default constructor + AnimationsSystem(); + //! @brief A Controllable system is copy constructable + AnimationsSystem(const AnimationsSystem &) = default; + //! @brief A default destructor + ~AnimationsSystem() override = default; + //! @brief A Controllable system is assignable. + AnimationsSystem &operator=(const AnimationsSystem &) = default; + }; +} \ No newline at end of file diff --git a/sources/System/Collision/CollisionSystem.hpp b/sources/System/Collision/CollisionSystem.hpp index c4c2de25..b3d78b55 100644 --- a/sources/System/Collision/CollisionSystem.hpp +++ b/sources/System/Collision/CollisionSystem.hpp @@ -8,6 +8,7 @@ #include #include "Wal.hpp" #include "System/System.hpp" +#include "Models/Vector3.hpp" namespace BBM { diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index 0a9c789c..41c54895 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -10,8 +10,6 @@ namespace BBM { - float ControllableSystem::speed = .25f; - ControllableSystem::ControllableSystem() : WAL::System({ typeid(ControllableComponent), diff --git a/sources/System/Controllable/ControllableSystem.hpp b/sources/System/Controllable/ControllableSystem.hpp index fac4db08..ec6eef9d 100644 --- a/sources/System/Controllable/ControllableSystem.hpp +++ b/sources/System/Controllable/ControllableSystem.hpp @@ -14,7 +14,7 @@ namespace BBM { public: //! @brief The speed applied to every controllable entities. - static float speed; + static constexpr const float speed = .25f; //! @inherit void onFixedUpdate(WAL::Entity &entity) override; diff --git a/sources/System/Renderer/Render2DScreenSystem.cpp b/sources/System/Renderer/Render2DScreenSystem.cpp deleted file mode 100644 index 938d107f..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#include "Render2DScreenSystem.hpp" - -namespace BBM -{ - Render2DScreenSystem::Render2DScreenSystem(RAY::Window &window) - : WAL::System({}), - _window(window), - _camera(RAY::Vector2(10, 10), RAY::Vector2(), 0) - {} - - void Render2DScreenSystem::onSelfUpdate() - { - this->_window.useCamera(this->_camera); - } -} \ No newline at end of file diff --git a/sources/System/Renderer/Render2DScreenSystem.hpp b/sources/System/Renderer/Render2DScreenSystem.hpp deleted file mode 100644 index 12adaf34..00000000 --- a/sources/System/Renderer/Render2DScreenSystem.hpp +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#pragma once - -#include -#include - -namespace BBM -{ - class Render2DScreenSystem : public WAL::System - { - //! @brief The window to render on - RAY::Window &_window; - - //! @brief The camera used to render. - RAY::Camera::Camera2D _camera; - public: - //! @brief A method called after all entities that this system manage has been updated. - //! @note render on screen here - void onSelfUpdate() override; - - //! @brief ctor - explicit Render2DScreenSystem(RAY::Window &window); - //! @brief Default copy ctor - Render2DScreenSystem(const Render2DScreenSystem &) = default; - //! @brief Default dtor - ~Render2DScreenSystem() override = default; - //! @brief A render screen system can't be assigned. - Render2DScreenSystem &operator=(const Render2DScreenSystem &) = delete; - }; -} \ No newline at end of file diff --git a/sources/System/Renderer/RenderScreenSystem.cpp b/sources/System/Renderer/RenderScreenSystem.cpp deleted file mode 100644 index c85e6971..00000000 --- a/sources/System/Renderer/RenderScreenSystem.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// -// Created by Zoe Roux on 5/27/21. -// - -#include "RenderScreenSystem.hpp" -#include "Component/Renderer/CameraComponent.hpp" -#include "Component/Position/PositionComponent.hpp" - -namespace BBM -{ - RenderScreenSystem::RenderScreenSystem(RAY::Window &window) - : WAL::System({ - typeid(CameraComponent), - typeid(PositionComponent) - }), - _window(window), - _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE) - { - this->_window.setFPS(60); - } - - void RenderScreenSystem::onSelfUpdate() - { - this->_window.draw(); - this->_window.clear(); - this->_window.useCamera(this->_camera); - } - - void RenderScreenSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) - { - const auto &pos = entity.getComponent(); - const auto &cam = entity.getComponent(); - _camera.setPosition(pos.position); - _camera.setTarget(cam.target); - } -} \ No newline at end of file diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp new file mode 100644 index 00000000..03330498 --- /dev/null +++ b/sources/System/Renderer/RenderSystem.cpp @@ -0,0 +1,66 @@ +// +// Created by Zoe Roux on 5/27/21. +// + +#include +#include "Models/Vector2.hpp" +#include "RenderSystem.hpp" +#include "Component/Renderer/CameraComponent.hpp" +#include "Component/Position/PositionComponent.hpp" +#include "Component/Renderer/Drawable2DComponent.hpp" +#include "Drawables/ADrawable2D.hpp" +#include "Drawables/ADrawable3D.hpp" + +namespace BBM +{ + RenderSystem::RenderSystem(WAL::Wal &wal, RAY::Window &window) + : WAL::System({ + typeid(CameraComponent), + typeid(PositionComponent) + }), + _wal(wal), + _window(window), + _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE) + {} + + void RenderSystem::onSelfUpdate() + { + this->_camera.update(); + this->_window.beginDrawing(); + this->_window.clear(); + + this->_window.useCamera(this->_camera); + for (auto &entity : this->_wal.scene->getEntities()) { + if (!entity.hasComponent() + || !entity.hasComponent()) + continue; + auto &drawable = entity.getComponent(); + auto &pos = entity.getComponent(); + + drawable.drawable->setPosition(pos.position); + drawable.drawable->drawOn(this->_window); + } + this->_window.unuseCamera(); + + // TODO sort entities based on the Z axis + for (auto &entity : this->_wal.scene->getEntities()) { + if (!entity.hasComponent() + || !entity.hasComponent()) + continue; + auto &drawable = entity.getComponent(); + auto &pos = entity.getComponent(); + + drawable.drawable->setPosition(Vector2f(pos.position.x, pos.position.y)); + drawable.drawable->drawOn(this->_window); + } + this->_window.endDrawing(); + } + + void RenderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) + { + const auto &pos = entity.getComponent(); + const auto &cam = entity.getComponent(); + _camera.setPosition(pos.position); + _camera.setTarget(cam.target); + } +} \ No newline at end of file diff --git a/sources/System/Renderer/RenderScreenSystem.hpp b/sources/System/Renderer/RenderSystem.hpp similarity index 69% rename from sources/System/Renderer/RenderScreenSystem.hpp rename to sources/System/Renderer/RenderSystem.hpp index e9b2ea85..5524f614 100644 --- a/sources/System/Renderer/RenderScreenSystem.hpp +++ b/sources/System/Renderer/RenderSystem.hpp @@ -7,11 +7,15 @@ #include "System/System.hpp" #include "Camera/Camera2D.hpp" #include "Window.hpp" +#include "Wal.hpp" namespace BBM { - class RenderScreenSystem : public WAL::System + class RenderSystem : public WAL::System { + //! @brief The ECS to update. + WAL::Wal &_wal; + //! @brief The window to render on RAY::Window &_window; @@ -26,12 +30,12 @@ namespace BBM void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override; //! @brief ctor - explicit RenderScreenSystem(RAY::Window &window); + RenderSystem(WAL::Wal &wal, RAY::Window &window); //! @brief Default copy ctor - RenderScreenSystem(const RenderScreenSystem &) = default; + RenderSystem(const RenderSystem &) = default; //! @brief Default dtor - ~RenderScreenSystem() override = default; + ~RenderSystem() override = default; //! @brief A render screen system can't be assigned. - RenderScreenSystem &operator=(const RenderScreenSystem &) = delete; + RenderSystem &operator=(const RenderSystem &) = delete; }; } diff --git a/sources/System/Renderer/Renderer2DSystem.hpp b/sources/System/Renderer/Renderer2DSystem.hpp deleted file mode 100644 index a1622c0d..00000000 --- a/sources/System/Renderer/Renderer2DSystem.hpp +++ /dev/null @@ -1,48 +0,0 @@ -// -// Created by cbihan on 24/05/2021. -// - -#pragma once - -#include -#include "System/System.hpp" -#include "Entity/Entity.hpp" -#include "Component/Position/PositionComponent.hpp" -#include "Component/Renderer/Drawable2DComponent.hpp" -#include "Window.hpp" - -namespace BBM -{ - template - class Renderer2DSystem : public WAL::System - { - private: - //! @brief The class to render - RAY::Window &_window; - public: - explicit Renderer2DSystem() - : WAL::System({typeid(PositionComponent), typeid(Drawable2DComponent)}), - _window(RAY::Window::getInstance()) - { - } - - //! @brief Update the corresponding component of the given entity - //! @param entity The entity to update. - //! @param dtime The delta time. - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override - { - auto &comp = entity.getComponent>(); - auto &pos = entity.getComponent(); - - comp.member.setPosition({pos.getX(), pos.getY()}); - comp.member.drawOn(this->_window); - } - - //! @brief default copy ctor - Renderer2DSystem(const Renderer2DSystem &) = default; - //! @brief default dtor - ~Renderer2DSystem() override = default; - //! @brief Default assignment operator - Renderer2DSystem &operator=(const Renderer2DSystem &) = delete; - }; -} \ No newline at end of file diff --git a/sources/System/Renderer/Renderer3DSystem.hpp b/sources/System/Renderer/Renderer3DSystem.hpp deleted file mode 100644 index 8dba98f6..00000000 --- a/sources/System/Renderer/Renderer3DSystem.hpp +++ /dev/null @@ -1,48 +0,0 @@ -// -// Created by cbihan on 24/05/2021. -// - -#pragma once - -#include -#include "System/System.hpp" -#include "Entity/Entity.hpp" -#include "Component/Position/PositionComponent.hpp" -#include "Component/Renderer/Drawable3DComponent.hpp" -#include "Window.hpp" - -namespace BBM -{ - template - class Renderer3DSystem : public WAL::System - { - private: - //! @brief The class to render - RAY::Window &_window; - public: - //! @brief ctor - explicit Renderer3DSystem() - : WAL::System({typeid(PositionComponent), typeid(Drawable3DComponent)}), - _window(RAY::Window::getInstance()) - {} - - //! @brief Update the corresponding component of the given entity - //! @param entity The entity to update. - //! @param dtime The delta time. - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override - { - auto &comp = entity.getComponent>(); - auto &pos = entity.getComponent(); - - comp.member.setPosition(static_cast(pos.position)); - comp.member.drawOn(this->_window); - } - - //! @brief Default copy ctor - Renderer3DSystem(const Renderer3DSystem &) = default; - //! @brief Default dtor - ~Renderer3DSystem() override = default; - //! @brief Default assignment operator - Renderer3DSystem &operator=(const Renderer3DSystem &) = delete; - }; -} \ No newline at end of file diff --git a/sources/main.cpp b/sources/main.cpp index cb4a0269..bd13441a 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -9,50 +9,6 @@ #include #include "Runner/Runner.hpp" -// Dependencies of the demo -#include "Camera/Camera3D.hpp" -#include "Controllers/Keyboard.hpp" -#include "Drawables/2D/Text.hpp" -#include "Drawables/Image.hpp" -#include "Drawables/3D/Grid.hpp" -#include "Drawables/Texture.hpp" -#include "Drawables/3D/Circle.hpp" -#include "Drawables/2D/Circle.hpp" -#include "Drawables/3D/Cube.hpp" -#include "Drawables/3D/Sphere.hpp" -#include "Model/Model.hpp" -#include "Model/ModelAnimations.hpp" -#include -#include -#include -#include -#include "Component/Renderer/Drawable3DComponent.hpp" -#include "Component/Renderer/Drawable2DComponent.hpp" -#include "System/Renderer/RenderScreenSystem.hpp" -#include "Vector/Vector3.hpp" -#include "Window.hpp" -#include "TraceLog.hpp" -#include "Wal.hpp" - -const std::vectortextures = { - "blue", "cyan", "green", "purple", "red", "yellow" -}; - -std::string get_full_path(const std::string &color) -{ - std::string path = "assets/player/"; - - path += color; - path += ".png"; - return path; -} - -int demo(void) -{ - - return (0); -} - void usage(const std::string &bin) { std::cout << "Bomberman." << std::endl @@ -67,6 +23,5 @@ int main(int argc, char **argv) usage(argv[0]); return 1; } - //return demo(); return BBM::run(); } diff --git a/sources/wasm/frontend.html b/sources/wasm/frontend.html new file mode 100644 index 00000000..2816e377 --- /dev/null +++ b/sources/wasm/frontend.html @@ -0,0 +1,147 @@ + + + + + + Emscripten-Generated Code + + + +
+
emscripten
+
Downloading...
+
+ +
+
+ +
+
+
+ Resize canvas + Lock/hide mouse pointer +     + +
+ +
+ + {{{ SCRIPT }}} + + \ No newline at end of file