menu controllable system and buttonComponent

This commit is contained in:
Bluub
2021-06-03 11:07:01 +02:00
parent 94fe25fd39
commit 36657affc6
4 changed files with 140 additions and 0 deletions
@@ -0,0 +1,25 @@
//
// Created by Louis Auzuret on 06/03/21.
//
#include "ButtonComponent.hpp"
namespace BBM
{
ButtonComponent::ButtonComponent(WAL::Entity &entity)
: WAL::Component(entity), onIdle(), onHover(), onClick()
{ }
WAL::Component *ButtonComponent::clone(WAL::Entity &entity) const
{
return new ButtonComponent(entity, onIdle, onHover, onClick);
}
ButtonComponent::ButtonComponent(WAL::Entity &entity, WAL::Callback<> idleCallback,
WAL::Callback<> hoverCallback, WAL::Callback<> clickCallback)
: WAL::Component(entity),
onIdle(idleCallback),
onHover(hoverCallback),
onClick(clickCallback)
{ }
}
@@ -0,0 +1,41 @@
//
// Created by Louis Auzuret on 06/03/21
//
#pragma once
#include "Models/Callback.hpp"
#include "Component/Component.hpp"
#include "Entity/Entity.hpp"
namespace BBM
{
class ButtonComponent : public WAL::Component
{
public:
//! @brief onIdle callback
WAL::Callback<> onIdle;
//! @brief onHover callback
WAL::Callback<> onHover;
//! @brief onClick callback
WAL::Callback<> onClick;
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Initialize a new Button component.
explicit ButtonComponent(WAL::Entity &entity);
//! @brief Constructor with the 3 callback
ButtonComponent(WAL::Entity &entity, WAL::Callback<> idleCallback, WAL::Callback<> hoverCallback, WAL::Callback<> clickCallback);
//! @brief A Controllable component is copy constructable.
ButtonComponent(const ButtonComponent &) = default;
//! @brief default destructor
~ButtonComponent() override = default;
//! @brief A Button component default assign operator
ButtonComponent &operator=(const ButtonComponent &) = default;
};
}
@@ -0,0 +1,44 @@
//
// Created by Louis Auzuret 06/03/21
//
#include <algorithm>
#include "Component/Position/PositionComponent.hpp"
#include "System/MenuControllable/MenuControllableSystem.hpp"
#include "Component/Controllable/ControllableComponent.hpp"
#include "Entity/Entity.hpp"
namespace BBM
{
MenuControllableSystem::MenuControllableSystem()
: WAL::System({
typeid(ControllableComponent)
})
{}
void MenuControllableSystem::onFixedUpdate(WAL::Entity &entity)
{
auto &controllable = entity.getComponent<ControllableComponent>();
auto buttons = ecs.view<<Button>(entities);
std::sort(buttons.begin(), buttons.end(),
[](WAL::Entity &first, WAL::Entity &second) {
auto &posA = first.getComponent<PositionComponent>();
auto &posB = second.getComponent<PositionComponent>();
return (posA.position.y < posB.position.y);
});
_buttonIndex -= (controllable.move.y > 0);
_buttonIndex += (controllable.move.y < 0);
if (_buttonIndex < 0)
_buttonIndex = buttons.length() - 1;
_buttonIndex %= buttons.length();
auto currentButton = buttons[_buttonIndex].getComponent<Button>();
currentButton.onSelected();
if (controllable.bomb)
currentButton.onClick();
std::for_each_n(buttons.begin(), _buttonIndex,
[](WAL::Entity &curr) {
curr.getComponent<Button>().onIdle();
});
}
}
@@ -0,0 +1,30 @@
//
// Created by Louis Auzuret on 06/03/21
//
#pragma once
#include "System/System.hpp"
namespace BBM
{
//! @brief A system to handle Controllable entities in a menu.
class MenuControllableSystem : public WAL::System
{
private:
unsigned _buttonIndex = 0;
public:
//! @inherit
void onFixedUpdate(WAL::Entity &entity) override;
//! @brief A default constructor
MenuControllableSystem();
//! @brief A MenuControllable system is not copy constructable
MenuControllableSystem(const MenuControllableSystem &) = delete;
//! @brief A default destructor
~MenuControllableSystem() override = default;
//! @brief A MenuControllable system is assignable.
MenuControllableSystem &operator=(const MenuControllableSystem &) = default;
};
}