|
1 |
|
2 #include "NetworkClient.hh" |
|
3 #include "Engine.hh" |
|
4 #include "Logger.hh" |
|
5 |
|
6 #include <cassert> |
|
7 |
|
8 NetworkClient::NetworkClient (GameState &state, const NetworkAddress &connect_to) : |
|
9 NetworkCore(state), netsession(NETWORK_MAGIC_ID), server(netsession.connect(connect_to)), netobjs(netsession, NETCHAN_CORE, server) { |
|
10 |
|
11 // connect slots |
|
12 slots.connect(netobjs.sig_create(), this, &NetworkClient::on_create); |
|
13 |
|
14 // XXX: sig_disconnected |
|
15 } |
|
16 |
|
17 void NetworkClient::on_create (NetworkObject_Client *obj, NetworkMessageID msg_id, NetworkPacket &pkt) { |
|
18 switch (msg_id) { |
|
19 case NETMSG_SERVER_HELLO: |
|
20 on_server_hello(obj, pkt); |
|
21 |
|
22 break; |
|
23 |
|
24 case NETMSG_PLAYER_INFO: |
|
25 on_player_info(obj, pkt); |
|
26 |
|
27 break; |
|
28 |
|
29 case NETMSG_PLAYER_JOIN: |
|
30 on_player_join(obj, pkt); |
|
31 |
|
32 break; |
|
33 |
|
34 default: |
|
35 Engine::log(WARN, "client.on_create_object") << "unknown msg_id=" << msg_id << " for obj=" << obj; |
|
36 } |
|
37 } |
|
38 |
|
39 void NetworkClient::on_server_hello (NetworkObject_Client *obj, NetworkPacket &pkt) { |
|
40 // read the packet |
|
41 Vector position = pkt.read_vector(); |
|
42 |
|
43 Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << position; |
|
44 |
|
45 // create the LocalPlayer object |
|
46 NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, position); |
|
47 |
|
48 // inform state |
|
49 state.newLocalPlayer(player); |
|
50 } |
|
51 |
|
52 void NetworkClient::on_player_info (NetworkObject_Client *obj, NetworkPacket &pkt) { |
|
53 // read the packet |
|
54 Vector position = pkt.read_vector(); |
|
55 |
|
56 Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << position; |
|
57 |
|
58 // create the LocalPlayer object |
|
59 NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position); |
|
60 |
|
61 // inform state |
|
62 state.newRemotePlayer(player); |
|
63 |
|
64 } |
|
65 |
|
66 void NetworkClient::on_player_join (NetworkObject_Client *obj, NetworkPacket &pkt) { |
|
67 // read the packet |
|
68 Vector position = pkt.read_vector(); |
|
69 |
|
70 Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << position; |
|
71 |
|
72 // create the RemotePlayer object |
|
73 NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position); |
|
74 |
|
75 // inform state |
|
76 state.newRemotePlayer(player); |
|
77 } |
|
78 |
|
79 void NetworkClient::player_quit (NetworkClientRemotePlayer *player) { |
|
80 // inform state |
|
81 state.removePlayer(player); |
|
82 |
|
83 // delete |
|
84 // XXX: leak because deleting the slot while it's being called breaks ClanLib |
|
85 // delete player; |
|
86 } |
|
87 |
|
88 NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, NetworkObject_Client *obj, Vector position) : |
|
89 LocalPlayer(client.state, position, true), client(client), obj(obj) { |
|
90 |
|
91 // receive messages |
|
92 slots.connect(obj->sig_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position); |
|
93 } |
|
94 |
|
95 void NetworkClientLocalPlayer::handleMove (PlayerInput_Move input) { |
|
96 // always send move, in all cases |
|
97 NetworkPacket pkt; |
|
98 pkt.write_uint16(input); |
|
99 |
|
100 obj->send(NETMSG_CLIENT_MOVE, pkt, false); |
|
101 |
|
102 // do not handle locally |
|
103 } |
|
104 |
|
105 void NetworkClientLocalPlayer::on_position (NetworkPacket &pkt) { |
|
106 Vector position = pkt.read_vector(); |
|
107 Vector velocity = pkt.read_vector(); |
|
108 uint8_t flags = pkt.read_uint8(); |
|
109 |
|
110 Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity; |
|
111 |
|
112 // just update... |
|
113 updatePhysics(position, velocity, flags & NETWORK_PHYSICS_INAIR); |
|
114 } |
|
115 |
|
116 NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, NetworkObject_Client *obj, Vector position) : |
|
117 RemotePlayer(client.state, position, true), client(client), obj(obj) { |
|
118 |
|
119 // receive messages |
|
120 slots.connect(obj->sig_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position); |
|
121 slots.connect(obj->sig_message(NETMSG_PLAYER_QUIT), this, &NetworkClientRemotePlayer::on_quit); |
|
122 } |
|
123 |
|
124 void NetworkClientRemotePlayer::on_position (NetworkPacket &pkt) { |
|
125 Vector position = pkt.read_vector(); |
|
126 Vector velocity = pkt.read_vector(); |
|
127 uint8_t flags = pkt.read_uint8(); |
|
128 |
|
129 Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity; |
|
130 |
|
131 // just update... |
|
132 updatePhysics(position, velocity, flags & NETWORK_PHYSICS_INAIR); |
|
133 } |
|
134 |
|
135 void NetworkClientRemotePlayer::on_quit (NetworkPacket &pkt) { |
|
136 // pkt is empty |
|
137 (void) pkt; |
|
138 |
|
139 Engine::log(INFO, "client_player.on_quit") << "obj=" << obj; |
|
140 |
|
141 client.player_quit(this); |
|
142 } |
|
143 |