diff --git a/CMakeLists.txt b/CMakeLists.txt index 352a531b..588f5096 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,10 @@ set(SOURCES sources/Component/Collision/CollisionComponent.hpp sources/System/Collision/CollisionSystem.hpp sources/System/Collision/CollisionSystem.cpp + sources/Component/Animator/AnimatorComponent.cpp + sources/Component/Animator/AnimatorComponent.hpp + sources/System/Animator/AnimatorSystem.cpp + sources/System/Animator/AnimatorSystem.hpp ) add_executable(bomberman sources/main.cpp diff --git a/assets/map/upper_floor_hole.mtl b/assets/map/upper_floor_hole.mtl new file mode 100644 index 00000000..829daba3 --- /dev/null +++ b/assets/map/upper_floor_hole.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl floor +Ns 225.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 +map_Kd upper_floor_hole.png diff --git a/assets/map/upper_floor_hole.obj b/assets/map/upper_floor_hole.obj new file mode 100644 index 00000000..2042783c --- /dev/null +++ b/assets/map/upper_floor_hole.obj @@ -0,0 +1,54 @@ +# Blender v2.92.0 OBJ File: '' +# www.blender.org +mtllib upper_floor_hole.mtl +o floor +v -0.500000 -0.499144 0.500854 +v -0.500000 -0.500854 -0.499144 +v 0.500000 -0.499144 0.500855 +v 0.500000 -0.500855 -0.499144 +v 0.500000 0.500854 0.499144 +v 0.500000 0.499144 -0.500854 +v -0.500000 0.500855 0.499144 +v -0.500000 0.499144 -0.500855 +vt 0.000977 0.667790 +vt 0.000977 0.333151 +vt 0.333659 0.667790 +vt 0.333659 0.333151 +vt 0.666341 0.667790 +vt 0.666341 0.333151 +vt 0.333659 0.667790 +vt 0.333659 1.002429 +vt 0.000977 0.667790 +vt 0.000977 1.002429 +vt 0.666341 0.667790 +vt 0.666341 1.002429 +vt 0.333659 0.667790 +vt 0.333659 1.002429 +vt 0.666341 0.333151 +vt 0.999024 0.333151 +vt 0.666341 0.667790 +vt 0.999024 0.667790 +vt 0.999024 1.002429 +vt 0.666341 1.002429 +vt 0.999024 0.667790 +vt 0.666341 0.667790 +vn -0.0000 -1.0000 0.0017 +vn 1.0000 -0.0000 0.0000 +vn 0.0000 1.0000 -0.0017 +vn -1.0000 0.0000 -0.0000 +vn 0.0000 -0.0017 -1.0000 +vn -0.0000 0.0017 1.0000 +usemtl floor +s 1 +f 1/1/1 2/2/1 3/3/1 +f 3/3/1 2/2/1 4/4/1 +f 3/3/2 4/4/2 5/5/2 +f 5/5/2 4/4/2 6/6/2 +f 5/7/3 6/8/3 7/9/3 +f 7/9/3 6/8/3 8/10/3 +f 7/11/4 8/12/4 1/13/4 +f 1/13/4 8/12/4 2/14/4 +f 2/15/5 8/16/5 4/17/5 +f 4/17/5 8/16/5 6/18/5 +f 7/19/6 1/20/6 5/21/6 +f 5/21/6 1/20/6 3/22/6 diff --git a/assets/map/upper_floor_hole.png b/assets/map/upper_floor_hole.png new file mode 100644 index 00000000..43da7cd6 Binary files /dev/null and b/assets/map/upper_floor_hole.png differ diff --git a/build_web.sh b/build_web.sh index b5975bba..0fde576d 100755 --- a/build_web.sh +++ b/build_web.sh @@ -8,4 +8,4 @@ fi 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 +cd build_web && make -j $2 \ No newline at end of file diff --git a/lib/Ray/sources/Audio/Music.cpp b/lib/Ray/sources/Audio/Music.cpp index b397c04e..db3dae78 100644 --- a/lib/Ray/sources/Audio/Music.cpp +++ b/lib/Ray/sources/Audio/Music.cpp @@ -8,57 +8,50 @@ #include "Audio/Music.hpp" #include +RAY::Cache<::Music> RAY::Audio::Music::_musicsCache(LoadMusicStream, UnloadMusicStream); + RAY::Audio::Music::Music(const std::string &path): - _music(LoadMusicStream(path.c_str())) + _music(this->_musicsCache.fetch(path.c_str())) { } -RAY::Audio::Music::Music() -{ -} - -RAY::Audio::Music::~Music() -{ - UnloadMusicStream(_music); -} - bool RAY::Audio::Music::isPlaying(void) { - return IsMusicStreamPlaying(_music); + return IsMusicStreamPlaying(*_music); } RAY::Audio::Music &RAY::Audio::Music::play(void) { - PlayMusicStream(_music); + PlayMusicStream(*_music); return *this; } RAY::Audio::Music &RAY::Audio::Music::stop(void) { - StopMusicStream(_music); + StopMusicStream(*_music); return *this; } RAY::Audio::Music &RAY::Audio::Music::pause(void) { - PauseMusicStream(_music); + PauseMusicStream(*_music); return *this; } RAY::Audio::Music &RAY::Audio::Music::resume(void) { - ResumeMusicStream(_music); + ResumeMusicStream(*_music); return *this; } RAY::Audio::Music &RAY::Audio::Music::setVolume(float volume) { - SetMusicVolume(_music, volume); + SetMusicVolume(*_music, volume); return *this; } RAY::Audio::Music &RAY::Audio::Music::setPitch(float pitch) { - SetMusicPitch(_music, pitch); + SetMusicPitch(*_music, pitch); return *this; } diff --git a/lib/Ray/sources/Audio/Music.hpp b/lib/Ray/sources/Audio/Music.hpp index 85f31c52..c28e8089 100644 --- a/lib/Ray/sources/Audio/Music.hpp +++ b/lib/Ray/sources/Audio/Music.hpp @@ -9,9 +9,8 @@ #define MUSIC_HPP_ #include "Audio/IAudio.hpp" - #include - +#include "Utils/Cache.hpp" namespace RAY::Audio { @@ -22,11 +21,8 @@ namespace RAY::Audio //! @brief Load Music stream from file Music(const std::string &path); - //! @brief Default constructor - Music(); - //! @brief Default destructor - ~Music(); + ~Music() = default; //! @brief A copy constructor constructor Music(const Music &Music) = default; @@ -56,7 +52,9 @@ namespace RAY::Audio Music &setPitch(float pitch) override; private: - ::Music _music; + std::shared_ptr<::Music> _music; + + static RAY::Cache<::Music> _musicsCache; }; } diff --git a/lib/Ray/sources/Audio/Sound.cpp b/lib/Ray/sources/Audio/Sound.cpp index 4bfcb95a..da8df7ba 100644 --- a/lib/Ray/sources/Audio/Sound.cpp +++ b/lib/Ray/sources/Audio/Sound.cpp @@ -7,58 +7,51 @@ #include "Audio/Sound.hpp" +RAY::Cache<::Sound> RAY::Audio::Sound::_soundsCache(LoadSound, UnloadSound); + RAY::Audio::Sound::Sound(const std::string &path): - _sound(LoadSound(path.c_str())) + _sound(_soundsCache.fetch(path.c_str())) { } -RAY::Audio::Sound::Sound() -{ -} - -RAY::Audio::Sound::~Sound() -{ - UnloadSound(_sound); -} - bool RAY::Audio::Sound::isPlaying(void) { - return IsSoundPlaying(_sound); + return IsSoundPlaying(*_sound); } RAY::Audio::Sound &RAY::Audio::Sound::play(void) { - PlaySound(_sound); + PlaySound(*_sound); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::stop(void) { - StopSound(_sound); + StopSound(*_sound); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::pause(void) { - PauseSound(_sound); + PauseSound(*_sound); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::resume(void) { - ResumeSound(_sound); + ResumeSound(*_sound); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::setVolume(float volume) { - SetSoundVolume(_sound, volume); + SetSoundVolume(*_sound, volume); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::setPitch(float pitch) { - SetSoundPitch(_sound, pitch); + SetSoundPitch(*_sound, pitch); return *this; } diff --git a/lib/Ray/sources/Audio/Sound.hpp b/lib/Ray/sources/Audio/Sound.hpp index 32dd8cb0..00ba3ddc 100644 --- a/lib/Ray/sources/Audio/Sound.hpp +++ b/lib/Ray/sources/Audio/Sound.hpp @@ -9,7 +9,7 @@ #define SOUND_HPP_ #include "Audio/IAudio.hpp" - +#include "Utils/Cache.hpp" #include @@ -22,11 +22,8 @@ namespace RAY::Audio //! @brief Load Sound stream from file Sound(const std::string &path); - //! @brief Default constructor - Sound(); - //! @brief Default destructor - ~Sound(); + ~Sound() = default; //! @brief A copy constructor constructor Sound(const Sound &sound) = default; @@ -56,7 +53,9 @@ namespace RAY::Audio Sound &setPitch(float pitch) override; private: - ::Sound _sound; + std::shared_ptr<::Sound> _sound; + + static RAY::Cache<::Sound> _soundsCache; }; } diff --git a/lib/Ray/sources/Utils/Cache.hpp b/lib/Ray/sources/Utils/Cache.hpp index 7f041853..653933f5 100644 --- a/lib/Ray/sources/Utils/Cache.hpp +++ b/lib/Ray/sources/Utils/Cache.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace RAY { //! @brief A templated class used to cache ressources, indexed with a string diff --git a/lib/wal/sources/Models/Callback.hpp b/lib/wal/sources/Models/Callback.hpp index 2c988db4..dc12a18b 100644 --- a/lib/wal/sources/Models/Callback.hpp +++ b/lib/wal/sources/Models/Callback.hpp @@ -64,7 +64,4 @@ namespace WAL this->addCallback(callback); } }; - - template - static constexpr Callback EmptyCallback; } // namespace WAL \ No newline at end of file diff --git a/sources/Component/Animator/AnimatorComponent.cpp b/sources/Component/Animator/AnimatorComponent.cpp new file mode 100644 index 00000000..fa5d8cc2 --- /dev/null +++ b/sources/Component/Animator/AnimatorComponent.cpp @@ -0,0 +1,16 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#include "AnimatorComponent.hpp" + +namespace BBM { + AnimatorComponent::AnimatorComponent(WAL::Entity &entity) + : WAL::Component(entity) + {} + + WAL::Component *AnimatorComponent::clone(WAL::Entity &entity) const + { + return new AnimatorComponent(entity); + } +} \ No newline at end of file diff --git a/sources/Component/Animator/AnimatorComponent.hpp b/sources/Component/Animator/AnimatorComponent.hpp new file mode 100644 index 00000000..0c31b576 --- /dev/null +++ b/sources/Component/Animator/AnimatorComponent.hpp @@ -0,0 +1,28 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#pragma once + +#include + +namespace BBM { + class AnimatorComponent : public WAL::Component + { + public: + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief An Animator component can't be instantiated, it should be derived. + explicit AnimatorComponent(WAL::Entity &entity); + + //! @brief An Animator component can't be instantiated, it should be derived. + AnimatorComponent(const AnimatorComponent &) = default; + + //! @brief default destructor + ~AnimatorComponent() override = default; + + //! @brief An Animator component can't be assigned + AnimatorComponent &operator=(const AnimatorComponent &) = delete; + }; +} \ No newline at end of file diff --git a/sources/Component/Health/HealthComponent.cpp b/sources/Component/Health/HealthComponent.cpp index 80c9ef7e..8901ab31 100644 --- a/sources/Component/Health/HealthComponent.cpp +++ b/sources/Component/Health/HealthComponent.cpp @@ -6,14 +6,11 @@ #include "HealthComponent.hpp" +#include + namespace BBM { - HealthComponent::HealthComponent(WAL::Entity &entity) - : WAL::Component(entity), - _healthPoint() - {} - - HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint, WAL::Callback onDeath) + HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint, const WAL::Callback &onDeath) : WAL::Component(entity), _healthPoint(healthPoint), onDeath(onDeath) @@ -21,7 +18,7 @@ namespace BBM WAL::Component *HealthComponent::clone(WAL::Entity &entity) const { - return new HealthComponent(entity); + return new HealthComponent(entity, this->_healthPoint, this->onDeath); } void HealthComponent::addHealthPoint(unsigned int healthPoint) diff --git a/sources/Component/Health/HealthComponent.hpp b/sources/Component/Health/HealthComponent.hpp index c613c6b3..29c202d4 100644 --- a/sources/Component/Health/HealthComponent.hpp +++ b/sources/Component/Health/HealthComponent.hpp @@ -34,12 +34,9 @@ namespace BBM //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; - - //! @brief A Health component can't be instantiated, it should be derived. - explicit HealthComponent(WAL::Entity &entity); //! @brief Constructor - HealthComponent(WAL::Entity &entity, unsigned int healthPoint, WAL::Callback onDeath = WAL::Callback()); + HealthComponent(WAL::Entity &entity, unsigned int healthPoint, const WAL::Callback &onDeath = WAL::Callback()); //! @brief A Health component can't be instantiated, it should be derived. HealthComponent(const HealthComponent &) = default; diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index ed26beb0..754e080f 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -46,6 +46,7 @@ namespace BBM const std::string MapGenerator::stairsPath = MapGenerator::wallAssetsPath + "stairs"; const std::string MapGenerator::bumperPath = MapGenerator::wallAssetsPath + "bumper"; const std::string MapGenerator::holePath = MapGenerator::wallAssetsPath + "hole"; + const std::string MapGenerator::secondFloorHolePath = MapGenerator::secondFloorPath + "_hole"; void MapGenerator::generateUnbreakableBlock(int width, int height, std::shared_ptr scene) { @@ -181,10 +182,17 @@ namespace BBM { static const std::string holeObj = holePath + objExtension; static const std::string holePng = holePath + imageExtension; + static const std::string secondFloorObj = secondFloorHolePath + objExtension; + static const std::string secondFloorPng = secondFloorHolePath + imageExtension; - scene->addEntity("Hole Block") - .addComponent(Vector3f(coords.x, coords.y - 1, coords.z)) - .addComponent(holeObj, std::make_pair(MAP_DIFFUSE, holePng)); + WAL::Entity &holeEntity = scene->addEntity("Hole Block"); + + holeEntity.addComponent(Vector3f(coords.x, coords.y - 1, coords.z)); + + if (coords.y == 0) + holeEntity.addComponent(holeObj, std::make_pair(MAP_DIFFUSE, holePng)); + else + holeEntity.addComponent(secondFloorObj, std::make_pair(MAP_DIFFUSE, secondFloorPng)); /*.addComponent([](WAL::Entity &other, const WAL::Entity &entity) { if (other.hasComponent()) { auto &health = other.getComponent(); diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 49e82e5b..7085d662 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -153,6 +153,8 @@ namespace BBM static const std::string holePath; + static const std::string secondFloorHolePath; + public: static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall); static void wallDestroyed(WAL::Entity &entity); diff --git a/sources/Models/Vector2.hpp b/sources/Models/Vector2.hpp index 629a11b8..b3dacf2b 100644 --- a/sources/Models/Vector2.hpp +++ b/sources/Models/Vector2.hpp @@ -9,6 +9,8 @@ #include #include "Vector/Vector2.hpp" +#define PI_NUMBER 3.14159265359 + namespace BBM { //! @brief A Vector2 data type. (templated to allow any kind of vector2) @@ -120,6 +122,13 @@ namespace BBM return std::sqrt(std::pow(this->x - o.x, 2) + std::pow(this->y - o.y, 2)); } + double angle(const Vector2 &o) const + { + float dot = this->x * o.x + this->y * o.y; + float det = this->x * o.y - this->y * o.x; + return (std::atan2(det, dot) * (180.0f / PI_NUMBER)); + } + double magnitude() const { return std::sqrt(std::pow(this->x, 2) + std::pow(this->y, 2)); diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 13009d4e..57494e5e 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -9,12 +9,6 @@ #include #include #include -#include -#include "System/Event/EventSystem.hpp" -#include "System/Health/HealthSystem.hpp" -#include "System/Timer/TimerSystem.hpp" -#include "Component/BombHolder/BombHolderComponent.hpp" -#include "System/BombHolder/BombHolderSystem.hpp" #include "System/Keyboard/KeyboardSystem.hpp" #include "System/Controllable/ControllableSystem.hpp" #include "Component/Movable/MovableComponent.hpp" @@ -28,6 +22,12 @@ #include "Runner.hpp" #include "Models/GameState.hpp" #include +#include +#include +#include +#include +#include +#include #include "Component/Animation/AnimationsComponent.hpp" #include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" @@ -64,6 +64,7 @@ namespace BBM RAY::TraceLog::setLevel(LOG_WARNING); RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); wal.addSystem() + .addSystem() .addSystem(window); } @@ -74,12 +75,16 @@ namespace BBM .addComponent() .addComponent("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() + .addComponent() .addComponent() .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(1) .addComponent() .addComponent() - .addComponent(1); + .addComponent(1, [](WAL::Entity &entity) { + auto &animation = entity.getComponent(); + animation.setAnimIndex(5); + }); scene->addEntity("camera") .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); diff --git a/sources/System/Animator/AnimatorSystem.cpp b/sources/System/Animator/AnimatorSystem.cpp new file mode 100644 index 00000000..f03aeb9b --- /dev/null +++ b/sources/System/Animator/AnimatorSystem.cpp @@ -0,0 +1,37 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#include +#include +#include +#include +#include +#include +#include "AnimatorSystem.hpp" +#include "Component/Renderer/Drawable3DComponent.hpp" + +using Keyboard = RAY::Controller::Keyboard; +namespace RAY3D = RAY::Drawables::Drawables3D; +using Key = RAY::Controller::Keyboard::Key; + +namespace BBM +{ + AnimatorSystem::AnimatorSystem(WAL::Wal &wal) + : System(wal) + {} + + void AnimatorSystem::onFixedUpdate(WAL::ViewEntity &entity) + { + const auto &controllable = entity.get(); + auto drawable = entity.get().drawable.get(); + auto &animation = entity.get(); + auto anim = dynamic_cast(drawable); + if (anim && controllable.move != Vector2f(0, 0)) { + anim->setRotationAngle(controllable.move.angle(Vector2f(-1, 0))); + animation.setAnimIndex(0); + return; + } + animation.setAnimIndex(1); + } +} \ No newline at end of file diff --git a/sources/System/Animator/AnimatorSystem.hpp b/sources/System/Animator/AnimatorSystem.hpp new file mode 100644 index 00000000..fcddfc13 --- /dev/null +++ b/sources/System/Animator/AnimatorSystem.hpp @@ -0,0 +1,29 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#pragma once + +#include +#include "Component/Animation/AnimationsComponent.hpp" +#include "System/System.hpp" + +namespace BBM +{ + //! @brief A system to handle Animator entities. + class AnimatorSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::ViewEntity &entity) override; + + //! @brief A default constructor + AnimatorSystem(WAL::Wal &wal); + //! @brief An Animator system is copy constructable + AnimatorSystem(const AnimatorSystem &) = default; + //! @brief A default destructor + ~AnimatorSystem() override = default; + //! @brief An Animator system is assignable. + AnimatorSystem &operator=(const AnimatorSystem &) = default; + }; +} \ No newline at end of file diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index 45b28069..2d46b3b8 100644 --- a/sources/System/BombHolder/BombHolderSystem.cpp +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -22,7 +22,6 @@ namespace BBM void BombHolderSystem::_bombExplosion(WAL::Entity &bomb, WAL::Wal &wal) { - std::cout << "Boom" << std::endl; bomb.scheduleDeletion(); auto &bombPosition = bomb.getComponent(); wal.getSystem().dispatchEvent([&bombPosition](WAL::Entity &entity){ @@ -40,7 +39,6 @@ namespace BBM void BombHolderSystem::_spawnBomb(Vector3f position) { - std::cout << "Spawnned" << std::endl; this->_wal.scene->scheduleNewEntity("Bomb") .addComponent(position) .addComponent(BombHolderSystem::explosionTimer, &BombHolderSystem::_bombExplosion) diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index 29e0db1f..4419e9e1 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -3,6 +3,7 @@ // Edited by Benjamin Henry on 2021-05-20. // +#include #include "HealthSystem.hpp" #include "Component/Health/HealthComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" @@ -18,7 +19,8 @@ namespace BBM { auto &health = entity.get(); - if (health.getHealthPoint() == 0) + if (health.getHealthPoint() == 0) { health.onDeath(entity); + } } } \ No newline at end of file