summaryrefslogtreecommitdiffstats
path: root/src/Event.hpp
blob: b137a85297ba4d37083b712d50b034dd376bf843 (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#pragma once

#include <queue>
#include <map>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <variant>
#include <functional>

#include <SFML/Window.hpp>

#include "Vector.hpp"
#include "Packet.hpp"

enum class EventType {
	Echo,
	ChunkChanged,
	ConnectToServer,
	ConnectionSuccessfull,
	Disconnect,
	RequestNetworkClient,
	RegisterNetworkClient,
	PlayerConnected,
	RemoveLoadingScreen,
	ConnectionFailed,
    Exit,
    Disconnected,
    Connecting,
    NetworkClientException,
    MouseMoved,
    KeyPressed,
    KeyReleased,
    InitalizeSectionRender,
    UpdateSectionsRender,
    CreateSectionRender,
    CreatedSectionRender,
    PlayerPosChanged,
    DeleteSectionRender,
    EntityChanged,
    NewRenderDataAvailable,
    BlockChange,
    RendererWorkerTask,
    ChunkDeleted,
};

struct EchoData {
	std::chrono::time_point<std::chrono::high_resolution_clock> time;
};

struct ChunkChangedData {
	Vector chunkPosition;
};

struct ConnectToServerData {
	std::string address;
	unsigned short port;
};

class NetworkClient;

struct ConnectionSuccessfullData {
    std::shared_ptr<NetworkClient> ptr;
};

struct DisconnectData {
    std::string reason;
};

struct SendPacketData {
	std::shared_ptr<Packet> packet;
};

struct ReceivePacketData {
	std::shared_ptr<Packet> packet;
};

struct RequestNetworkClientData {

};

struct RegisterNetworkClientData {
	NetworkClient *ptr;
};

class GameState;

struct PlayerConnectedData {
    std::shared_ptr<GameState> ptr;
};

struct RemoveLoadingScreenData {

};

struct ConnectionFailedData {
	std::string reason;
};

struct ExitData {

};

struct DisconnectedData {
    std::string reason;
};

struct ConnectingData {

};

struct NetworkClientExceptionData {
    std::string what;
};

struct MouseMovedData {
    double x, y;
};

struct KeyPressedData {
    sf::Keyboard::Key key;
};

struct KeyReleasedData {
    sf::Keyboard::Key key;
};

struct InitalizeSectionRenderData {
    Vector pos;
};

struct CreateSectionRenderData {
    Vector pos;
};

struct CreatedSectionRenderData {
    Vector pos;
};

struct PlayerPosChangedData {
    VectorF newPos;
};

struct UpdateSectionsRenderData {

};

struct DeleteSectionRenderData {
    Vector pos;
};

struct EntityChangedData {
    unsigned int EntityId;
};

struct NewRenderDataAvailableData {

};

struct BlockChangeData {
    Vector SectionPos;
};

struct RendererWorkerTaskData {
    size_t WorkerId;
    Vector Task;
};

struct ChunkDeletedData {
    Vector pos;
};

using EventData = std::variant<EchoData, ChunkChangedData, ConnectToServerData, ConnectionSuccessfullData,
        DisconnectData, SendPacketData, ReceivePacketData, RequestNetworkClientData, RegisterNetworkClientData,
        PlayerConnectedData, RemoveLoadingScreenData, ConnectionFailedData, ExitData, DisconnectedData,
        ConnectingData, NetworkClientExceptionData, MouseMovedData, KeyPressedData, KeyReleasedData, 
        InitalizeSectionRenderData, CreateSectionRenderData, CreatedSectionRenderData, PlayerPosChangedData,
        UpdateSectionsRenderData, DeleteSectionRenderData, EntityChangedData,NewRenderDataAvailableData,
        BlockChangeData, RendererWorkerTaskData, ChunkDeletedData>;

struct Event {
	EventType type;
	EventData data;
};

class EventListener {
	friend class EventAgregator;

	using HandlerFunc = std::function<void(EventData)>;

	std::map<EventType, HandlerFunc> handlers; //TODO: There must be more elegant solution than std::variant of all data

	std::mutex handlersMutex;

	std::queue<Event> events;

	std::mutex eventsMutex;

	void PushEvent(Event event);

    void DirectCall(Event event);

public:
	EventListener();
	~EventListener();
	bool IsEventsQueueIsNotEmpty();

	void RegisterHandler(EventType type, HandlerFunc handler);

	void HandleEvent();
};

class EventAgregator {
	friend EventListener;

	EventAgregator() = default;
	static std::queue<Event> eventsToHandle;
	static std::mutex queueMutex;
	static bool isStarted;
	static std::vector<EventListener *> listeners;
	static std::mutex listenersMutex;

	static void EventHandlingLoop();

	static void RegisterListener(EventListener &listener);
	static void UnregisterListener(EventListener &listener);

public:
	static void PushEvent(EventType type, EventData data);
    static void DirectEventCall(EventType, EventData data);
};