add ui on game

This commit is contained in:
arthur.jamet
2021-06-15 15:36:55 +02:00
21 changed files with 287 additions and 8 deletions
+7
View File
@@ -134,8 +134,15 @@ set(SOURCES
sources/Runner/PauseMenuScene.cpp
sources/Runner/SettingsMenuScene.cpp
sources/Runner/CreditScene.cpp
sources/Component/Score/ScoreComponent.cpp
sources/Component/Score/ScoreComponent.hpp
sources/System/Score/ScoreSystem.cpp
sources/System/Score/ScoreSystem.hpp
sources/System/EndCondition/EndConditionSystem.hpp
sources/System/EndCondition/EndConditionSystem.cpp
sources/Runner/LobbyScene.cpp
sources/Runner/HowToPlayScene.cpp
sources/Runner/ScoreScene.cpp
)
add_executable(bomberman
sources/main.cpp
Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

+5
View File
@@ -42,6 +42,11 @@ namespace RAY {
return *this;
}
const std::string &Texture::getResourcePath() const
{
return this->_resourcePath;
}
Texture::operator ::Texture() const
{
return *this->_texture;
+3
View File
@@ -44,6 +44,9 @@ namespace RAY
//! @brief Load texture from file, lets one use one entity for multiple files
Texture &use(const std::string &filename);
//! @return path of loaded texture
const std::string &getResourcePath() const;
protected:
private:
//! @brief Texture, really, that's just it...
+5
View File
@@ -61,6 +61,11 @@ namespace RAY::Drawables::Drawables3D
return true;
}
Texture &Model::getTextureByMaterial(MaterialType materialType)
{
return this->_textureList[materialType];
}
Model::operator ::Model() const
{
return *this->_model;
+4
View File
@@ -90,6 +90,10 @@ namespace RAY::Drawables::Drawables3D {
//! @brief Draw model's wires on window
void drawWiresOn(RAY::Window &) override;
//! @param materialType type of material
//! @return texture
Texture &getTextureByMaterial(MaterialType materialType);
private:
//! @brief Raw data from raylib
std::shared_ptr<::Model> _model;
@@ -0,0 +1,16 @@
#include "ScoreComponent.hpp"
namespace BBM
{
ScoreComponent::ScoreComponent(WAL::Entity &entity)
: Component(entity),
aliveTime(std::chrono::nanoseconds::zero())
{}
WAL::Component *ScoreComponent::clone(WAL::Entity &entity) const
{
return new ScoreComponent(entity);
}
} // namespace WAL
@@ -0,0 +1,28 @@
#pragma once
#include "Component/Component.hpp"
#include <chrono>
namespace BBM
{
//! @brief A basic position component
class ScoreComponent : public WAL::Component
{
public:
//! @brief the score of the player
std::chrono::nanoseconds aliveTime;
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Create a new ScoreComponent linked to a specific entity
explicit ScoreComponent(WAL::Entity &entity);
//! @brief A position component is copy constructable
ScoreComponent(const ScoreComponent &) = default;
//! @brief A default destructor
~ScoreComponent() override = default;
//! @brief A position component is not assignable
ScoreComponent &operator=(const ScoreComponent &) = delete;
};
} // namespace WAL
+1
View File
@@ -27,6 +27,7 @@ namespace BBM
TitleScreenScene,
CreditScene,
HowToPlayScene,
ScoreScene,
};
+11 -8
View File
@@ -17,14 +17,18 @@
#include "Component/Shaders/ShaderComponent.hpp"
#include "Component/Tag/TagComponent.hpp"
#include "Component/Renderer/Drawable3DComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Component/Button/ButtonComponent.hpp"
#include "Drawables/2D/Text.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"
namespace RAY3D = RAY::Drawables::Drawables3D;
namespace RAY2D = RAY::Drawables::Drawables2D;
namespace BBM
{
@@ -34,6 +38,10 @@ namespace BBM
scene->addEntity("camera")
.addComponent<PositionComponent>(8, 20, 7)
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
scene->addEntity("Timer")
.addComponent<TimerComponent>(std::chrono::seconds(60), [](WAL::Entity &, WAL::Wal &) {
Runner::gameState.nextScene = GameState::ScoreScene;
});
MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
return scene;
}
@@ -47,10 +55,11 @@ namespace BBM
//{SoundComponent::DEATH, "assets/sounds/death.ogg"}
};
auto &player = scene.addEntity("player")
return scene.addEntity("player")
.addComponent<PositionComponent>()
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", true)
.addComponent<ControllableComponent>()
.addComponent<ScoreComponent>()
.addComponent<AnimatorComponent>()
.addComponent<GravityComponent>()
.addComponent<BumperTimerComponent>()
@@ -67,11 +76,5 @@ namespace BBM
auto &animation = entity.getComponent<AnimationsComponent>();
animation.setAnimIndex(5);
});
RAY3D::Model *model = dynamic_cast<RAY3D::Model *>(player.getComponent<Drawable3DComponent>().drawable.get());
//std::string texturePath = model->get
auto &player = scene.addEntity("player")
.addComponent<PositionComponent>()
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", true)
return player;
}
}
+6
View File
@@ -34,6 +34,8 @@
#include "System/BumperTimer/BumperTimerSystem.hpp"
#include "System/Music/MusicSystem.hpp"
#include "System/Lobby/LobbySystem.hpp"
#include "System/Score/ScoreSystem.hpp"
#include "System/EndCondition/EndConditionSystem.hpp"
#include "Component/Lobby/LobbyComponent.hpp"
namespace BBM
@@ -58,6 +60,8 @@ namespace BBM
}
if (gameState.nextScene == gameState.currentScene)
return;
if (gameState.nextScene == GameState::SceneID::ScoreScene)
gameState._loadedScenes[GameState::SceneID::ScoreScene] = Runner::loadScoreScene(*engine.getScene());
gameState._loadedScenes[gameState.currentScene] = engine.getScene();
engine.changeScene(gameState._loadedScenes[gameState.nextScene]);
gameState.currentScene = gameState.nextScene;
@@ -84,6 +88,8 @@ namespace BBM
.addSystem<IntroAnimationSystem>()
.addSystem<GravitySystem>()
.addSystem<BumperTimerSystem>()
.addSystem<EndConditionSystem>()
.addSystem<ScoreSystem>()
.addSystem<MusicSystem>();
}
+4
View File
@@ -61,6 +61,10 @@ namespace BBM
//! @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);
//! @brief loads all scenes in the game state
static void loadScenes();
+93
View File
@@ -0,0 +1,93 @@
#include <Wal.hpp>
#include "Runner.hpp"
#include <map>
#include "Component/Button/ButtonComponent.hpp"
#include "Component/Music/MusicComponent.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Component/Renderer/Drawable3DComponent.hpp"
#include "Component/Sound/SoundComponent.hpp"
#include "Drawables/Texture.hpp"
#include "Drawables/2D/Text.hpp"
#include "Component/Score/ScoreComponent.hpp"
#include "Model/Model.hpp"
namespace RAY2D = RAY::Drawables::Drawables2D;
namespace RAY3D = RAY::Drawables::Drawables3D;
namespace BBM
{
std::shared_ptr<WAL::Scene> Runner::loadScoreScene(WAL::Scene &gameScene)
{
auto scene = std::make_shared<WAL::Scene>();
std::vector<std::string> playersIconPath;
std::vector<std::reference_wrapper<WAL::Entity>>players;
static const std::map<SoundComponent::SoundIndex, std::string> sounds = {
{SoundComponent::JUMP, "assets/sounds/click.ogg"}
};
static const std::vector<RAY::Color> tilesColor = {
GOLD, GRAY, BROWN, PURPLE
};
static const std::vector<std::string> rankName = {
"1st", "2nd", "3rd", "4th"
};
for (auto &[entity, score, drawable]: gameScene.view<ScoreComponent, Drawable3DComponent>())
players.push_back(entity);
std::sort(players.begin(), players.end(), [](WAL::Entity &entityA, WAL::Entity &entityB) {
return entityA.getComponent<ScoreComponent>().aliveTime > entityB.getComponent<ScoreComponent>().aliveTime;
});
for (auto &entity: players) {
RAY3D::Model *model = dynamic_cast<RAY3D::Model *>(entity.get().getComponent<Drawable3DComponent>().drawable.get());
std::string path = model->getTextureByMaterial(MAP_DIFFUSE).getResourcePath();
playersIconPath.push_back(path.replace(path.find("textures"), std::string("textures").size(), "icons"));
}
addMenuControl(*scene);
scene->addEntity("Audio ressources")
.addComponent<MusicComponent>("assets/musics/music_result.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.25, 100, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("GAME OVER", 120, RAY::Vector2(), ORANGE);
scene->addEntity("scene title text")
.addComponent<PositionComponent>(1920 / 2.37, 250, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("CONGRATS", 50, RAY::Vector2(), ORANGE);
for (int i = 0; i < players.size(); i++) {
auto &playerTile = scene->addEntity("player tile")
.addComponent<PositionComponent>(224 * (i + 1) + 200 * i, 1080 / 2.5, 0)
.addComponent<Drawable2DComponent, RAY2D::Rectangle>(RAY::Vector2(224 * (i + 1) + 200 * i, 1080 / 3), RAY::Vector2(200, 200),tilesColor[i]);
auto &playerRank = scene->addEntity("player rank name")
.addComponent<PositionComponent>(224 * (i + 1) + 200 * i, 1080 / 2.75, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>(rankName[i], 30, RAY::Vector2(224 * (i + 1) + 200 * i, 1080 / 3), tilesColor[i]);
auto &player = scene->addEntity("player")
.addComponent<PositionComponent>(224 * (i + 1) + 200 * i, 1080 / 2.5, 0)
.addComponent<Drawable2DComponent, RAY::Texture>(playersIconPath[i]);
}
scene->addEntity("back to main 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::MainMenuScene;
})
.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;
}
}
@@ -0,0 +1,25 @@
#include "EndConditionSystem.hpp"
#include <map>
#include "Runner/Runner.hpp"
#include "Component/Score/ScoreComponent.hpp"
namespace BBM {
EndConditionSystem::EndConditionSystem(WAL::Wal &wal)
: System(wal)
{}
void EndConditionSystem::onSelfUpdate()
{
unsigned int alivePlayersCount = 0;
auto &view = this->_wal.getScene()->view<ScoreComponent, HealthComponent>();
if (!view.size())
return;
for (auto & [_ , scoreComponent, healthComponent]: view)
alivePlayersCount += (healthComponent.getHealthPoint() != 0);
if (alivePlayersCount <= 1)
Runner::gameState.nextScene = Runner::gameState.ScoreScene;
}
}
@@ -0,0 +1,26 @@
#pragma once
#include "System/System.hpp"
#include "Component/Score/ScoreComponent.hpp"
#include "Component/Health/HealthComponent.hpp"
#include "Wal.hpp"
namespace BBM
{
class EndConditionSystem : public WAL::System<ScoreComponent, HealthComponent>
{
public:
//! @inherit
void onSelfUpdate() override;
//! @brief ctor
EndConditionSystem(WAL::Wal &wal);
//! @brief Default copy ctor
EndConditionSystem(const EndConditionSystem &) = default;
//! @brief Default dtor
~EndConditionSystem() override = default;
//! @brief A SoundManager screen system can't be assigned.
EndConditionSystem &operator=(const EndConditionSystem &) = delete;
};
}
+8
View File
@@ -154,8 +154,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);
@@ -165,6 +167,12 @@ namespace BBM
mapHeight * ((playerCount + 1) % 2));
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 tile")
.addComponent<PositionComponent>(x, y, 0)
.addComponent<Drawable2DComponent, RAY::Texture>(texturePath);
playerCount++;
}
Runner::gameState._loadedScenes[GameState::SceneID::GameScene] = scene;
+19
View File
@@ -0,0 +1,19 @@
//
// Created by Tom Augier on 05/06/2021
//
#include "ScoreSystem.hpp"
#include <map>
namespace BBM {
ScoreSystem::ScoreSystem(WAL::Wal &wal)
: System(wal)
{}
void ScoreSystem::onUpdate(WAL::ViewEntity<ScoreComponent, HealthComponent> &entity, std::chrono::nanoseconds dtime)
{
if (entity.get<HealthComponent>().getHealthPoint())
entity.get<ScoreComponent>().aliveTime += dtime;
}
}
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include "System/System.hpp"
#include "Component/Score/ScoreComponent.hpp"
#include "Component/Health/HealthComponent.hpp"
#include "Wal.hpp"
namespace BBM
{
class ScoreSystem : public WAL::System<ScoreComponent, HealthComponent>
{
public:
//! @inherit
void onUpdate(WAL::ViewEntity<ScoreComponent, HealthComponent> &entity, std::chrono::nanoseconds dtime) override;
//! @brief ctor
ScoreSystem(WAL::Wal &wal);
//! @brief Default copy ctor
ScoreSystem(const ScoreSystem &) = default;
//! @brief Default dtor
~ScoreSystem() override = default;
//! @brief A SoundManager screen system can't be assigned.
ScoreSystem &operator=(const ScoreSystem &) = delete;
};
}