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
|
#include "ThreadGame.hpp"
ThreadGame::ThreadGame() {
}
ThreadGame::~ThreadGame() {
}
void ThreadGame::Execute() {
EventListener listener;
listener.RegisterHandler(EventType::Exit, [this] (EventData eventData) {
isRunning = false;
});
listener.RegisterHandler(EventType::ConnectionSuccessfull, [this](EventData eventData) {
auto data = std::get<ConnectionSuccessfullData>(eventData);
gs = std::make_shared<GameState>(data.ptr);
gs->gs = gs;
});
listener.RegisterHandler(EventType::Disconnected, [this](EventData eventData) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (!gs)
return;
gs->gs.reset();
gs.reset();
});
listener.RegisterHandler(EventType::KeyPressed, [this](EventData eventData) {
if (!gs)
return;
switch (std::get<KeyPressedData>(eventData).key) {
case sf::Keyboard::W:
isMoving[GameState::FORWARD] = true;
break;
case sf::Keyboard::A:
isMoving[GameState::LEFT] = true;
break;
case sf::Keyboard::S:
isMoving[GameState::BACKWARD] = true;
break;
case sf::Keyboard::D:
isMoving[GameState::RIGHT] = true;
break;
case sf::Keyboard::Space:
isMoving[GameState::JUMP] = true;
break;
}
});
listener.RegisterHandler(EventType::KeyReleased, [this](EventData eventData) {
if (!gs)
return;
switch (std::get<KeyReleasedData>(eventData).key) {
case sf::Keyboard::W:
isMoving[GameState::FORWARD] = false;
break;
case sf::Keyboard::A:
isMoving[GameState::LEFT] = false;
break;
case sf::Keyboard::S:
isMoving[GameState::BACKWARD] = false;
break;
case sf::Keyboard::D:
isMoving[GameState::RIGHT] = false;
break;
case sf::Keyboard::Space:
isMoving[GameState::JUMP] = false;
break;
}
});
listener.RegisterHandler(EventType::MouseMoved, [this](EventData eventData) {
if (!gs)
return;
auto data = std::get<MouseMovedData>(eventData);
gs->HandleRotation(data.x, data.y);
});
LoopExecutionTimeController timer(std::chrono::milliseconds(int(1.0f / 60.0f * 1000.0f)));
while (isRunning) {
if (gs != nullptr)
gs->Update(timer.GetRealDeltaS());
listener.HandleEvent();
if (gs != nullptr) {
gs->UpdatePacket();
if (isMoving[GameState::FORWARD])
gs->HandleMovement(GameState::FORWARD, timer.GetRealDeltaS());
if (isMoving[GameState::BACKWARD])
gs->HandleMovement(GameState::BACKWARD, timer.GetRealDeltaS());
if (isMoving[GameState::LEFT])
gs->HandleMovement(GameState::LEFT, timer.GetRealDeltaS());
if (isMoving[GameState::RIGHT])
gs->HandleMovement(GameState::RIGHT, timer.GetRealDeltaS());
if (isMoving[GameState::JUMP])
gs->HandleMovement(GameState::JUMP, timer.GetRealDeltaS());
}
timer.Update();
}
gs.reset();
}
|