Chargement des objets en tant que liste

This commit is contained in:
Mattéo Delabre 2016-04-01 22:44:10 +02:00
parent 1b34dc86ec
commit 25287bcc4b
6 changed files with 60 additions and 23 deletions

View File

@ -2,6 +2,7 @@
#define __PTF_BLOCK_HPP__
#include <SFML/Graphics.hpp>
#include <fstream>
#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<Object> load(std::ifstream& file);
/**
* Dessin du bloc dans la fenêtre donnée
*/

View File

@ -2,6 +2,7 @@
#define __PTF_PLAYER_HPP__
#include <SFML/Graphics.hpp>
#include <fstream>
#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<Object> load(std::ifstream& file);
/**
* Dessine la balle dans la fenêtre donnée
*/

Binary file not shown.

View File

@ -16,6 +16,10 @@ Block::Block(float x, float y) : Object(x, y) {
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) {
Object::draw(manager);

View File

@ -1,11 +1,24 @@
#include "game.hpp"
#include "constants.hpp"
#include "object.hpp"
#include "block.hpp"
#include "player.hpp"
#include <arpa/inet.h>
#include <cstring>
#include <queue>
#include <utility>
#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() {
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> 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
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<char*>(&pos_x), sizeof(pos_x));
file.read(reinterpret_cast<char*>(&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<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() {

View File

@ -13,6 +13,22 @@ Player::Player(float x, float y) : Object(x, y) {
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 forces = Object::getForces(manager, objects);