-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClientObject.hpp
129 lines (116 loc) · 3.15 KB
/
ClientObject.hpp
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
#pragma once
#include "GameObject.hpp"
#include "LobbyObject.hpp"
#include "MetaServerObject.hpp"
#include "CursorObject.hpp"
#include "Client.hpp"
struct ClientObject {
ui::CursorObject cursor;
Client &client;
std::string dir;
GameObject *gObject = nullptr;
LobbyObject lObject;
MetaServerObject mObject;
ClientObject(Client &client, const std::string &dir):
cursor(dir),
client(client), dir(dir),
mObject(client.mclient, dir),
lObject(dir)
{}
void init() {
Logger::Info("cobject: initialized\n");
mObject.init();
lObject.init();
gObject = nullptr; // need to set/unset actors!
cursor.init();
}
void keypress(int key, int mods) {
if(gObject != nullptr) {
gObject->keypress(key, mods);
}
}
bool is_active() {
return client.state == Client::State::DEFAULT;
}
void mouse(float m_x, float m_y) {
if(mObject.is_active()) {
mObject.mouse(m_x, m_y);
}
if(lObject.is_active()) {
lObject.mouse(m_x, m_y);
}
if(gObject != nullptr) {
gObject->mouse(m_x, m_y);
}
cursor.mouse(m_x, m_y);
}
void mouse_click(int button, int action) {
if(mObject.is_active()) {
mObject.mouse_click(button, action);
} else if(lObject.is_active()) {
lObject.mouse_click(button, action);
} else if(gObject != nullptr) {
gObject->mouse_click(button, action);
}
}
void update_states() {
// perform actions
if(client.mclient.has_quit()) {
client.action_quit();
}
if(client.is_active_mclient() && !client.is_active_lobby() && !client.is_active_game()) {
if(client.mclient.has_hosted()) {
client.action_host_game();
} else if(client.mclient.has_joined()) {
client.action_join_game();
}
} else if(client.is_active_lobby()) {
if(client.l_actor->has_quit()) {
client.action_quit_lobby();
} else if(client.l_actor->has_started()) {
client.action_start_game();
}
}
if(client.is_active_game() && client.intelligence->has_quit()) {
client.action_quit_game();
}
// set lObject
lObject.lobbyActor = client.l_actor;
// set gObject
if(gObject == nullptr && client.is_active_game()) {
gObject = new GameObject(*client.soccer, *client.intelligence, cursor, dir);
gObject->init();
} else if(gObject != nullptr && !client.is_active_game()) {
gObject->clear();
delete gObject;
gObject = nullptr;
}
ASSERT(!(lObject.is_active() && gObject != nullptr));
}
void display(GLFWwindow *window, size_t wgt, size_t hgt) {
update_states();
if(mObject.is_active()) {
mObject.display();
} else if(lObject.is_active()) {
lObject.display();
} else if(client.is_active_game()) {
gObject->set_winsize(wgt, hgt);
gObject->keyboard(window);
gObject->idle();
gObject->current_time += 1./60;
gObject->display();
}
cursor.display();
}
void clear() {
Logger::Info("cobject: clearance\n");
mObject.clear();
lObject.clear();
if(gObject != nullptr) {
gObject->clear();
delete gObject;
gObject = nullptr;
}
cursor.display();
}
};