Chargement des objets en tant que liste
This commit is contained in:
		
							parent
							
								
									1b34dc86ec
								
							
						
					
					
						commit
						25287bcc4b
					
				| 
						 | 
					@ -2,6 +2,7 @@
 | 
				
			||||||
#define __PTF_BLOCK_HPP__
 | 
					#define __PTF_BLOCK_HPP__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <SFML/Graphics.hpp>
 | 
					#include <SFML/Graphics.hpp>
 | 
				
			||||||
 | 
					#include <fstream>
 | 
				
			||||||
#include "object.hpp"
 | 
					#include "object.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Block : public Object {
 | 
					class Block : public Object {
 | 
				
			||||||
| 
						 | 
					@ -17,6 +18,11 @@ public:
 | 
				
			||||||
    Block(float x, float y);
 | 
					    Block(float x, float y);
 | 
				
			||||||
    virtual ~Block();
 | 
					    virtual ~Block();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Chargement de l'objet depuis le fichier donné
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    static std::shared_ptr<Object> load(std::ifstream& file);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Dessin du bloc dans la fenêtre donnée
 | 
					     * Dessin du bloc dans la fenêtre donnée
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,7 @@
 | 
				
			||||||
#define __PTF_PLAYER_HPP__
 | 
					#define __PTF_PLAYER_HPP__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <SFML/Graphics.hpp>
 | 
					#include <SFML/Graphics.hpp>
 | 
				
			||||||
 | 
					#include <fstream>
 | 
				
			||||||
#include "object.hpp"
 | 
					#include "object.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Player : public Object {
 | 
					class Player : public Object {
 | 
				
			||||||
| 
						 | 
					@ -20,10 +21,15 @@ public:
 | 
				
			||||||
     * Identifiant unique du type d'objet
 | 
					     * Identifiant unique du type d'objet
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    static const unsigned int TYPE_ID;
 | 
					    static const unsigned int TYPE_ID;
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
    Player(float x, float y);
 | 
					    Player(float x, float y);
 | 
				
			||||||
    virtual ~Player();
 | 
					    virtual ~Player();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Chargement de l'objet depuis le fichier donné
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    static std::shared_ptr<Object> load(std::ifstream& file);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Dessine la balle dans la fenêtre donnée
 | 
					     * Dessine la balle dans la fenêtre donnée
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
										
											Binary file not shown.
										
									
								
							| 
						 | 
					@ -16,6 +16,10 @@ Block::Block(float x, float y) : Object(x, y) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Block::~Block() {}
 | 
					Block::~Block() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::shared_ptr<Object> Block::load(std::ifstream& file) {
 | 
				
			||||||
 | 
					    return std::shared_ptr<Object>(new Block(10, 10));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Block::draw(Manager& manager) {
 | 
					void Block::draw(Manager& manager) {
 | 
				
			||||||
    Object::draw(manager);
 | 
					    Object::draw(manager);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										49
									
								
								src/game.cpp
								
								
								
								
							
							
						
						
									
										49
									
								
								src/game.cpp
								
								
								
								
							| 
						 | 
					@ -1,11 +1,24 @@
 | 
				
			||||||
#include "game.hpp"
 | 
					#include "game.hpp"
 | 
				
			||||||
#include "constants.hpp"
 | 
					#include "constants.hpp"
 | 
				
			||||||
 | 
					#include "object.hpp"
 | 
				
			||||||
 | 
					#include "block.hpp"
 | 
				
			||||||
#include "player.hpp"
 | 
					#include "player.hpp"
 | 
				
			||||||
 | 
					#include <arpa/inet.h>
 | 
				
			||||||
#include <cstring>
 | 
					#include <cstring>
 | 
				
			||||||
#include <queue>
 | 
					#include <queue>
 | 
				
			||||||
#include <utility>
 | 
					#include <utility>
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Dictionnaire associant les types d'objets
 | 
				
			||||||
 | 
					 * à des instances qui seront utilisées pour la
 | 
				
			||||||
 | 
					 * construction d'autres objets de ces types
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					std::map<unsigned int, std::function<std::shared_ptr<Object>(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(Manager& manager) : View(manager), accumulator(0.f) {}
 | 
				
			||||||
Game::~Game() {
 | 
					Game::~Game() {
 | 
				
			||||||
    objects.clear();
 | 
					    objects.clear();
 | 
				
			||||||
| 
						 | 
					@ -40,26 +53,6 @@ void Game::load(std::ifstream& file) {
 | 
				
			||||||
    // lecture du nom du niveau
 | 
					    // lecture du nom du niveau
 | 
				
			||||||
    std::getline(file, level_name, '\0');
 | 
					    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> player =
 | 
					 | 
				
			||||||
            std::shared_ptr<Player>(new Player(pos_x, pos_y));
 | 
					 | 
				
			||||||
        player->setPlayerNumber(i);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        objects.push_back(std::dynamic_pointer_cast<Object>(player));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // lecture de la zone de jeu
 | 
					    // lecture de la zone de jeu
 | 
				
			||||||
    char control_points;
 | 
					    char control_points;
 | 
				
			||||||
    file.read(&control_points, 1);
 | 
					    file.read(&control_points, 1);
 | 
				
			||||||
| 
						 | 
					@ -68,8 +61,8 @@ void Game::load(std::ifstream& file) {
 | 
				
			||||||
    for (int i = 0; i < control_points; i++) {
 | 
					    for (int i = 0; i < control_points; i++) {
 | 
				
			||||||
        float pos_x, pos_y;
 | 
					        float pos_x, pos_y;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        file.read((char*) &pos_x, sizeof(pos_x));
 | 
					        file.read(reinterpret_cast<char*>(&pos_x), sizeof(pos_x));
 | 
				
			||||||
        file.read((char*) &pos_y, sizeof(pos_y));
 | 
					        file.read(reinterpret_cast<char*>(&pos_y), sizeof(pos_y));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        pos_x *= Constants::GRID;
 | 
					        pos_x *= Constants::GRID;
 | 
				
			||||||
        pos_y *= Constants::GRID;
 | 
					        pos_y *= Constants::GRID;
 | 
				
			||||||
| 
						 | 
					@ -87,6 +80,18 @@ void Game::load(std::ifstream& file) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    std::getline(file, background_name, '\0');
 | 
					    std::getline(file, background_name, '\0');
 | 
				
			||||||
    background.setTexture(resource_manager.getTexture(background_name));
 | 
					    background.setTexture(resource_manager.getTexture(background_name));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // lecture du nombre de blocs
 | 
				
			||||||
 | 
					    int block_count;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    file.read(reinterpret_cast<char*>(&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() {
 | 
					void Game::save() {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,6 +13,22 @@ Player::Player(float x, float y) : Object(x, y) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Player::~Player() {}
 | 
					Player::~Player() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::shared_ptr<Object> Player::load(std::ifstream& file) {
 | 
				
			||||||
 | 
					    float pos_x, pos_y;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    file.read(reinterpret_cast<char*>(&pos_x), sizeof(pos_x));
 | 
				
			||||||
 | 
					    file.read(reinterpret_cast<char*>(&pos_y), sizeof(pos_y));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pos_x *= Constants::GRID;
 | 
				
			||||||
 | 
					    pos_y *= Constants::GRID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    std::shared_ptr<Player> player =
 | 
				
			||||||
 | 
					        std::shared_ptr<Player>(new Player(pos_x, pos_y));
 | 
				
			||||||
 | 
					    // player->setPlayerNumber(i);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return std::dynamic_pointer_cast<Object>(player);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sf::Vector2f Player::getForces(const Manager& manager, const std::vector<ObjectPtr>& objects) const {
 | 
					sf::Vector2f Player::getForces(const Manager& manager, const std::vector<ObjectPtr>& objects) const {
 | 
				
			||||||
    sf::Vector2f forces = Object::getForces(manager, objects);
 | 
					    sf::Vector2f forces = Object::getForces(manager, objects);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue