-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.cpp
70 lines (57 loc) · 1.36 KB
/
Scene.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
#include "Scene.hpp"
#include "Config.hpp"
#include <QDebug>
Scene::Scene(QObject *parent)
: QObject(parent)
{
qRegisterMetaType<TubeGeometry*>("TubeGeometry*");
_obstaclesModel = QSharedPointer<ObstaclesModel>::create();
for (int i = 0; i < Config::TubesCount; ++i) {
_geometries << new TubeGeometry();
}
}
int Scene::tubesCount() const
{
return Config::TubesCount;
}
void Scene::init()
{
while (_tubes.size() < Config::TubesCount) {
auto tube = TubeModel::create(_obstaclesModel);
if (tube) {
if (!tube->advance()) {
delete tube;
continue;
}
_tubes << tube;
}
}
for (int i = 0; i < Config::TubesCount; ++i) {
_geometries[i]->update(_tubes[i]->points());
}
}
bool Scene::advance()
{
int stuckCount = 0;
for (int i = 0; i < _tubes.size(); ++i) {
if (_tubes[i]->advance()) {
_geometries[i]->update(_tubes[i]->points());
} else {
stuckCount++;
}
}
return stuckCount < Config::TubesCount / 2;
}
void Scene::clear()
{
while (!_tubes.isEmpty()) {
delete _tubes.takeLast();
}
_geometries.clear();
_obstaclesModel->clear();
emit cleared();
}
TubeGeometry *Scene::getTubeGeometry(int index) const
{
return _geometries.value(index, nullptr);
}