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 "level.hpp"
|
||||
#include "game.hpp"
|
||||
|
||||
/**
|
||||
* La classe Editor permet l'édition de
|
||||
|
|
|
@ -21,6 +21,7 @@ public:
|
|||
|
||||
private:
|
||||
Mode mode;
|
||||
bool test;
|
||||
|
||||
sf::Time next_frame_time;
|
||||
unsigned int skipped_frames;
|
||||
|
@ -60,7 +61,7 @@ protected:
|
|||
void ensureCentered();
|
||||
|
||||
public:
|
||||
Game(Manager& manager);
|
||||
Game(Manager& manager, bool test = false);
|
||||
virtual ~Game();
|
||||
|
||||
/**
|
||||
|
@ -94,6 +95,11 @@ public:
|
|||
* Modifie le temps total du niveau
|
||||
*/
|
||||
virtual void setTotalTime(int set_total_time);
|
||||
|
||||
/**
|
||||
* Récupère si on est en mode test
|
||||
*/
|
||||
bool isTest();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -213,6 +213,12 @@ public:
|
|||
* Modifie la caméra
|
||||
*/
|
||||
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
|
||||
|
|
|
@ -65,6 +65,11 @@ void Editor::enable() {
|
|||
getManager().setTitle(sf::String(L"Édition de ") + getName());
|
||||
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
|
||||
ResourceManager::get().playMusic("editor.ogg");
|
||||
|
||||
|
@ -504,30 +509,31 @@ void Editor::selectAll() {
|
|||
}
|
||||
|
||||
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();
|
||||
|
||||
// copie des propriétés
|
||||
game->setName(getName());
|
||||
game->setTotalTime(getTotalTime());
|
||||
game->setBackground(getBackground());
|
||||
game->setMusic(getMusic());
|
||||
test_game->setName(getName());
|
||||
test_game->setTotalTime(getTotalTime());
|
||||
test_game->setBackground(getBackground());
|
||||
test_game->setMusic(getMusic());
|
||||
|
||||
test_game->getObjects().clear();
|
||||
test_game->getZone().clear();
|
||||
|
||||
// copie des objets du niveau vers le jeu
|
||||
std::vector<Object::Ptr>& objects = getObjects();
|
||||
game->getObjects().clear();
|
||||
|
||||
for (auto it = objects.begin(); it != objects.end(); it++) {
|
||||
game->addObject((*it)->clone());
|
||||
for (auto const &object : getObjects()) {
|
||||
test_game->addObject(object->clone());
|
||||
}
|
||||
|
||||
// copie de la zone de jeu
|
||||
std::vector<sf::Vector2f>& zone = getZone();
|
||||
game->getZone().clear();
|
||||
|
||||
for (auto it = zone.begin(); it != zone.end(); it++) {
|
||||
game->getZone().push_back(*it);
|
||||
for (auto const &point : getZone()) {
|
||||
test_game->getZone().push_back(point);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Game::Game(Manager& manager) : Level(manager),
|
||||
mode(Game::Mode::NORMAL),
|
||||
Game::Game(Manager& manager, bool test) : Level(manager),
|
||||
mode(Game::Mode::NORMAL), test(test),
|
||||
next_frame_time(manager.getCurrentTime()),
|
||||
skipped_frames(0),
|
||||
death_cause(Game::DeathCause::NONE) {}
|
||||
|
@ -31,6 +31,14 @@ void Game::enable() {
|
|||
getManager().setTitle(getName());
|
||||
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
|
||||
if (getMusic() != "") {
|
||||
ResourceManager::get().playMusic("levels/" + getMusic());
|
||||
|
@ -43,20 +51,28 @@ void Game::processEvent(const sf::Event& event) {
|
|||
Level::processEvent(event);
|
||||
|
||||
if (event.type == sf::Event::KeyPressed) {
|
||||
// appui sur espace : retour
|
||||
if (event.key.code == sf::Keyboard::Space) {
|
||||
// en mode test, retour, échap et sortie pour revenir à l'éditeur
|
||||
if (event.key.code == sf::Keyboard::Space ||
|
||||
event.key.code == sf::Keyboard::Escape ||
|
||||
event.key.code == sf::Keyboard::BackSpace) {
|
||||
getManager().popState();
|
||||
return;
|
||||
}
|
||||
|
||||
// appui sur retour échap : échange entre le mode pause et normal
|
||||
if (event.key.code == sf::Keyboard::Escape ||
|
||||
event.key.code == sf::Keyboard::BackSpace) {
|
||||
// appui sur espace : échange entre le mode pause et normal
|
||||
if (event.key.code == sf::Keyboard::Space) {
|
||||
if (getMode() == Game::Mode::NORMAL) {
|
||||
setMode(Game::Mode::PAUSED);
|
||||
} else if (getMode() == Game::Mode::PAUSED) {
|
||||
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() {
|
||||
std::vector<Player::Ptr>& players = getPlayers();
|
||||
unsigned int player_count = players.size();
|
||||
|
||||
sf::View camera = getCamera();
|
||||
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++) {
|
||||
position_sum += players[i]->getPosition();
|
||||
}
|
||||
|
||||
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);
|
||||
// on anime le centre vers le centre des joueurs
|
||||
previous_center.x = Utility::animateValue(previous_center.x, 5, new_center.x);
|
||||
previous_center.y = Utility::animateValue(previous_center.y, 5, new_center.y);
|
||||
|
||||
camera.setCenter(previous_center);
|
||||
setCamera(camera);
|
||||
|
@ -293,3 +294,7 @@ void Game::setTotalTime(int set_total_time) {
|
|||
Level::setTotalTime(set_total_time);
|
||||
time_left = getTotalTime();
|
||||
}
|
||||
|
||||
bool Game::isTest() {
|
||||
return test;
|
||||
}
|
||||
|
|
|
@ -103,7 +103,6 @@ void Level::enable() {
|
|||
sf::Vector2u window_size = getManager().getWindow().getSize();
|
||||
|
||||
camera.setSize(window_size.x, window_size.y);
|
||||
camera.setCenter(0, 0);
|
||||
camera_angle = 180.f;
|
||||
|
||||
// on affiche la barre d'actions
|
||||
|
@ -559,3 +558,17 @@ sf::Vector2i Level::coordsToPixel(sf::Vector2f coords) {
|
|||
void Level::setCamera(sf::View 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