skizzle/src/objects/player.cpp

149 lines
4.1 KiB
C++
Raw Normal View History

#include "manager.hpp"
2016-04-20 15:31:13 +00:00
#include "resource_manager.hpp"
#include "states/game.hpp"
#include "objects/player.hpp"
2016-03-04 15:29:31 +00:00
2016-04-02 13:18:29 +00:00
const unsigned int Player::TYPE_ID = 1;
2016-04-02 13:18:29 +00:00
Player::Player() : Object() {
// déplacement de l'origine au centre de la balle
sprite.setOutlineThickness(1.5f);
}
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-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-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-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-10 04:53:45 +00:00
void Player::save(std::ofstream& file) const {
// écriture des propriétés communes
Object::save(file);
}
2016-04-11 12:16:20 +00:00
sf::Vector2f Player::getForces(const Game& game) const {
sf::Vector2f forces = Object::getForces(game);
2016-03-25 19:56:04 +00:00
// déplacement de la balle après appui sur les touches de direction
if (getPlayerNumber() == 0) {
2016-04-11 12:16:20 +00:00
if (game.getManager().isKeyPressed(sf::Keyboard::Left)) {
forces += game.getLeftDirection();
2016-03-25 17:40:39 +00:00
}
2016-04-11 12:16:20 +00:00
if (game.getManager().isKeyPressed(sf::Keyboard::Right)) {
forces += game.getRightDirection();
2016-03-25 17:40:39 +00:00
}
}
2016-03-25 19:56:04 +00:00
if (getPlayerNumber() == 1) {
2016-04-11 12:16:20 +00:00
if (game.getManager().isKeyPressed(sf::Keyboard::Q)) {
forces += game.getLeftDirection();
2016-03-25 17:40:39 +00:00
}
2016-04-11 12:16:20 +00:00
if (game.getManager().isKeyPressed(sf::Keyboard::D)) {
forces += game.getRightDirection();
2016-03-25 17:40:39 +00:00
}
}
return forces;
2016-03-04 15:29:31 +00:00
}
void Player::draw(Level& level) {
2016-04-11 20:49:41 +00:00
// on charge la texture selon le numéro du joueur
std::string texture_name;
if (getPlayerNumber() == 0) {
texture_name = "player_1.tga";
} else {
texture_name = "player_2.tga";
}
2016-04-20 15:31:13 +00:00
sprite.setTexture(ResourceManager::get().getTexture("objects/" + texture_name).get());
sprite.setRadius(getRadius());
sprite.setOrigin(sf::Vector2f(getRadius(), getRadius()));
2016-04-11 20:49:41 +00:00
sprite.setPosition(getPosition());
2016-04-20 15:31:13 +00:00
shadow_sprite.setTexture(ResourceManager::get().getTexture("objects/player_shadow.tga").get());
2016-04-11 20:49:41 +00:00
shadow_sprite.setRadius(getRadius() - 1);
shadow_sprite.setOrigin(sf::Vector2f(getRadius() - 1, getRadius() - 1));
shadow_sprite.setPosition(getPosition());
// on fait tourner le sprite selon la différence de position
if (previous_position != sf::Vector2f(0, 0)) {
sf::Vector2f right_dir = level.getRightDirection();
sf::Vector2f diff = getPosition() - previous_position;
float rotate = (right_dir.x * diff.x + right_dir.y * diff.y) * 3 / 200;
sprite.rotate(rotate);
2016-04-11 20:49:41 +00:00
}
previous_position = getPosition();
2016-04-09 01:53:17 +00:00
// si le joueur est sélectionné, on met sa bordure en rouge
if (isSelected()) {
sprite.setOutlineColor(sf::Color(255, 0, 0));
} else {
sprite.setOutlineColor(sf::Color::Black);
}
// coloration du joueur en fonction de son numéro
2016-04-11 17:35:39 +00:00
if (getCharge() > 0) {
sprite.setFillColor(sf::Color(92, 107, 192));
2016-04-11 17:35:39 +00:00
} else if (getCharge() < 0) {
sprite.setFillColor(sf::Color(239, 83, 80));
} else {
sprite.setFillColor(sf::Color::White);
}
2016-04-06 15:04:03 +00:00
2016-04-11 20:49:41 +00:00
// dessin des sprites
2016-04-20 15:31:13 +00:00
level.getManager().getWindow().draw(sprite);
level.getManager().getWindow().draw(shadow_sprite);
}
void Player::activate(Game& game, Object::Ptr 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
}
sf::FloatRect Player::getAABB() const {
return sf::FloatRect(
getPosition().x - getRadius(),
getPosition().y - getRadius(),
2 * getRadius(), 2 * getRadius()
);
}
2016-04-09 13:32:42 +00:00
float Player::getRadius() const {
return 10 * getMass();
}
unsigned int Player::getTypeId() const {
return TYPE_ID;
}
2016-04-09 13:32:42 +00:00
CollisionType Player::getCollisionType() const {
return CollisionType::CIRCLE;
}
unsigned int Player::getPlayerNumber() const {
return player_number;
}
void Player::setPlayerNumber(unsigned int set_number) {
player_number = set_number;
}