diff --git a/lib/wal/sources/Component/Component.cpp b/lib/wal/sources/Component/Component.cpp index aba34221..8609b397 100644 --- a/lib/wal/sources/Component/Component.cpp +++ b/lib/wal/sources/Component/Component.cpp @@ -10,16 +10,6 @@ namespace WAL : _entity(entity) { } - bool Component::isDisabled() const - { - return this->_disabled; - } - - void Component::setDisable(bool disabled) - { - this->_disabled = disabled; - } - void Component::onStart() { // TODO handle events here diff --git a/lib/wal/sources/Component/Component.hpp b/lib/wal/sources/Component/Component.hpp index 33794d89..d67522e2 100644 --- a/lib/wal/sources/Component/Component.hpp +++ b/lib/wal/sources/Component/Component.hpp @@ -16,9 +16,6 @@ namespace WAL //! @brief Represent a single component of WAL. class Component { - private: - //! @brief Is this component disabled? - bool _disabled = false; protected: //! @brief The entity that own this component Entity &_entity; @@ -37,11 +34,6 @@ namespace WAL //! @param entity The entity that owns the ne component. virtual Component *clone(Entity &entity) const = 0; - //! @brief Used if the component is disabled - bool isDisabled() const; - //! @brief Disable this component. - void setDisable(bool disabled); - //! @brief The entity or this component has just been enabled. virtual void onStart(); diff --git a/lib/wal/sources/Entity/Entity.cpp b/lib/wal/sources/Entity/Entity.cpp index 0142a4aa..8cdf763e 100644 --- a/lib/wal/sources/Entity/Entity.cpp +++ b/lib/wal/sources/Entity/Entity.cpp @@ -52,7 +52,7 @@ namespace WAL Entity &Entity::addComponent(const Component &component) { const std::type_index &type = typeid(component); - if (this->hasComponent(type, false)) + if (this->hasComponent(type)) throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists."); this->_components.emplace(type, component.clone(*this)); if (this->_notifyScene) @@ -60,19 +60,14 @@ namespace WAL return *this; } - bool Entity::hasComponent(const std::type_info &type, bool skipDisabled) const + bool Entity::hasComponent(const std::type_info &type) const { - return this->hasComponent(static_cast(type), skipDisabled); + return this->hasComponent(static_cast(type)); } - bool Entity::hasComponent(const std::type_index &type, bool skipDisabled) const + bool Entity::hasComponent(const std::type_index &type) const { - auto cmp = this->_components.find(type); - if (cmp == this->_components.end()) - return false; - if (skipDisabled) - return !cmp->second->isDisabled(); - return true; + return this->_components.contains(type); } void Entity::_componentAdded(const std::type_index &type) diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 0cad61b9..9d8b2d13 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -117,24 +117,21 @@ namespace WAL } //! @brief Check if this entity has a component. - //! @param skipDisabled True if you want to skip disabled components (consider them non present), false otherwise. //! @tparam T The type of the component template - bool hasComponent(bool skipDisabled = true) const + bool hasComponent() const { const std::type_info &type = typeid(T); - return this->hasComponent(type, skipDisabled); + return this->hasComponent(type); } //! @brief Check if this entity has a component. - //! @param skipDisabled True if you want to skip disabled components (consider them non present), false otherwise. //! @param type The type of the component - bool hasComponent(const std::type_info &type, bool skipDisabled = true) const; + bool hasComponent(const std::type_info &type) const; //! @brief Check if this entity has a component. - //! @param skipDisabled True if you want to skip disabled components (consider them non present), false otherwise. //! @param type The type of the component - bool hasComponent(const std::type_index &type, bool skipDisabled = true) const; + bool hasComponent(const std::type_index &type) const; //! @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. diff --git a/sources/Component/Animation/AnimationsComponent.cpp b/sources/Component/Animation/AnimationsComponent.cpp index 26367a0b..6822458b 100644 --- a/sources/Component/Animation/AnimationsComponent.cpp +++ b/sources/Component/Animation/AnimationsComponent.cpp @@ -12,7 +12,7 @@ namespace BBM : WAL::Component(entity), _modelAnimation(path), _currentAnimIndex(animIndex), - _animDisabled(play) + _animDisabled(!play) { this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(0); } diff --git a/sources/Component/Controllable/ControllableComponent.cpp b/sources/Component/Controllable/ControllableComponent.cpp index 959d372a..6e0edf31 100644 --- a/sources/Component/Controllable/ControllableComponent.cpp +++ b/sources/Component/Controllable/ControllableComponent.cpp @@ -7,11 +7,17 @@ namespace BBM { ControllableComponent::ControllableComponent(WAL::Entity &entity) - : WAL::Component(entity) + : WAL::Component(entity), + disabled(false) {} WAL::Component *ControllableComponent::clone(WAL::Entity &entity) const { return new ControllableComponent(entity); } + + ControllableComponent::ControllableComponent(WAL::Entity &entity, bool isDisabled) + : WAL::Component(entity), + disabled(isDisabled) + {} } \ No newline at end of file diff --git a/sources/Component/Controllable/ControllableComponent.hpp b/sources/Component/Controllable/ControllableComponent.hpp index f16de468..c2446c83 100644 --- a/sources/Component/Controllable/ControllableComponent.hpp +++ b/sources/Component/Controllable/ControllableComponent.hpp @@ -29,6 +29,9 @@ namespace BBM AI }; + //! @brief True if the entity should not be controllable. + bool disabled; + //! @brief The X and Z abscis of the movement. Vector2f move; //! @brief input value to select @@ -47,6 +50,8 @@ namespace BBM //! @brief Initialize a new controllable component. explicit ControllableComponent(WAL::Entity &entity); + //! @brief Initialize a new controllable component. + ControllableComponent(WAL::Entity &entity, bool isDisabled); //! @brief A Controllable component is copy constructable. ControllableComponent(const ControllableComponent &) = default; //! @brief default destructor diff --git a/sources/Runner/GameScene.cpp b/sources/Runner/GameScene.cpp index fc6e1c9a..7136042f 100644 --- a/sources/Runner/GameScene.cpp +++ b/sources/Runner/GameScene.cpp @@ -60,6 +60,7 @@ namespace BBM .addComponent() .addComponent() .addComponent() + .addComponent(true) .addComponent>() .addComponent>() .addComponent("assets/player/player.iqm", 3) @@ -77,6 +78,7 @@ namespace BBM entity.removeComponent(); if (entity.hasComponent()) return; + entity.getComponent().disabled = true; entity.addComponent(1s, [](WAL::Entity &entity, WAL::Wal &wal) { entity.scheduleDeletion(); }); diff --git a/sources/Runner/ScoreScene.cpp b/sources/Runner/ScoreScene.cpp index 50ef165b..1cf5b40d 100644 --- a/sources/Runner/ScoreScene.cpp +++ b/sources/Runner/ScoreScene.cpp @@ -42,16 +42,20 @@ namespace BBM }); int playerID = 0; - for (auto &entity: players) { + for (auto &entity : players) { auto *model = dynamic_cast(entity.get().getComponent().drawable.get()); std::string path = model->getTextureByMaterial(MAP_DIFFUSE).getResourcePath(); playersIconPath.push_back(path.replace(path.find("textures"), std::string("textures").size(), "icons")); + auto &newPlayer = scene->addEntity("add"); newPlayer.addComponent(playerID++, newPlayer, newPlayer); auto &lobby = newPlayer.getComponent(); - lobby.layout = entity.get().getComponent().layout; // TODO layout was none. - std::string color = path.substr(path.find_last_of('/'), path.find_last_of('.')); - lobby.color = std::find(LobbySystem::colors.begin(), LobbySystem::colors.end(), color) - LobbySystem::colors.begin(); + lobby.layout = entity.get().getComponent().layout; + + auto start = path.find_last_of('/') + 1; + std::string color = path.substr(start, path.find_last_of('.') - start); + auto iterator = std::find(LobbySystem::colors.begin(), LobbySystem::colors.end(), color); + lobby.color = static_cast(iterator - LobbySystem::colors.begin()); } addMenuControl(*scene, sounds); @@ -96,13 +100,8 @@ namespace BBM }) .addComponent([](WAL::Entity &entity, WAL::Wal &wal) { - - if (Runner::gameState.currentScene != GameState::ScoreScene - || !LobbySystem::playersAreReady(*wal.getScene())) - return; LobbySystem::switchToGame(wal); - }) - .addComponent>(); + }); auto &back = scene->addEntity("back to main menu") .addComponent(10, 1080 - 85, 0) .addComponent("assets/buttons/button_back.png") diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index 5aaa4597..64acfbde 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -2,7 +2,6 @@ // Created by cbihan on 01/06/2021. // -#include #include "AnimationsSystem.hpp" #include "Component/Animation/AnimationsComponent.hpp" #include "Model/Model.hpp" @@ -20,7 +19,7 @@ namespace BBM auto &model = entity.get(); auto &anim = entity.get(); - if (anim.isDisabled()) + if (anim.isAnimDisabled()) return; auto modelPtr = std::dynamic_pointer_cast(model.drawable); if (modelPtr) { diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index 36e9815a..a5979f6f 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -7,7 +7,6 @@ #include "Component/Movable/MovableComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" #include "Component/Health/HealthComponent.hpp" -#include "Entity/Entity.hpp" namespace BBM { @@ -22,8 +21,6 @@ namespace BBM auto health = entity->tryGetComponent(); Vector2f move = controllable.move.normalized() * controllable.speed; - if (health && health->getHealthPoint() <= 0) - return; movable.addForce(Vector3f(move.x, 0, move.y)); } } \ No newline at end of file diff --git a/sources/System/Gamepad/GamepadSystem.cpp b/sources/System/Gamepad/GamepadSystem.cpp index 7ff78016..036e4623 100644 --- a/sources/System/Gamepad/GamepadSystem.cpp +++ b/sources/System/Gamepad/GamepadSystem.cpp @@ -21,8 +21,11 @@ namespace BBM { const auto &gamepadComponent = entity.get(); auto &controllable = entity.get(); - Gamepad gamepad(gamepadComponent.getID()); + if (controllable.disabled) + return; + + Gamepad gamepad(gamepadComponent.getID()); const std::map keyPressedMap = { {gamepadComponent.keyJump, controllable.select}, {gamepadComponent.keyBomb, controllable.bomb}, diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index cec372fc..d199a58f 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -158,6 +158,9 @@ namespace BBM auto &bombHolder = entity.get(); MapInfo player(pos.position, MapGenerator::NOTHING); + if (controllable.disabled) + return; + UpdateMapInfos(entity); ia._state.getGlobal("Update"); diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp index 8dbda5ab..e6820f38 100644 --- a/sources/System/Keyboard/KeyboardSystem.cpp +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -21,6 +21,9 @@ namespace BBM const auto &keyboard = entity.get(); auto &controllable = entity.get(); + if (controllable.disabled) + return; + const std::map keyPressedMap = { {keyboard.keyJump, controllable.select}, {keyboard.keyBomb, controllable.bomb}, diff --git a/sources/System/Lobby/LobbySystem.cpp b/sources/System/Lobby/LobbySystem.cpp index 142dfc92..e845e7aa 100644 --- a/sources/System/Lobby/LobbySystem.cpp +++ b/sources/System/Lobby/LobbySystem.cpp @@ -198,6 +198,7 @@ namespace BBM default: throw std::runtime_error("Invalid controller for a player."); } + player.getComponent().layout = layout; } void LobbySystem::switchToGame(WAL::Wal &wal) @@ -217,6 +218,8 @@ namespace BBM mapHeight * (!(playerCount % 3))); auto *model = dynamic_cast(player.getComponent().drawable.get()); model->setTextureToMaterial(MAP_DIFFUSE, "assets/player/textures/" + colors[lobby.color] + ".png"); + + std::string texturePath = "assets/player/ui/" + colors[lobby.color] + ".png"; int x = (playerCount % 2 == 0) ? 1920 - 10 - 320 : 10; int y = (playerCount % 3 != 0) ? 1080 - 10 - 248 : 10; diff --git a/sources/System/Renderer/CameraSystem.cpp b/sources/System/Renderer/CameraSystem.cpp index 661eddf2..1a51ad90 100644 --- a/sources/System/Renderer/CameraSystem.cpp +++ b/sources/System/Renderer/CameraSystem.cpp @@ -28,8 +28,18 @@ namespace BBM hasEnded = false; return (false); } - if (pos.position.distance(posTarget) < 4 || hasEnded) { + if (hasEnded) + return true; + if (pos.position.distance(posTarget) < 4) { hasEnded = true; + this->_wal.getScene()->scheduleNewEntity("Timer") + .addComponent(std::chrono::minutes (3), [](WAL::Entity &, WAL::Wal &) { + Runner::gameState.nextScene = GameState::ScoreScene; + }) + .addComponent(1920 / 2 - 2 * 30, 30, 0) + .addComponent("", 60, RAY::Vector2(), ORANGE); + for (WAL::Entity &player : this->_wal.getScene()->view>()) + player.getComponent().disabled = false; return (true); } @@ -53,18 +63,10 @@ namespace BBM float lowerZDist = 0; for (auto &[entity, pos, _] : this->_wal.getScene()->view>()) { - if (!entity.hasComponent()) - entity.addComponent(); playerPos.emplace_back(pos.position); } - if (playerPos.size() == 0) + if (!playerPos.empty()) introAnimation(entity, true); - static auto &timer = this->_wal.getScene()->addEntity("Timer") - .addComponent(std::chrono::minutes (3), [](WAL::Entity &, WAL::Wal &) { - Runner::gameState.nextScene = GameState::ScoreScene; - }) - .addComponent(1920 / 2 - 2 * 30, 30, 0) - .addComponent("", 60, RAY::Vector2(), ORANGE); if (playerPos.size() == 1) newCameraPos = playerPos[0]; for (int i = 0; i < playerPos.size(); i++) diff --git a/sources/System/Timer/TimerSystem.cpp b/sources/System/Timer/TimerSystem.cpp index 3760534c..ded0b52e 100644 --- a/sources/System/Timer/TimerSystem.cpp +++ b/sources/System/Timer/TimerSystem.cpp @@ -19,7 +19,6 @@ namespace BBM auto &timer = entity.get(); timer.ringIn -= dtime; if (timer.ringIn <= 0ns) { - timer.setDisable(true); timer.callback(entity, this->_wal); } }