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-08 22:26:47 +00:00
|
|
|
const float CAMERA_TOLERANCE_RATIO = 2.f / 3.f;
|
|
|
|
|
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-05 23:13:00 +00:00
|
|
|
next_frame_time(manager.getCurrentTime()),
|
2016-04-09 13:53:12 +00:00
|
|
|
test_mode(false), return_state(nullptr) {}
|
2016-04-03 20:08:11 +00:00
|
|
|
|
2016-04-03 19:05:27 +00:00
|
|
|
Game::~Game() {}
|
2016-03-28 17:57:55 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Game::begin() {
|
|
|
|
Level::begin();
|
2016-04-09 02:36:30 +00:00
|
|
|
getWindow().setFramerateLimit(0);
|
2016-04-05 18:07:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Game::frame(const std::vector<sf::Event>& events) {
|
|
|
|
// traitement des événements
|
|
|
|
Level::frame(events);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
next_frame_time += Constants::PHYSICS_TIME;
|
|
|
|
|
|
|
|
// on met à jour la physique d'un cran temporel
|
2016-03-30 12:03:52 +00:00
|
|
|
update();
|
2016-03-04 15:29:31 +00:00
|
|
|
|
2016-04-08 22:26:47 +00:00
|
|
|
// on s'assure que la caméré soit centrée sur nos joueurs
|
|
|
|
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-08 00:35:17 +00:00
|
|
|
void Game::processEvent(const sf::Event& event) {
|
|
|
|
Level::processEvent(event);
|
|
|
|
|
2016-04-07 22:19:01 +00:00
|
|
|
// appui sur espace en mode test : retour à l'éditeur
|
|
|
|
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space && test_mode) {
|
|
|
|
test_mode = false;
|
2016-04-09 13:53:12 +00:00
|
|
|
getManager().setState(return_state);
|
2016-04-07 22:19:01 +00:00
|
|
|
}
|
2016-03-09 18:35:40 +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-08 00:35:17 +00:00
|
|
|
// dessin du timer
|
2016-04-06 13:21:46 +00:00
|
|
|
widget_timer.setTimeLeft(getTotalTime());
|
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() {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-03-30 12:03:52 +00:00
|
|
|
void Game::update() {
|
2016-03-28 00:03:56 +00:00
|
|
|
std::vector<CollisionData> colliding;
|
|
|
|
|
|
|
|
// détection des objets en collision
|
2016-04-03 19:05:27 +00:00
|
|
|
for (unsigned int i = 0; i < getObjects().size(); i++) {
|
2016-04-09 13:32:42 +00:00
|
|
|
ObjectPtr obj_a = getObjects()[i];
|
2016-03-13 18:07:35 +00:00
|
|
|
|
2016-04-03 19:05:27 +00:00
|
|
|
for (unsigned int j = i + 1; j < getObjects().size(); j++) {
|
2016-04-09 13:32:42 +00:00
|
|
|
ObjectPtr obj_b = getObjects()[j];
|
|
|
|
CollisionData data(*obj_a, *obj_b);
|
2016-03-28 00:03:56 +00:00
|
|
|
|
2016-04-09 13:32:42 +00:00
|
|
|
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-08 13:53:30 +00:00
|
|
|
// intégration des forces dans la vitesse (seconde moitié)
|
2016-04-03 19:05:27 +00:00
|
|
|
for (unsigned int i = 0; i < getObjects().size(); i++) {
|
2016-04-09 02:36:30 +00:00
|
|
|
getObjects()[i]->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
|
|
|
|
for (unsigned int i = 0; i < colliding.size(); i++) {
|
|
|
|
CollisionData& collided = colliding[i];
|
2016-04-09 13:32:42 +00:00
|
|
|
collided.obj_a.solveCollision(*this, collided.obj_b, collided.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-03 19:05:27 +00:00
|
|
|
for (unsigned int i = 0; i < getObjects().size(); i++) {
|
2016-04-09 02:36:30 +00:00
|
|
|
getObjects()[i]->updatePosition();
|
2016-03-27 21:43:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:03:56 +00:00
|
|
|
// application de la correction positionnelle
|
|
|
|
for (unsigned int i = 0; i < colliding.size(); i++) {
|
|
|
|
CollisionData& collided = colliding[i];
|
2016-04-09 13:32:42 +00:00
|
|
|
collided.obj_a.positionalCorrection(
|
|
|
|
collided.obj_b, collided.normal, collided.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-09 13:53:12 +00:00
|
|
|
void Game::setTestMode(std::shared_ptr<State> set_return_state) {
|
|
|
|
return_state = set_return_state;
|
2016-04-05 23:13:00 +00:00
|
|
|
test_mode = true;
|
|
|
|
}
|