-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharShape.cpp
118 lines (111 loc) · 3.36 KB
/
CharShape.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
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
#include "CharShape.h"
#include <stdexcept>
CharShape::CharShape(char type, Color color, Vector2f wing_vector, float thickness, Color background_color = Color::Black)
:type{ type }
,wing_vector{ abs(wing_vector.x), abs(wing_vector.y) }
,thickness{ thickness }
{
assign_points();
shape.setFillColor(background_color);
shape.setOutlineColor(color);
shape.setOutlineThickness(4);
/*spline.setColor(color);
assign_points(true);
spline.setThickness(4);
spline.setClosed(true);
spline.setThickCornerType(sw::Spline::ThickCornerType::Round);
spline.smoothHandles();
spline.update();*/
}
void CharShape::setPosition(Vector2f position)
{
/*Vector2f move_vector = position - this->getPosition();
Transformable::setPosition(position);
this->move(move_vector);*/
this->shape.setPosition(position);
}
void CharShape::move(Vector2f offset)
{
this->shape.setPosition(this->shape.getPosition() + offset);
}
/*void CharShape::move(Vector2f move_vector)
{
/*for (int i = 0; i < vertices.size(); i++) {
vertices[i] += move_vector;
}
spline.setPositions(vertices);
spline.update();
}*/
void CharShape::flip(Vector2f origin)
{
shape.setOrigin(origin - shape.getPosition());
shape.move(origin - shape.getPosition());
shape.rotate(180);
/*this->spline.rotate(180.f, origin);
for (int i = 0; i < vertices.size(); i++) {
vertices[i] = spline.getPosition(i);
}
if (vertices[0].y > vertices[1].y) {
if (type == '<' || type == '>') {
Transformable::setPosition(vertices[0] - Vector2f(0, wing_vector.y));
}
else {
Transformable::setPosition(vertices[1]);
}
}
else {
if (type == '<' || type == '>') {
Transformable::setPosition(vertices[4]);
}
else {
Transformable::setPosition(vertices[0] - Vector2f(wing_vector.x, 0));
}
}
this->spline.update();*/
}
void CharShape::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
//target.draw(spline, states);
target.draw(shape, states);
}
void CharShape::assign_points()
{
std::vector<Vector2f> vertices;
if (type == '^') {
vertices.push_back({ wing_vector.x, 0 });
vertices.push_back({ wing_vector.x * 2, wing_vector.y });
vertices.push_back({ wing_vector.x * 2 - thickness, wing_vector.y });
vertices.push_back({ wing_vector.x, wing_vector.y * (thickness / wing_vector.x) });
vertices.push_back({ thickness, wing_vector.y });
vertices.push_back({ 0, wing_vector.y });
}
else if (type == '\\') {
vertices.push_back({ thickness, 0 });
vertices.push_back({ thickness + wing_vector.x, wing_vector.y });
vertices.push_back(wing_vector);
vertices.push_back({ 0, 0 });
}
else if (type == '<') {
vertices.push_back({ 0, wing_vector.y });
vertices.push_back({ wing_vector.x, 0 });
vertices.push_back({ wing_vector.x + thickness, 0 });
vertices.push_back({ thickness, wing_vector.y });
vertices.push_back({ wing_vector.x + thickness, wing_vector.y * 2 });
vertices.push_back({ wing_vector.x, wing_vector.y * 2 });
}
else if (type == '>') {
vertices.push_back({ thickness + wing_vector.x, wing_vector.y });
vertices.push_back({ thickness, wing_vector.y * 2 });
vertices.push_back({ 0, wing_vector.y * 2 });
vertices.push_back(wing_vector);
vertices.push_back({0, 0});
vertices.push_back({thickness, 0});
}
else {
throw std::invalid_argument("invalid character");
}
shape.setPointCount(vertices.size());
for (int i = 0; i < vertices.size(); i++) {
shape.setPoint(i, vertices[i]);
}
}