skizzle/src/player.cpp

74 lines
1.9 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-03-25 17:40:39 +00:00
Player::Player(float x, float y) : Object(x, y) {
// déplacement de l'origine au centre de la balle
sprite.setOrigin(sf::Vector2f(getRadius(), getRadius()));
}
2016-03-25 17:40:39 +00:00
sf::Vector2f Player::getForces(EngineState& state) {
sf::Vector2f forces = Object::getForces(state);
2016-03-25 19:56:04 +00:00
// déplacement de la balle après appui sur les touches de direction
if (getPlayerNumber() == 1) {
2016-03-25 17:40:39 +00:00
if (state.keys[sf::Keyboard::Left]) {
forces += sf::Vector2f(-Constants::MOVE, 0);
}
2016-03-25 17:40:39 +00:00
if (state.keys[sf::Keyboard::Right]) {
forces += sf::Vector2f(Constants::MOVE, 0);
}
}
2016-03-25 19:56:04 +00:00
if (getPlayerNumber() == 2) {
if (state.keys[sf::Keyboard::Q]) {
2016-03-25 17:40:39 +00:00
forces += sf::Vector2f(-Constants::MOVE, 0);
}
2016-03-25 19:56:04 +00:00
if (state.keys[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
}
2016-03-25 17:40:39 +00:00
void Player::draw(sf::RenderWindow& window, ResourceManager& resources) {
Object::draw(window, resources);
// utilisation de la texture
2016-03-25 17:40:39 +00:00
sprite.setTexture(resources.getTexture("ball.bmp"));
// déplacement du sprite à la position de la balle
sprite.rotate(getVelocity().x * .1f);
sprite.setPosition(getPosition());
window.draw(sprite);
}
2016-03-25 17:40:39 +00:00
std::unique_ptr<sf::FloatRect> Player::getAABB() {
return std::unique_ptr<sf::FloatRect>(new sf::FloatRect(
getPosition().x - getRadius(),
getPosition().y - getRadius(),
2 * getRadius(), 2 * getRadius()
));
}
unsigned int Player::getTypeId() {
return Player::TYPE_ID;
}
float Player::getRadius() {
return 10 * getMass();
}
unsigned int Player::getPlayerNumber() {
return player_number;
}
void Player::setPlayerNumber(unsigned int set_number) {
player_number = set_number;
}