Merge pull request #174 from AnonymusRaccoon/ui_improve

UI Improvements
This commit is contained in:
Arthi-chaud
2021-06-16 17:42:39 +02:00
committed by GitHub
28 changed files with 488 additions and 22 deletions
+9
View File
@@ -141,7 +141,16 @@ set(SOURCES
sources/System/EndCondition/EndConditionSystem.hpp
sources/System/EndCondition/EndConditionSystem.cpp
sources/Runner/LobbyScene.cpp
sources/Runner/HowToPlayScene.cpp
sources/Runner/ScoreScene.cpp
sources/System/Timer/TimerUISystem.hpp
sources/System/Timer/TimerUISystem.cpp
sources/System/Bonus/BonusUISystem.hpp
sources/System/Bonus/BonusUISystem.cpp
sources/Component/Color/ColorComponent.hpp
sources/Component/Color/ColorComponent.cpp
sources/Component/Stat/StatComponent.cpp
sources/Component/Stat/StatComponent.hpp
)
add_executable(bomberman
sources/main.cpp
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

@@ -0,0 +1,24 @@
//
//
//
#include "ColorComponent.hpp"
namespace BBM
{
ColorComponent::ColorComponent(WAL::Entity &entity, RAY::Color color)
: Component(entity),
color(color)
{}
ColorComponent::ColorComponent(WAL::Entity &entity, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
: Component(entity),
color(r, g, b, a)
{}
WAL::Component *ColorComponent::clone(WAL::Entity &entity) const
{
return new ColorComponent(entity, this->color);
}
} // namespace WAL
@@ -0,0 +1,33 @@
//
//
//
#pragma once
#include "Color.hpp"
#include "Component/Component.hpp"
namespace BBM
{
//! @brief A basic color component
class ColorComponent : public WAL::Component
{
public:
//! @brief Get the editable color of this entity
RAY::Color color;
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Create a new ColorComponent at a certain color
ColorComponent(WAL::Entity &entity, RAY::Color color);
//! @brief Create a new ColorComponent at a certain color
ColorComponent(WAL::Entity &entity, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
//! @brief A color component is copy constructable
ColorComponent(const ColorComponent &) = default;
//! @brief A default destructor
~ColorComponent() override = default;
//! @brief A color component is not assignable
ColorComponent &operator=(const ColorComponent &) = delete;
};
} // namespace WAL
+19
View File
@@ -0,0 +1,19 @@
//
//
//
#include "StatComponent.hpp"
namespace BBM
{
StatComponent::StatComponent(WAL::Entity &entity, WAL::Callback<Drawable2DComponent &> callback)
: Component(entity),
updater(callback)
{}
WAL::Component *StatComponent::clone(WAL::Entity &entity) const
{
return new StatComponent(entity, this->updater);
}
} // namespace WAL
+33
View File
@@ -0,0 +1,33 @@
//
//
//
#pragma once
#include "Component/Component.hpp"
#include "Models/Callback.hpp"
#include "Wal.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
namespace BBM
{
//! @brief A Stat component which contains a text and a callback
class StatComponent : public WAL::Component
{
public:
//! @brief onEvent callback
WAL::Callback<Drawable2DComponent &> updater;
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Create a new StatComponent with a callback
StatComponent(WAL::Entity &entity, WAL::Callback<Drawable2DComponent &> callback);
//! @brief A color component is copy constructable
StatComponent(const StatComponent &) = default;
//! @brief A default destructor
~StatComponent() override = default;
//! @brief A color component is not assignable
StatComponent &operator=(const StatComponent &) = delete;
};
} // namespace WAL
+1
View File
@@ -26,6 +26,7 @@ namespace BBM
LobbyScene,
TitleScreenScene,
CreditScene,
HowToPlayScene,
ScoreScene,
};
+8 -1
View File
@@ -16,14 +16,19 @@
#include "Component/BombHolder/BombHolderComponent.hpp"
#include "Component/Tag/TagComponent.hpp"
#include "Component/Renderer/Drawable3DComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Component/Button/ButtonComponent.hpp"
#include "Drawables/Texture.hpp"
#include "Component/Gravity/GravityComponent.hpp"
#include "Component/BumperTimer/BumperTimerComponent.hpp"
#include "Component/Timer/TimerComponent.hpp"
#include "Model/Model.hpp"
#include "Map/Map.hpp"
#include "Component/Score/ScoreComponent.hpp"
#include "Drawables/2D/Text.hpp"
namespace RAY3D = RAY::Drawables::Drawables3D;
namespace RAY2D = RAY::Drawables::Drawables2D;
namespace BBM
{
@@ -36,7 +41,9 @@ namespace BBM
scene->addEntity("Timer")
.addComponent<TimerComponent>(std::chrono::minutes (3), [](WAL::Entity &, WAL::Wal &) {
Runner::gameState.nextScene = GameState::ScoreScene;
});
})
.addComponent<PositionComponent>(1920 / 2 - 2 * 30, 30, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("", 60, RAY::Vector2(), ORANGE);
MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16, hasHeights), scene);
return scene;
}
+81
View File
@@ -0,0 +1,81 @@
#include <memory>
#include <Wal.hpp>
#include "Runner.hpp"
#include <map>
#include "Component/Music/MusicComponent.hpp"
#include "Component/Sound/SoundComponent.hpp"
#include "Component/Controllable/ControllableComponent.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "Component/Keyboard/KeyboardComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Component/Button/ButtonComponent.hpp"
#include "Drawables/2D/Text.hpp"
namespace RAY2D = RAY::Drawables::Drawables2D;
namespace BBM
{
std::shared_ptr<WAL::Scene> Runner::loadHowToPlayScene()
{
auto scene = std::make_shared<WAL::Scene>();
static const std::map<SoundComponent::SoundIndex, std::string> sounds = {
{SoundComponent::JUMP, "assets/sounds/click.ogg"}
};
addMenuControl(*scene);
scene->addEntity("Control entity")
.addComponent<MusicComponent>("assets/musics/music_player_select.ogg")
.addComponent<SoundComponent>(sounds);
scene->addEntity("background")
.addComponent<PositionComponent>()
.addComponent<Drawable2DComponent, RAY::Texture>("assets/plain_menu_background.png");
scene->addEntity("scene title text")
.addComponent<PositionComponent>(1920 / 3, 100, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("How To Play?", 120, RAY::Vector2(), ORANGE);
scene->addEntity("select text")
.addComponent<PositionComponent>(1920 / 8, 1080 / 3, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Select:", 60, RAY::Vector2(), ORANGE);
scene->addEntity("select")
.addComponent<PositionComponent>(1920 / 7, 1080 / 2.5, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Space/A Button", 35, RAY::Vector2(), BLACK);
scene->addEntity("change skin text")
.addComponent<PositionComponent>(1920 / 8, 1080 / 2, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Change Skin/Drop Bomb:", 60, RAY::Vector2(), ORANGE);
scene->addEntity("change skin")
.addComponent<PositionComponent>(1920 / 7, 1080 / 1.75, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("E/B Button", 35, RAY::Vector2(), BLACK);
scene->addEntity("move text")
.addComponent<PositionComponent>(1920 / 1.75, 1080 / 3, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Move:", 60, RAY::Vector2(), ORANGE);
scene->addEntity("move")
.addComponent<PositionComponent>(1920 / 1.75, 1080 / 2.5, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Q-Z-S-D/Arrow/Joystick", 35, RAY::Vector2(), BLACK);
scene->addEntity("back text")
.addComponent<PositionComponent>(1920 / 1.75, 1080 / 2, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Back/Pause:", 60, RAY::Vector2(), ORANGE);
scene->addEntity("back")
.addComponent<PositionComponent>(1920 / 1.75, 1080 / 1.75, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Esc / Controller's Home button:", 35, RAY::Vector2(), BLACK);
auto &back = scene->addEntity("back to menu")
.addComponent<PositionComponent>(10, 1080 - 85, 0)
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_back.png")
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &)
{
gameState.nextScene = BBM::GameState::SceneID::LobbyScene;
})
.addComponent<OnIdleComponent>([](WAL::Entity &entity, WAL::Wal &)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_back.png");
})
.addComponent<OnHoverComponent>([](WAL::Entity &entity, WAL::Wal &)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_back_hovered.png");
});
return scene;
}
}
+27 -10
View File
@@ -59,39 +59,55 @@ namespace BBM
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &wal)
{
if (Runner::gameState.currentScene != GameState::LobbyScene
|| !LobbySystem::playersAreReady(*wal.getScene()))
|| !LobbySystem::playersAreReady(*wal.getScene()))
return;
LobbySystem::switchToGame(wal);
})
.addComponent<TagComponent<"PlayButton">>();
auto &back = scene->addEntity("back to menu")
.addComponent<PositionComponent>(10, 1080 - 85, 0)
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_back.png")
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &wal)
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &)
{
gameState.nextScene = BBM::GameState::SceneID::MainMenuScene;
wal.getSystem<LobbySystem>().unloadLobby();
})
.addComponent<OnIdleComponent>([](WAL::Entity &entity, WAL::Wal &)
{
auto *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_back.png");
texture->use("assets/buttons/button_back.png");
})
.addComponent<OnHoverComponent>([](WAL::Entity &entity, WAL::Wal &)
{
auto *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_back_hovered.png");
});
auto &howToPlay = scene->addEntity("to to play")
.addComponent<PositionComponent>(1920 - 10 - 75, 1080 - 85, 0)
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_htp.png")
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &)
{
gameState.nextScene = BBM::GameState::SceneID::HowToPlayScene;
})
.addComponent<OnIdleComponent>([](WAL::Entity &entity, WAL::Wal &)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_htp.png");
})
.addComponent<OnHoverComponent>([](WAL::Entity &entity, WAL::Wal &)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_htp_hovered.png");
});
auto &lavaOption = scene->addEntity("lava option text")
.addComponent<PositionComponent>(1920 / 6, 1.85 * 1080 / 3, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Lava: Off", 70, RAY::Vector2(), BLACK)
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &wal)
{
auto *text = dynamic_cast<RAY2D::Text *>(entity.getComponent<Drawable2DComponent>().drawable.get());
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(entity.getComponent<Drawable2DComponent>().drawable.get());
if (text->getString().find("Off") != std::string::npos) {
text->setText("Lava: On");
@@ -185,7 +201,8 @@ namespace BBM
scene->addEntity("camera")
.addComponent<PositionComponent>(8, 20, 7)
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
play.getComponent<OnClickComponent>().setButtonLinks(&heightOption, &back, &back, nullptr);
play.getComponent<OnClickComponent>().setButtonLinks(&lavaOption, &back, &back, &howToPlay);
howToPlay.getComponent<OnClickComponent>().setButtonLinks(&play, nullptr, &play);
back.getComponent<OnClickComponent>().setButtonLinks(&play, nullptr, nullptr, &play);
lavaOption.getComponent<OnClickComponent>().setButtonLinks(nullptr, &heightOption, nullptr, &aiMore);
heightOption.getComponent<OnClickComponent>().setButtonLinks(&lavaOption, &play, nullptr, &aiLess);
+6 -4
View File
@@ -16,6 +16,7 @@
#include "Runner.hpp"
#include "Models/GameState.hpp"
#include <System/Timer/TimerSystem.hpp>
#include <System/Timer/TimerUISystem.hpp>
#include <System/BombHolder/BombHolderSystem.hpp>
#include <System/Event/EventSystem.hpp>
#include <System/Health/HealthSystem.hpp>
@@ -37,6 +38,7 @@
#include "System/Score/ScoreSystem.hpp"
#include "System/EndCondition/EndConditionSystem.hpp"
#include "Component/Lobby/LobbyComponent.hpp"
#include "System/Bonus/BonusUISystem.hpp"
namespace BBM
{
@@ -48,14 +50,11 @@ namespace BBM
auto &view = engine.getScene()->view<ControllableComponent>();
if (RAY::Window::getInstance().shouldClose())
engine.shouldClose = true;
if (gameState.currentScene == GameState::SceneID::GameScene || gameState.currentScene == GameState::SceneID::SplashScreen) {
if (gameState.currentScene == GameState::SceneID::GameScene) {
for (auto &[_, component]: engine.getScene()->view<ControllableComponent>()) {
if (component.pause && gameState.currentScene == GameState::SceneID::GameScene) {
gameState.nextScene = GameState::SceneID::PauseMenuScene;
break;
} else if (gameState.currentScene == GameState::SceneID::SplashScreen && component.select) {
gameState.nextScene = GameState::SceneID::TitleScreenScene;
break;
}
}
}
@@ -71,6 +70,7 @@ namespace BBM
void Runner::addSystems(WAL::Wal &wal)
{
wal.addSystem<TimerSystem>()
.addSystem<TimerUISystem>()
.addSystem<KeyboardSystem>()
.addSystem<GamepadSystem>()
.addSystem<LobbySystem>()
@@ -79,6 +79,7 @@ namespace BBM
.addSystem<BombHolderSystem>()
.addSystem<EventSystem>()
.addSystem<HealthSystem>()
.addSystem<BonusUISystem>()
.addSystem<CollisionSystem>()
.addSystem<LevitateSystem>()
.addSystem<PlayerBonusSystem>()
@@ -127,6 +128,7 @@ namespace BBM
gameState._loadedScenes[GameState::SceneID::CreditScene] = loadCreditScene();
gameState._loadedScenes[GameState::SceneID::SplashScreen] = loadSplashScreenScene();
gameState._loadedScenes[GameState::SceneID::LobbyScene] = loadLobbyScene();
gameState._loadedScenes[GameState::SceneID::HowToPlayScene] = loadHowToPlayScene();
}
int Runner::run()
+2
View File
@@ -61,6 +61,8 @@ namespace BBM
//! @brief load all data related to splash screen
static std::shared_ptr<WAL::Scene> loadSplashScreenScene();
//! @brief load how to play screen
static std::shared_ptr<WAL::Scene> loadHowToPlayScene();
//! @brief load all data related to score scene screen
//! @param gameScene scene containing players (to know the scores)
static std::shared_ptr<WAL::Scene> loadScoreScene(WAL::Scene &gameScene);
+7 -1
View File
@@ -30,7 +30,13 @@ namespace BBM
.addComponent<Drawable2DComponent, RAY2D::Text>("powered by", 30, RAY::Vector2(), BLACK);
auto &skipText = scene->addEntity("Press space to skip")
.addComponent<PositionComponent>(1920 - 250, 1080 - 30, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Press space to skip", 20, RAY::Vector2(), BLACK);
.addComponent<Drawable2DComponent, RAY2D::Text>("Press space to skip", 20, RAY::Vector2(), BLACK)
.addComponent<OnIdleComponent>()
.addComponent<OnHoverComponent>()
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &)
{
gameState.nextScene = BBM::GameState::SceneID::TitleScreenScene;
});
return scene;
}
}
+17
View File
@@ -0,0 +1,17 @@
//
// Created by hbenjamin on 09/06/2021.
//
#include "BonusUISystem.hpp"
namespace BBM
{
BonusUISystem::BonusUISystem(WAL::Wal &wal)
: System(wal)
{}
void BonusUISystem::onFixedUpdate(WAL::ViewEntity<StatComponent, Drawable2DComponent> &entity)
{
entity.get<StatComponent>().updater(entity.get<Drawable2DComponent>());
}
}
+32
View File
@@ -0,0 +1,32 @@
//
//
//
#pragma once
#include "System/System.hpp"
#include "Wal.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Component/Stat/StatComponent.hpp"
namespace BBM
{
//! @brief The system that allow the text of the ui to display current values
class BonusUISystem : public WAL::System<StatComponent, Drawable2DComponent>
{
private:
public:
//! @inherit
void onFixedUpdate(WAL::ViewEntity<StatComponent, Drawable2DComponent> &entity) override;
//! @brief A default constructor
explicit BonusUISystem(WAL::Wal &wal);
//! @brief A bomb holder system is copy constructable
BonusUISystem(const BonusUISystem &) = default;
//! @brief A default destructor
~BonusUISystem() override = default;
//! @brief A bomb holder system is not assignable.
BonusUISystem &operator=(const BonusUISystem &) = delete;
};
}
+70
View File
@@ -14,8 +14,15 @@
#include <Component/Gamepad/GamepadComponent.hpp>
#include <Component/Position/PositionComponent.hpp>
#include <Component/Renderer/Drawable3DComponent.hpp>
#include <Drawables/2D/Text.hpp>
#include <Drawables/2D/Text.hpp>
#include "Component/Color/ColorComponent.hpp"
#include "Component/Stat/StatComponent.hpp"
#include "Component/Bonus/PlayerBonusComponent.hpp"
#include "Component/BombHolder/BombHolderComponent.hpp"
namespace RAY3D = RAY::Drawables::Drawables3D;
namespace RAY2D = RAY::Drawables::Drawables2D;
namespace BBM
{
@@ -198,8 +205,10 @@ namespace BBM
int mapWidth = 16;
int mapHeight = 16;
int playerCount = 0;
int playerID = 0;
for (auto &[_, lobby] : wal.getScene()->view<LobbyComponent>()) {
playerID++;
if (lobby.layout == ControllableComponent::NONE)
continue;
auto &player = Runner::createPlayer(*scene);
@@ -209,6 +218,67 @@ namespace BBM
mapHeight * (!(playerCount % 3)));
auto *model = dynamic_cast<RAY3D::Model *>(player.getComponent<Drawable3DComponent>().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 = (playerID % 2 == 0) ? 1920 - 10 - 320 : 10;
int y = playerID > 2 ? 1080 - 10 - 248 : 10;
scene->addEntity("player color tile")
.addComponent<PositionComponent>(x, y - 2, 0)
.addComponent<Drawable2DComponent, RAY2D::Rectangle>(x, y, 320, 248, _rayColors[lobby.color]);
scene->addEntity("player ui tile")
.addComponent<PositionComponent>(x, y, 0)
.addComponent<Drawable2DComponent, RAY::Texture>(texturePath);
scene->addEntity("player hide fireup")
.addComponent<PositionComponent>(x + 220, y + 35, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("", 20, x, y, WHITE)
.addComponent<StatComponent>([&player](Drawable2DComponent &drawble) {
const BombHolderComponent *bonus = player.tryGetComponent<BombHolderComponent>();
if (!bonus)
return;
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(drawble.drawable.get());
if (!text)
return;
text->setText(std::to_string(bonus->explosionRadius));
});
scene->addEntity("player hide bombup")
.addComponent<PositionComponent>(x + 220, y + 77, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("", 20, x, y, WHITE)
.addComponent<StatComponent>([&player](Drawable2DComponent &drawble) {
const BombHolderComponent *bonus = player.tryGetComponent<BombHolderComponent>();
if (!bonus)
return;
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(drawble.drawable.get());
if (!text)
return;
text->setText(std::to_string(bonus->bombCount) + " / " + std::to_string(bonus->maxBombCount));
});
scene->addEntity("player hide speedup")
.addComponent<PositionComponent>(x + 220, y + 122, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("", 20, x, y, WHITE)
.addComponent<StatComponent>([&player](Drawable2DComponent &drawble) {
const ControllableComponent *bonus = player.tryGetComponent<ControllableComponent>();
if (!bonus)
return;
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(drawble.drawable.get());
if (!text)
return;
text->setText(std::to_string(static_cast<int>(bonus->speed * 100)));
});
scene->addEntity("player hide wall")
.addComponent<PositionComponent>(x + 220, y + 161, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("", 20, x, y, WHITE)
.addComponent<StatComponent>([&player](Drawable2DComponent &drawble) {
const PlayerBonusComponent *bonus = player.tryGetComponent<PlayerBonusComponent>();
if (!bonus)
return;
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(drawble.drawable.get());
if (!text)
return;
text->setText(bonus->isNoClipOn ? "YES" : "NO");
});
playerCount++;
}
Runner::gameState._loadedScenes[GameState::SceneID::GameScene] = scene;
@@ -7,6 +7,12 @@
#include "System/MenuControllable/MenuControllableSystem.hpp"
#include "Component/Controllable/ControllableComponent.hpp"
#include "Entity/Entity.hpp"
#include "Drawables/Texture.hpp"
#include "Drawables/2D/Text.hpp"
#include "Controllers/Mouse.hpp"
namespace RAYControl = RAY::Controller;
namespace RAY2D = RAY::Drawables::Drawables2D;
namespace BBM
{
@@ -45,14 +51,41 @@ namespace BBM
this->_currentButton->getComponent<OnClickComponent>().onEvent(*this->_currentButton, this->_wal);
}
void MenuControllableSystem::onFixedUpdate(WAL::ViewEntity<ControllableComponent> &entity)
bool MenuControllableSystem::_mouseOnButton(WAL::ViewEntity<OnClickComponent, OnHoverComponent, OnIdleComponent, PositionComponent, Drawable2DComponent> &entity) const
{
auto &controllable = entity.get<ControllableComponent>();
auto &buttons = _wal.getScene()->view<OnClickComponent, OnHoverComponent, OnIdleComponent>();
auto &positionComponent = entity.get<PositionComponent>();
RAY::Vector2 rayMousePos = RAYControl::Mouse::getCursorPosition();
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.get<Drawable2DComponent>().drawable.get());
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(entity.get<Drawable2DComponent>().drawable.get());
Vector2f buttonPos(positionComponent.getX(), positionComponent.getY());
Vector2f mousePos(rayMousePos.x, rayMousePos.y);
Vector2f dimensions;
WAL::Entity *newButton = nullptr;
if (texture) {
dimensions.x = texture->getDimensions().x;
dimensions.y = texture->getDimensions().y;
} else if (text) {
dimensions.y = text->getFontSize();
dimensions.x = text->getString().size() * (text->getLetterSpacing() + text->getFontSize());
} else
return false;
return ((buttonPos.x <= mousePos.x && mousePos.x <= buttonPos.x + dimensions.x)
&& (buttonPos.y <= mousePos.y && mousePos.y <= buttonPos.y + dimensions.y));
}
void MenuControllableSystem::onSelfUpdate()
{
auto &controllableView = this->_wal.getScene()->view<ControllableComponent>();
auto &buttons = _wal.getScene()->view<OnClickComponent, OnHoverComponent, OnIdleComponent, PositionComponent, Drawable2DComponent>();
if (this->_currentButton && this->_currentButton->_scene.getID() != this->_wal.getScene()->getID()) {
this->_currentButton->getComponent<OnIdleComponent>().onEvent(*this->_currentButton, this->_wal);
this->_currentButton = nullptr;
return;
}
if (this->_currentButton == nullptr && buttons.size()) {
this->_currentButton = &(*buttons.front());
@@ -60,6 +93,20 @@ namespace BBM
}
if (!this->_currentButton)
return;
this->_updateCurrentButton(controllable.select, controllable.move);
for (auto &[_, controllable]: controllableView)
if (controllable.move.x || controllable.move.y || controllable.select) {
this->_updateCurrentButton(controllable.select, controllable.move);
return;
}
for (auto &entity: buttons) {
if (_mouseOnButton(entity)) {
if (this->_currentButton)
this->_currentButton->getComponent<OnIdleComponent>().onEvent(*this->_currentButton, this->_wal);
this->_currentButton = &(*entity);
this->_currentButton->getComponent<OnHoverComponent>().onEvent(*this->_currentButton, this->_wal);
if (RAYControl::Mouse::isPressed(RAYControl::Mouse::Button::MOUSE_BUTTON_LEFT))
this->_currentButton->getComponent<OnClickComponent>().onEvent(*this->_currentButton, this->_wal);
}
}
}
}
@@ -7,11 +7,14 @@
#include "Component/Controllable/ControllableComponent.hpp"
#include "Models/Vector2.hpp"
#include "System/System.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Component/Button/ButtonComponent.hpp"
namespace BBM
{
//! @brief A system to handle Controllable entities in a menu.
class MenuControllableSystem : public WAL::System<ControllableComponent>
class MenuControllableSystem : public WAL::System<>
{
private:
//! @brief index of the current button selected
@@ -21,11 +24,13 @@ namespace BBM
//! @param selected lets know if te new selected button is 'pressed'
void _updateCurrentButton(bool selected, Vector2f move);
//! @return true if mouse on entity
bool _mouseOnButton(WAL::ViewEntity<OnClickComponent, OnHoverComponent, OnIdleComponent, PositionComponent, Drawable2DComponent> &entity) const;
public:
//! @brief time (in millisecond) since last check
std::chrono::time_point<std::chrono::steady_clock> now;
//! @inherit
void onFixedUpdate(WAL::ViewEntity<ControllableComponent> &entities) override;
void onSelfUpdate() override;
//! @brief A default constructor
explicit MenuControllableSystem(WAL::Wal &wal);
+1
View File
@@ -4,6 +4,7 @@
#include "TimerSystem.hpp"
#include "Component/Timer/TimerComponent.hpp"
#include "Drawables/2D/Text.hpp"
using namespace std::chrono_literals;
+1
View File
@@ -7,6 +7,7 @@
#include <System/System.hpp>
#include <Wal.hpp>
#include <Component/Timer/TimerComponent.hpp>
#include "Component/Renderer/Drawable2DComponent.hpp"
namespace BBM
{
+30
View File
@@ -0,0 +1,30 @@
//
// Created by Zoe Roux on 5/31/21.
//
#include "TimerUISystem.hpp"
#include "Component/Timer/TimerComponent.hpp"
#include "Drawables/2D/Text.hpp"
using namespace std::chrono_literals;
namespace RAY2D = RAY::Drawables::Drawables2D;
namespace BBM
{
TimerUISystem::TimerUISystem(WAL::Wal &wal)
: System(wal)
{}
void TimerUISystem::onUpdate(WAL::ViewEntity<TimerComponent, Drawable2DComponent> &entity, std::chrono::nanoseconds dtime)
{
auto &timer = entity.get<TimerComponent>();
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(entity.get<Drawable2DComponent>().drawable.get());
if (text) {
unsigned long second = std::chrono::duration_cast<std::chrono::seconds>(timer.ringIn).count();
unsigned long minutes = second / 60;
second = second % 60;
text->setText(std::to_string(minutes) + ":" + std::to_string(second));
}
}
}
+29
View File
@@ -0,0 +1,29 @@
//
// Created by Zoe Roux on 5/31/21.
//
#pragma once
#include <System/System.hpp>
#include <Wal.hpp>
#include <Component/Timer/TimerComponent.hpp>
#include "Component/Renderer/Drawable2DComponent.hpp"
namespace BBM
{
class TimerUISystem : public WAL::System<TimerComponent, Drawable2DComponent>
{
public:
//! @inherit
void onUpdate(WAL::ViewEntity<TimerComponent, Drawable2DComponent> &entity, std::chrono::nanoseconds dtime) override;
//! @brief A default constructor
TimerUISystem(WAL::Wal &);
//! @brief A timer system is copy constructable.
TimerUISystem(const TimerUISystem &) = default;
//! @brief A default destructor
~TimerUISystem() override = default;
//! @breief A timer system is assignable.
TimerUISystem &operator=(const TimerUISystem &) = default;
};
}