attribution automatique des numéros de joueurs
This commit is contained in:
parent
48adba301e
commit
201b99b24a
|
@ -5,6 +5,7 @@
|
|||
#include <vector>
|
||||
#include "state.hpp"
|
||||
#include "object.hpp"
|
||||
#include "player.hpp"
|
||||
#include "manager.hpp"
|
||||
#include "resource_manager.hpp"
|
||||
|
||||
|
@ -30,6 +31,7 @@ private:
|
|||
std::string music;
|
||||
|
||||
std::vector<Object::Ptr> objects;
|
||||
std::vector<Player::Ptr> players;
|
||||
std::vector<sf::Vector2f> zone;
|
||||
|
||||
protected:
|
||||
|
@ -146,10 +148,30 @@ public:
|
|||
std::vector<Object::Ptr>& getObjects();
|
||||
|
||||
/**
|
||||
* Récupère la liste des objets (version constante)
|
||||
* Récupère la liste des objets (constante)
|
||||
*/
|
||||
const std::vector<Object::Ptr>& getObjects() const;
|
||||
|
||||
/**
|
||||
* Récupère la liste des joueurs
|
||||
*/
|
||||
std::vector<Player::Ptr>& getPlayers();
|
||||
|
||||
/**
|
||||
* Récupère la liste des joueurs (constante)
|
||||
*/
|
||||
const std::vector<Player::Ptr>& getPlayers() const;
|
||||
|
||||
/**
|
||||
* Ajoute un nouvel objet
|
||||
*/
|
||||
void addObject(Object::Ptr object);
|
||||
|
||||
/**
|
||||
* Supprime l'objet donné
|
||||
*/
|
||||
void removeObject(Object::Ptr object);
|
||||
|
||||
/**
|
||||
* Récupère la zone du niveau
|
||||
*/
|
||||
|
|
Binary file not shown.
|
@ -189,12 +189,8 @@ void Editor::processEvent(const sf::Event& event) {
|
|||
if (event.type == sf::Event::KeyPressed) {
|
||||
// appui sur suppr : suppression des blocs sélectionnés
|
||||
if (event.key.code == sf::Keyboard::Delete) {
|
||||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
|
||||
for (unsigned int i = 0; i < selection.size(); i++) {
|
||||
objects.erase(std::remove(
|
||||
objects.begin(), objects.end(), selection[i]
|
||||
), objects.end());
|
||||
removeObject(selection[i]);
|
||||
}
|
||||
|
||||
clearSelection();
|
||||
|
@ -299,7 +295,7 @@ void Editor::draw() {
|
|||
}
|
||||
|
||||
Object::Ptr Editor::getObject(sf::Vector2f position) {
|
||||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
const std::vector<Object::Ptr>& objects = getObjects();
|
||||
|
||||
for (unsigned int i = 0; i < objects.size(); i++) {
|
||||
if (objects[i]->getAABB().contains(position)) {
|
||||
|
@ -323,7 +319,7 @@ sf::Vector2f* Editor::getControlPoint(sf::Vector2f position) {
|
|||
}
|
||||
|
||||
Object::Ptr Editor::addObject(sf::Vector2f position) {
|
||||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
const std::vector<Object::Ptr>& objects = getObjects();
|
||||
|
||||
// on arrondit à l'unité de grille la plus proche
|
||||
position = roundVectorToGrid(position);
|
||||
|
@ -346,7 +342,7 @@ Object::Ptr Editor::addObject(sf::Vector2f position) {
|
|||
}
|
||||
|
||||
if (!overlaps) {
|
||||
objects.push_back(object);
|
||||
Level::addObject(object);
|
||||
return object;
|
||||
}
|
||||
|
||||
|
@ -358,17 +354,12 @@ void Editor::removeObject(Object::Ptr object) {
|
|||
return;
|
||||
}
|
||||
|
||||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
|
||||
// on supprime l'objet de la sélection
|
||||
selection.erase(std::remove(
|
||||
selection.begin(), selection.end(), object
|
||||
), selection.end());
|
||||
|
||||
// on supprime l'objet de la liste d'objets
|
||||
objects.erase(std::remove(
|
||||
objects.begin(), objects.end(), object
|
||||
), objects.end());
|
||||
Level::removeObject(object);
|
||||
}
|
||||
|
||||
void Editor::removeControlPoint(sf::Vector2f* control_point) {
|
||||
|
@ -421,7 +412,7 @@ void Editor::select(sf::Vector2f position, Editor::SelectionMode mode) {
|
|||
}
|
||||
|
||||
void Editor::select(sf::Vector2f top_left, sf::Vector2f bottom_right) {
|
||||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
const std::vector<Object::Ptr>& objects = getObjects();
|
||||
sf::FloatRect selection_rect(
|
||||
std::min(top_left.x, bottom_right.x),
|
||||
std::min(top_left.y, bottom_right.y),
|
||||
|
@ -448,7 +439,7 @@ void Editor::clearSelection() {
|
|||
}
|
||||
|
||||
void Editor::selectAll() {
|
||||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
const std::vector<Object::Ptr>& objects = getObjects();
|
||||
|
||||
for (unsigned int i = 0; i < objects.size(); i++) {
|
||||
objects[i]->setSelected(true);
|
||||
|
@ -470,7 +461,7 @@ void Editor::test() {
|
|||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
|
||||
for (unsigned int i = 0; i < objects.size(); i++) {
|
||||
game->getObjects().push_back(objects[i]->clone());
|
||||
game->addObject(objects[i]->clone());
|
||||
}
|
||||
|
||||
// copie de la zone de jeu
|
||||
|
|
33
src/game.cpp
33
src/game.cpp
|
@ -91,19 +91,15 @@ void Game::draw() {
|
|||
}
|
||||
|
||||
void Game::ensureCentered() {
|
||||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
std::vector<Player::Ptr>& players = getPlayers();
|
||||
|
||||
sf::Vector2f total_position;
|
||||
int player_count = 0;
|
||||
|
||||
for (unsigned int i = 0; i < objects.size(); i++) {
|
||||
if (Player* player = dynamic_cast<Player*>(objects[i].get())) {
|
||||
total_position += player->getPosition();
|
||||
player_count++;
|
||||
}
|
||||
}
|
||||
|
||||
sf::View camera = getCamera();
|
||||
unsigned int player_count = players.size();
|
||||
|
||||
for (unsigned int i = 0; i < player_count; i++) {
|
||||
total_position += players[i]->getPosition();
|
||||
}
|
||||
|
||||
if (player_count == 0) {
|
||||
// on évite la division par zéro
|
||||
|
@ -118,13 +114,14 @@ void Game::ensureCentered() {
|
|||
|
||||
void Game::update() {
|
||||
std::vector<CollisionData> colliding;
|
||||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
|
||||
// détection des objets en collision
|
||||
for (unsigned int i = 0; i < getObjects().size(); i++) {
|
||||
Object::Ptr obj_a = getObjects()[i];
|
||||
for (unsigned int i = 0; i < objects.size(); i++) {
|
||||
Object::Ptr obj_a = objects[i];
|
||||
|
||||
for (unsigned int j = i + 1; j < getObjects().size(); j++) {
|
||||
Object::Ptr obj_b = getObjects()[j];
|
||||
for (unsigned int j = i + 1; j < objects.size(); j++) {
|
||||
Object::Ptr obj_b = objects[j];
|
||||
CollisionData data(*obj_a, *obj_b);
|
||||
|
||||
if (obj_a->detectCollision(*obj_b, data)) {
|
||||
|
@ -134,8 +131,8 @@ void Game::update() {
|
|||
}
|
||||
|
||||
// intégration des forces dans la vitesse (seconde moitié)
|
||||
for (unsigned int i = 0; i < getObjects().size(); i++) {
|
||||
getObjects()[i]->updateVelocity(*this);
|
||||
for (unsigned int i = 0; i < objects.size(); i++) {
|
||||
objects[i]->updateVelocity(*this);
|
||||
}
|
||||
|
||||
// résolution des collisions détectées
|
||||
|
@ -145,8 +142,8 @@ void Game::update() {
|
|||
}
|
||||
|
||||
// intégration de la vitesse dans la position
|
||||
for (unsigned int i = 0; i < getObjects().size(); i++) {
|
||||
getObjects()[i]->updatePosition();
|
||||
for (unsigned int i = 0; i < objects.size(); i++) {
|
||||
objects[i]->updatePosition();
|
||||
}
|
||||
|
||||
// application de la correction positionnelle
|
||||
|
|
|
@ -126,7 +126,13 @@ void Level::load(std::string filename) {
|
|||
}
|
||||
|
||||
// lecture de l'objet
|
||||
objects.push_back(object_type_map[object_type](file));
|
||||
Object::Ptr read_object = object_type_map[object_type](file);
|
||||
objects.push_back(read_object);
|
||||
|
||||
// si c'est un joueur, on l'ajoute à la liste
|
||||
if ((unsigned int) object_type == Player::TYPE_ID) {
|
||||
players.push_back(std::dynamic_pointer_cast<Player>(read_object));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -198,7 +204,7 @@ void Level::begin() {
|
|||
camera.setSize(window_size.x, window_size.y);
|
||||
camera.setCenter(0, 0);
|
||||
camera_angle = 180.f;
|
||||
|
||||
|
||||
gravity_direction = GravityDirection::SOUTH;
|
||||
}
|
||||
|
||||
|
@ -334,7 +340,7 @@ void Level::setGravityDirection(GravityDirection set_gravity_direction) {
|
|||
gravity_direction = set_gravity_direction;
|
||||
}
|
||||
|
||||
std::vector<Object::Ptr>& Level::getObjects() {
|
||||
std::vector<Object::Ptr>& Level::getObjects() {
|
||||
return objects;
|
||||
}
|
||||
|
||||
|
@ -342,6 +348,47 @@ const std::vector<Object::Ptr>& Level::getObjects() const {
|
|||
return objects;
|
||||
}
|
||||
|
||||
std::vector<Player::Ptr>& Level::getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
const std::vector<Player::Ptr>& Level::getPlayers() const {
|
||||
return players;
|
||||
}
|
||||
|
||||
void Level::addObject(Object::Ptr object) {
|
||||
// si c'est un joueur, on le met dans le tableau des joueurs
|
||||
// et on lui attribue un numéro
|
||||
if (object->getTypeId() == Player::TYPE_ID) {
|
||||
Player::Ptr player = std::dynamic_pointer_cast<Player>(object);
|
||||
|
||||
player->setPlayerNumber(players.size());
|
||||
players.push_back(std::dynamic_pointer_cast<Player>(object));
|
||||
}
|
||||
|
||||
objects.push_back(object);
|
||||
}
|
||||
|
||||
void Level::removeObject(Object::Ptr object) {
|
||||
// si c'est un joueur, on le supprime de la liste des joueurs
|
||||
if (object->getTypeId() == Player::TYPE_ID) {
|
||||
Player::Ptr player = std::dynamic_pointer_cast<Player>(object);
|
||||
players.erase(std::remove(
|
||||
players.begin(), players.end(), player
|
||||
), players.end());
|
||||
|
||||
// on réattribue les numéros de joueurs
|
||||
for (unsigned int i = 0; i < players.size(); i++) {
|
||||
players[i]->setPlayerNumber(i);
|
||||
}
|
||||
}
|
||||
|
||||
// on supprime l'objet de la liste d'objets
|
||||
objects.erase(std::remove(
|
||||
objects.begin(), objects.end(), object
|
||||
), objects.end());
|
||||
}
|
||||
|
||||
std::vector<sf::Vector2f>& Level::getZone() {
|
||||
return zone;
|
||||
}
|
||||
|
|
|
@ -21,11 +21,6 @@ Object::Ptr Player::clone() const {
|
|||
void Player::init(std::ifstream& file, Object::Ptr object) {
|
||||
Player::Ptr player = std::dynamic_pointer_cast<Player>(object);
|
||||
|
||||
// lecture du numéro de joueur
|
||||
char player_number;
|
||||
file.read(&player_number, 1);
|
||||
player->setPlayerNumber(player_number);
|
||||
|
||||
// lecture des propriétés communes des objets
|
||||
Object::init(file, object);
|
||||
}
|
||||
|
@ -37,10 +32,6 @@ Object::Ptr Player::load(std::ifstream& file) {
|
|||
}
|
||||
|
||||
void Player::save(std::ofstream& file) const {
|
||||
// écriture du numéro de joueur
|
||||
char write_player_number = player_number;
|
||||
file.write(&write_player_number, 1);
|
||||
|
||||
// écriture des propriétés communes
|
||||
Object::save(file);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue