Fixing play button and menu handling

This commit is contained in:
Zoe Roux
2021-06-14 11:39:00 +02:00
parent de579ccda6
commit a42ca68595
6 changed files with 55 additions and 42 deletions
+2 -3
View File
@@ -74,13 +74,12 @@ namespace WAL
//! @brief A default constructor
Scene() = default;
//! @brief A scene is copy constructable
Scene(const Scene &) = default;
//! @brief A scene is not copy constructable
Scene(const Scene &) = delete;
//! @brief A default destructor
~Scene() = default;
//! @brief A scene is assignable
Scene &operator=(const Scene &);
Scene(Scene &&) = default;
friend Entity;
};
+8 -30
View File
@@ -7,12 +7,8 @@
#include "System/Movable/MovableSystem.hpp"
#include "System/Renderer/RenderSystem.hpp"
#include <Model/Model.hpp>
#include <Drawables/3D/Cube.hpp>
#include <Drawables/2D/Rectangle.hpp>
#include <Drawables/2D/Text.hpp>
#include <Audio/Music.hpp>
#include <Audio/Sound.hpp>
#include <Drawables/2D/Rectangle.hpp>
#include <TraceLog.hpp>
#include "System/Keyboard/KeyboardSystem.hpp"
#include "System/Controllable/ControllableSystem.hpp"
@@ -22,7 +18,6 @@
#include "System/Gamepad/GamepadSystem.hpp"
#include <System/Collision/CollisionSystem.hpp>
#include "Component/Button/ButtonComponent.hpp"
#include <Component/Movable/MovableComponent.hpp>
#include <Component/Collision/CollisionComponent.hpp>
#include "Component/Renderer/CameraComponent.hpp"
#include "Component/Renderer/Drawable3DComponent.hpp"
@@ -35,7 +30,6 @@
#include <System/Event/EventSystem.hpp>
#include <System/Health/HealthSystem.hpp>
#include <System/Animator/AnimatorSystem.hpp>
#include <Component/Renderer/Drawable2DComponent.hpp>
#include <Component/Animator/AnimatorComponent.hpp>
#include <Component/Tag/TagComponent.hpp>
#include "Component/Animation/AnimationsComponent.hpp"
@@ -62,10 +56,6 @@ namespace BBM
void Runner::updateState(WAL::Wal &engine, GameState &state)
{
auto &view = engine.getScene()->view<ControllableComponent>();
// You can change the scene here or update the game state based on entities values.
// If you want to keep a scene loaded but not running, store it in the state.loadedScenes.
// If you don't need the scene anymore, remember to remove it from the loadedScene array.
if (RAY::Window::getInstance().shouldClose())
engine.shouldClose = true;
if (gameState.currentScene == GameState::SceneID::GameScene) {
@@ -88,6 +78,7 @@ namespace BBM
wal.addSystem<TimerSystem>()
.addSystem<KeyboardSystem>()
.addSystem<GamepadSystem>()
.addSystem<LobbySystem>()
.addSystem<MenuControllableSystem>()
.addSystem<ControllableSystem>()
.addSystem<BombHolderSystem>()
@@ -97,7 +88,6 @@ namespace BBM
.addSystem<MovableSystem>()
.addSystem<PlayerSoundManagerSystem>()
.addSystem<MenuSoundManagerSystem>()
.addSystem<LobbySystem>()
.addSystem<MusicSystem>();
}
@@ -260,36 +250,24 @@ namespace BBM
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_new_game.png")
.addComponent<OnIdleComponent>([](WAL::Entity &entity, WAL::Wal &wal)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
auto *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_new_game.png");
auto &lobby = wal.getScene()->view<LobbyComponent>();
if (std::any_of(lobby.begin(), lobby.end(), [](WAL::ViewEntity<LobbyComponent> &entity) {
return !entity.get<LobbyComponent>().ready;
}))
texture->setColor(GRAY);
})
.addComponent<OnHoverComponent>([](WAL::Entity &entity, WAL::Wal &wal)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
auto *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_new_game_hovered.png");
auto &lobby = wal.getScene()->view<LobbyComponent>();
if (std::any_of(lobby.begin(), lobby.end(), [](WAL::ViewEntity<LobbyComponent> &entity) {
return !entity.get<LobbyComponent>().ready;
}))
texture->setColor(GRAY);
})
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &wal)
{
auto &lobby = wal.getScene()->view<LobbyComponent>();
if (std::any_of(lobby.begin(), lobby.end(), [](WAL::ViewEntity<LobbyComponent> &entity) {
return !entity.get<LobbyComponent>().ready;
}))
if (gameState.currentScene != GameState::LobbyScene)
return;
if (!LobbySystem::playersAreReady(*wal.getScene()))
return;
gameState._loadedScenes[GameState::SceneID::GameScene] = loadGameScene();
gameState.nextScene = BBM::GameState::SceneID::GameScene;
});
})
.addComponent<TagComponent<"PlayButton">>();
auto &back = scene->addEntity("back to menu")
+34 -4
View File
@@ -2,11 +2,13 @@
// Created by Zoe Roux on 6/11/21.
//
#include <Component/Animation/AnimationsComponent.hpp>
#include <System/Event/EventSystem.hpp>
#include <Component/Renderer/Drawable2DComponent.hpp>
#include "Component/Animation/AnimationsComponent.hpp"
#include "System/Event/EventSystem.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "System/Lobby/LobbySystem.hpp"
#include "Component/Controllable/ControllableComponent.hpp"
#include "System/MenuControllable/MenuControllableSystem.hpp"
#include "Component/Tag/TagComponent.hpp"
#include <algorithm>
namespace BBM
@@ -32,6 +34,7 @@ namespace BBM
return;
lobby.lastInput = lastTick;
lobby.layout = controller.layout;
controller.jump = false;
entity.get<Drawable2DComponent>().drawable = std::make_shared<RAY::Texture>("assets/player/icons/blue.png");
return;
}
@@ -39,13 +42,40 @@ namespace BBM
}
for (auto &[_, controller] : this->_wal.getScene()->view<ControllableComponent>()) {
if (controller.layout == lobby.layout && controller.jump) {
if (controller.layout == lobby.layout && controller.jump && !lobby.ready) {
lobby.ready = true;
lobby.lastInput = lastTick;
controller.jump = false;
this->_wal.getSystem<MenuControllableSystem>().now = lastTick;
auto *texture = dynamic_cast<RAY::Texture *>(lobby.readyButton.getComponent<Drawable2DComponent>().drawable.get());
if (texture)
texture->use("assets/player/icons/ready.png");
}
}
}
void LobbySystem::onSelfUpdate()
{
auto &view = this->_wal.getScene()->view<TagComponent<"PlayButton">, Drawable2DComponent>();
if (view.size() == 0)
return;
auto *texture = dynamic_cast<RAY::Texture *>(view.front().get<Drawable2DComponent>().drawable.get());
if (!texture)
return;
if (playersAreReady(*this->_wal.getScene()))
texture->setColor(WHITE);
else
texture->setColor(GRAY);
}
bool LobbySystem::playersAreReady(WAL::Scene &scene)
{
auto &lobby = scene.view<LobbyComponent>();
return std::all_of(lobby.begin(), lobby.end(), [](WAL::ViewEntity<LobbyComponent> &entity) {
auto &lobbyPlayer = entity.get<LobbyComponent>();
return lobbyPlayer.ready || lobbyPlayer.layout == ControllableComponent::NONE;
});
}
}
+7
View File
@@ -16,6 +16,13 @@ namespace BBM
//! @inherit
void onUpdate(WAL::ViewEntity<LobbyComponent, Drawable2DComponent> &entity, std::chrono::nanoseconds dtime) override;
//! @inherit
void onSelfUpdate() override;
//! @brief Check if every player is ready.
//! @param scene The lobby scene containing lobby players.
static bool playersAreReady(WAL::Scene &scene);
//! @brief A default constructor
explicit LobbySystem(WAL::Wal &wal);
//! @brief A Lobby system is copy constructable
@@ -4,7 +4,6 @@
#include <algorithm>
#include "Component/Button/ButtonComponent.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "System/MenuControllable/MenuControllableSystem.hpp"
#include "Component/Controllable/ControllableComponent.hpp"
#include "Entity/Entity.hpp"
@@ -32,9 +31,9 @@ namespace BBM
if (newButton || selected) {
auto lastTick = std::chrono::steady_clock::now();
if (lastTick - this->_now < std::chrono::milliseconds(150))
if (lastTick - this->now < std::chrono::milliseconds(150))
return;
this->_now = lastTick;
this->now = lastTick;
}
if (newButton) {
@@ -21,9 +21,9 @@ namespace BBM
//! @param selected lets know if te new selected button is 'pressed'
void _updateCurrentButton(bool selected, Vector2f move);
//! @brief time (in millisecond) since last check
std::chrono::time_point<std::chrono::steady_clock> _now;
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;