Allowing the game to be started

This commit is contained in:
Zoe Roux
2021-06-14 13:36:11 +02:00
parent a42ca68595
commit 60a5ad0ded
7 changed files with 86 additions and 23 deletions
+2 -2
View File
@@ -19,7 +19,7 @@ namespace RAY {
public:
//! @brief A Model animation constructor
//! @param filePath Path to the file containing animations
ModelAnimations(const std::string &filePath);
explicit ModelAnimations(const std::string &filePath);
//! @brief default copy ctor
ModelAnimations(const ModelAnimations &) = default;
@@ -59,7 +59,7 @@ namespace RAY {
int _animationCount;
//! @brief The file where the animations were loaded (used to create a copy of this class)
const std::string _filePath;
std::string _filePath;
static Cache<::ModelAnimation> _animationsCache;
};
@@ -8,9 +8,9 @@
namespace BBM
{
AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex, bool play)
AnimationsComponent::AnimationsComponent(WAL::Entity &entity, const RAY::ModelAnimations &modelAnimation, int animIndex, bool play)
: WAL::Component(entity),
_modelAnimation(std::move(modelAnimation)),
_modelAnimation(modelAnimation),
_currentAnimIndex(animIndex),
_animDisabled(play)
{
@@ -52,7 +52,7 @@ namespace BBM
bool isAnimDisabled() const;
//! @brief ctor entity and the path of the animation file
explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex, bool play = true);
explicit AnimationsComponent(WAL::Entity &entity, const RAY::ModelAnimations& modelAnimation, int animIndex, bool play = true);
//! @brief copy ctor
AnimationsComponent(const AnimationsComponent &) = default;
//! @brief dtor
+19 -18
View File
@@ -260,12 +260,10 @@ namespace BBM
})
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &wal)
{
if (gameState.currentScene != GameState::LobbyScene)
if (Runner::gameState.currentScene != GameState::LobbyScene
|| !LobbySystem::playersAreReady(*wal.getScene()))
return;
if (!LobbySystem::playersAreReady(*wal.getScene()))
return;
gameState._loadedScenes[GameState::SceneID::GameScene] = loadGameScene();
gameState.nextScene = BBM::GameState::SceneID::GameScene;
LobbySystem::switchToGame(wal);
})
.addComponent<TagComponent<"PlayButton">>();
@@ -637,21 +635,30 @@ namespace BBM
std::shared_ptr<WAL::Scene> Runner::loadGameScene()
{
auto scene = std::make_shared<WAL::Scene>();
scene->addEntity("camera")
.addComponent<PositionComponent>(8, 20, 7)
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
return scene;
}
WAL::Entity &Runner::createPlayer(WAL::Scene &scene)
{
std::map<SoundComponent::SoundIndex, std::string> soundPath ={
{SoundComponent::JUMP, "assets/sounds/jump.wav"},
{SoundComponent::MOVE, "assets/sounds/move.ogg"},
{SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"},
//{SoundComponent::DEATH, "assets/sounds/death.ogg"}
{SoundComponent::JUMP, "assets/sounds/jump.wav"},
{SoundComponent::MOVE, "assets/sounds/move.ogg"},
{SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"},
//{SoundComponent::DEATH, "assets/sounds/death.ogg"}
};
scene->addEntity("player")
return scene.addEntity("player")
.addComponent<PositionComponent>()
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/textures/blue.png"))
.addComponent<ControllableComponent>()
.addComponent<AnimatorComponent>()
.addComponent<KeyboardComponent>()
.addComponent<ShaderComponentModel>("assets/shaders/glsl330/predator.fs")
.addComponent<TagComponent<Blowable>>()
//.addComponent<GamepadComponent>(0)
.addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 3)
.addComponent<CollisionComponent>(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75})
.addComponent<MovableComponent>()
@@ -662,12 +669,6 @@ namespace BBM
auto &animation = entity.getComponent<AnimationsComponent>();
animation.setAnimIndex(5);
});
scene->addEntity("camera")
.addComponent<PositionComponent>(8, 20, 7)
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
return scene;
}
std::shared_ptr<WAL::Scene> Runner::loadCreditScene()
+5
View File
@@ -51,6 +51,11 @@ namespace BBM
//! @brief load all data related to lobby screen
static std::shared_ptr<WAL::Scene> loadLobbyScene();
//! @brief Create a player (without any controllable) and add it to the scene.
//! @param scene The scene where to player should reside.
//! @return A reference to the created player.
static WAL::Entity &createPlayer(WAL::Scene &scene);
//! @brief loads all scenes in the game state
static void loadScenes();
};
+48
View File
@@ -10,6 +10,10 @@
#include "System/MenuControllable/MenuControllableSystem.hpp"
#include "Component/Tag/TagComponent.hpp"
#include <algorithm>
#include <Runner/Runner.hpp>
#include <Component/Keyboard/KeyboardComponent.hpp>
#include <Component/Gamepad/GamepadComponent.hpp>
#include <Component/Position/PositionComponent.hpp>
namespace BBM
{
@@ -78,4 +82,48 @@ namespace BBM
return lobbyPlayer.ready || lobbyPlayer.layout == ControllableComponent::NONE;
});
}
void LobbySystem::_addController(WAL::Entity &player, ControllableComponent::Layout layout)
{
switch (layout) {
case ControllableComponent::KEYBOARD_0:
case ControllableComponent::KEYBOARD_1:
player.addComponent<KeyboardComponent>(layout);
break;
case ControllableComponent::GAMEPAD_0:
player.addComponent<GamepadComponent>(0);
break;
case ControllableComponent::GAMEPAD_1:
player.addComponent<GamepadComponent>(1);
break;
case ControllableComponent::GAMEPAD_2:
player.addComponent<GamepadComponent>(2);
break;
case ControllableComponent::GAMEPAD_3:
player.addComponent<GamepadComponent>(3);
break;
default:
throw std::runtime_error("Invalid controller for a player.");
}
}
void LobbySystem::switchToGame(WAL::Wal &wal)
{
auto scene = Runner::loadGameScene();
int mapWidth = 16;
int mapHeight = 16;
int playerCount = 0;
for (auto &[_, lobby] : wal.getScene()->view<LobbyComponent>()) {
if (lobby.layout == ControllableComponent::NONE)
continue;
auto &player = Runner::createPlayer(*scene);
_addController(player, lobby.layout);
player.getComponent<PositionComponent>().position = Vector3f(mapWidth * playerCount % 2, 0,
static_cast<int>(mapHeight * playerCount / 2));
playerCount++;
}
Runner::gameState._loadedScenes[GameState::SceneID::GameScene] = scene;
Runner::gameState.nextScene = BBM::GameState::SceneID::GameScene;
}
}
+9
View File
@@ -6,12 +6,17 @@
#include "System/System.hpp"
#include "Component/Lobby/LobbyComponent.hpp"
#include "Component/Controllable/ControllableComponent.hpp"
#include "Entity/Entity.hpp"
namespace BBM
{
//! @brief A system to handle Health entities.
class LobbySystem : public WAL::System<LobbyComponent, Drawable2DComponent>
{
private:
//! @brief Add a controller for the player.
static void _addController(WAL::Entity &player, ControllableComponent::Layout layout);
public:
//! @inherit
void onUpdate(WAL::ViewEntity<LobbyComponent, Drawable2DComponent> &entity, std::chrono::nanoseconds dtime) override;
@@ -23,6 +28,10 @@ namespace BBM
//! @param scene The lobby scene containing lobby players.
static bool playersAreReady(WAL::Scene &scene);
//! @brief Inform the engine that the next scene should be the game scene and load it.
//! @param wal The engine.
static void switchToGame(WAL::Wal &wal);
//! @brief A default constructor
explicit LobbySystem(WAL::Wal &wal);
//! @brief A Lobby system is copy constructable