-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asteroid.cpp
65 lines (48 loc) · 1.58 KB
/
Asteroid.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "Asteroid.h"
#include "Playership.h"
Asteroid::Asteroid(unsigned int ScreenWidth, unsigned int ScreenHeight)
: _ScreenWidth{ ScreenWidth }
, _ScreenHeight{ ScreenHeight }
{
AsteroidTexture.loadFromFile("resources/asteroid.png");
AsteroidObj.setTexture(AsteroidTexture);
AsteroidObj.setScale(0.25, 0.25);
AsteroidObj.setOrigin(AsteroidObj.getOrigin());
AsteroidObj.setPosition(_ScreenWidth / 2.0f, _ScreenHeight / 2.0f);
}
void Asteroid::MoveAsteroid()
{
float AsteroidSpeed = 5;
_DistToPlayer = sqrt((_LastXCoord - _ScreenWidth / 2.0f) * (_LastXCoord - _ScreenWidth / 2.0f) +
(_LastYCoord - _ScreenHeight / 2.0f) * (_LastYCoord - _ScreenHeight / 2.0f));
auto _xIncrement = AsteroidSpeed * (_LastXCoord - _ScreenWidth / 2.0f) / _DistToPlayer;
auto _yIncrement = AsteroidSpeed * (_LastYCoord - _ScreenHeight / 2.0f) / _DistToPlayer;
_CentreScreenX += _xIncrement;
_CentreScreenY += _yIncrement;
Coordinate_x = _CentreScreenX;
Coordinate_y = _CentreScreenY;
AsteroidObj.setPosition(Coordinate_x, Coordinate_y);
}
void Asteroid::DrawAsteroid(sf::RenderWindow& window)
{
window.draw(AsteroidObj);
}
float Asteroid::XGetPos()
{
return AsteroidObj.getPosition().x;
}
float Asteroid::YGetPos()
{
return AsteroidObj.getPosition().y;
}
void Asteroid::setPlayerLastCoord(int X, int Y)
{
_LastXCoord = X;
_LastYCoord = Y;
}
void Asteroid::ResetPos()
{
_CentreScreenX = _ScreenWidth / 2.0f;
_CentreScreenY = _ScreenHeight / 2.0f;
AsteroidObj.setPosition(_ScreenWidth / 2.0f, _ScreenHeight / 2.0f);
}