-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswPlayerKillMsg.cpp
46 lines (40 loc) · 1.23 KB
/
swPlayerKillMsg.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
#include "swPlayerKillMsg.h"
#include "swFactory.h"
#include "swDebris.h"
#include "swShip.h"
#include "swMesh.h"
#include <cmath>
swPlayerKillMsg::swPlayerKillMsg() : killerId(-1) {
type = SW_PLAYER_KILL_MSG;
}
swPlayerKillMsg::~swPlayerKillMsg() {}
void swPlayerKillMsg::read(swStream* stream) {
stream->readInt(index);
stream->readInt(killerId);
}
void swPlayerKillMsg::write(swStream* stream) {
stream->writeInt(index);
stream->writeInt(killerId);
}
void swPlayerKillMsg::cliHandle(swClient* client) {
// if the player hasn't already been killed
if(client->players[index].shipId != -1) {
// destroy the player ship (adds debris)
swShip* ship = (swShip*)client->game->simulator.objects[client->players[index].shipId];
ship->die();
// increment killer's score
if(killerId != -1)
client->players[killerId].score++;
}
}
void swPlayerKillMsg::servHandle(swServer* server, QTcpSocket* sock) {
// check that this is client 0
if(server->socks.indexOf(sock) == 0) {
// forward the message to all clients
if(killerId != -1)
server->clientList[killerId].score++;
foreach(QTcpSocket* sock, server->socks) {
swFactory::writeObject(this, sock);
}
}
}