2016-04-11 11:19:44 +00:00
|
|
|
#include <iostream>
|
2016-04-08 13:53:30 +00:00
|
|
|
#include <cmath>
|
2016-03-28 13:05:18 +00:00
|
|
|
#include "game.hpp"
|
2016-04-08 13:53:30 +00:00
|
|
|
#include "player.hpp"
|
2016-03-20 13:32:34 +00:00
|
|
|
#include "constants.hpp"
|
2016-03-04 15:29:31 +00:00
|
|
|
|
2016-04-05 23:13:00 +00:00
|
|
|
Game::Game(Manager& manager) : Level(manager),
|
2016-04-06 13:21:46 +00:00
|
|
|
widget_timer(manager, false),
|
2016-04-11 19:11:34 +00:00
|
|
|
next_frame_time(manager.getCurrentTime()) {
|
2016-04-10 00:11:37 +00:00
|
|
|
|
2016-04-10 15:40:04 +00:00
|
|
|
if (getMusic() != "") {
|
|
|
|
getResourceManager().playMusic(getMusic());
|
|
|
|
} else {
|
|
|
|
getResourceManager().stopMusic();
|
|
|
|
}
|
|
|
|
|
2016-04-10 00:11:37 +00:00
|
|
|
mode = Game::Mode::NORMAL;
|
2016-04-11 12:16:20 +00:00
|
|
|
death_cause = Game::DeathCause::NONE;
|
|
|
|
|
2016-04-09 02:36:30 +00:00
|
|
|
getWindow().setFramerateLimit(0);
|
2016-04-05 18:07:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 19:11:34 +00:00
|
|
|
Game::~Game() {}
|
|
|
|
|
2016-04-09 21:57:23 +00:00
|
|
|
void Game::processEvent(const sf::Event& event) {
|
|
|
|
Level::processEvent(event);
|
|
|
|
|
2016-04-10 00:11:37 +00:00
|
|
|
if (event.type == sf::Event::KeyPressed) {
|
2016-04-11 19:11:34 +00:00
|
|
|
// appui sur espace : retour
|
|
|
|
if (event.key.code == sf::Keyboard::Space) {
|
|
|
|
getManager().popState();
|
2016-04-10 00:11:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 19:38:12 +00:00
|
|
|
// appui sur retour échap : échange entre le mode pause et normal
|
|
|
|
if (event.key.code == sf::Keyboard::Escape ||
|
|
|
|
event.key.code == sf::Keyboard::BackSpace) {
|
2016-04-11 11:19:44 +00:00
|
|
|
if (getMode() == Game::Mode::NORMAL) {
|
|
|
|
setMode(Game::Mode::PAUSED);
|
|
|
|
} else if (getMode() == Game::Mode::PAUSED) {
|
|
|
|
setMode(Game::Mode::NORMAL);
|
2016-04-10 00:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 21:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 00:32:11 +00:00
|
|
|
|
2016-04-09 21:57:23 +00:00
|
|
|
void Game::frame() {
|
2016-04-09 00:32:11 +00:00
|
|
|
// titre de la fenêtre
|
2016-04-09 02:36:30 +00:00
|
|
|
getManager().setTitle(getName());
|
|
|
|
sf::Time current_time = getManager().getCurrentTime();
|
2016-04-07 22:19:01 +00:00
|
|
|
|
2016-04-03 18:19:48 +00:00
|
|
|
if (current_time >= next_frame_time) {
|
|
|
|
// si nous sommes en retard ou dans les temps
|
|
|
|
// on replanifie la prochaine frame
|
2016-04-10 02:47:02 +00:00
|
|
|
next_frame_time += Manager::FRAME_TIME;
|
2016-04-03 18:19:48 +00:00
|
|
|
|
2016-04-10 00:11:37 +00:00
|
|
|
// on met à jour la physique d'un cran temporel,
|
|
|
|
// si on est en mode normal
|
2016-04-11 11:19:44 +00:00
|
|
|
if (getMode() == Game::Mode::NORMAL) {
|
2016-04-10 00:11:37 +00:00
|
|
|
update();
|
2016-04-11 12:16:20 +00:00
|
|
|
} else {
|
|
|
|
// TODO: pour le débogage affichage du mode actuel
|
|
|
|
switch (getMode()) {
|
|
|
|
case Game::Mode::NORMAL:
|
|
|
|
std::cout << "<< Reprise >>" << std::endl;
|
|
|
|
break;
|
|
|
|
case Game::Mode::PAUSED:
|
|
|
|
std::cout << "<< En pause >>" << std::endl;
|
|
|
|
break;
|
|
|
|
case Game::Mode::WON:
|
|
|
|
std::cout << "<< Gagné ! >>" << std::endl;
|
|
|
|
break;
|
|
|
|
case Game::Mode::LOST:
|
|
|
|
std::cout << "<< Perdu : ";
|
|
|
|
|
|
|
|
switch (getDeathCause()) {
|
|
|
|
case Game::DeathCause::OUT_OF_BOUNDS:
|
|
|
|
std::cout << "sortie du cadre";
|
|
|
|
break;
|
|
|
|
case Game::DeathCause::KILLED:
|
|
|
|
std::cout << "tué par bloc";
|
|
|
|
break;
|
|
|
|
case Game::DeathCause::TIME_OUT:
|
|
|
|
std::cout << "temps écoulé";
|
|
|
|
break;
|
2016-04-11 18:01:41 +00:00
|
|
|
case Game::DeathCause::NONE:
|
|
|
|
std::cout << "sans aucune raison";
|
|
|
|
break;
|
2016-04-11 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 18:01:41 +00:00
|
|
|
std::cout << " ! >>" << std::endl;
|
2016-04-11 12:16:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-10 00:11:37 +00:00
|
|
|
}
|
2016-03-04 15:29:31 +00:00
|
|
|
|
2016-04-10 00:11:37 +00:00
|
|
|
// on s'assure que la caméra soit centrée sur nos joueurs
|
2016-04-08 22:26:47 +00:00
|
|
|
ensureCentered();
|
2016-04-08 13:53:30 +00:00
|
|
|
|
2016-04-03 18:19:48 +00:00
|
|
|
// si on a encore suffisamment de temps, on dessine
|
|
|
|
if (current_time < next_frame_time) {
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// si nous sommes en avance, on endort le processus
|
|
|
|
// le temps nécessaire pour revenir dans les temps
|
|
|
|
sf::sleep(next_frame_time - current_time);
|
|
|
|
}
|
2016-04-07 22:19:01 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 13:21:46 +00:00
|
|
|
void Game::draw() {
|
2016-04-09 02:36:30 +00:00
|
|
|
sf::Vector2i window_size = (sf::Vector2i) getWindow().getSize();
|
2016-04-06 13:21:46 +00:00
|
|
|
|
2016-04-08 00:35:17 +00:00
|
|
|
// dessin des objets du niveau
|
|
|
|
Level::draw();
|
2016-04-06 13:21:46 +00:00
|
|
|
|
2016-04-10 06:33:09 +00:00
|
|
|
// on passe au dessin d'éléments d'interface.
|
|
|
|
// Changement de vue sur la vue par défaut
|
2016-04-10 18:34:29 +00:00
|
|
|
getManager().useGUIView();
|
2016-04-10 06:33:09 +00:00
|
|
|
|
2016-04-08 00:35:17 +00:00
|
|
|
// dessin du timer
|
2016-04-11 11:19:44 +00:00
|
|
|
widget_timer.setTimeLeft(
|
2016-04-11 19:20:05 +00:00
|
|
|
std::max(std::ceil(time_left), 0.f)
|
2016-04-11 11:19:44 +00:00
|
|
|
);
|
|
|
|
|
2016-04-08 00:35:17 +00:00
|
|
|
widget_timer.draw(sf::Vector2f(window_size.x / 2 - 50, 0));
|
2016-04-06 13:21:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 22:26:47 +00:00
|
|
|
void Game::ensureCentered() {
|
2016-04-10 19:18:21 +00:00
|
|
|
std::vector<Player::Ptr>& players = getPlayers();
|
2016-04-08 22:26:47 +00:00
|
|
|
|
|
|
|
sf::Vector2f total_position;
|
2016-04-10 19:18:21 +00:00
|
|
|
sf::View camera = getCamera();
|
|
|
|
unsigned int player_count = players.size();
|
2016-04-08 22:26:47 +00:00
|
|
|
|
2016-04-10 19:18:21 +00:00
|
|
|
for (unsigned int i = 0; i < player_count; i++) {
|
|
|
|
total_position += players[i]->getPosition();
|
2016-04-08 22:26:47 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 04:53:31 +00:00
|
|
|
if (player_count == 0) {
|
|
|
|
// on évite la division par zéro
|
|
|
|
camera.setCenter(sf::Vector2f(0, 0));
|
|
|
|
} else {
|
|
|
|
// on place la caméra à la position médiane de tous les joueurs
|
|
|
|
camera.setCenter(total_position / (float) player_count);
|
|
|
|
}
|
|
|
|
|
2016-04-08 22:26:47 +00:00
|
|
|
setCamera(camera);
|
|
|
|
}
|
|
|
|
|
2016-03-30 12:03:52 +00:00
|
|
|
void Game::update() {
|
2016-03-28 00:03:56 +00:00
|
|
|
std::vector<CollisionData> colliding;
|
2016-04-10 19:18:21 +00:00
|
|
|
std::vector<Object::Ptr>& objects = getObjects();
|
2016-03-28 00:03:56 +00:00
|
|
|
|
2016-04-11 11:19:44 +00:00
|
|
|
// on décrémente le temps, si le temps est nul,
|
|
|
|
// on a perdu
|
|
|
|
if (time_left <= 0) {
|
|
|
|
setMode(Game::Mode::LOST);
|
2016-04-11 12:16:20 +00:00
|
|
|
setDeathCause(Game::DeathCause::TIME_OUT);
|
2016-04-11 11:19:44 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
time_left -= Manager::FRAME_TIME.asSeconds();
|
|
|
|
}
|
|
|
|
|
2016-04-10 22:56:22 +00:00
|
|
|
// on tue les objets qui étaient déjà planifiés pour mourir
|
|
|
|
for (auto it = pending_kill.begin(); it != pending_kill.end(); it++) {
|
|
|
|
removeObject(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_kill.empty();
|
|
|
|
|
2016-04-11 10:34:43 +00:00
|
|
|
// détection des objets en collision
|
2016-04-10 22:56:22 +00:00
|
|
|
for (auto it = objects.begin(); it != objects.end(); it++) {
|
2016-04-11 10:34:43 +00:00
|
|
|
Object::Ptr obj_a = *it;
|
2016-04-10 22:56:22 +00:00
|
|
|
|
2016-04-11 10:34:43 +00:00
|
|
|
// l'objet est sorti de la zone, on le signale et on
|
|
|
|
// planifie sa mort à la prochaine frame
|
|
|
|
if (!isInZone(obj_a) && obj_a->getMass() != 0) {
|
2016-04-11 14:32:19 +00:00
|
|
|
kill(obj_a);
|
2016-04-11 18:01:41 +00:00
|
|
|
|
|
|
|
// si c'était un joueur, on a perdu
|
|
|
|
if (obj_a->getTypeId() == Player::TYPE_ID) {
|
|
|
|
setMode(Game::Mode::LOST);
|
|
|
|
setDeathCause(Game::DeathCause::OUT_OF_BOUNDS);
|
|
|
|
}
|
2016-04-11 10:34:43 +00:00
|
|
|
}
|
2016-03-13 18:07:35 +00:00
|
|
|
|
2016-04-11 10:34:43 +00:00
|
|
|
// on regarde s'il est en collision avec
|
|
|
|
// d'autres objets
|
|
|
|
for (auto jt = it + 1; jt != objects.end(); jt++) {
|
|
|
|
Object::Ptr obj_b = *jt;
|
2016-04-10 20:00:58 +00:00
|
|
|
CollisionData data;
|
2016-03-28 00:03:56 +00:00
|
|
|
|
2016-04-10 20:00:58 +00:00
|
|
|
data.obj_a = obj_a;
|
|
|
|
data.obj_b = obj_b;
|
|
|
|
|
|
|
|
if (obj_a->detectCollision(obj_b, data)) {
|
2016-03-28 00:03:56 +00:00
|
|
|
colliding.push_back(data);
|
|
|
|
}
|
2016-03-13 18:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-27 21:43:05 +00:00
|
|
|
|
2016-04-11 10:34:43 +00:00
|
|
|
// intégration des forces dans la vitesse
|
|
|
|
for (auto it = objects.begin(); it != objects.end(); it++) {
|
|
|
|
(*it)->updateVelocity(*this);
|
2016-03-27 21:43:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:03:56 +00:00
|
|
|
// résolution des collisions détectées
|
2016-04-11 10:34:43 +00:00
|
|
|
for (auto it = colliding.begin(); it != colliding.end(); it++) {
|
|
|
|
it->obj_a->solveCollision(*this, it->obj_b, it->normal);
|
2016-03-28 00:03:56 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 21:43:05 +00:00
|
|
|
// intégration de la vitesse dans la position
|
2016-04-11 10:34:43 +00:00
|
|
|
for (auto it = objects.begin(); it != objects.end(); it++) {
|
|
|
|
(*it)->updatePosition();
|
2016-03-27 21:43:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:03:56 +00:00
|
|
|
// application de la correction positionnelle
|
2016-04-11 10:34:43 +00:00
|
|
|
for (auto it = colliding.begin(); it != colliding.end(); it++) {
|
|
|
|
it->obj_a->positionalCorrection(it->obj_b, it->normal, it->depth);
|
2016-03-28 00:03:56 +00:00
|
|
|
}
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|
2016-04-05 23:13:00 +00:00
|
|
|
|
2016-04-11 14:32:19 +00:00
|
|
|
void Game::kill(Object::Ptr object) {
|
|
|
|
pending_kill.push_back(object);
|
|
|
|
}
|
|
|
|
|
2016-04-10 22:56:22 +00:00
|
|
|
/**
|
|
|
|
* Implémentation tirée de
|
|
|
|
* http://stackoverflow.com/a/2922778/3452708
|
|
|
|
*/
|
|
|
|
bool Game::isInZone(Object::Ptr object) {
|
|
|
|
std::vector<sf::Vector2f>& zone = getZone();
|
|
|
|
sf::Vector2f pos = object->getPosition();
|
|
|
|
unsigned int vertices = getZone().size();
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
for (unsigned int i = 0, j = vertices - 1; i < vertices; j = i++) {
|
|
|
|
if (((zone[i].y > pos.y) != (zone[j].y > pos.y)) &&
|
|
|
|
(pos.x < (zone[j].x - zone[i].x) * (pos.y - zone[i].y) /
|
|
|
|
(zone[j].y - zone[i].y) + zone[i].x)) {
|
|
|
|
result = !result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-10 00:11:37 +00:00
|
|
|
Game::Mode Game::getMode() {
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2016-04-11 12:16:20 +00:00
|
|
|
Game::DeathCause Game::getDeathCause() {
|
|
|
|
return death_cause;
|
|
|
|
}
|
|
|
|
|
2016-04-10 00:11:37 +00:00
|
|
|
void Game::setMode(Game::Mode set_mode) {
|
|
|
|
mode = set_mode;
|
2016-04-11 12:16:20 +00:00
|
|
|
}
|
2016-04-11 11:19:44 +00:00
|
|
|
|
2016-04-11 12:16:20 +00:00
|
|
|
void Game::setDeathCause(Game::DeathCause set_death_cause) {
|
|
|
|
death_cause = set_death_cause;
|
2016-04-10 00:11:37 +00:00
|
|
|
}
|