src/proto2/NetworkClient.cc
changeset 66 1415a2d45686
parent 60 26571fd9a8d1
child 89 825c4613e087
equal deleted inserted replaced
65:be91c125f741 66:1415a2d45686
    36     }
    36     }
    37 }
    37 }
    38         
    38         
    39 void NetworkClient::on_server_hello (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    39 void NetworkClient::on_server_hello (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    40     // read the packet
    40     // read the packet
    41     uint32_t x = pkt.input.read_uint32();
    41     Vector position = readVector(pkt);
    42     uint32_t y = pkt.input.read_uint32();
       
    43 
       
    44     Vector initial_position(x, y);
       
    45     
    42     
    46     Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << initial_position;
    43     Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << position;
    47 
    44 
    48     // create the LocalPlayer object
    45     // create the LocalPlayer object
    49     NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, initial_position);
    46     NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, position);
    50 
    47 
    51     // inform state
    48     // inform state
    52     state.newLocalPlayer(player);
    49     state.newLocalPlayer(player);
    53 }
    50 }
    54         
    51         
    55 void NetworkClient::on_player_info (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    52 void NetworkClient::on_player_info (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    56     // read the packet
    53     // read the packet
    57     uint32_t x = pkt.input.read_uint32();
    54     Vector position = readVector(pkt);
    58     uint32_t y = pkt.input.read_uint32();
       
    59     
    55     
    60     Vector initial_position(x, y);
    56     Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << position;
    61     
       
    62     Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << initial_position;
       
    63 
    57 
    64     // create the LocalPlayer object
    58     // create the LocalPlayer object
    65     NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
    59     NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position);
    66 
    60 
    67     // inform state
    61     // inform state
    68     state.newRemotePlayer(player);
    62     state.newRemotePlayer(player);
    69 
    63 
    70 }
    64 }
    71         
    65         
    72 void NetworkClient::on_player_join (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    66 void NetworkClient::on_player_join (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    73     // read the packet
    67     // read the packet
    74     uint32_t x = pkt.input.read_uint32();
    68     Vector position = readVector(pkt);
    75     uint32_t y = pkt.input.read_uint32();
       
    76     
    69     
    77     Vector initial_position(x, y);
    70     Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << position;
    78     
       
    79     Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << initial_position;
       
    80     
    71     
    81     // create the RemotePlayer object
    72     // create the RemotePlayer object
    82     NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
    73     NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position);
    83 
    74 
    84     // inform state
    75     // inform state
    85     state.newRemotePlayer(player);
    76     state.newRemotePlayer(player);
    86 }
    77 }
    87         
    78         
    92     // delete
    83     // delete
    93     // XXX: leak because deleting the slot while it's being called breaks ClanLib
    84     // XXX: leak because deleting the slot while it's being called breaks ClanLib
    94     //  delete player;
    85     //  delete player;
    95 }
    86 }
    96 
    87 
    97 NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector initial_position) :
    88 NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector position) :
    98     LocalPlayer(client.state, initial_position, true), client(client), obj(obj) {
    89     LocalPlayer(client.state, position, true), client(client), obj(obj) {
    99     
    90     
   100     // receive messages
    91     // receive messages
   101     slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position);
    92     slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position);
   102 }
    93 }
   103         
    94         
   104 void NetworkClientLocalPlayer::handleMove (PlayerInput_Move move) {
    95 void NetworkClientLocalPlayer::applyForce (Vector force, uint16_t dt) {
   105     // always send move, in all cases
    96     // always send move, in all cases
   106     CL_NetPacket pkt;
    97     CL_NetPacket pkt;
   107     pkt.output.write_uint16(move);
    98     writeVector(pkt, force);
       
    99     pkt.output.write_uint16(dt);
   108 
   100 
   109     obj.send(NETMSG_CLIENT_MOVE, pkt, false);
   101     obj.send(NETMSG_CLIENT_MOVE, pkt, false);
   110     
   102     
   111     // handle locally
   103     // do not handle locally
   112     LocalPlayer::handleMove(move);
       
   113 }
   104 }
   114         
   105         
   115 void NetworkClientLocalPlayer::on_position (CL_NetPacket &pkt) {
   106 void NetworkClientLocalPlayer::on_position (CL_NetPacket &pkt) {
   116     uint32_t x = pkt.input.read_uint32();
   107     Vector position = readVector(pkt);
   117     uint32_t y = pkt.input.read_uint32();
   108     Vector velocity = readVector(pkt);
   118 
   109 
   119     Vector pos (x, y);
   110     Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity;
   120 
       
   121     Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos;
       
   122     
   111     
   123     // XXX: transmit velocity/force    
   112     // just update... 
   124     updatePhysics(pos, Vector(0, 0));
   113     updatePhysics(position, velocity);
   125 }
   114 }
   126         
   115         
   127 NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector initial_position) :
   116 NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector position) :
   128     RemotePlayer(client.state, initial_position, true), client(client), obj(obj) {
   117     RemotePlayer(client.state, position, true), client(client), obj(obj) {
   129     
   118     
   130     // receive messages
   119     // receive messages
   131     slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position);
   120     slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position);
   132     slots.connect(obj.sig_received_message(NETMSG_PLAYER_QUIT), this, &NetworkClientRemotePlayer::on_quit);
   121     slots.connect(obj.sig_received_message(NETMSG_PLAYER_QUIT), this, &NetworkClientRemotePlayer::on_quit);
   133 }
   122 }
   134 
   123 
   135 void NetworkClientRemotePlayer::on_position (CL_NetPacket &pkt) {
   124 void NetworkClientRemotePlayer::on_position (CL_NetPacket &pkt) {
   136     uint32_t x = pkt.input.read_uint32();
   125     Vector position = readVector(pkt);
   137     uint32_t y = pkt.input.read_uint32();
   126     Vector velocity = readVector(pkt);
       
   127 
       
   128     Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity;
   138     
   129     
   139     Vector pos (x, y);
   130     // just update... 
   140 
   131     updatePhysics(position, velocity);
   141     Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos;
       
   142     
       
   143     // XXX: transmit velocity/force    
       
   144     updatePhysics(pos, Vector(0, 0));
       
   145 }
   132 }
   146 
   133 
   147 void NetworkClientRemotePlayer::on_quit (CL_NetPacket &pkt) {
   134 void NetworkClientRemotePlayer::on_quit (CL_NetPacket &pkt) {
   148     // pkt is empty
   135     // pkt is empty
   149     (void) pkt;
   136     (void) pkt;