-
Notifications
You must be signed in to change notification settings - Fork 0
/
05Scripting.cpp
77 lines (68 loc) · 1.62 KB
/
05Scripting.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
#include "00Names.hpp"
namespace Script {
#pragma region Nodes
Node::Node() {
scriptBank.push_back(this);
}
/* DIALOG */
Dialog* Dialog::answer(String answer, Node* action) {
answers.push_back(answer);
actions.push_back(action);
return this;
}
void Dialog::run() {
UI::dialog = UI::Dialog(title, answers);
}
bool Dialog::update() {
if (UI::dialog.choosed) {
next = actions[UI::dialog.choice];
return true;
}
return false;
}
#pragma endregion Nodes
#pragma region Manager
struct Thread {
Node* ptr = nullptr;
Node* head;
Thread(Node* head = nullptr)
: ptr(nullptr), head(head) {}
};
Vector<Thread> threads(0, Thread());
void addThread(Node* head) {
threads.push_back(Thread(head));
}
void update() {
for (int i = 0; i < threads.size(); i++) {
if (!threads[i].ptr) threads[i].ptr = threads[i].head, threads[i].ptr->run();
if (threads[i].ptr->update()) {
Node* next = threads[i].ptr->next;
if (next) {
threads[i].ptr = next;
next->run();
} else threads.erase(i--);
}
}
}
void save() {
File file = SD.open(savepath + "/scripts.dat", FILE_WRITE);
for (int i = 0; i < threads.size(); i++) {
uint32_t index = 0;
for (int j = 0; j < scriptBank.size(); j++) {
if (scriptBank[j] == threads[i].ptr) {
index = j;
break;
}
}
write<uint32_t>(file, index);
}
file.close();
}
void load() {
File file = SD.open(savepath + "/scripts.dat", FILE_READ);
while (file.available()) Script::addThread(scriptBank[(int)read<uint32_t>(file)]);
file.close();
}
#pragma endregion Manager
}
Vector<ScriptNode*> scriptBank;