mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-05-29 17:02:11 +00:00
start of the load
This commit is contained in:
@@ -132,6 +132,7 @@ add_executable(unit_tests EXCLUDE_FROM_ALL
|
||||
tests/MoveTests.cpp
|
||||
tests/ViewTest.cpp
|
||||
tests/CollisionTest.cpp
|
||||
tests/ParserTest.cpp
|
||||
)
|
||||
target_include_directories(unit_tests PUBLIC sources)
|
||||
target_link_libraries(unit_tests PUBLIC wal ray)
|
||||
|
||||
@@ -12,9 +12,10 @@ namespace BBM
|
||||
: WAL::Component(entity)
|
||||
{}
|
||||
|
||||
BombHolderComponent::BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount)
|
||||
BombHolderComponent::BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount, float explosionRadius)
|
||||
: WAL::Component(entity),
|
||||
maxBombCount(maxBombCount)
|
||||
maxBombCount(maxBombCount),
|
||||
explosionRadius(explosionRadius)
|
||||
{}
|
||||
|
||||
WAL::Component *BombHolderComponent::clone(WAL::Entity &entity) const
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace BBM
|
||||
explicit BombHolderComponent(WAL::Entity &entity);
|
||||
|
||||
//! @brief Constructor
|
||||
BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount);
|
||||
BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount, float explosionRadius = 3);
|
||||
|
||||
//! @brief A component can't be instantiated, it should be derived.
|
||||
BombHolderComponent(const BombHolderComponent &) = default;
|
||||
|
||||
+2
-1
@@ -41,7 +41,8 @@ namespace BBM
|
||||
FLOOR,
|
||||
BUMPER,
|
||||
SPAWNER,
|
||||
UNBREAKABLE
|
||||
UNBREAKABLE,
|
||||
INVISIBLE
|
||||
};
|
||||
|
||||
using MapBlock = std::map<std::tuple<int, int, int>, BlockType>;
|
||||
|
||||
@@ -7,15 +7,28 @@
|
||||
#include <Component/Position/PositionComponent.hpp>
|
||||
#include <Component/Timer/TimerComponent.hpp>
|
||||
#include <Map/Map.hpp>
|
||||
#include <Component/BombHolder/BombHolderComponent.hpp>
|
||||
#include <sstream>
|
||||
#include <Component/Controllable/ControllableComponent.hpp>
|
||||
#include <Component/Keyboard/KeyboardComponent.hpp>
|
||||
#include <Component/Shaders/ShaderComponent.hpp>
|
||||
#include <Component/Animator/AnimatorComponent.hpp>
|
||||
#include <Component/Tag/TagComponent.hpp>
|
||||
#include <Component/Animation/AnimationsComponent.hpp>
|
||||
#include <Component/Sound/SoundComponent.hpp>
|
||||
#include <Component/Bonus/PlayerBonusComponent.hpp>
|
||||
#include <Component/Music/MusicComponent.hpp>
|
||||
#include "ParserYaml.hpp"
|
||||
|
||||
namespace RAY3D = RAY::Drawables::Drawables3D;
|
||||
|
||||
namespace BBM {
|
||||
|
||||
std::string ParserYAML::_block = "blocks:\n\t";
|
||||
std::string ParserYAML::_bonus = "bonuses:\n\t";
|
||||
std::string ParserYAML::_player = "players:\n\t";
|
||||
std::string ParserYAML::_block = "\nblocks:";
|
||||
std::string ParserYAML::_bonus = "\nbonuses:";
|
||||
std::string ParserYAML::_player = "players:";
|
||||
|
||||
char *_getBlockType(std::string blockName)
|
||||
const char *ParserYAML::_getBlockType(std::string blockName)
|
||||
{
|
||||
static std::map<std::string, MapGenerator::BlockType> map {
|
||||
{"Upper Floor", MapGenerator::BlockType::UPPERFLOOR},
|
||||
@@ -30,45 +43,62 @@ namespace BBM {
|
||||
{"Hole Block", MapGenerator::BlockType::HOLE}
|
||||
};
|
||||
|
||||
return (std::string(map.at(blockName)));
|
||||
return (std::to_string(map.at(blockName)).c_str());
|
||||
}
|
||||
|
||||
void ParserYAML::_saveBonus(WAL::Entity entity)
|
||||
void ParserYAML::_saveBonus(const WAL::Entity &entity)
|
||||
{
|
||||
auto *position = entity.tryGetComponent<PositionComponent>();
|
||||
auto name = entity.getName();
|
||||
|
||||
if (!position)
|
||||
return;
|
||||
_bonus.append(entity.getName() + ":\n\t\t");
|
||||
std::replace(name.begin(), name.end(), ' ', '_');
|
||||
_bonus.append("\n\t" + name + ":\n\t\t");
|
||||
_bonus.append("position:\n\t\t\t");
|
||||
_bonus.append("x: " + std::to_string(position->getX()) + "\n\t\t\t");
|
||||
_bonus.append("y: " + std::to_string(position->getY()) + "\n\t\t\t");
|
||||
_bonus.append("z: " + std::to_string(position->getZ()) + "\n");
|
||||
_bonus.append("z: " + std::to_string(position->getZ()));
|
||||
}
|
||||
|
||||
void ParserYAML::_savePlayer(WAL::Entity entity)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ParserYAML::_saveBlock(WAL::Entity entity)
|
||||
void ParserYAML::_savePlayer(const WAL::Entity &entity)
|
||||
{
|
||||
auto *position = entity.tryGetComponent<PositionComponent>();
|
||||
auto *bombHolder = entity.tryGetComponent<BombHolderComponent>();
|
||||
auto name = entity.getName();
|
||||
|
||||
if (!position || !bombHolder)
|
||||
return;
|
||||
std::replace(name.begin(), name.end(), ' ', '_');
|
||||
_player.append("\n\t" + name + ":\n\t\t");
|
||||
_player.append("max_bomb: " + std::to_string(bombHolder->maxBombCount) + "\n\t\t");
|
||||
_player.append("explosion_radius: " + std::to_string(bombHolder->explosionRadius) + "\n\t\t");
|
||||
_player.append("position:\n\t\t\t");
|
||||
_player.append("x: " + std::to_string(position->getX()) + "\n\t\t\t");
|
||||
_player.append("y: " + std::to_string(position->getY()) + "\n\t\t\t");
|
||||
_player.append("z: " + std::to_string(position->getZ()));
|
||||
}
|
||||
|
||||
void ParserYAML::_saveBlock(const WAL::Entity &entity)
|
||||
{
|
||||
auto *position = entity.tryGetComponent<PositionComponent>();
|
||||
auto name = entity.getName();
|
||||
|
||||
if (!position)
|
||||
return;
|
||||
_block.append(entity.getName() + ":\n\t\t");
|
||||
std::replace(name.begin(), name.end(), ' ', '_');
|
||||
_block.append("\n\t" + name + ":\n\t\t");
|
||||
_block.append(std::string("block_type: ") + _getBlockType(entity.getName()) + "\n\t\t");
|
||||
_block.append("position:\n\t\t\t");
|
||||
_block.append("x: " + std::to_string(position->getX()) + "\n\t\t\t");
|
||||
_block.append("y: " + std::to_string(position->getY()) + "\n\t\t\t");
|
||||
_block.append("z: " + std::to_string(position->getZ()) + "\n");
|
||||
_block.append("z: " + std::to_string(position->getZ()));
|
||||
}
|
||||
|
||||
void ParserYAML::save(std::shared_ptr<WAL::Scene> scene, std::string filename)
|
||||
void ParserYAML::save(std::shared_ptr<WAL::Scene> scene, std::string &filename)
|
||||
{
|
||||
std::string path = std::string("save/" + filename);
|
||||
std::map<std::string, std::function<void (WAL::Entity)>> savingGame = {
|
||||
std::map<std::string, std::function<void (const WAL::Entity &)>> savingGame = {
|
||||
{"Bonus", &_saveBonus},
|
||||
{"Block", &_saveBlock},
|
||||
{"Floor", &_saveBlock},
|
||||
@@ -76,12 +106,60 @@ namespace BBM {
|
||||
{"Player", &_savePlayer}
|
||||
};
|
||||
std::ofstream file(path.c_str());
|
||||
for (auto entity : scene->getEntities()) {
|
||||
for (auto type : savingGame) {
|
||||
for (const auto &entity : scene->getEntities()) {
|
||||
for (const auto& type : savingGame) {
|
||||
if (entity.getName().find(type.first) != std::string::npos) {
|
||||
type.second(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
file << _player << _block << _bonus << std::endl;
|
||||
}
|
||||
|
||||
void ParserYAML::loadPlayers(std::shared_ptr<WAL::Scene> scene, std::string filename)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ParserYAML::loadBlocks(std::shared_ptr<WAL::Scene> scene, std::string filename)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ParserYAML::loadBonuses(std::shared_ptr<WAL::Scene> scene, std::string filename)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ParserYAML::load(std::shared_ptr<WAL::Scene> scene, std::string &filename)
|
||||
{
|
||||
std::map<std::string, std::vector<std::string>> parser = {
|
||||
{"players", {}},
|
||||
{"blocks", {}},
|
||||
{"bonuses", {}}
|
||||
};
|
||||
std::ifstream file("save/" + filename);
|
||||
if (file.good()) {
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vector3f ParserYAML::getPosition(std::string &filename)
|
||||
{
|
||||
|
||||
return Vector3f();
|
||||
}
|
||||
|
||||
unsigned int ParserYAML::getMaxBomb(std::string &filename)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
float ParserYAML::getExplosionRadius(std::string &filename)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -13,11 +13,20 @@ namespace BBM {
|
||||
static std::string _bonus;
|
||||
static std::string _player;
|
||||
|
||||
static void _saveBlock(WAL::Entity entity);
|
||||
static void _saveBonus(WAL::Entity entity);
|
||||
static void _savePlayer(WAL::Entity entity);
|
||||
static char *_getBlockType(std::string blockName);
|
||||
static void _saveBlock(const WAL::Entity &entity);
|
||||
static void _saveBonus(const WAL::Entity &entity);
|
||||
static void _savePlayer(const WAL::Entity &entity);
|
||||
static const char *_getBlockType(std::string blockName);
|
||||
static Vector3f getPosition(std::string &filename);
|
||||
static unsigned int getMaxBomb(std::string &filename);
|
||||
static float getExplosionRadius(std::string &filename);
|
||||
|
||||
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);
|
||||
public:
|
||||
static void save(std::shared_ptr<WAL::Scene> scene, std::string filename);
|
||||
static void save(std::shared_ptr<WAL::Scene> scene, std::string &filename);
|
||||
static void load(std::shared_ptr<WAL::Scene> scene, std::string &filename);
|
||||
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by hbenjamin on 11/06/2021.
|
||||
//
|
||||
|
||||
#include "Entity/Entity.hpp"
|
||||
#include "Component/Position/PositionComponent.hpp"
|
||||
#include <catch2/catch.hpp>
|
||||
#include <Wal.hpp>
|
||||
|
||||
#define private public
|
||||
#include "System/Controllable/ControllableSystem.hpp"
|
||||
#include "System/Movable/MovableSystem.hpp"
|
||||
#include <Component/Controllable/ControllableComponent.hpp>
|
||||
#include <Component/Movable/MovableComponent.hpp>
|
||||
#include "Component/BombHolder/BombHolderComponent.hpp"
|
||||
#include "Parser/ParserYaml.hpp"
|
||||
|
||||
using namespace WAL;
|
||||
using namespace BBM;
|
||||
|
||||
TEST_CASE("Parser Test", "[PARSER]")
|
||||
{
|
||||
Wal wal;
|
||||
wal.changeScene(std::make_shared<Scene>());
|
||||
wal.getScene()->addEntity("Player")
|
||||
.addComponent<ControllableComponent>()
|
||||
.addComponent<MovableComponent>()
|
||||
.addComponent<BombHolderComponent>()
|
||||
.addComponent<PositionComponent>();
|
||||
wal.getScene()->addEntity("Bonus Fire Up")
|
||||
.addComponent<PositionComponent>(2, 0, 2);
|
||||
wal.getScene()->addEntity("Breakable Block")
|
||||
.addComponent<PositionComponent>(1, 0, 1);
|
||||
wal.getScene()->addEntity("Unbreakable Block")
|
||||
.addComponent<PositionComponent>(1, 5, 1);
|
||||
ParserYAML::save(wal.getScene(), "test_filename.yml");
|
||||
}
|
||||
Reference in New Issue
Block a user