-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_gameloop.cpp
79 lines (61 loc) · 2.44 KB
/
server_gameloop.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
78
79
#include "server_gameloop.h"
Server_gameloop::Server_gameloop(Queue<uint8_t> &queue,
Server_protected_queue &protected_queue) : queue_server(queue),
protected_queue(protected_queue), cantidad_enemigos_vivos(MAX_ENEMIGOS),
cantidad_enemigos_muertos(0), termino_juego(false), byte_recibido(0x00) {}
void Server_gameloop::realizar_broadcast(uint8_t const &byte_accion) {
Server_data dato(this->cantidad_enemigos_vivos, this->cantidad_enemigos_muertos, byte_accion);
this->protected_queue.realizar_broadcast(std::move(dato));
}
void Server_gameloop::mostrar_linea(std::string const &accion) {
std::cout << "Un enemigo ha " << accion << ". " << this->cantidad_enemigos_vivos <<
" vivo(s) y " << this->cantidad_enemigos_muertos << " muerto(s)." << std::endl;
}
void Server_gameloop::eliminar_enemigo() {
this->cantidad_enemigos_vivos --;
this->cantidad_enemigos_muertos ++;
for (int i = 0; i < MAX_ENEMIGOS; i++) {
if (this->enemigos[i].verificar_vivo()) {
this->enemigos[i].eliminar();
break;
}
}
this->byte_recibido = 0x00;
realizar_broadcast(BYTE_ELIMINAR);
mostrar_linea(PALABRA_MUERTO);
}
void Server_gameloop::revivir_enemigo(Enemigo &enemigo) {
if (enemigo.get_cantidad_iteraciones() == MAX_ITERACIONES) {
enemigo.resetear_iteraciones();
if (this->cantidad_enemigos_vivos < MAX_ENEMIGOS) {
this->cantidad_enemigos_vivos ++;
this->cantidad_enemigos_muertos --;
this->byte_recibido = 0x00;
enemigo.revivir();
realizar_broadcast(BYTE_REVIVIR);
mostrar_linea(PALABRA_REVIVIDO);
}
}
}
void Server_gameloop::run() {
while (!termino_juego) {
try {
queue_server.try_pop(this->byte_recibido);
} catch (ClosedQueue &error) {
termino_juego = true;
byte_recibido = 0x00;
}
if (this->byte_recibido == BYTE_ATACAR && cantidad_enemigos_muertos != MAX_ENEMIGOS) {
eliminar_enemigo();
}
for (int i = 0; i < MAX_ENEMIGOS; i++) {
if (!enemigos[i].verificar_vivo()) {
enemigos[i].aumnentar_itearciones();
}
}
for (int i = 0; i < MAX_ENEMIGOS; i++) {
revivir_enemigo(this->enemigos[i]);
}
std::this_thread::sleep_for(std::chrono::milliseconds(TIEMPO_LOOP));
}
}