just need to test, but need to merge before

This commit is contained in:
EternalRat
2021-06-14 15:18:28 +02:00
parent 8c035d404c
commit 8122a2f31d
8 changed files with 179 additions and 72 deletions
+4 -2
View File
@@ -109,10 +109,12 @@ set(SOURCES
sources/System/Sound/PlayerSoundManagerSystem.hpp
sources/System/Music/MusicSystem.hpp
sources/System/Music/MusicSystem.cpp
sources/System/Bomb/BombSystem.cpp
sources/System/Bomb/BombSystem.hpp
sources/Parser/ParserYaml.hpp
sources/Parser/ParserYaml.cpp
sources/Exception/Error.hpp
sources/Exception/Error.cpp
sources/System/Bomb/BombSystem.cpp
sources/System/Bomb/BombSystem.hpp
)
add_executable(bomberman
sources/main.cpp
+1 -1
View File
@@ -11,6 +11,6 @@ namespace BBM
{}
ParserError::ParserError(const std::string &what)
: WalError(what)
: Error(what)
{}
} // namespace BBM
+1 -1
View File
@@ -2,7 +2,7 @@
// Created by hbenjamin on 11/06/2021.
//
#pramga once
#pragma once
#include <exception>
#include <stdexcept>
-5
View File
@@ -15,11 +15,6 @@ namespace BBM {
//! @brief Apply bonus effect that allows players to carry one more bomb than before
static void BombUpBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis);
//! @param bonus bonus
//! @param player the entity on which the effect will be applied
//! @brief Apply bonus effect who increased the bomb damage
static void DamageIncreasedBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis);
//! @param bonus bonus
//! @param player the entity on which the effect will be applied
//! @brief Apply bonus effect that expend the explosion range of the bomb
+5 -5
View File
@@ -64,6 +64,11 @@ namespace BBM
//! @param scene Scene where the map is instanced
//! @brief Generate the map
static void loadMap(int width, int height, MapBlock map, const std::shared_ptr<WAL::Scene> &scene);
//! @param coords coords of the element
//! @param scene Scene where the map is instanced
//! @brief Create element of the map
static void createElement(Vector3f coords, std::shared_ptr<WAL::Scene> scene, BlockType blockType);
private:
using MapElem = std::function<void(Vector3f coords, std::shared_ptr<WAL::Scene> scene)>;
@@ -97,11 +102,6 @@ namespace BBM
//! @brief Generate the floor of the map
static void generateFloor(MapBlock map, int width, int height, std::shared_ptr<WAL::Scene> scene);
//! @param coords coords of the element
//! @param scene Scene where the map is instanced
//! @brief Create element of the map
static void createElement(Vector3f coords, std::shared_ptr<WAL::Scene> scene, BlockType blockType);
//! @param coords coords of the element
//! @param scene Scene where the map is instanced
//! @brief Create breakable of the map
+155 -53
View File
@@ -22,6 +22,7 @@
#include <Exception/Error.hpp>
#include "ParserYaml.hpp"
#include <algorithm>
#include <Component/Levitate/LevitateComponent.hpp>
namespace RAY3D = RAY::Drawables::Drawables3D;
@@ -131,55 +132,117 @@ namespace BBM {
_bonus.clear();
}
WAL::Entity &ParserYAML::_parseEntityName(std::string line, WAL::Entity &entity)
void ParserYAML::_loadPlayer(std::shared_ptr<WAL::Scene> scene, std::vector<std::string> lines, int &index)
{
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
auto name = line.substr(0, line.find(':'));
entity.setName(name);
return (entity);
auto &entity = scene->addEntity("");
static 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"}
};
int maxBomb = 0;
float explosionRadius = 0;
Vector3f pos;
for (; index != lines.size(); index++) {
if (lines[index].find("max_bomb") != std::string::npos) {
maxBomb = _parseMaxBomb(lines[index]);
} else if (lines[index].find("explosion_radius") != std::string::npos) {
explosionRadius = _parseExplosionRadius(lines[index]);
} else if (lines[index].find("position") != std::string::npos) {
pos = _parsePosition(lines[index]);
} else {
if (!entity.getName().empty()) {
break;
}
_parseEntityName(lines[index], entity);
}
}
entity.addComponent<PositionComponent>(pos)
.addComponent<BombHolderComponent>(maxBomb, explosionRadius)
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
.addComponent<ControllableComponent>()
.addComponent<AnimatorComponent>()
.addComponent<KeyboardComponent>()
.addComponent<ShaderComponentModel>("assets/shaders/glsl330/predator.fs")
.addComponent<TagComponent<Blowable>>()
.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>()
.addComponent<SoundComponent>(soundPath)
.addComponent<MusicComponent>("assets/musics/music_battle.ogg")
.addComponent<PlayerBonusComponent>()
.addComponent<HealthComponent>(1, [](WAL::Entity &entity, WAL::Wal &wal) {
auto &animation = entity.getComponent<AnimationsComponent>();
animation.setAnimIndex(5);
});
}
void ParserYAML::_loadPlayers(std::shared_ptr<WAL::Scene> scene, std::string filename)
{
std::ifstream file("save/" + filename + "_player.yml");
std::string line;
WAL::Entity entity;
std::vector<std::string> lines;
if (file.good()) {
while (std::getline(file, line)) {
if (line.find("max_bomb") != std::string::npos)
_parseMaxBomb(line, entity);
else if (line.find("explosion_radius") != std::string::npos)
_parseExplosionRadius(line, entity);
else if (line.find("position") != std::string::npos)
_parsePosition(line, entity);
else
_parseEntityName(line, entity);
if (!file.good())
return;
while (std::getline(file, line)) {
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
line.erase(std::remove(line.begin(), line.end(), '\t'), line.end());
if (line.empty() || !line.compare("players:"))
continue;
lines.push_back(line);
}
for (int index = 0; lines.size() != index; index++) {
_loadPlayer(scene, lines, index);
index--;
}
}
void ParserYAML::_loadBlock(std::shared_ptr<WAL::Scene> scene, std::vector<std::string> lines, int &index)
{
std::string tmpName = "";
Vector3f pos;
MapGenerator::BlockType blockType;
for (; index != lines.size(); index++) {
if (lines[index].find("position") != std::string::npos) {
pos = _parsePosition(lines[index]);
} else if (lines[index].find("block_type") != std::string::npos) {
blockType = _parseBlockType(lines[index]);
} else {
if (!tmpName.empty()) {
break;
}
tmpName = lines[index];
}
}
MapGenerator::createElement(pos, scene, blockType);
}
void ParserYAML::_loadBlocks(std::shared_ptr<WAL::Scene> scene, std::string filename)
{
std::ifstream file("save/" + filename + "block.yml");
WAL::Entity entity(*scene, "");
std::string line;
std::vector<std::string> lines;
if (file.good()) {
while (std::getline(file, line)) {
if (line.find("max_bomb") != std::string::npos)
_parseMaxBomb(line, entity);
else if (line.find("explosion_radius") != std::string::npos)
_parseExplosionRadius(line, entity);
else if (line.find("position") != std::string::npos)
_parsePosition(line, entity);
else
_parseEntityName(line, entity);
}
if (!file.good())
return;
while (std::getline(file, line)) {
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
line.erase(std::remove(line.begin(), line.end(), '\t'), line.end());
if (line.empty() || !line.compare("blocks:"))
continue;
lines.push_back(line);
}
for (int index = 0; lines.size() != index; index++) {
_loadBlock(scene, lines, index);
index--;
}
}
void ParserYAML::_loadBonuses(std::shared_ptr<WAL::Scene> scene, std::string filename)
void ParserYAML::_loadBonus(std::shared_ptr<WAL::Scene> scene, std::vector<std::string> lines, int &index)
{
static std::map<Bonus::BonusType, std::vector<std::string>> map = {
{Bonus::BonusType::BOMBSTOCK, {"Bonus Bomb Up", "assets/items/bombup"}},
@@ -189,15 +252,57 @@ namespace BBM {
static std::vector<std::function<void (WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis)>> func = {
&Bonus::BombUpBonus, &Bonus::SpeedUpBonus, &Bonus::ExplosionRangeBonus
};
WAL::Entity entity(*scene, "");
std::string line;
std::ifstream file("save/" + filename + "_bonus.yml");
if (file.good()) {
while (std::getline(file, line)) {
auto &entity = scene->addEntity("");
std::string tmpName = "";
Vector3f pos;
Bonus::BonusType bonusType;
for (; index != lines.size(); index++) {
if (lines[index].find("position") != std::string::npos) {
pos = _parsePosition(lines[index]);
} else if (lines[index].find("block_type") != std::string::npos) {
bonusType = _parseBonusType(lines[index]);
} else {
if (!tmpName.empty()) {
break;
}
_parseEntityName(lines[index], entity);
}
}
entity.addComponent<PositionComponent>(pos)
.addComponent<HealthComponent>(1, [](WAL::Entity &entity, WAL::Wal &wal) {
entity.scheduleDeletion();
})
.addComponent<LevitateComponent>(pos.y)
.addComponent<CollisionComponent>([](WAL::Entity &bonus, const WAL::Entity &player, CollisionComponent::CollidedAxis axis) {
bonus.scheduleDeletion();
}, func[bonusType - 1], 0.5, .5)
.addComponent<TimerComponent>(5s, [](WAL::Entity &bonus, WAL::Wal &wal){
bonus.scheduleDeletion();
})
.addComponent<Drawable3DComponent, RAY3D::Model>(map.at(bonusType)[1] + ".obj", false,
std::make_pair(MAP_DIFFUSE, "assets/items/items.png"));
}
void ParserYAML::_loadBonuses(std::shared_ptr<WAL::Scene> scene, std::string filename)
{
std::ifstream file("save/" + filename + "bonus.yml");
std::string line;
std::vector<std::string> lines;
if (!file.good())
return;
while (std::getline(file, line)) {
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
line.erase(std::remove(line.begin(), line.end(), '\t'), line.end());
if (line.empty() || !line.compare("bonuses:"))
continue;
lines.push_back(line);
}
for (int index = 0; lines.size() != index; index++) {
_loadBonus(scene, lines, index);
index--;
}
}
void ParserYAML::load(std::shared_ptr<WAL::Scene> scene, std::string filename)
@@ -207,7 +312,16 @@ namespace BBM {
_loadPlayers(scene, filename);
}
WAL::Entity &ParserYAML::_parsePosition(std::string &line, WAL::Entity &entity) {
WAL::Entity &ParserYAML::_parseEntityName(std::string line, WAL::Entity &entity)
{
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
auto name = line.substr(0, line.find(':'));
std::replace(name.begin(), name.end(), '_', ' ');
entity.setName(name);
return (entity);
}
Vector3f ParserYAML::_parsePosition(std::string &line) {
std::string x;
std::string y;
std::string z;
@@ -221,33 +335,21 @@ namespace BBM {
} catch (const std::out_of_range &err) {
throw (ParserError("Error parsing position"));
}
return entity.addComponent<PositionComponent>(Vector3f(std::atof(x.c_str()), std::atof(y.c_str()), std::atof(z.c_str())));
return Vector3f(std::atof(x.c_str()), std::atof(y.c_str()), std::atof(z.c_str()));
}
WAL::Entity &ParserYAML::_parseMaxBomb(std::string &line, WAL::Entity &entity)
int ParserYAML::_parseMaxBomb(std::string &line)
{
auto *bombHolder = entity.tryGetComponent<BombHolderComponent>();
if (line.find(": ") == std::string::npos)
throw (ParserError("Couldn't parse max bomb"));
if (bombHolder) {
bombHolder->maxBombCount = std::atoi(line.substr(line.find(": ")).c_str());
return (entity);
}
return (entity.addComponent<BombHolderComponent>(std::atoi(line.substr(line.find(": ")).c_str())));
return (std::atoi(line.substr(line.find(": ")).c_str()));
}
WAL::Entity &ParserYAML::_parseExplosionRadius(std::string &line, WAL::Entity &entity)
float ParserYAML::_parseExplosionRadius(std::string &line)
{
auto *bombHolder = entity.tryGetComponent<BombHolderComponent>();
if (line.find(": ") == std::string::npos)
throw (ParserError("Couldn't parse explosion radius"));
if (bombHolder) {
bombHolder->explosionRadius = std::atoi(line.substr(line.find(": ")).c_str());
return (entity);
}
return (entity.addComponent<BombHolderComponent>(3, bombHolder->explosionRadius = std::atoi(line.substr(line.find(": ")).c_str())));
return (std::atof(line.substr(line.find(": ")).c_str()));
}
MapGenerator::BlockType ParserYAML::_parseBlockType(std::string blockType)
+8 -3
View File
@@ -5,6 +5,7 @@
#pragma once
#include <Wal.hpp>
#include "Items/Bonus.hpp"
namespace BBM {
class ParserYAML {
@@ -20,12 +21,16 @@ namespace BBM {
static std::string _getBonusType(std::string bonusName);
static WAL::Entity &_parseEntityName(std::string line, WAL::Entity &entity);
static WAL::Entity &_parseMaxBomb(std::string &filename, WAL::Entity &entity);
static WAL::Entity &_parseExplosionRadius(std::string &filename, WAL::Entity &entity);
static WAL::Entity &_parsePosition(std::string &filename, WAL::Entity &entity);
static int _parseMaxBomb(std::string &filename);
static float _parseExplosionRadius(std::string &filename);
static Vector3f _parsePosition(std::string &filename);
static MapGenerator::BlockType _parseBlockType(std::string blockType);
static Bonus::BonusType _parseBonusType(std::string bonusType);
static void _loadPlayer(std::shared_ptr<WAL::Scene> scene, std::vector<std::string> lines, int &index);
static void _loadBlock(std::shared_ptr<WAL::Scene> scene, std::vector<std::string> lines, int &index);
static void _loadBonus(std::shared_ptr<WAL::Scene> scene, std::vector<std::string> lines, int &index);
static void _loadPlayers(std::shared_ptr<WAL::Scene> scene, std::string filename);
static void _loadBlocks(std::shared_ptr<WAL::Scene> scene, std::string filename);
static void _loadBonuses(std::shared_ptr<WAL::Scene> scene, std::string filename);
+5 -2
View File
@@ -41,6 +41,7 @@
#include "System/MenuControllable/MenuControllableSystem.hpp"
#include <Drawables/Texture.hpp>
#include <System/Bomb/BombSystem.hpp>
#include <Parser/ParserYaml.hpp>
#include "Component/Music/MusicComponent.hpp"
#include "Component/Sound/SoundComponent.hpp"
#include "System/Sound/PlayerSoundManagerSystem.hpp"
@@ -56,11 +57,13 @@ namespace BBM
void Runner::updateState(WAL::Wal &engine, GameState &state)
{
if (RAY::Window::getInstance().shouldClose())
if (RAY::Window::getInstance().shouldClose()) {
engine.shouldClose = true;
}
if (gameState.currentScene == GameState::SceneID::GameScene) {
for (auto &[_, component]: engine.getScene()->view<ControllableComponent>()) {
if (component.pause) {
ParserYAML::save(engine.getScene(), "test");
gameState.nextScene = GameState::SceneID::PauseMenuScene;
break;
}
@@ -544,7 +547,7 @@ namespace BBM
.addComponent<PositionComponent>(8, 20, 7)
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
//ParserYAML::load(scene, "test");
return scene;
}