parser start

This commit is contained in:
EternalRat
2021-06-10 22:23:29 +02:00
parent d13fe98b14
commit ed99b93299
5 changed files with 111 additions and 30 deletions
+6 -6
View File
@@ -37,10 +37,10 @@ namespace BBM
{
entity.scheduleDeletion();
auto &position = entity.getComponent<PositionComponent>().position;
static std::map<Bonus::BonusType, std::string> map = {
{Bonus::BonusType::BOMBSTOCK, "assets/items/bombup"},
{Bonus::BonusType::SPEEDUP, "assets/items/speedup"},
{Bonus::BonusType::EXPLOSIONINC, "assets/items/fireup"}
static std::map<Bonus::BonusType, std::vector<std::string>> map = {
{Bonus::BonusType::BOMBSTOCK, {"Bonus Bomb Up", "assets/items/bombup"}},
{Bonus::BonusType::SPEEDUP, {"Bonus Speed Up", "assets/items/speedup"}},
{Bonus::BonusType::EXPLOSIONINC, {"Bonus Fire Up", "assets/items/fireup"}}
};
static std::vector<std::function<void (WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis)>> func = {
&Bonus::BombUpBonus, &Bonus::SpeedUpBonus, &Bonus::ExplosionRangeBonus
@@ -51,7 +51,7 @@ namespace BBM
return;
if (!map.contains(bonusType))
return;
wal.getScene()->scheduleNewEntity("Bonus")
wal.getScene()->scheduleNewEntity(map.at(bonusType)[0])
.addComponent<PositionComponent>(position)
.addComponent<HealthComponent>(1, [](WAL::Entity &entity, WAL::Wal &wal) {
entity.scheduleDeletion();
@@ -63,7 +63,7 @@ namespace BBM
.addComponent<TimerComponent>(5s, [](WAL::Entity &bonus, WAL::Wal &wal){
bonus.scheduleDeletion();
})
.addComponent<Drawable3DComponent, RAY3D::Model>(map.at(bonusType) + ".obj", false,
.addComponent<Drawable3DComponent, RAY3D::Model>(map.at(bonusType)[1] + ".obj", false,
std::make_pair(MAP_DIFFUSE, "assets/items/items.png"));
}
+22 -21
View File
@@ -30,7 +30,7 @@ namespace BBM
class MapGenerator
{
private:
public:
//! @brief Enum of the block available.
enum BlockType
{
@@ -44,9 +44,29 @@ namespace BBM
UNBREAKABLE
};
using MapElem = std::function<void(Vector3f coords, std::shared_ptr<WAL::Scene> scene)>;
using MapBlock = std::map<std::tuple<int, int, int>, BlockType>;
static void wallCollided(WAL::Entity &entity,
const WAL::Entity &wall,
CollisionComponent::CollidedAxis collidedAxis);
static void wallDestroyed(WAL::Entity &entity, WAL::Wal &wal);
//! @param width Width of the map
//! @param height Height of the map
//! @brief Generate map of block to be loaded
static MapBlock createMap(int width, int height);
//! @param width Width of the map
//! @param height Height of the map
//! @param map Map to load with block declared inside
//! @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);
private:
using MapElem = std::function<void(Vector3f coords, std::shared_ptr<WAL::Scene> scene)>;
//! @brief Generate random block type
static BlockType getRandomBlockType();
@@ -154,24 +174,5 @@ namespace BBM
static const std::string secondFloorHolePath;
public:
static void wallCollided(WAL::Entity &entity,
const WAL::Entity &wall,
CollisionComponent::CollidedAxis collidedAxis);
static void wallDestroyed(WAL::Entity &entity, WAL::Wal &wal);
//! @param width Width of the map
//! @param height Height of the map
//! @brief Generate map of block to be loaded
static MapBlock createMap(int width, int height);
//! @param width Width of the map
//! @param height Height of the map
//! @param map Map to load with block declared inside
//! @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);
};
} // namespace BBM
+73 -2
View File
@@ -3,14 +3,85 @@
//
#include <fstream>
#include <map>
#include <Component/Position/PositionComponent.hpp>
#include <Component/Timer/TimerComponent.hpp>
#include <Map/Map.hpp>
#include "ParserYaml.hpp"
namespace BBM {
void ParserYAML::save(std::shared_ptr<WAL::Scene> scene, std::string filename) {
std::string ParserYAML::_block = "blocks:\n\t";
std::string ParserYAML::_bonus = "bonuses:\n\t";
std::string ParserYAML::_player = "players:\n\t";
char *_getBlockType(std::string blockName)
{
static std::map<std::string, MapGenerator::BlockType> map {
{"Upper Floor", MapGenerator::BlockType::UPPERFLOOR},
{"Bottom Wall", MapGenerator::BlockType::UNBREAKABLE},
{"Upper Wall", MapGenerator::BlockType::UNBREAKABLE},
{"Left Wall", MapGenerator::BlockType::UNBREAKABLE},
{"Right Wall", MapGenerator::BlockType::UNBREAKABLE},
{"Bumper Block", MapGenerator::BlockType::BUMPER},
{"Breakable Block", MapGenerator::BlockType::BREAKABLE},
{"Unbreakable Block", MapGenerator::BlockType::UNBREAKABLE},
{"Unbreakable Wall", MapGenerator::BlockType::FLOOR},
{"Hole Block", MapGenerator::BlockType::HOLE}
};
return (std::string(map.at(blockName)));
}
void ParserYAML::_saveBonus(WAL::Entity entity)
{
auto *position = entity.tryGetComponent<PositionComponent>();
if (!position)
return;
_bonus.append(entity.getName() + ":\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");
}
void ParserYAML::_savePlayer(WAL::Entity entity)
{
}
void ParserYAML::_saveBlock(WAL::Entity entity)
{
auto *position = entity.tryGetComponent<PositionComponent>();
if (!position)
return;
_block.append(entity.getName() + ":\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");
}
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 = {
{"Bonus", &_saveBonus},
{"Block", &_saveBlock},
{"Floor", &_saveBlock},
{"Wall", &_saveBlock},
{"Player", &_savePlayer}
};
std::ofstream file(path.c_str());
for (auto entity : scene->getEntities()) {
for (auto type : savingGame) {
if (entity.getName().find(type.first) != std::string::npos) {
type.second(entity);
}
}
}
}
}
+9
View File
@@ -8,6 +8,15 @@
namespace BBM {
class ParserYAML {
private:
static std::string _block;
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);
public:
static void save(std::shared_ptr<WAL::Scene> scene, std::string filename);
};
+1 -1
View File
@@ -520,7 +520,7 @@ namespace BBM
{SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"},
//{SoundComponent::DEATH, "assets/sounds/death.ogg"}
};
scene->addEntity("player")
scene->addEntity("Player")
.addComponent<PositionComponent>()
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
.addComponent<ControllableComponent>()