-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rectangle.cc
168 lines (148 loc) · 5.81 KB
/
Rectangle.cc
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "Rectangle.hpp"
#define PI 3.14159265
Rectangle::Rectangle(float x, float y, int width, int height, float rotation, bool hasGravity, float velocityOnX, float velocityOnY, bool collide, sf::Color color, sf::Texture texture, Repeated repeat) :
Element(x, y, rotation, hasGravity, velocityOnX, velocityOnY, collide, color, texture), m_width(width), m_height(height), m_repeat(repeat)
{
}
Rectangle::Rectangle(float x, float y, int width, int height, float rotation, bool hasGravity, float velocityOnX, float velocityOnY, bool collide, sf::Texture texture, Repeated repeat) :
Element(x, y, rotation, hasGravity, velocityOnX, velocityOnY, collide, texture), m_width(width), m_height(height), m_repeat(repeat)
{
}
Rectangle::~Rectangle()
{
}
void Rectangle::draw(sf::RenderWindow &window)
{
auto size = m_texture.getSize(); // Get the size of the texture
sf::RectangleShape rectangle(sf::Vector2f(m_width, m_height));
rectangle.setFillColor(m_color);
rectangle.setPosition(m_position.x, m_position.y);
rectangle.rotate(m_rotation);
rectangle.setTexture(&m_texture);
// Here we suppose that an object can't be repeated and facing to a direction
if (m_facingDirection == FACING_RIGHT)
{
rectangle.setTextureRect(sf::IntRect(size.x, 0, -size.x, size.y));
}
if (m_repeat == VERTICALY) {
rectangle.setTextureRect(sf::IntRect(0, 0, size.x, size.y * m_height / m_width)); // Set the position of the texture to be displayed in the rect
}
else if (m_repeat == HORIZONTALY) {
rectangle.setTextureRect(sf::IntRect(0, 0, size.x * m_width / m_height, size.y)); // Set the position of the texture to be displayed in the rect
}
else if (m_repeat == BOTH) {
rectangle.setTextureRect(sf::IntRect(0, 0, size.x * m_width / m_height, size.y * m_height / m_width)); // Set the position of the texture to be displayed in the rect
}
window.draw(rectangle);
}
Vector2D Rectangle::get_center() const
{
const float angleInRadiant = (m_rotation*PI)/180;
const float cosAngle = cos(angleInRadiant);
const float sinAngle = sin(angleInRadiant);
const float halfWidth = m_width/2;
const float halfHeight = m_height/2;
const Vector2D center = {m_position.x + halfWidth*cosAngle - halfHeight*sinAngle, m_position.y + halfWidth*sinAngle + halfHeight*cosAngle};
return center;
}
std::vector<Vector2D> Rectangle::get_vertices_coord() const
{
const float angleInRadiant = (m_rotation*PI)/180;
const float cosAngle = cos(angleInRadiant);
const float sinAngle = sin(angleInRadiant);
// calculate the coordinates of the 4 vertices by taking in account the rotation of the rectangle
// using this formula : https://stackoverflow.com/questions/1469149/calculating-vertices-of-a-rotated-rectangle#answer-1469166
std::vector<Vector2D> vertices;
vertices.push_back(Vector2D(m_position.x, m_position.y)); // top left corner
vertices.push_back(Vector2D(m_position.x + m_width*cosAngle, m_position.y + sinAngle*m_width)); // top right corner
vertices.push_back(Vector2D(m_position.x + m_width*cosAngle - m_height*sinAngle, m_position.y + m_width*sinAngle + m_height*cosAngle)); // bottom right corner
vertices.push_back(Vector2D(m_position.x - m_height*sinAngle, m_position.y + m_height*cosAngle)); // bottom left corner
return vertices;
}
Vector2D Rectangle::get_futhest_point(const Vector2D &direction) const
{
std::vector<Vector2D> vertices = Rectangle::get_vertices_coord();
Vector2D futhest = vertices[0]; // initialize the futhest point with the coordinates of a random corner
float bestScore = Math::dot(direction, futhest); // calculate the score of this point
// search for a point with a superior score
for (int i = 1; i < 4; ++i)
{
const float newScore = Math::dot(direction, vertices[i]);
if (newScore > bestScore)
{
bestScore = newScore;
futhest = vertices[i];
}
}
return futhest;
}
void Rectangle::update_animation(float dt, const sf::Image img[])
{
m_clock += dt;
if (m_clock >= 0.3)
{
m_clock = 0;
switch (m_animation)
{
case 0:
m_texture.loadFromImage(img[0]);
m_animation += 1;
break;
case 1:
m_texture.loadFromImage(img[1]);
m_animation += 1;
break;
case 2:
m_texture.loadFromImage(img[2]);
m_animation += 1;
break;
case 3:
m_texture.loadFromImage(img[2]);
m_animation += 1;
m_facingDirection = FACING_RIGHT;
break;
case 4:
m_texture.loadFromImage(img[1]);
m_animation = 0;
m_facingDirection = FACING_LEFT;
break;
default:
std::cout << "Error: Unknown animation" << std::endl;
}
}
}
void Rectangle::update_animation2(float dt, const sf::Image img[])
{
m_clock += dt;
if (m_clock >= 0.3)
{
m_clock = 0;
switch (m_animation)
{
case 0:
m_texture.loadFromImage(img[0]);
m_animation += 1;
m_facingDirection = FACING_LEFT;
break;
case 1:
m_texture.loadFromImage(img[1]);
m_animation += 1;
break;
case 2:
m_texture.loadFromImage(img[1]);
m_animation += 1;
m_facingDirection = FACING_RIGHT;
break;
case 3:
m_texture.loadFromImage(img[0]);
m_animation = 0;
break;
default:
std::cout << "Error: Unknown animation" << std::endl;
}
}
}
// Getters
int Rectangle::get_height() const { return m_height; }
int Rectangle::get_width() const { return m_width; }
void Rectangle::set_repeated(Repeated repeat) { m_repeat = repeat; };