From 9fb90a03714f615e9a892ccb1a376490b563528e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Sat, 2 Apr 2016 15:18:29 +0200 Subject: [PATCH] Lecture des objets depuis un fichier --- include/block.hpp | 3 +- include/object.hpp | 21 ++++++++++-- include/player.hpp | 3 +- levels/level1.dat | Bin 94 -> 98 bytes src/block.cpp | 12 +++++-- src/object.cpp | 84 +++++++++++++++++++++++++++++++++++++++++---- src/player.cpp | 24 ++++++------- 7 files changed, 119 insertions(+), 28 deletions(-) diff --git a/include/block.hpp b/include/block.hpp index b37904a..bdc7c9e 100644 --- a/include/block.hpp +++ b/include/block.hpp @@ -2,7 +2,6 @@ #define __PTF_BLOCK_HPP__ #include -#include #include "object.hpp" class Block : public Object { @@ -15,7 +14,7 @@ public: */ static const unsigned int TYPE_ID; - Block(float x, float y); + Block(); virtual ~Block(); /** diff --git a/include/object.hpp b/include/object.hpp index e58bc69..04a0b2f 100644 --- a/include/object.hpp +++ b/include/object.hpp @@ -2,6 +2,7 @@ #define __PTF_OBJECT_HPP__ #include +#include #include #include "collision.hpp" #include "manager.hpp" @@ -17,9 +18,9 @@ private: sf::VertexArray acceleration_line; sf::VertexArray velocity_line; + mutable float inv_mass; float mass; - mutable float inv_mass; float charge; float restitution; float static_friction; @@ -33,9 +34,25 @@ protected: virtual sf::Vector2f getForces(const Manager& manager, const std::vector& objects) const; public: - Object(float x, float y); + /** + * Identifiants uniques des propriétés communes modifiables + */ + static const unsigned int PROP_MASS; + static const unsigned int PROP_CHARGE; + static const unsigned int PROP_RESTITUTION; + static const unsigned int PROP_STATIC_FRICTION; + static const unsigned int PROP_DYNAMIC_FRICTION; + static const unsigned int PROP_LAYER; + + Object(); virtual ~Object(); + /** + * Charge les propriétés communes à tous les objets + * depuis le fichier donné dans l'objet donné + */ + static void load(std::ifstream& file, std::shared_ptr object); + /** * Dessine l'objet dans la fenêtre donnée */ diff --git a/include/player.hpp b/include/player.hpp index 4493aeb..2f40528 100644 --- a/include/player.hpp +++ b/include/player.hpp @@ -2,7 +2,6 @@ #define __PTF_PLAYER_HPP__ #include -#include #include "object.hpp" class Player : public Object { @@ -22,7 +21,7 @@ public: */ static const unsigned int TYPE_ID; - Player(float x, float y); + Player(); virtual ~Player(); /** diff --git a/levels/level1.dat b/levels/level1.dat index e5bcb07fbcc644eb6a5e5d8d7e5dcbb51d5ef891..2d326812c2db7a8eb560909b86c1f617fe20f034 100644 GIT binary patch delta 27 dcmaz`n&2bG$iTpm;K0D3;K;xTWKVGfGXX)*1ML6+ delta 23 bcmYd_o8TkFz`&5;z`&s32xLqF5( Block::load(std::ifstream& file) { - return std::shared_ptr(new Block(10, 10)); + std::shared_ptr object = std::shared_ptr(new Block); + + // lecture des propriétés communes des objets + Object::load(file, object); + file.seekg(1, file.cur); + + return object; } void Block::draw(Manager& manager) { diff --git a/src/object.cpp b/src/object.cpp index 18645fb..00590f7 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -2,20 +2,90 @@ #include "constants.hpp" #include "collision.hpp" -Object::Object(float x, float y) : - acceleration(0, 0), velocity(0, 0), position(x, y), +const unsigned int Object::PROP_MASS = 1; +const unsigned int Object::PROP_CHARGE = 2; +const unsigned int Object::PROP_RESTITUTION = 3; +const unsigned int Object::PROP_STATIC_FRICTION = 4; +const unsigned int Object::PROP_DYNAMIC_FRICTION = 5; +const unsigned int Object::PROP_LAYER = 6; + +Object::Object() : + acceleration(0, 0), velocity(0, 0), position(0, 0), acceleration_line(sf::Lines, 2), velocity_line(sf::Lines, 2), - mass(Constants::DEFAULT_MASS), - inv_mass(1.f / Constants::DEFAULT_MASS), - charge(Constants::DEFAULT_CHARGE), - restitution(Constants::DEFAULT_RESTITUTION), + inv_mass(-1.f), + + // valeurs par défaut pour les propriétés + // de tous les objets du jeu + mass(1.f), charge(0.f), + restitution(0.4f), static_friction(0.4f), dynamic_friction(0.2f), - layer(Constants::DEFAULT_LAYER) {} + layer(0) {} Object::~Object() {} +void Object::load(std::ifstream& file, std::shared_ptr object) { + // lecture de la position de l'objet + float pos_x, pos_y; + + file.read(reinterpret_cast(&pos_x), sizeof(pos_x)); + file.read(reinterpret_cast(&pos_y), sizeof(pos_y)); + + object->setPosition(sf::Vector2f( + pos_x * Constants::GRID, pos_y * Constants::GRID + )); + + // lecture des propriétés facultatives + char prop_type = -1; + + while (file.read(&prop_type, 1)) { + switch (prop_type) { + case Object::PROP_MASS: + float mass; + file.read(reinterpret_cast(&mass), sizeof(mass)); + object->setMass(mass); + break; + + case Object::PROP_CHARGE: + float charge; + file.read(reinterpret_cast(&charge), sizeof(charge)); + object->setCharge(charge); + break; + + case Object::PROP_RESTITUTION: + float restitution; + file.read(reinterpret_cast(&restitution), sizeof(restitution)); + object->setRestitution(restitution); + break; + + case Object::PROP_STATIC_FRICTION: + float static_friction; + file.read(reinterpret_cast(&static_friction), sizeof(static_friction)); + object->setStaticFriction(static_friction); + break; + + case Object::PROP_DYNAMIC_FRICTION: + float dynamic_friction; + file.read(reinterpret_cast(&dynamic_friction), sizeof(dynamic_friction)); + object->setDynamicFriction(dynamic_friction); + break; + + case Object::PROP_LAYER: + char layer; + file.read(&layer, 1); + object->setLayer((int) layer - 127); + break; + + default: + // propriété de type inconnu : on recule + // d'un octet et on sort + file.seekg(-1, file.cur); + return; + } + } +} + sf::Vector2f Object::getForces( const Manager& manager, const std::vector& objects ) const { diff --git a/src/player.cpp b/src/player.cpp index 4e2c149..a88b483 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -4,9 +4,9 @@ #include #include -const unsigned int Player::TYPE_ID = 0; +const unsigned int Player::TYPE_ID = 1; -Player::Player(float x, float y) : Object(x, y) { +Player::Player() : Object() { // déplacement de l'origine au centre de la balle sprite.setOrigin(sf::Vector2f(getRadius(), getRadius())); } @@ -14,19 +14,19 @@ Player::Player(float x, float y) : Object(x, y) { Player::~Player() {} std::shared_ptr Player::load(std::ifstream& file) { - float pos_x, pos_y; + std::shared_ptr object = std::shared_ptr(new Player); + std::shared_ptr player = std::dynamic_pointer_cast(object); - file.read(reinterpret_cast(&pos_x), sizeof(pos_x)); - file.read(reinterpret_cast(&pos_y), sizeof(pos_y)); + // lecture du numéro de joueur + char player_number; + file.read(&player_number, 1); + player->setPlayerNumber(player_number); - pos_x *= Constants::GRID; - pos_y *= Constants::GRID; + // lecture des propriétés communes des objets + Object::load(file, object); + file.seekg(1, file.cur); - std::shared_ptr player = - std::shared_ptr(new Player(pos_x, pos_y)); - // player->setPlayerNumber(i); - - return std::dynamic_pointer_cast(player); + return object; } sf::Vector2f Player::getForces(const Manager& manager, const std::vector& objects) const {