Nouvelle caméra suivant les joueurs dans game
This commit is contained in:
parent
ebb203833b
commit
f258e178c6
|
@ -33,6 +33,11 @@ protected:
|
||||||
*/
|
*/
|
||||||
virtual void processEvent(const sf::Event& event);
|
virtual void processEvent(const sf::Event& event);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* S'assure que la caméra est centrée sur les joueurs
|
||||||
|
*/
|
||||||
|
void ensureCentered();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Game(Manager& manager);
|
Game(Manager& manager);
|
||||||
virtual ~Game();
|
virtual ~Game();
|
||||||
|
|
|
@ -38,7 +38,13 @@ protected:
|
||||||
* Convertit les coordonnées à l'écran en coordonnées du monde
|
* Convertit les coordonnées à l'écran en coordonnées du monde
|
||||||
* d'après la vue caméra
|
* d'après la vue caméra
|
||||||
*/
|
*/
|
||||||
sf::Vector2f convertCoords(sf::Vector2i initial);
|
sf::Vector2f pixelToCoords(sf::Vector2i pixel);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convertit les coordonnées du monde en coordonnées à l'écran
|
||||||
|
* d'après la vue caméra
|
||||||
|
*/
|
||||||
|
sf::Vector2i coordsToPixel(sf::Vector2f coords);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Level(Manager& manager);
|
Level(Manager& manager);
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
class Player : public Object {
|
class Player : public Object {
|
||||||
private:
|
private:
|
||||||
mutable sf::Sprite sprite;
|
mutable sf::CircleShape sprite;
|
||||||
unsigned int player_number;
|
unsigned int player_number;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
49
src/game.cpp
49
src/game.cpp
|
@ -3,6 +3,8 @@
|
||||||
#include "player.hpp"
|
#include "player.hpp"
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
|
|
||||||
|
const float CAMERA_TOLERANCE_RATIO = 2.f / 3.f;
|
||||||
|
|
||||||
Game::Game(Manager& manager) : Level(manager),
|
Game::Game(Manager& manager) : Level(manager),
|
||||||
widget_timer(manager, false),
|
widget_timer(manager, false),
|
||||||
next_frame_time(manager.getCurrentTime()),
|
next_frame_time(manager.getCurrentTime()),
|
||||||
|
@ -29,32 +31,8 @@ void Game::frame() {
|
||||||
// on met à jour la physique d'un cran temporel
|
// on met à jour la physique d'un cran temporel
|
||||||
update();
|
update();
|
||||||
|
|
||||||
// on place la caméra à la position médiane des joueurs
|
// on s'assure que la caméré soit centrée sur nos joueurs
|
||||||
// s'ils sont trop éloignés
|
ensureCentered();
|
||||||
std::vector<ObjectPtr>& objects = getObjects();
|
|
||||||
sf::Vector2f position_first_player;
|
|
||||||
sf::Vector2f position_second_player;
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < objects.size(); i++) {
|
|
||||||
if (Player* player = dynamic_cast<Player*>(objects[i].get())) {
|
|
||||||
if (player->getPlayerNumber() == 0) {
|
|
||||||
position_first_player = player->getPosition();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (player->getPlayerNumber() == 1) {
|
|
||||||
position_second_player = player->getPosition();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sf::Vector2f diff = position_second_player - position_first_player;
|
|
||||||
sf::View camera = getCamera();
|
|
||||||
|
|
||||||
if (std::pow(diff.x, 2) + std::pow(diff.y, 2) > std::pow(camera.getSize().x / 3, 2)) {
|
|
||||||
sf::Vector2f middle = (position_second_player + position_first_player) / 2.f;
|
|
||||||
camera.setCenter(floor(middle.x), floor(middle.y));
|
|
||||||
setCamera(camera);
|
|
||||||
}
|
|
||||||
|
|
||||||
// si on a encore suffisamment de temps, on dessine
|
// si on a encore suffisamment de temps, on dessine
|
||||||
if (current_time < next_frame_time) {
|
if (current_time < next_frame_time) {
|
||||||
|
@ -88,6 +66,25 @@ void Game::draw() {
|
||||||
widget_timer.draw(sf::Vector2f(window_size.x / 2 - 50, 0));
|
widget_timer.draw(sf::Vector2f(window_size.x / 2 - 50, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Game::ensureCentered() {
|
||||||
|
sf::Vector2i window_size = (sf::Vector2i) manager.getWindow().getSize();
|
||||||
|
std::vector<ObjectPtr>& objects = getObjects();
|
||||||
|
|
||||||
|
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();
|
||||||
|
camera.setCenter(total_position / (float) player_count);
|
||||||
|
setCamera(camera);
|
||||||
|
}
|
||||||
|
|
||||||
void Game::update() {
|
void Game::update() {
|
||||||
std::vector<CollisionData> colliding;
|
std::vector<CollisionData> colliding;
|
||||||
|
|
||||||
|
|
|
@ -202,15 +202,26 @@ sf::View Level::getCamera() {
|
||||||
return camera;
|
return camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Vector2f Level::convertCoords(sf::Vector2i initial) {
|
sf::Vector2f Level::pixelToCoords(sf::Vector2i pixel) {
|
||||||
sf::RenderWindow& window = manager.getWindow();
|
sf::RenderWindow& window = manager.getWindow();
|
||||||
sf::View old_view = window.getView();
|
sf::View old_view = window.getView();
|
||||||
|
|
||||||
window.setView(camera);
|
window.setView(camera);
|
||||||
sf::Vector2f converted = window.mapPixelToCoords(initial);
|
sf::Vector2f coords = window.mapPixelToCoords(pixel);
|
||||||
window.setView(old_view);
|
window.setView(old_view);
|
||||||
|
|
||||||
return converted;
|
return coords;
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::Vector2i Level::coordsToPixel(sf::Vector2f coords) {
|
||||||
|
sf::RenderWindow& window = manager.getWindow();
|
||||||
|
sf::View old_view = window.getView();
|
||||||
|
|
||||||
|
window.setView(camera);
|
||||||
|
sf::Vector2i pixel = window.mapCoordsToPixel(coords);
|
||||||
|
window.setView(old_view);
|
||||||
|
|
||||||
|
return pixel;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::setCamera(sf::View set_camera) {
|
void Level::setCamera(sf::View set_camera) {
|
||||||
|
|
|
@ -8,6 +8,9 @@ const unsigned int Player::TYPE_ID = 1;
|
||||||
Player::Player() : Object() {
|
Player::Player() : Object() {
|
||||||
// déplacement de l'origine au centre de la balle
|
// déplacement de l'origine au centre de la balle
|
||||||
sprite.setOrigin(sf::Vector2f(getRadius(), getRadius()));
|
sprite.setOrigin(sf::Vector2f(getRadius(), getRadius()));
|
||||||
|
sprite.setRadius(getRadius());
|
||||||
|
sprite.setOutlineColor(sf::Color::Black);
|
||||||
|
sprite.setOutlineThickness(1.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
Player::~Player() {}
|
Player::~Player() {}
|
||||||
|
@ -62,20 +65,20 @@ sf::Vector2f Player::getForces(const Manager& manager, const std::vector<ObjectP
|
||||||
void Player::draw(Manager& manager) {
|
void Player::draw(Manager& manager) {
|
||||||
// utilisation de la texture
|
// utilisation de la texture
|
||||||
sprite.setTexture(
|
sprite.setTexture(
|
||||||
manager.getResourceManager().getTexture("ball.png")
|
&manager.getResourceManager().getTexture("player.tga")
|
||||||
);
|
);
|
||||||
|
|
||||||
// si le joueur est sélectionné, on le colore en rouge vif
|
// si le joueur est sélectionné, on le colore en rouge vif
|
||||||
if (isSelected()) {
|
// if (isSelected()) {
|
||||||
sprite.setColor(sf::Color(255, 0, 0));
|
// sprite.setColor(sf::Color(255, 0, 0));
|
||||||
} else {
|
// } else {
|
||||||
// coloration du joueur en fonction de son numéro
|
// coloration du joueur en fonction de son numéro
|
||||||
if (getPlayerNumber() == 0) {
|
if (getPlayerNumber() == 0) {
|
||||||
sprite.setColor(sf::Color(239, 83, 80));
|
sprite.setFillColor(sf::Color(239, 83, 80));
|
||||||
} else if (getPlayerNumber() == 1) {
|
} else if (getPlayerNumber() == 1) {
|
||||||
sprite.setColor(sf::Color(92, 107, 192));
|
sprite.setFillColor(sf::Color(92, 107, 192));
|
||||||
}
|
}
|
||||||
}
|
// }
|
||||||
|
|
||||||
// déplacement du sprite à la position de la balle
|
// déplacement du sprite à la position de la balle
|
||||||
sprite.setPosition(getPosition());
|
sprite.setPosition(getPosition());
|
||||||
|
|
Loading…
Reference in New Issue