Ajout du bouton recommencer & fluidification de la caméra
This commit is contained in:
parent
a0a48f98ad
commit
7d46367eb5
|
@ -74,6 +74,11 @@ private:
|
||||||
*/
|
*/
|
||||||
void selectAll();
|
void selectAll();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lance le test du niveau
|
||||||
|
*/
|
||||||
|
void test();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/**
|
/**
|
||||||
* Demande le dessin d'une frame
|
* Demande le dessin d'une frame
|
||||||
|
@ -99,11 +104,6 @@ public:
|
||||||
* Traite l'événement donné
|
* Traite l'événement donné
|
||||||
*/
|
*/
|
||||||
void processEvent(const sf::Event& event) override;
|
void processEvent(const sf::Event& event) override;
|
||||||
|
|
||||||
/**
|
|
||||||
* Lance le test du niveau
|
|
||||||
*/
|
|
||||||
void test();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -43,6 +43,17 @@ private:
|
||||||
*/
|
*/
|
||||||
bool isInZone(Object::Ptr object);
|
bool isInZone(Object::Ptr object);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recommence le niveau depuis le début
|
||||||
|
*/
|
||||||
|
void restart();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Échange entre le mode pause et le mode normal
|
||||||
|
* (si on est dans un autre mode, ne fait rien)
|
||||||
|
*/
|
||||||
|
void switchPause();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/**
|
/**
|
||||||
* Demande le dessin d'une frame
|
* Demande le dessin d'une frame
|
||||||
|
@ -55,11 +66,6 @@ protected:
|
||||||
*/
|
*/
|
||||||
void draw() override;
|
void draw() override;
|
||||||
|
|
||||||
/**
|
|
||||||
* S'assure que la caméra est centrée sur les joueurs
|
|
||||||
*/
|
|
||||||
void ensureCentered();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Game(Manager& manager, bool test = false);
|
Game(Manager& manager, bool test = false);
|
||||||
virtual ~Game();
|
virtual ~Game();
|
||||||
|
|
|
@ -20,6 +20,8 @@ class Level : public State {
|
||||||
private:
|
private:
|
||||||
sf::View camera;
|
sf::View camera;
|
||||||
float camera_angle;
|
float camera_angle;
|
||||||
|
sf::Vector2f goal_center;
|
||||||
|
|
||||||
Utility::Direction gravity_direction;
|
Utility::Direction gravity_direction;
|
||||||
|
|
||||||
sf::String name;
|
sf::String name;
|
||||||
|
@ -214,6 +216,16 @@ public:
|
||||||
*/
|
*/
|
||||||
void setCamera(sf::View set_camera);
|
void setCamera(sf::View set_camera);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récupère la cible de l'animation de la caméra
|
||||||
|
*/
|
||||||
|
sf::Vector2f getCenterGoal();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modifie la cible de l'animation de la caméra
|
||||||
|
*/
|
||||||
|
void setCenterGoal(sf::Vector2f set_center);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Récupère le centre de tous les joueurs ou
|
* Récupère le centre de tous les joueurs ou
|
||||||
* zéro si aucun joueur
|
* zéro si aucun joueur
|
||||||
|
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 655 B |
Binary file not shown.
|
@ -208,7 +208,7 @@ void Editor::processEvent(const sf::Event& event) {
|
||||||
|
|
||||||
// sinon, on déplace la vue
|
// sinon, on déplace la vue
|
||||||
else {
|
else {
|
||||||
sf::View camera = getCamera();
|
sf::Vector2f cur_center = getCenterGoal();
|
||||||
|
|
||||||
// la molette est horizontale ssi. elle l'est vraiment ou
|
// la molette est horizontale ssi. elle l'est vraiment ou
|
||||||
// si on utilise la molette verticale et shift
|
// si on utilise la molette verticale et shift
|
||||||
|
@ -219,18 +219,12 @@ void Editor::processEvent(const sf::Event& event) {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!horizontal) {
|
if (!horizontal) {
|
||||||
camera.move(
|
cur_center.y += event.mouseWheelScroll.delta * WHEEL_SCROLL_SPEED;
|
||||||
sf::Vector2f(0, event.mouseWheelScroll.delta) *
|
|
||||||
WHEEL_SCROLL_SPEED
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
camera.move(
|
cur_center.x += event.mouseWheelScroll.delta * WHEEL_SCROLL_SPEED;
|
||||||
sf::Vector2f(event.mouseWheelScroll.delta, 0) *
|
|
||||||
WHEEL_SCROLL_SPEED
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setCamera(camera);
|
setCenterGoal(cur_center);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,21 +273,21 @@ void Editor::frame() {
|
||||||
|
|
||||||
// scroll de la caméra lorsque la souris se situe sur les bords
|
// scroll de la caméra lorsque la souris se situe sur les bords
|
||||||
if (window.hasFocus()) {
|
if (window.hasFocus()) {
|
||||||
sf::View camera = getCamera();
|
sf::Vector2f cur_center = getCenterGoal();
|
||||||
sf::Vector2i mouse = sf::Mouse::getPosition(window);
|
sf::Vector2i mouse = sf::Mouse::getPosition(window);
|
||||||
|
|
||||||
// détection du dépassement sur un des 4 bords
|
// détection du dépassement sur un des 4 bords
|
||||||
if (mouse.x < POINTER_SCROLL_PADDING && mouse.x >= -POINTER_SCROLL_PADDING) {
|
if (mouse.x < POINTER_SCROLL_PADDING && mouse.x >= -POINTER_SCROLL_PADDING) {
|
||||||
camera.move(sf::Vector2f(-POINTER_SCROLL_SPEED, 0));
|
cur_center.x -= POINTER_SCROLL_SPEED;
|
||||||
} else if (mouse.x >= window_size.x - POINTER_SCROLL_PADDING && mouse.x < window_size.x + POINTER_SCROLL_PADDING) {
|
} else if (mouse.x >= window_size.x - POINTER_SCROLL_PADDING && mouse.x < window_size.x + POINTER_SCROLL_PADDING) {
|
||||||
camera.move(sf::Vector2f(POINTER_SCROLL_SPEED, 0));
|
cur_center.x += POINTER_SCROLL_SPEED;
|
||||||
} else if (mouse.y < POINTER_SCROLL_PADDING && mouse.y >= -POINTER_SCROLL_PADDING) {
|
} else if (mouse.y < POINTER_SCROLL_PADDING && mouse.y >= -POINTER_SCROLL_PADDING) {
|
||||||
camera.move(sf::Vector2f(0, -POINTER_SCROLL_SPEED));
|
cur_center.y -= POINTER_SCROLL_SPEED;
|
||||||
} else if (mouse.y >= window_size.y - POINTER_SCROLL_PADDING && mouse.y < window_size.y + POINTER_SCROLL_PADDING) {
|
} else if (mouse.y >= window_size.y - POINTER_SCROLL_PADDING && mouse.y < window_size.y + POINTER_SCROLL_PADDING) {
|
||||||
camera.move(sf::Vector2f(0, POINTER_SCROLL_SPEED));
|
cur_center.y += POINTER_SCROLL_SPEED;
|
||||||
}
|
}
|
||||||
|
|
||||||
setCamera(camera);
|
setCenterGoal(cur_center);
|
||||||
}
|
}
|
||||||
|
|
||||||
// màj du titre de la fenêtre
|
// màj du titre de la fenêtre
|
||||||
|
|
|
@ -20,7 +20,21 @@ Game::Game(Manager& manager, bool test) : Level(manager),
|
||||||
mode(Game::Mode::NORMAL), test(test),
|
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) {
|
||||||
|
|
||||||
|
// ajout des boutons d'action de la barre d'action
|
||||||
|
if (!isTest()) {
|
||||||
|
action_toolbar.addButton(
|
||||||
|
*ResourceManager::get().getImage("toolbar/icon_restart.tga"),
|
||||||
|
std::bind(&Game::restart, this)
|
||||||
|
);
|
||||||
|
|
||||||
|
action_toolbar.addButton(
|
||||||
|
*ResourceManager::get().getImage("toolbar/icon_pause.tga"),
|
||||||
|
std::bind(&Game::switchPause, this)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Game::~Game() {}
|
Game::~Game() {}
|
||||||
|
|
||||||
|
@ -51,21 +65,22 @@ 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) {
|
||||||
// en mode test, retour, échap et sortie pour revenir à l'éditeur
|
// en mode test, retour, échap et espace pour revenir à l'éditeur
|
||||||
|
if (isTest()) {
|
||||||
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::Escape ||
|
||||||
event.key.code == sf::Keyboard::BackSpace) {
|
event.key.code == sf::Keyboard::BackSpace) {
|
||||||
getManager().popState();
|
getManager().popState();
|
||||||
return;
|
}
|
||||||
|
} else {
|
||||||
|
// appui sur R : recommencer le niveau
|
||||||
|
if (event.key.code == sf::Keyboard::R) {
|
||||||
|
restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
// appui sur espace : échange entre le mode pause et normal
|
// appui sur espace : échange entre le mode pause et normal
|
||||||
if (event.key.code == sf::Keyboard::Space) {
|
if (event.key.code == sf::Keyboard::Space) {
|
||||||
if (getMode() == Game::Mode::NORMAL) {
|
switchPause();
|
||||||
setMode(Game::Mode::PAUSED);
|
|
||||||
} else if (getMode() == Game::Mode::PAUSED) {
|
|
||||||
setMode(Game::Mode::NORMAL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// appui sur retour ou échap : sortie
|
// appui sur retour ou échap : sortie
|
||||||
|
@ -74,6 +89,7 @@ void Game::processEvent(const sf::Event& event) {
|
||||||
getManager().popState();
|
getManager().popState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::frame() {
|
void Game::frame() {
|
||||||
|
@ -125,7 +141,7 @@ void Game::frame() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// on s'assure que la caméra soit centrée sur nos joueurs
|
// on s'assure que la caméra soit centrée sur nos joueurs
|
||||||
ensureCentered();
|
setCenterGoal(getPlayerCenter());
|
||||||
|
|
||||||
// si on a encore suffisamment de temps, ou si on a sauté
|
// si on a encore suffisamment de temps, ou si on a sauté
|
||||||
// trop de frames, on dessine
|
// trop de frames, on dessine
|
||||||
|
@ -151,17 +167,20 @@ void Game::draw() {
|
||||||
Level::draw();
|
Level::draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::ensureCentered() {
|
void Game::restart() {
|
||||||
sf::View camera = getCamera();
|
load();
|
||||||
sf::Vector2f previous_center = camera.getCenter();
|
|
||||||
sf::Vector2f new_center = getPlayerCenter();
|
|
||||||
|
|
||||||
// on anime le centre vers le centre des joueurs
|
setGravityDirection(Utility::Direction::SOUTH);
|
||||||
previous_center.x = Utility::animateValue(previous_center.x, 5, new_center.x);
|
setMode(Game::Mode::NORMAL);
|
||||||
previous_center.y = Utility::animateValue(previous_center.y, 5, new_center.y);
|
setDeathCause(Game::DeathCause::NONE);
|
||||||
|
}
|
||||||
|
|
||||||
camera.setCenter(previous_center);
|
void Game::switchPause() {
|
||||||
setCamera(camera);
|
if (getMode() == Game::Mode::NORMAL) {
|
||||||
|
setMode(Game::Mode::PAUSED);
|
||||||
|
} else if (getMode() == Game::Mode::PAUSED) {
|
||||||
|
setMode(Game::Mode::NORMAL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::update() {
|
void Game::update() {
|
||||||
|
|
|
@ -181,11 +181,14 @@ void Level::load() {
|
||||||
setMusic(read_music);
|
setMusic(read_music);
|
||||||
setBackground(read_background);
|
setBackground(read_background);
|
||||||
|
|
||||||
// lecture des objets si une callback a été fournie
|
// lecture des objets
|
||||||
int read_object_count;
|
int read_object_count;
|
||||||
file.read(reinterpret_cast<char*>(&read_object_count), 4);
|
file.read(reinterpret_cast<char*>(&read_object_count), 4);
|
||||||
read_object_count = ntohl(read_object_count);
|
read_object_count = ntohl(read_object_count);
|
||||||
|
|
||||||
|
objects.clear();
|
||||||
|
players.clear();
|
||||||
|
|
||||||
for (int i = 0; i < read_object_count; i++) {
|
for (int i = 0; i < read_object_count; i++) {
|
||||||
char read_object_type;
|
char read_object_type;
|
||||||
file.read(&read_object_type, 1);
|
file.read(&read_object_type, 1);
|
||||||
|
@ -281,7 +284,8 @@ void Level::processEvent(const sf::Event& event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::frame() {
|
void Level::frame() {
|
||||||
sf::Vector2i window_size = (sf::Vector2i) getManager().getWindow().getSize();
|
sf::RenderWindow& window = getManager().getWindow();
|
||||||
|
sf::Vector2i window_size = (sf::Vector2i) window.getSize();
|
||||||
|
|
||||||
// mise à jour de l'icône du mute en fonction de l'état
|
// mise à jour de l'icône du mute en fonction de l'état
|
||||||
sf::Image image;
|
sf::Image image;
|
||||||
|
@ -299,20 +303,27 @@ void Level::frame() {
|
||||||
0, 0, window_size.x,
|
0, 0, window_size.x,
|
||||||
action_toolbar.getHeight()
|
action_toolbar.getHeight()
|
||||||
));
|
));
|
||||||
}
|
|
||||||
|
|
||||||
void Level::draw() {
|
// animation du centre et de la rotation de la caméra
|
||||||
sf::RenderWindow& window = getManager().getWindow();
|
sf::Vector2f cur_center = camera.getCenter();
|
||||||
sf::Vector2u window_size = window.getSize();
|
|
||||||
|
cur_center.x = Utility::animateValue(cur_center.x, 5, goal_center.x);
|
||||||
|
cur_center.y = Utility::animateValue(cur_center.y, 5, goal_center.y);
|
||||||
|
|
||||||
// animation de la rotation de la caméra
|
|
||||||
camera_angle = Utility::animateValue(
|
camera_angle = Utility::animateValue(
|
||||||
camera_angle, 5,
|
camera_angle, 5,
|
||||||
std::fmod((float) gravity_direction * 90, 360)
|
std::fmod((float) gravity_direction * 90, 360)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
camera.setCenter(cur_center);
|
||||||
camera.setRotation(camera_angle + 180);
|
camera.setRotation(camera_angle + 180);
|
||||||
|
|
||||||
window.setView(camera);
|
window.setView(camera);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Level::draw() {
|
||||||
|
sf::RenderWindow& window = getManager().getWindow();
|
||||||
|
sf::Vector2u window_size = window.getSize();
|
||||||
|
|
||||||
// efface la scène précédente
|
// efface la scène précédente
|
||||||
window.clear(sf::Color(66, 165, 245));
|
window.clear(sf::Color(66, 165, 245));
|
||||||
|
@ -533,6 +544,11 @@ sf::View Level::getCamera() const {
|
||||||
return camera;
|
return camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Level::setCamera(sf::View set_camera) {
|
||||||
|
camera = set_camera;
|
||||||
|
goal_center = set_camera.getCenter();
|
||||||
|
}
|
||||||
|
|
||||||
sf::Vector2f Level::pixelToCoords(sf::Vector2i pixel) {
|
sf::Vector2f Level::pixelToCoords(sf::Vector2i pixel) {
|
||||||
sf::RenderWindow& window = getManager().getWindow();
|
sf::RenderWindow& window = getManager().getWindow();
|
||||||
sf::View old_view = window.getView();
|
sf::View old_view = window.getView();
|
||||||
|
@ -555,8 +571,12 @@ sf::Vector2i Level::coordsToPixel(sf::Vector2f coords) {
|
||||||
return pixel;
|
return pixel;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::setCamera(sf::View set_camera) {
|
sf::Vector2f Level::getCenterGoal() {
|
||||||
camera = set_camera;
|
return goal_center;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Level::setCenterGoal(sf::Vector2f set_center) {
|
||||||
|
goal_center = set_center;
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Vector2f Level::getPlayerCenter() {
|
sf::Vector2f Level::getPlayerCenter() {
|
||||||
|
|
|
@ -7,7 +7,7 @@ float Utility::animateValue(float current, float speed, float goal) {
|
||||||
float diff = gap * Manager::FRAME_TIME.asSeconds() * speed;
|
float diff = gap * Manager::FRAME_TIME.asSeconds() * speed;
|
||||||
|
|
||||||
// si on est très proches de la fin, on termine
|
// si on est très proches de la fin, on termine
|
||||||
if (std::abs(gap) < .05f) {
|
if (std::abs(gap) < .1f) {
|
||||||
return goal;
|
return goal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue