skizzle/src/player.cpp

96 lines
2.5 KiB
C++
Raw Normal View History

2016-03-25 17:40:39 +00:00
#include "player.hpp"
#include "block.hpp"
#include "constants.hpp"
#include <array>
2016-03-19 14:33:31 +00:00
#include <iostream>
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.setOrigin(sf::Vector2f(getRadius(), getRadius()));
}
Player::~Player() {}
2016-03-28 18:02:23 +00:00
std::shared_ptr<Object> Player::load(std::ifstream& file) {
2016-04-02 13:18:29 +00:00
std::shared_ptr<Object> object = std::shared_ptr<Object>(new Player);
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(object);
2016-04-02 13:18:29 +00:00
// lecture du numéro de joueur
char player_number;
file.read(&player_number, 1);
player->setPlayerNumber(player_number);
2016-04-02 13:18:29 +00:00
// lecture des propriétés communes des objets
Object::load(file, object);
file.seekg(1, file.cur);
2016-04-02 13:18:29 +00:00
return object;
}
sf::Vector2f Player::getForces(const Manager& manager, const std::vector<ObjectPtr>& objects) const {
sf::Vector2f forces = Object::getForces(manager, objects);
2016-03-25 19:56:04 +00:00
// déplacement de la balle après appui sur les touches de direction
if (getPlayerNumber() == 0) {
if (manager.isKeyPressed(sf::Keyboard::Left)) {
2016-03-25 17:40:39 +00:00
forces += sf::Vector2f(-Constants::MOVE, 0);
}
if (manager.isKeyPressed(sf::Keyboard::Right)) {
2016-03-25 17:40:39 +00:00
forces += sf::Vector2f(Constants::MOVE, 0);
}
}
2016-03-25 19:56:04 +00:00
if (getPlayerNumber() == 1) {
if (manager.isKeyPressed(sf::Keyboard::Q)) {
2016-03-25 17:40:39 +00:00
forces += sf::Vector2f(-Constants::MOVE, 0);
}
if (manager.isKeyPressed(sf::Keyboard::D)) {
2016-03-25 17:40:39 +00:00
forces += sf::Vector2f(Constants::MOVE, 0);
}
}
return forces;
2016-03-04 15:29:31 +00:00
}
void Player::draw(Manager& manager) {
Object::draw(manager);
// utilisation de la texture
sprite.setTexture(
manager.getResourceManager().getTexture("ball.png")
);
// déplacement du sprite à la position de la balle
sprite.rotate(getVelocity().x * Constants::PHYSICS_TIME * .5f);
sprite.setPosition(getPosition());
manager.getWindow().draw(sprite);
}
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(
getPosition().x - getRadius(),
getPosition().y - getRadius(),
2 * getRadius(), 2 * getRadius()
2016-03-31 08:20:09 +00:00
));
}
unsigned int Player::getTypeId() const {
return TYPE_ID;
}
float Player::getRadius() const {
return 10 * getMass();
}
unsigned int Player::getPlayerNumber() const {
return player_number;
}
void Player::setPlayerNumber(unsigned int set_number) {
player_number = set_number;
}