-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswFactory.cpp
95 lines (87 loc) · 2.51 KB
/
swFactory.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "swFactory.h"
#include "swMessage.h"
#include "swShip.h"
#include "swStar.h"
#include "swMissile.h"
#include "swClientInitMsg.h"
#include "swPhysCreateMsg.h"
#include "swPlayerCreateMsg.h"
#include "swPlayerInputMsg.h"
#include "swPhysUpdateMsg.h"
#include "swPhysDeleteMsg.h"
#include "swPlayerKillMsg.h"
#include "swReadyMsg.h"
swFactory::swFactory() {}
swObject* swFactory::readObject(QIODevice* stream) {
return readObject((swStream*)stream);
}
swObject* swFactory::readObject(swStream* stream) {
int type = stream->readInt();
swObject* data;
switch(type) {
case(swObject::SW_OBJECT):
data = new swObject();
break;
case(swObject::SW_SHIP):
data = new swShip();
break;
case(swObject::SW_STAR):
data = new swStar();
break;
case(swObject::SW_MISSILE):
data = new swMissile();
break;
case(swObject::SW_MESSAGE):
data = new swMessage();
break;
case(swObject::SW_CLIENT_INIT_MSG):
data = new swClientInitMsg();
break;
case(swObject::SW_PLAYER_CREATE_MSG):
data = new swPlayerCreateMsg();
break;
case(swObject::SW_PHYS_CREATE_MSG):
data = new swPhysCreateMsg();
break;
case(swObject::SW_PLAYER_INPUT_MSG):
data = new swPlayerInputMsg();
break;
case(swObject::SW_PHYS_UPDATE_MSG):
data = new swPhysUpdateMsg();
break;
case(swObject::SW_PHYS_DELETE_MSG):
data = new swPhysDeleteMsg();
break;
case(swObject::SW_PLAYER_KILL_MSG):
data = new swPlayerKillMsg();
break;
case(swObject::SW_READY_MSG):
data = new swReadyMsg();
break;
default:
qWarning("Factory attempt to read unsupported type: %d", type);
}
data->read(stream);
return data;
}
swMessage* swFactory::readMessage(QIODevice* stream) {
swObject* msg = readObject(stream);
if(msg->type >= swObject::SW_MESSAGE)
return (swMessage*)msg;
qWarning("Invalid message type read: %d", msg->type);
return NULL;
}
swPhysical* swFactory::readPhysical(QIODevice* stream) {
swObject* obj = readObject(stream);
if(obj->type >= swObject::SW_PHYSICAL && obj->type <= swObject::SW_DEBRIS)
return (swPhysical*)obj;
qWarning("Invalid physical type read: %d", obj->type);
return NULL;
}
void swFactory::writeObject(swObject* obj, QIODevice* stream) {
writeObject(obj, (swStream*)stream);
}
void swFactory::writeObject(swObject* obj, swStream* stream) {
stream->writeInt(obj->type);
obj->write(stream);
}