-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawingmodule.cpp
More file actions
144 lines (137 loc) · 7.2 KB
/
drawingmodule.cpp
File metadata and controls
144 lines (137 loc) · 7.2 KB
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
#include "drawingmodule.h"
DrawingModule::DrawingModule(sf::Vector2f newOrigin, float newSide, std::string newSerial, sf::Font newFont):
BaseModule(newOrigin, newSide, newSerial, newFont), stringSoundBuffer("StringPull.wav"), stringPull(stringSoundBuffer)
{
stringPull.setLooping(true);
genertePoints();
stringPull.setVolume(20.f);
}
void DrawingModule::genertePoints()
{
auto getFakeNumber = [=](int real){if (amt == 12) {return numbers_12[real];} else if (amt == 13) {return numbers_13[real];} else {return numbers_14[real];} };
/*
* Я переписал логику ложной точки на лямбду, ура. Понятнее не стало, но по крайне мере это не так страшно выглядит.
* Точка с номером 2 обозначена как 10, но если остаток от деления кол-ва точек на 3 равен 0, то как 9
* Точка с номером 3 обозначена как 1, но если остаток от деления кол-ва точек на 3 равен 1, то как 5
* Точка с номером 4 обозначена как 8, но если остаток от деления кол-ва точек на 3 равен 1, то как 3
* Точка с номером 5 обозначена как 2, но если остаток от деления кол-ва точек на 3 равен 2, то как 4
* Точка с номером 6 обозначена как 9, но если остаток от деления кол-ва точек на 3 равен 0, то как 11
* Точка с номером 7 обозначена как 6, но если остаток от деления кол-ва точек на 3 равен 2, то как 12
* Точка с номером 8 обозначена как 12, но если остаток от деления кол-ва точек на 3 равен 2, то как 14
* Точка с номером 9 обозначена как 13, но если остаток от деления кол-ва точек на 3 равен 0, то как 3
* Точка с номером 10 обозначена как 5, но если остаток от деления кол-ва точек на 3 равен 1, то как 1
* Точка с номером 11 обозначена как 7, но если остаток от деления кол-ва точек на 3 равен 2, то как 2
* Точка с номером 12 обозначена как 11, но если остаток от деления кол-ва точек на 3 равен 0, то как 10
* Точка с номером 13 обозначена как 8, но если остаток от деления кол-ва точек на 3 равен 2, то как 7
* Точка с номером 14 обозначена как 3, но если остаток от деления кол-ва точек на 3 равен 1, то как 4
*/
currentPoint = 0;
points.clear();
amt = 12 + rand() % 3;
int j = 0;
for (int i = 0; i < amt; i++){
sf::Vector2f pointPos = origin + sf::Vector2f{side * 0.8f * 0.01f * (rand() % 100) + side * 0.1f, side * 0.8f * 0.01f * (rand() % 100) + side * 0.1f};
if (points.empty()){
points.push_back(DrawingPoint(font, pointPos, side * 0.01f, i + 1, getFakeNumber(i)));
continue;
}
j = 0;
while (1){
if ((points.at(j).getPosition() - pointPos).length() <= side * 0.1f) {
pointPos = origin + sf::Vector2f{side * 0.8f * 0.01f * (rand() % 100) + side * 0.1f, side * 0.8f * 0.01f * (rand() % 100) + side * 0.1f};
j = 0;
} else {
j++;
if (j >= points.size()) {
break;
}
}
}
points.push_back(DrawingPoint(font, pointPos, side * 0.01f, i + 1, getFakeNumber(i)));
}
}
bool DrawingModule::checkWin()
{
bool isAllRight = true;
for (int i = 0; i < amt; i++){
if (!points.at(i).isPrecursor()) {
return false;
} else if (!points.at(i).isRight() && ((i + 1) != amt)) {
isAllRight = false;
}
}
if (isAllRight) {
return true;
} else {
mistakes++;
genertePoints();
return false;
}
}
void DrawingModule::process(sf::RenderWindow *window, int time)
{
if (!isDone) {
if (isPosInModule(sf::Vector2f(sf::Mouse::getPosition(*window)))) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
if (currentPoint) {
points.at(currentPoint - 1).setTarget(sf::Vector2f(sf::Mouse::getPosition(*window)));
for (int i = 0; i < amt; i++){
if ((points.at(i).getPosition() - sf::Vector2f(sf::Mouse::getPosition(*window))).length() <= side * 0.035f && currentPoint != i + 1 && !points.at(i).isPrecursor()) {
points.at(currentPoint - 1).setNext(&points.at(i));
points.at(i).setTarget(sf::Vector2f(sf::Mouse::getPosition(*window)));
currentPoint = i + 1;
stringPull.play();
break;
}
}
stringPull.setPitch(stringPull.getPitch() + ((points.at(currentPoint - 1).getPosition() - sf::Vector2f(sf::Mouse::getPosition(*window))).length() / (side * 2) - stringPull.getPitch()) * 0.1f);
} else {
for (int i = 0; i < amt; i++){
if ((points.at(i).getPosition() - sf::Vector2f(sf::Mouse::getPosition(*window))).length() <= side * 0.035f) {
points.at(i).setTarget(sf::Vector2f(sf::Mouse::getPosition(*window)));
currentPoint = i + 1;
stringPull.play();
break;
}
}
}
} else {
currentPoint = 0;
stringPull.pause();
for (int i = 0; i < amt; i++){
points.at(i).resetTarget();
}
}
} else {
currentPoint = 0;
stringPull.pause();
for (int i = 0; i < amt; i++){
points.at(i).resetTarget();
}
}
if (checkWin()) {
for (int i = 0; i < amt; i++){
if (points.at(i).getNext() == NULL) {
points.at(i).resetTarget();
break;
}
}
isDone = true;
}
}
}
void DrawingModule::render(sf::RenderWindow *window)
{
std::array vertices =
{
sf::Vertex{{origin.x, origin.y}},
sf::Vertex{{origin.x + side, origin.y}},
sf::Vertex{{origin.x + side, origin.y + side}},
sf::Vertex{{origin.x, origin.y + side}},
sf::Vertex{{origin.x, origin.y}},
};
window->draw(vertices.data(), vertices.size(), sf::PrimitiveType::LineStrip);
for (int i = 0; i < amt; i++){
points.at(i).render(window);
}
}