2016-03-25 17:40:39 +00:00
|
|
|
#include "player.hpp"
|
2016-04-10 22:56:22 +00:00
|
|
|
#include "game.hpp"
|
2016-04-09 02:36:30 +00:00
|
|
|
#include "level.hpp"
|
2016-03-15 21:00:03 +00:00
|
|
|
#include "block.hpp"
|
2016-03-14 20:20:40 +00:00
|
|
|
#include "constants.hpp"
|
2016-03-15 21:00:03 +00:00
|
|
|
#include <array>
|
2016-03-04 15:29:31 +00:00
|
|
|
|
2016-04-02 13:18:29 +00:00
|
|
|
const unsigned int Player::TYPE_ID = 1;
|
2016-03-31 17:45:57 +00:00
|
|
|
|
2016-04-02 13:18:29 +00:00
|
|
|
Player::Player() : Object() {
|
2016-03-22 19:04:18 +00:00
|
|
|
// déplacement de l'origine au centre de la balle
|
|
|
|
sprite.setOrigin(sf::Vector2f(getRadius(), getRadius()));
|
2016-04-08 22:26:47 +00:00
|
|
|
sprite.setOutlineThickness(1.5f);
|
2016-03-14 20:11:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 18:06:49 +00:00
|
|
|
Player::~Player() {}
|
2016-03-28 18:02:23 +00:00
|
|
|
|
2016-04-10 05:37:09 +00:00
|
|
|
Object::Ptr Player::clone() const {
|
|
|
|
return Object::Ptr(new Player(*this));
|
2016-04-06 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 05:37:09 +00:00
|
|
|
void Player::init(std::ifstream& file, Object::Ptr object) {
|
|
|
|
Player::Ptr player = std::dynamic_pointer_cast<Player>(object);
|
2016-04-01 20:44:10 +00:00
|
|
|
|
2016-04-02 13:18:29 +00:00
|
|
|
// lecture des propriétés communes des objets
|
2016-04-10 05:37:09 +00:00
|
|
|
Object::init(file, object);
|
|
|
|
}
|
2016-04-01 20:44:10 +00:00
|
|
|
|
2016-04-10 05:37:09 +00:00
|
|
|
Object::Ptr Player::load(std::ifstream& file) {
|
|
|
|
Object::Ptr object = Object::Ptr(new Player);
|
|
|
|
Player::init(file, object);
|
2016-04-02 13:18:29 +00:00
|
|
|
return object;
|
2016-04-01 20:44:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 04:53:45 +00:00
|
|
|
void Player::save(std::ofstream& file) const {
|
|
|
|
// écriture des propriétés communes
|
|
|
|
Object::save(file);
|
|
|
|
}
|
|
|
|
|
2016-04-09 02:36:30 +00:00
|
|
|
sf::Vector2f Player::getForces(const Level& level) const {
|
|
|
|
sf::Vector2f forces = Object::getForces(level);
|
2016-03-25 19:13:47 +00:00
|
|
|
|
2016-03-25 19:56:04 +00:00
|
|
|
// déplacement de la balle après appui sur les touches de direction
|
2016-03-30 12:05:01 +00:00
|
|
|
if (getPlayerNumber() == 0) {
|
2016-04-09 23:29:42 +00:00
|
|
|
if (level.getManager().isKeyPressed(sf::Keyboard::Left)) {
|
2016-04-09 14:12:18 +00:00
|
|
|
forces += level.getLeftDirection();
|
2016-03-25 17:40:39 +00:00
|
|
|
}
|
2016-03-08 18:52:55 +00:00
|
|
|
|
2016-04-09 23:29:42 +00:00
|
|
|
if (level.getManager().isKeyPressed(sf::Keyboard::Right)) {
|
2016-04-09 14:12:18 +00:00
|
|
|
forces += level.getRightDirection();
|
2016-03-25 17:40:39 +00:00
|
|
|
}
|
2016-03-08 18:52:55 +00:00
|
|
|
}
|
2016-03-25 19:56:04 +00:00
|
|
|
|
2016-03-30 12:05:01 +00:00
|
|
|
if (getPlayerNumber() == 1) {
|
2016-04-09 23:29:42 +00:00
|
|
|
if (level.getManager().isKeyPressed(sf::Keyboard::Q)) {
|
2016-04-09 14:12:18 +00:00
|
|
|
forces += level.getLeftDirection();
|
2016-03-25 17:40:39 +00:00
|
|
|
}
|
2016-03-25 19:13:47 +00:00
|
|
|
|
2016-04-09 23:29:42 +00:00
|
|
|
if (level.getManager().isKeyPressed(sf::Keyboard::D)) {
|
2016-04-09 14:12:18 +00:00
|
|
|
forces += level.getRightDirection();
|
2016-03-25 17:40:39 +00:00
|
|
|
}
|
2016-03-08 18:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return forces;
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|
2016-03-15 21:00:03 +00:00
|
|
|
|
2016-04-09 02:36:30 +00:00
|
|
|
void Player::draw(Level& level) {
|
2016-03-22 19:04:18 +00:00
|
|
|
// utilisation de la texture
|
2016-04-10 03:10:27 +00:00
|
|
|
sprite.setRadius(getRadius());
|
2016-03-28 15:23:47 +00:00
|
|
|
sprite.setTexture(
|
2016-04-09 02:36:30 +00:00
|
|
|
&level.getResourceManager().getTexture("player.tga")
|
2016-03-28 15:23:47 +00:00
|
|
|
);
|
2016-03-15 21:00:03 +00:00
|
|
|
|
2016-04-09 01:53:17 +00:00
|
|
|
// si le joueur est sélectionné, on met sa bordure en rouge
|
2016-04-10 03:10:27 +00:00
|
|
|
if (isSelected()) {
|
|
|
|
sprite.setOutlineColor(sf::Color(255, 0, 0));
|
|
|
|
} else {
|
|
|
|
sprite.setOutlineColor(sf::Color::Black);
|
|
|
|
|
2016-04-07 09:22:20 +00:00
|
|
|
// coloration du joueur en fonction de son numéro
|
|
|
|
if (getPlayerNumber() == 0) {
|
2016-04-08 22:26:47 +00:00
|
|
|
sprite.setFillColor(sf::Color(239, 83, 80));
|
2016-04-07 09:22:20 +00:00
|
|
|
} else if (getPlayerNumber() == 1) {
|
2016-04-08 22:26:47 +00:00
|
|
|
sprite.setFillColor(sf::Color(92, 107, 192));
|
2016-04-07 09:22:20 +00:00
|
|
|
}
|
2016-04-10 03:10:27 +00:00
|
|
|
}
|
2016-04-06 15:04:03 +00:00
|
|
|
|
2016-03-22 19:04:18 +00:00
|
|
|
// déplacement du sprite à la position de la balle
|
|
|
|
sprite.setPosition(getPosition());
|
2016-04-09 02:36:30 +00:00
|
|
|
level.getWindow().draw(sprite);
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 22:56:22 +00:00
|
|
|
void Player::activate(Level& level, Object* object) {
|
2016-04-09 01:53:17 +00:00
|
|
|
// ne rien faire si le joueur est activé.
|
|
|
|
// en règle générale, c'est l'objet activé par le joueur
|
|
|
|
// qui s'occupe de la réponse
|
|
|
|
}
|
|
|
|
|
2016-04-10 22:56:22 +00:00
|
|
|
void Player::kill(Game& game) {
|
|
|
|
game.setMode(Game::Mode::LOST);
|
|
|
|
}
|
|
|
|
|
2016-04-09 02:36:30 +00:00
|
|
|
void Player::updatePosition() {
|
2016-04-03 18:36:07 +00:00
|
|
|
// calcul de la différence de position pour connaître
|
|
|
|
// (approximativement) la rotation de la balle
|
|
|
|
sf::Vector2f last_position = getPosition();
|
2016-04-09 02:36:30 +00:00
|
|
|
Object::updatePosition();
|
2016-04-03 18:36:07 +00:00
|
|
|
sprite.rotate((getPosition() - last_position).x * 3.f);
|
|
|
|
}
|
|
|
|
|
2016-04-10 03:10:27 +00:00
|
|
|
sf::FloatRect Player::getAABB() const {
|
|
|
|
return sf::FloatRect(
|
2016-03-19 21:59:44 +00:00
|
|
|
getPosition().x - getRadius(),
|
|
|
|
getPosition().y - getRadius(),
|
|
|
|
2 * getRadius(), 2 * getRadius()
|
2016-04-10 03:10:27 +00:00
|
|
|
);
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 13:32:42 +00:00
|
|
|
float Player::getRadius() const {
|
|
|
|
return 10 * getMass();
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
unsigned int Player::getTypeId() const {
|
2016-03-30 18:28:51 +00:00
|
|
|
return TYPE_ID;
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 13:32:42 +00:00
|
|
|
CollisionType Player::getCollisionType() const {
|
|
|
|
return CollisionType::CIRCLE;
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
unsigned int Player::getPlayerNumber() const {
|
2016-03-25 19:13:47 +00:00
|
|
|
return player_number;
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
2016-03-19 21:59:44 +00:00
|
|
|
|
2016-03-25 19:13:47 +00:00
|
|
|
void Player::setPlayerNumber(unsigned int set_number) {
|
|
|
|
player_number = set_number;
|
2016-03-19 21:59:44 +00:00
|
|
|
}
|