author | nireco |
Fri, 28 Nov 2008 13:11:51 +0000 | |
changeset 116 | 0d36aade845e |
parent 107 | 505bfa531496 |
permissions | -rw-r--r-- |
21 | 1 |
|
2 |
#include "NetworkClient.hh" |
|
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
3 |
#include "Engine.hh" |
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
4 |
#include "Logger.hh" |
21 | 5 |
|
25 | 6 |
#include <cassert> |
7 |
||
89 | 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) { |
|
24 | 10 |
|
11 |
// connect slots |
|
89 | 12 |
slots.connect(netobjs.sig_create(), this, &NetworkClient::on_create); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
13 |
|
24 | 14 |
// XXX: sig_disconnected |
21 | 15 |
} |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
16 |
|
89 | 17 |
void NetworkClient::on_create (NetworkObject_Client *obj, NetworkMessageID msg_id, NetworkPacket &pkt) { |
18 |
switch (msg_id) { |
|
24 | 19 |
case NETMSG_SERVER_HELLO: |
20 |
on_server_hello(obj, pkt); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
21 |
|
24 | 22 |
break; |
23 |
||
24 |
case NETMSG_PLAYER_INFO: |
|
25 |
on_player_info(obj, pkt); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
26 |
|
24 | 27 |
break; |
28 |
||
29 |
case NETMSG_PLAYER_JOIN: |
|
30 |
on_player_join(obj, pkt); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
31 |
|
24 | 32 |
break; |
33 |
||
34 |
default: |
|
89 | 35 |
Engine::log(WARN, "client.on_create_object") << "unknown msg_id=" << msg_id << " for obj=" << obj; |
24 | 36 |
} |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
37 |
} |
24 | 38 |
|
89 | 39 |
void NetworkClient::on_server_hello (NetworkObject_Client *obj, NetworkPacket &pkt) { |
24 | 40 |
// read the packet |
89 | 41 |
Vector position = pkt.read_vector(); |
24 | 42 |
|
66 | 43 |
Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << position; |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
44 |
|
24 | 45 |
// create the LocalPlayer object |
66 | 46 |
NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, position); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
47 |
|
24 | 48 |
// inform state |
49 |
state.newLocalPlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
50 |
} |
24 | 51 |
|
89 | 52 |
void NetworkClient::on_player_info (NetworkObject_Client *obj, NetworkPacket &pkt) { |
24 | 53 |
// read the packet |
89 | 54 |
Vector position = pkt.read_vector(); |
24 | 55 |
|
66 | 56 |
Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << position; |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
57 |
|
24 | 58 |
// create the LocalPlayer object |
66 | 59 |
NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
60 |
|
24 | 61 |
// inform state |
62 |
state.newRemotePlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
63 |
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
64 |
} |
24 | 65 |
|
89 | 66 |
void NetworkClient::on_player_join (NetworkObject_Client *obj, NetworkPacket &pkt) { |
24 | 67 |
// read the packet |
89 | 68 |
Vector position = pkt.read_vector(); |
24 | 69 |
|
66 | 70 |
Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << position; |
24 | 71 |
|
72 |
// create the RemotePlayer object |
|
66 | 73 |
NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
74 |
|
24 | 75 |
// inform state |
76 |
state.newRemotePlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
77 |
} |
24 | 78 |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
79 |
void NetworkClient::player_quit (NetworkClientRemotePlayer *player) { |
24 | 80 |
// inform state |
81 |
state.removePlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
82 |
|
24 | 83 |
// delete |
84 |
// XXX: leak because deleting the slot while it's being called breaks ClanLib |
|
85 |
// delete player; |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
86 |
} |
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
87 |
|
89 | 88 |
NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, NetworkObject_Client *obj, Vector position) : |
66 | 89 |
LocalPlayer(client.state, position, true), client(client), obj(obj) { |
24 | 90 |
|
91 |
// receive messages |
|
89 | 92 |
slots.connect(obj->sig_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
93 |
} |
24 | 94 |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
89
diff
changeset
|
95 |
void NetworkClientLocalPlayer::handleMove (PlayerInput_Move input) { |
25 | 96 |
// always send move, in all cases |
89 | 97 |
NetworkPacket pkt; |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
89
diff
changeset
|
98 |
pkt.write_uint16(input); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
99 |
|
89 | 100 |
obj->send(NETMSG_CLIENT_MOVE, pkt, false); |
54 | 101 |
|
66 | 102 |
// do not handle locally |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
103 |
} |
24 | 104 |
|
89 | 105 |
void NetworkClientLocalPlayer::on_position (NetworkPacket &pkt) { |
106 |
Vector position = pkt.read_vector(); |
|
107 |
Vector velocity = pkt.read_vector(); |
|
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
96
diff
changeset
|
108 |
uint8_t flags = pkt.read_uint8(); |
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
109 |
|
66 | 110 |
Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity; |
24 | 111 |
|
66 | 112 |
// just update... |
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
96
diff
changeset
|
113 |
updatePhysics(position, velocity, flags & NETWORK_PHYSICS_INAIR); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
114 |
} |
24 | 115 |
|
89 | 116 |
NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, NetworkObject_Client *obj, Vector position) : |
66 | 117 |
RemotePlayer(client.state, position, true), client(client), obj(obj) { |
24 | 118 |
|
119 |
// receive messages |
|
89 | 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); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
122 |
} |
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
123 |
|
89 | 124 |
void NetworkClientRemotePlayer::on_position (NetworkPacket &pkt) { |
125 |
Vector position = pkt.read_vector(); |
|
126 |
Vector velocity = pkt.read_vector(); |
|
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
96
diff
changeset
|
127 |
uint8_t flags = pkt.read_uint8(); |
66 | 128 |
|
129 |
Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity; |
|
24 | 130 |
|
66 | 131 |
// just update... |
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
96
diff
changeset
|
132 |
updatePhysics(position, velocity, flags & NETWORK_PHYSICS_INAIR); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
133 |
} |
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
134 |
|
89 | 135 |
void NetworkClientRemotePlayer::on_quit (NetworkPacket &pkt) { |
24 | 136 |
// pkt is empty |
137 |
(void) pkt; |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
138 |
|
24 | 139 |
Engine::log(INFO, "client_player.on_quit") << "obj=" << obj; |
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
140 |
|
24 | 141 |
client.player_quit(this); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
142 |
} |
89 | 143 |