Positionnement fluide de la caméra

This commit is contained in:
Mattéo Delabre 2016-04-17 23:46:09 +02:00
parent 774b60464c
commit c537f2eac0
1 changed files with 13 additions and 6 deletions

View File

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include "utility.hpp"
#include "manager.hpp" #include "manager.hpp"
#include "game.hpp" #include "game.hpp"
#include "player.hpp" #include "player.hpp"
@ -148,23 +149,29 @@ void Game::draw() {
void Game::ensureCentered() { void Game::ensureCentered() {
std::vector<Player::Ptr>& players = getPlayers(); std::vector<Player::Ptr>& players = getPlayers();
sf::Vector2f total_position;
sf::View camera = getCamera();
unsigned int player_count = players.size(); unsigned int player_count = players.size();
sf::View camera = getCamera();
sf::Vector2f previous_center = camera.getCenter();
sf::Vector2f position_sum, goal_center;
for (unsigned int i = 0; i < player_count; i++) { for (unsigned int i = 0; i < player_count; i++) {
total_position += players[i]->getPosition(); position_sum += players[i]->getPosition();
} }
if (player_count == 0) { if (player_count == 0) {
// on évite la division par zéro // on évite la division par zéro
camera.setCenter(sf::Vector2f(0, 0)); goal_center = sf::Vector2f(0, 0);
} else { } else {
// on place la caméra à la position médiane de tous les joueurs // on place la caméra à la position médiane de tous les joueurs
camera.setCenter(total_position / (float) player_count); goal_center = position_sum / (float) player_count;
} }
// on anime le centre vers la nouvelle position
previous_center.x = Utility::animateValue(previous_center.x, 5, goal_center.x);
previous_center.y = Utility::animateValue(previous_center.y, 5, goal_center.y);
camera.setCenter(previous_center);
setCamera(camera); setCamera(camera);
} }