-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstriker.cpp
51 lines (46 loc) · 1.7 KB
/
striker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "striker.h"
Striker::Striker (const int &r, const sf::Vector2f &position, const int &up,const int &down)
: sf::CircleShape(r), up_limit_(up), down_limit_(down)
{
setPosition(position);
}
void Striker::animate(const sf::Time &elapsed, const bool& arrows, const sf::Vector2f &puck_position, const float &puck_radius)
{
sf::Keyboard::Key keys[4];
if (arrows)
{
keys[0] = sf::Keyboard::Up;
keys[1] = sf::Keyboard::Down;
keys[2] = sf::Keyboard::Left;
keys[3] = sf::Keyboard::Right;
}
else
{
keys[0] = sf::Keyboard::W;
keys[1] = sf::Keyboard::S;
keys[2] = sf::Keyboard::A;
keys[3] = sf::Keyboard::D;
}
float dx = this->getPosition().x - puck_position.x;
float dy = this->getPosition().y - puck_position.y;
float distance = sqrt(pow(dx, 2) + pow(dy, 2));
if(distance > puck_radius + this->getRadius())
{
if(sf::Keyboard::isKeyPressed(keys[0]) && getPosition().y > up_limit_)
this->move(0, -std::abs(v_.y * elapsed.asSeconds()));
if(sf::Keyboard::isKeyPressed(keys[1]) && getPosition().y < down_limit_)
this->move(0, std::abs(v_.y * elapsed.asSeconds()));
if(sf::Keyboard::isKeyPressed(keys[2]) && this->getPosition().x > left_limit_ + this->getRadius())
this->move(-std::abs(v_.x * elapsed.asSeconds()), 0);
if(sf::Keyboard::isKeyPressed(keys[3]) && getPosition().x < right_limit_ -this->getRadius())
this->move(std::abs(v_.x * elapsed.asSeconds()), 0);
}
}
void Striker::set_previous_position(const sf::Vector2f &p)
{
previous_position_ = p;
}
sf::Vector2f Striker::get_previous_position()
{
return previous_position_;
}