This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoalmanager.cpp
More file actions
124 lines (107 loc) · 2.61 KB
/
Copy pathgoalmanager.cpp
File metadata and controls
124 lines (107 loc) · 2.61 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
#include "goalmanager.h"
GoalManager::GoalManager()
: problemSet(0), task(0), received(0),
required(std::get<3>(levels[problemSet][task])), icon(nullptr) {
auto &[type, shape, trait, num] = levels[problemSet][task];
ref = getMine(type, shape, R0, trait);
updateIcon();
}
GoalManager::GoalManager(QDataStream &in) : icon(nullptr) {
in >> problemSet >> task >> received;
auto &[type, shape, trait, num] = levels[problemSet][task];
required = num;
assert(inRange());
ref = getMine(type, shape, R0, trait);
updateIcon();
}
void GoalManager::save(QDataStream &out) {
out << problemSet << task << received;
}
void GoalManager::receiveItem(const Item *item) {
const Task &t = levels[problemSet][task];
auto [type, shape, trait, num] = t;
if (auto dut = dynamic_cast<const Mine *>(item)) {
if (*dut == *ref) {
advance();
}
emit moneyChange(dut->value());
}
}
void GoalManager::advance() {
assert(inRange());
bool sync = false;
received++;
if (received == required) {
received = 0;
task++;
emit enhanceChange();
sync = true;
}
if (task == levels[problemSet].size()) {
task = 0;
emit mapConstructEvent();
problemSet++;
}
if (problemSet == levels.size()) {
problemSet = 0;
}
if (sync) {
auto &[type, shape, trait, num] = levels[problemSet][task];
required = num;
if (ref) {
delete ref;
}
ref = getMine(type, shape, R0, trait);
updateIcon();
}
emit updateGoal(problemSet, task, received, required, icon);
}
bool GoalManager::inRange()
{
// sanity check
if (!(0 <= problemSet && problemSet < levels.size())) {
return false;
}
auto &p = levels[problemSet];
if (!(0 <= task && task < p.size())) {
return false;
}
if (!(0 <= received && received < required)) {
return false;
}
return true;
}
void GoalManager::updateIcon()
{
if (icon != nullptr) {
delete icon;
}
icon = new QPicture();
QPainter painter(icon);
assert(ref);
ref->paint(&painter);
}
void GoalManager::init()
{
emit updateGoal(problemSet, task, received, required, icon);
}
const std::vector<GoalManager::ProblemSet> GoalManager::levels = {
{
// problem set 0: Miner, Belt and Cutter
{SQUARE, FULL, BLACK, 20},
{ROUND, FULL, BLACK, 30},
{SQUARE, HALF, BLACK, 50},
},
{
// problem set 1: Mixer, Rotater
{ROUND, FULL, RED, 20},
{SQUARE, QUARTER, BLUE, 40},
{SQUARE, HALF, BLUE, 80},
},
{
// problem set 2: Nothing new
{ROUND, HALF, BLACK, 10},
{SQUARE, QUARTER, BLUE, 20},
{ROUND, FULL, RED, 80},
},
};