2016-03-25 17:40:39 +00:00
|
|
|
#include "player.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-19 14:33:31 +00:00
|
|
|
#include <iostream>
|
2016-03-04 15:29:31 +00:00
|
|
|
|
2016-03-31 17:45:57 +00:00
|
|
|
const unsigned int Player::TYPE_ID = 0;
|
|
|
|
|
2016-03-25 17:40:39 +00:00
|
|
|
Player::Player(float x, float y) : Object(x, y) {
|
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-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-01 20:44:10 +00:00
|
|
|
std::shared_ptr<Object> Player::load(std::ifstream& file) {
|
|
|
|
float pos_x, pos_y;
|
|
|
|
|
|
|
|
file.read(reinterpret_cast<char*>(&pos_x), sizeof(pos_x));
|
|
|
|
file.read(reinterpret_cast<char*>(&pos_y), sizeof(pos_y));
|
|
|
|
|
|
|
|
pos_x *= Constants::GRID;
|
|
|
|
pos_y *= Constants::GRID;
|
|
|
|
|
|
|
|
std::shared_ptr<Player> player =
|
|
|
|
std::shared_ptr<Player>(new Player(pos_x, pos_y));
|
|
|
|
// player->setPlayerNumber(i);
|
|
|
|
|
|
|
|
return std::dynamic_pointer_cast<Object>(player);
|
|
|
|
}
|
|
|
|
|
2016-03-30 12:03:36 +00:00
|
|
|
sf::Vector2f Player::getForces(const Manager& manager, const std::vector<ObjectPtr>& objects) const {
|
2016-03-28 15:23:47 +00:00
|
|
|
sf::Vector2f forces = Object::getForces(manager, objects);
|
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-03-28 15:23:47 +00:00
|
|
|
if (manager.isKeyPressed(sf::Keyboard::Left)) {
|
2016-03-25 17:40:39 +00:00
|
|
|
forces += sf::Vector2f(-Constants::MOVE, 0);
|
|
|
|
}
|
2016-03-08 18:52:55 +00:00
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
if (manager.isKeyPressed(sf::Keyboard::Right)) {
|
2016-03-25 17:40:39 +00:00
|
|
|
forces += sf::Vector2f(Constants::MOVE, 0);
|
|
|
|
}
|
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-03-28 15:23:47 +00:00
|
|
|
if (manager.isKeyPressed(sf::Keyboard::Q)) {
|
2016-03-25 17:40:39 +00:00
|
|
|
forces += sf::Vector2f(-Constants::MOVE, 0);
|
|
|
|
}
|
2016-03-25 19:13:47 +00:00
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
if (manager.isKeyPressed(sf::Keyboard::D)) {
|
2016-03-25 17:40:39 +00:00
|
|
|
forces += sf::Vector2f(Constants::MOVE, 0);
|
|
|
|
}
|
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-03-28 15:23:47 +00:00
|
|
|
void Player::draw(Manager& manager) {
|
|
|
|
Object::draw(manager);
|
2016-03-15 21:00:03 +00:00
|
|
|
|
2016-03-22 19:04:18 +00:00
|
|
|
// utilisation de la texture
|
2016-03-28 15:23:47 +00:00
|
|
|
sprite.setTexture(
|
|
|
|
manager.getResourceManager().getTexture("ball.png")
|
|
|
|
);
|
2016-03-15 21:00:03 +00:00
|
|
|
|
2016-03-22 19:04:18 +00:00
|
|
|
// déplacement du sprite à la position de la balle
|
2016-03-28 15:23:47 +00:00
|
|
|
sprite.rotate(getVelocity().x * Constants::PHYSICS_TIME * .5f);
|
2016-03-22 19:04:18 +00:00
|
|
|
sprite.setPosition(getPosition());
|
2016-03-28 15:23:47 +00:00
|
|
|
manager.getWindow().draw(sprite);
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
std::unique_ptr<sf::FloatRect> Player::getAABB() const {
|
2016-03-31 08:20:09 +00:00
|
|
|
return std::unique_ptr<sf::FloatRect>(new sf::FloatRect(
|
2016-03-19 21:59:44 +00:00
|
|
|
getPosition().x - getRadius(),
|
|
|
|
getPosition().y - getRadius(),
|
|
|
|
2 * getRadius(), 2 * getRadius()
|
2016-03-31 08:20:09 +00:00
|
|
|
));
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
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-03-28 15:23:47 +00:00
|
|
|
float Player::getRadius() const {
|
2016-03-25 19:13:47 +00:00
|
|
|
return 10 * getMass();
|
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
|
|
|
}
|