Mode test dans jeu nouveaux raccourcis, amélioration transition caméra
This commit is contained in:
parent
656bd60643
commit
a0a48f98ad
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "../gui/object_toolbar.hpp"
|
#include "../gui/object_toolbar.hpp"
|
||||||
#include "level.hpp"
|
#include "level.hpp"
|
||||||
|
#include "game.hpp"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La classe Editor permet l'édition de
|
* La classe Editor permet l'édition de
|
||||||
|
|
|
@ -21,6 +21,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mode mode;
|
Mode mode;
|
||||||
|
bool test;
|
||||||
|
|
||||||
sf::Time next_frame_time;
|
sf::Time next_frame_time;
|
||||||
unsigned int skipped_frames;
|
unsigned int skipped_frames;
|
||||||
|
@ -60,7 +61,7 @@ protected:
|
||||||
void ensureCentered();
|
void ensureCentered();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Game(Manager& manager);
|
Game(Manager& manager, bool test = false);
|
||||||
virtual ~Game();
|
virtual ~Game();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -94,6 +95,11 @@ public:
|
||||||
* Modifie le temps total du niveau
|
* Modifie le temps total du niveau
|
||||||
*/
|
*/
|
||||||
virtual void setTotalTime(int set_total_time);
|
virtual void setTotalTime(int set_total_time);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récupère si on est en mode test
|
||||||
|
*/
|
||||||
|
bool isTest();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -213,6 +213,12 @@ public:
|
||||||
* Modifie la caméra
|
* Modifie la caméra
|
||||||
*/
|
*/
|
||||||
void setCamera(sf::View set_camera);
|
void setCamera(sf::View set_camera);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récupère le centre de tous les joueurs ou
|
||||||
|
* zéro si aucun joueur
|
||||||
|
*/
|
||||||
|
sf::Vector2f getPlayerCenter();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -65,6 +65,11 @@ void Editor::enable() {
|
||||||
getManager().setTitle(sf::String(L"Édition de ") + getName());
|
getManager().setTitle(sf::String(L"Édition de ") + getName());
|
||||||
getManager().getWindow().setFramerateLimit(Manager::FPS);
|
getManager().getWindow().setFramerateLimit(Manager::FPS);
|
||||||
|
|
||||||
|
// on positionne la caméra au centre des joueurs
|
||||||
|
sf::View camera = getCamera();
|
||||||
|
camera.setCenter(getPlayerCenter());
|
||||||
|
setCamera(camera);
|
||||||
|
|
||||||
// joue la musique de l'éditeur
|
// joue la musique de l'éditeur
|
||||||
ResourceManager::get().playMusic("editor.ogg");
|
ResourceManager::get().playMusic("editor.ogg");
|
||||||
|
|
||||||
|
@ -504,30 +509,31 @@ void Editor::selectAll() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Editor::test() {
|
void Editor::test() {
|
||||||
auto game = std::unique_ptr<Game>(new Game(getManager()));
|
// création d'une partie en mode test
|
||||||
|
auto test_game = std::unique_ptr<Game>(new Game(getManager(), true));
|
||||||
clearSelection();
|
clearSelection();
|
||||||
|
|
||||||
// copie des propriétés
|
// copie des propriétés
|
||||||
game->setName(getName());
|
test_game->setName(getName());
|
||||||
game->setTotalTime(getTotalTime());
|
test_game->setTotalTime(getTotalTime());
|
||||||
game->setBackground(getBackground());
|
test_game->setBackground(getBackground());
|
||||||
game->setMusic(getMusic());
|
test_game->setMusic(getMusic());
|
||||||
|
|
||||||
|
test_game->getObjects().clear();
|
||||||
|
test_game->getZone().clear();
|
||||||
|
|
||||||
// copie des objets du niveau vers le jeu
|
// copie des objets du niveau vers le jeu
|
||||||
std::vector<Object::Ptr>& objects = getObjects();
|
for (auto const &object : getObjects()) {
|
||||||
game->getObjects().clear();
|
test_game->addObject(object->clone());
|
||||||
|
|
||||||
for (auto it = objects.begin(); it != objects.end(); it++) {
|
|
||||||
game->addObject((*it)->clone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// copie de la zone de jeu
|
// copie de la zone de jeu
|
||||||
std::vector<sf::Vector2f>& zone = getZone();
|
for (auto const &point : getZone()) {
|
||||||
game->getZone().clear();
|
test_game->getZone().push_back(point);
|
||||||
|
|
||||||
for (auto it = zone.begin(); it != zone.end(); it++) {
|
|
||||||
game->getZone().push_back(*it);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getManager().pushState(std::move(game));
|
// repositionnement de la caméra
|
||||||
|
test_game->setCamera(getCamera());
|
||||||
|
|
||||||
|
getManager().pushState(std::move(test_game));
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ namespace {
|
||||||
const unsigned int MAX_FRAME_SKIP = 5;
|
const unsigned int MAX_FRAME_SKIP = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::Game(Manager& manager) : Level(manager),
|
Game::Game(Manager& manager, bool test) : Level(manager),
|
||||||
mode(Game::Mode::NORMAL),
|
mode(Game::Mode::NORMAL), test(test),
|
||||||
next_frame_time(manager.getCurrentTime()),
|
next_frame_time(manager.getCurrentTime()),
|
||||||
skipped_frames(0),
|
skipped_frames(0),
|
||||||
death_cause(Game::DeathCause::NONE) {}
|
death_cause(Game::DeathCause::NONE) {}
|
||||||
|
@ -31,6 +31,14 @@ void Game::enable() {
|
||||||
getManager().setTitle(getName());
|
getManager().setTitle(getName());
|
||||||
getManager().getWindow().setFramerateLimit(0);
|
getManager().getWindow().setFramerateLimit(0);
|
||||||
|
|
||||||
|
// en dehors du mode test, on positionne la caméra directement
|
||||||
|
// au centre des joueurs
|
||||||
|
if (!isTest()) {
|
||||||
|
sf::View camera = getCamera();
|
||||||
|
camera.setCenter(getPlayerCenter());
|
||||||
|
setCamera(camera);
|
||||||
|
}
|
||||||
|
|
||||||
// si musique il y a, on la joue
|
// si musique il y a, on la joue
|
||||||
if (getMusic() != "") {
|
if (getMusic() != "") {
|
||||||
ResourceManager::get().playMusic("levels/" + getMusic());
|
ResourceManager::get().playMusic("levels/" + getMusic());
|
||||||
|
@ -43,20 +51,28 @@ void Game::processEvent(const sf::Event& event) {
|
||||||
Level::processEvent(event);
|
Level::processEvent(event);
|
||||||
|
|
||||||
if (event.type == sf::Event::KeyPressed) {
|
if (event.type == sf::Event::KeyPressed) {
|
||||||
// appui sur espace : retour
|
// en mode test, retour, échap et sortie pour revenir à l'éditeur
|
||||||
if (event.key.code == sf::Keyboard::Space) {
|
if (event.key.code == sf::Keyboard::Space ||
|
||||||
|
event.key.code == sf::Keyboard::Escape ||
|
||||||
|
event.key.code == sf::Keyboard::BackSpace) {
|
||||||
getManager().popState();
|
getManager().popState();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// appui sur retour échap : échange entre le mode pause et normal
|
// appui sur espace : échange entre le mode pause et normal
|
||||||
if (event.key.code == sf::Keyboard::Escape ||
|
if (event.key.code == sf::Keyboard::Space) {
|
||||||
event.key.code == sf::Keyboard::BackSpace) {
|
|
||||||
if (getMode() == Game::Mode::NORMAL) {
|
if (getMode() == Game::Mode::NORMAL) {
|
||||||
setMode(Game::Mode::PAUSED);
|
setMode(Game::Mode::PAUSED);
|
||||||
} else if (getMode() == Game::Mode::PAUSED) {
|
} else if (getMode() == Game::Mode::PAUSED) {
|
||||||
setMode(Game::Mode::NORMAL);
|
setMode(Game::Mode::NORMAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// appui sur retour ou échap : sortie
|
||||||
|
if (event.key.code == sf::Keyboard::Escape ||
|
||||||
|
event.key.code == sf::Keyboard::BackSpace) {
|
||||||
|
getManager().popState();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,28 +152,13 @@ void Game::draw() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::ensureCentered() {
|
void Game::ensureCentered() {
|
||||||
std::vector<Player::Ptr>& players = getPlayers();
|
|
||||||
unsigned int player_count = players.size();
|
|
||||||
|
|
||||||
sf::View camera = getCamera();
|
sf::View camera = getCamera();
|
||||||
sf::Vector2f previous_center = camera.getCenter();
|
sf::Vector2f previous_center = camera.getCenter();
|
||||||
sf::Vector2f position_sum, goal_center;
|
sf::Vector2f new_center = getPlayerCenter();
|
||||||
|
|
||||||
for (unsigned int i = 0; i < player_count; i++) {
|
// on anime le centre vers le centre des joueurs
|
||||||
position_sum += players[i]->getPosition();
|
previous_center.x = Utility::animateValue(previous_center.x, 5, new_center.x);
|
||||||
}
|
previous_center.y = Utility::animateValue(previous_center.y, 5, new_center.y);
|
||||||
|
|
||||||
if (player_count == 0) {
|
|
||||||
// on évite la division par zéro
|
|
||||||
goal_center = sf::Vector2f(0, 0);
|
|
||||||
} else {
|
|
||||||
// on place la caméra à la position médiane de tous les joueurs
|
|
||||||
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);
|
camera.setCenter(previous_center);
|
||||||
setCamera(camera);
|
setCamera(camera);
|
||||||
|
@ -293,3 +294,7 @@ void Game::setTotalTime(int set_total_time) {
|
||||||
Level::setTotalTime(set_total_time);
|
Level::setTotalTime(set_total_time);
|
||||||
time_left = getTotalTime();
|
time_left = getTotalTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Game::isTest() {
|
||||||
|
return test;
|
||||||
|
}
|
||||||
|
|
|
@ -103,7 +103,6 @@ void Level::enable() {
|
||||||
sf::Vector2u window_size = getManager().getWindow().getSize();
|
sf::Vector2u window_size = getManager().getWindow().getSize();
|
||||||
|
|
||||||
camera.setSize(window_size.x, window_size.y);
|
camera.setSize(window_size.x, window_size.y);
|
||||||
camera.setCenter(0, 0);
|
|
||||||
camera_angle = 180.f;
|
camera_angle = 180.f;
|
||||||
|
|
||||||
// on affiche la barre d'actions
|
// on affiche la barre d'actions
|
||||||
|
@ -559,3 +558,17 @@ sf::Vector2i Level::coordsToPixel(sf::Vector2f coords) {
|
||||||
void Level::setCamera(sf::View set_camera) {
|
void Level::setCamera(sf::View set_camera) {
|
||||||
camera = set_camera;
|
camera = set_camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sf::Vector2f Level::getPlayerCenter() {
|
||||||
|
sf::Vector2f sum;
|
||||||
|
|
||||||
|
for (auto const &player : getPlayers()) {
|
||||||
|
sum += player->getPosition();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getPlayers().size() == 0) {
|
||||||
|
return sf::Vector2f(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum / (float) getPlayers().size();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue