menu controllable but still needs view

This commit is contained in:
Bluub
2021-06-03 14:47:27 +02:00
parent c68389d1e8
commit 06a7a2d3bf
2 changed files with 46 additions and 13 deletions

View File

@@ -3,8 +3,9 @@
// //
#include <algorithm> #include <algorithm>
#include "Component/Button/ButtonComponent.hpp"
#include "Component/Position/PositionComponent.hpp" #include "Component/Position/PositionComponent.hpp"
#include "System/MenuControllable/MenuControllableSystem.hpp" #include "System/MenuControllable/MenuControllableSystem.hpp"
#include "Component/Controllable/ControllableComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp"
#include "Entity/Entity.hpp" #include "Entity/Entity.hpp"
@@ -16,10 +17,29 @@ namespace BBM
}) })
{} {}
void MenuControllableSystem::updateButtonIndex(int length)
{
_buttonIndex -= (move.y > 0);
_buttonIndex += (move.y < 0);
if (_buttonIndex < 0)
_buttonIndex = length - 1;
if (_buttonIndex == length)
_buttonIndex = 0;
}
void MenuControllableSystem::onFixedUpdate(WAL::Entity &entity) void MenuControllableSystem::onFixedUpdate(WAL::Entity &entity)
{ {
auto &controllable = entity.getComponent<ControllableComponent>(); auto &controllable = entity.getComponent<ControllableComponent>();
auto buttons = ecs.view<<Button>(entities);
move = controllable.move;
select = controllable.bomb;
}
void MenuControllableSystem::onSelfUpdate(void)
{
auto buttons = ecs.view<<ButtonComponent>(entities);
ssize_t index = 0;
std::sort(buttons.begin(), buttons.end(), std::sort(buttons.begin(), buttons.end(),
[](WAL::Entity &first, WAL::Entity &second) { [](WAL::Entity &first, WAL::Entity &second) {
auto &posA = first.getComponent<PositionComponent>(); auto &posA = first.getComponent<PositionComponent>();
@@ -27,18 +47,15 @@ namespace BBM
return (posA.position.y < posB.position.y); return (posA.position.y < posB.position.y);
}); });
_buttonIndex -= (controllable.move.y > 0); updateButtonIndex(buttons.length);
_buttonIndex += (controllable.move.y < 0); auto currentButton = buttons[_buttonIndex].getComponent<ButtonComponent>();
if (_buttonIndex < 0)
_buttonIndex = buttons.length() - 1;
_buttonIndex %= buttons.length();
auto currentButton = buttons[_buttonIndex].getComponent<Button>();
currentButton.onSelected(); currentButton.onSelected();
if (controllable.bomb) if (select)
currentButton.onClick(); currentButton.onClick();
std::for_each_n(buttons.begin(), _buttonIndex, for (auto &button : buttons) {
[](WAL::Entity &curr) { if (index++ == _buttonIndex)
curr.getComponent<Button>().onIdle(); continue;
}); button.onIdle();
}
} }
} }

View File

@@ -12,8 +12,24 @@ namespace BBM
class MenuControllableSystem : public WAL::System class MenuControllableSystem : public WAL::System
{ {
private: private:
//! @brief index of the current button selected
unsigned _buttonIndex = 0; unsigned _buttonIndex = 0;
//! @brief move vector
Vector2f move;
//! @brief Select action
bool select = false;
//! @brief Cancel action
bool cancel = false;
//! @brief update button index
//! @param length length of the button set
void updateButtonIndex(int length);
public: public:
//! @inherit
void onSelfUpdate(void) override;
//! @inherit //! @inherit
void onFixedUpdate(WAL::Entity &entity) override; void onFixedUpdate(WAL::Entity &entity) override;