-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.cc
110 lines (105 loc) · 2.97 KB
/
Line.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
#include "Line.h"
Line::Line(bool isPolychromy, short lineSize, short xCoordinate, short yWindowBottomCoordinate, short symbolSpawnFrequency, short lineExplosionProbability, struct timespec currentTime)
{
this->isPolychromy = isPolychromy;
this->canExplode = false;
this->size = lineSize;
this->xCoordinate = xCoordinate;
this->yTopCoordinate = 0;
this->yBottomCoordinate = 0;
this->lineExplosionProbability = lineExplosionProbability;
this->yWindowBottomCoordinate = yWindowBottomCoordinate;
this->updateTimeStep.tv_sec = 0;
this->updateTimeStep.tv_nsec = 999999998 / symbolSpawnFrequency;
struct timespec delay;
delay.tv_sec = 0;
delay.tv_nsec = rand() % 999999998;
if (delay.tv_nsec + currentTime.tv_nsec >= 999999998)
{
delay.tv_sec = 1;
delay.tv_nsec = delay.tv_nsec - 999999998;
}
this->updateTime.tv_sec = currentTime.tv_sec + delay.tv_sec;
this->updateTime.tv_nsec = currentTime.tv_nsec + delay.tv_nsec;
}
void Line::printNextStep(struct timespec currentTime, bool& wasExplosion)
{
if ((currentTime.tv_sec * 1000000000 + currentTime.tv_nsec) >= (updateTime.tv_sec * 1000000000 + updateTime.tv_nsec))
{
if (0 >= size)
{
throw LineException("The line has completed its task.");
}
if ((yTopCoordinate) >= yWindowBottomCoordinate)
{
canExplode = false;
if ((yBottomCoordinate != yTopCoordinate) && (yBottomCoordinate != 0))
{
mvaddch(yBottomCoordinate, xCoordinate, ' ');
yBottomCoordinate++;
}
else if ((yBottomCoordinate != yTopCoordinate) && (yBottomCoordinate == 0))
{
if ((yTopCoordinate - yBottomCoordinate) < size)
{
size--;
}
else
{
mvaddch(yBottomCoordinate, xCoordinate, ' ');
yBottomCoordinate++;
}
}
else
{
throw LineException("The line has completed its task.");
}
}
else
{
canExplode = true;
if ((yTopCoordinate - yBottomCoordinate) >= size)
{
mvaddch(yBottomCoordinate, xCoordinate, ' ');
yBottomCoordinate++;
}
RandomSymbol* temp = new RandomSymbol(isPolychromy);
temp->printRandomSymbol(yTopCoordinate, xCoordinate);
delete temp;
yTopCoordinate++;
}
if (updateTimeStep.tv_nsec + currentTime.tv_nsec > 999999998) //Перезапись времени обновления. Данное условие отвечает за корректную запись времени.
{
updateTime.tv_sec = currentTime.tv_sec + 1;
updateTime.tv_nsec = abs(updateTimeStep.tv_nsec + currentTime.tv_nsec - 1000000000);
}
else
{
updateTime.tv_sec = currentTime.tv_sec;
updateTime.tv_nsec = updateTimeStep.tv_nsec + currentTime.tv_nsec;
}
if (((rand() % 100 + 1) > (100 - lineExplosionProbability)) && canExplode)
{
wasExplosion = true;
yTopCoordinate--;
size--;
mvaddch(yTopCoordinate, xCoordinate, ' ');
}
else
{
wasExplosion = false;
}
}
}
struct timespec Line::getUpdateTime()
{
return updateTime;
}
short Line::getXCoordinate()
{
return xCoordinate;
}
short Line::getYTopCoordinate()
{
return yTopCoordinate;
}