diff --git a/include/block.hpp b/include/block.hpp index 8e0e28e..b37904a 100644 --- a/include/block.hpp +++ b/include/block.hpp @@ -2,6 +2,7 @@ #define __PTF_BLOCK_HPP__ #include +#include #include "object.hpp" class Block : public Object { @@ -17,6 +18,11 @@ public: Block(float x, float y); virtual ~Block(); + /** + * Chargement de l'objet depuis le fichier donné + */ + static std::shared_ptr load(std::ifstream& file); + /** * Dessin du bloc dans la fenêtre donnée */ diff --git a/include/player.hpp b/include/player.hpp index 429a272..4493aeb 100644 --- a/include/player.hpp +++ b/include/player.hpp @@ -2,6 +2,7 @@ #define __PTF_PLAYER_HPP__ #include +#include #include "object.hpp" class Player : public Object { @@ -20,10 +21,15 @@ public: * Identifiant unique du type d'objet */ static const unsigned int TYPE_ID; - + Player(float x, float y); virtual ~Player(); + /** + * Chargement de l'objet depuis le fichier donné + */ + static std::shared_ptr load(std::ifstream& file); + /** * Dessine la balle dans la fenêtre donnée */ diff --git a/levels/level1.dat b/levels/level1.dat index 05c59fd..e5bcb07 100644 Binary files a/levels/level1.dat and b/levels/level1.dat differ diff --git a/src/block.cpp b/src/block.cpp index bce61f4..b8546f0 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -16,6 +16,10 @@ Block::Block(float x, float y) : Object(x, y) { Block::~Block() {} +std::shared_ptr Block::load(std::ifstream& file) { + return std::shared_ptr(new Block(10, 10)); +} + void Block::draw(Manager& manager) { Object::draw(manager); diff --git a/src/game.cpp b/src/game.cpp index 089911c..4460943 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,11 +1,24 @@ #include "game.hpp" #include "constants.hpp" +#include "object.hpp" +#include "block.hpp" #include "player.hpp" +#include #include #include #include #include +/** + * Dictionnaire associant les types d'objets + * à des instances qui seront utilisées pour la + * construction d'autres objets de ces types + */ +std::map(std::ifstream&)>> object_type_map = { + {Player::TYPE_ID, Player::load}, + {Block::TYPE_ID, Block::load} +}; + Game::Game(Manager& manager) : View(manager), accumulator(0.f) {} Game::~Game() { objects.clear(); @@ -40,26 +53,6 @@ void Game::load(std::ifstream& file) { // lecture du nom du niveau std::getline(file, level_name, '\0'); - // lecture des positions initiales - char player_amount; - file.read(&player_amount, 1); - - for (int i = 0; i < player_amount; i++) { - float pos_x, pos_y; - - file.read((char*) &pos_x, sizeof(pos_x)); - file.read((char*) &pos_y, sizeof(pos_y)); - - pos_x *= Constants::GRID; - pos_y *= Constants::GRID; - - std::shared_ptr player = - std::shared_ptr(new Player(pos_x, pos_y)); - player->setPlayerNumber(i); - - objects.push_back(std::dynamic_pointer_cast(player)); - } - // lecture de la zone de jeu char control_points; file.read(&control_points, 1); @@ -68,8 +61,8 @@ void Game::load(std::ifstream& file) { for (int i = 0; i < control_points; i++) { float pos_x, pos_y; - file.read((char*) &pos_x, sizeof(pos_x)); - file.read((char*) &pos_y, sizeof(pos_y)); + file.read(reinterpret_cast(&pos_x), sizeof(pos_x)); + file.read(reinterpret_cast(&pos_y), sizeof(pos_y)); pos_x *= Constants::GRID; pos_y *= Constants::GRID; @@ -87,6 +80,18 @@ void Game::load(std::ifstream& file) { std::getline(file, background_name, '\0'); background.setTexture(resource_manager.getTexture(background_name)); + + // lecture du nombre de blocs + int block_count; + + file.read(reinterpret_cast(&block_count), sizeof(block_count)); + block_count = ntohl(block_count); + + for (int i = 0; i < block_count; i++) { + char block_type; + file.read(&block_type, 1); + objects.push_back(object_type_map[block_type](file)); + } } void Game::save() { diff --git a/src/player.cpp b/src/player.cpp index 39ae41a..4e2c149 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -13,6 +13,22 @@ Player::Player(float x, float y) : Object(x, y) { Player::~Player() {} +std::shared_ptr Player::load(std::ifstream& file) { + float pos_x, pos_y; + + file.read(reinterpret_cast(&pos_x), sizeof(pos_x)); + file.read(reinterpret_cast(&pos_y), sizeof(pos_y)); + + pos_x *= Constants::GRID; + pos_y *= Constants::GRID; + + std::shared_ptr player = + std::shared_ptr(new Player(pos_x, pos_y)); + // player->setPlayerNumber(i); + + return std::dynamic_pointer_cast(player); +} + sf::Vector2f Player::getForces(const Manager& manager, const std::vector& objects) const { sf::Vector2f forces = Object::getForces(manager, objects);