-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCore.Coroutine.h
More file actions
117 lines (99 loc) · 3.13 KB
/
Copy pathCore.Coroutine.h
File metadata and controls
117 lines (99 loc) · 3.13 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
#pragma once
#include "CoroutineHelper.h"
#include "ClassProperty.h"
#include "TimeSystem.h"
class CoroutineManager : public Singleton<CoroutineManager>
{
private:
friend class Singleton;
CoroutineManager() = default;
~CoroutineManager() = default;
public:
void StartCoroutine(Coroutine<> coroutine);
void StartCoroutine(const std::string& alias, Coroutine<> coroutine);
void StopAllCoroutines();
void DestroyCoroutine();
void DestroyAllCoroutines();
void yield_Null();
void yield_StartCoroutine();
void yield_WaitForSeconds();
void yield_WaitForFixedUpdate();
void yield_WaitForEndOfFrame();
void yield_OtherEvent();
void yield_OnRender();
private:
struct CoroutineWrapper : public LinkProperty<CoroutineWrapper>
{
Coroutine<> coroutine;
std::string aliasName;
std::function<void()> onDone;
bool markedForDelete = false;
CoroutineWrapper(Coroutine<> c)
: coroutine(std::move(c)), LinkProperty(this)
{
}
};
LinkedList<CoroutineWrapper> StartCoroutineQueue;
LinkedList<CoroutineWrapper> waitForFixedUpdateQueue;
LinkedList<CoroutineWrapper> nullQueue;
LinkedList<CoroutineWrapper> waitForSecondsQueue;
LinkedList<CoroutineWrapper> waitForFramesQueue;
LinkedList<CoroutineWrapper> waitUntilQueue;
LinkedList<CoroutineWrapper> waitForSignalQueue;
LinkedList<CoroutineWrapper> waitForEndOfFrameQueue;
LinkedList<CoroutineWrapper> onRenderQueue;
std::unordered_map<std::string, CoroutineWrapper*> coroutineMap;
std::vector<CoroutineWrapper*> activeCoroutines;
void ProcessQueue(LinkedList<CoroutineWrapper>& queue);
};
static auto CoroutineManagers = CoroutineManager::GetInstance();
inline void StartCoroutine(Coroutine<> coroutine)
{
CoroutineManagers->StartCoroutine(std::move(coroutine));
}
inline void StartCoroutine(const std::string& alias, Coroutine<> coroutine)
{
CoroutineManagers->StartCoroutine(alias, std::move(coroutine));
}
inline void StopAllCoroutines()
{
CoroutineManagers->StopAllCoroutines();
}
inline YieldInstruction WaitForSeconds(float sec)
{
return YieldInstruction{ YieldInstructionType::WaitForSeconds, sec, 0 };
}
inline YieldInstruction WaitForFrames(int frames)
{
return YieldInstruction{ YieldInstructionType::WaitForFrames, 0.0f, frames };
}
inline YieldInstruction WaitUntil(std::function<bool()> func)
{
YieldInstruction inst;
inst.type = YieldInstructionType::WaitUntil;
inst.condition = std::move(func);
return inst;
}
inline YieldInstruction WaitForSignal(bool* signalFlag)
{
YieldInstruction inst;
inst.type = YieldInstructionType::WaitForSignal;
inst.signal = signalFlag;
return inst;
}
inline YieldInstruction WaitForFixedUpdate()
{
return YieldInstruction{ YieldInstructionType::WaitForFixedUpdate };
}
inline YieldInstruction WaitForEndOfFrame()
{
return YieldInstruction{ YieldInstructionType::WaitForEndOfFrame };
}
inline YieldInstruction ReturnNull()
{
return YieldInstruction{ YieldInstructionType::Null };
}
inline YieldInstruction OnRender()
{
return YieldInstruction{ YieldInstructionType::OnRender };
}