Started working on receiving information about other players
This commit is contained in:
+41
-1
@@ -20,6 +20,7 @@ InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nU
|
||||
}
|
||||
|
||||
InGame::~InGame() {
|
||||
//placeholder disconnect
|
||||
PacketData p;
|
||||
p.type = PacketList::DISCONNECT;
|
||||
p.disconnect.playerID = *playerID;
|
||||
@@ -44,7 +45,46 @@ void InGame::FrameEnd() {
|
||||
}
|
||||
|
||||
void InGame::Update() {
|
||||
//
|
||||
Receive();
|
||||
}
|
||||
|
||||
void InGame::Receive() {
|
||||
PacketData packet;
|
||||
while(netUtil->Receive()) {
|
||||
memcpy(&packet, netUtil->GetInData(), sizeof(PacketData));
|
||||
switch(packet.type) {
|
||||
// case PacketList::NONE:
|
||||
// //
|
||||
// break;
|
||||
// case PacketList::PING:
|
||||
// //
|
||||
// break;
|
||||
// case PacketList::PONG:
|
||||
// //
|
||||
// break;
|
||||
// case PacketList::JOINREQUEST:
|
||||
// //
|
||||
// break;
|
||||
// case PacketList::JOINCONFIRM:
|
||||
// //
|
||||
// break;
|
||||
// case PacketList::DISCONNECT:
|
||||
// //
|
||||
// break;
|
||||
// case PacketList::SYNCHRONIZE:
|
||||
// //
|
||||
// break;
|
||||
case PacketList::NEWPLAYER:
|
||||
cout << "NEWPLAYER triggered" << endl;
|
||||
break;
|
||||
// case PacketList::DELETEPLAYER:
|
||||
// //
|
||||
// break;
|
||||
// case PacketList::MOVEMENT:
|
||||
// //
|
||||
// break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InGame::Render(SDL_Surface* const screen) {
|
||||
|
||||
@@ -19,6 +19,7 @@ protected:
|
||||
virtual void FrameStart();
|
||||
virtual void FrameEnd();
|
||||
virtual void Update();
|
||||
virtual void Receive();
|
||||
virtual void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
|
||||
+9
-5
@@ -63,7 +63,6 @@ void Lobby::Update() {
|
||||
}
|
||||
|
||||
void Lobby::Receive() {
|
||||
//dump to the console
|
||||
PacketData packet;
|
||||
while(netUtil->Receive()) {
|
||||
memcpy(&packet, netUtil->GetInData(), sizeof(PacketData));
|
||||
@@ -80,12 +79,13 @@ void Lobby::Receive() {
|
||||
// case PacketList::JOINREQUEST:
|
||||
// //
|
||||
// break;
|
||||
case PacketList::JOINCONFIRM:
|
||||
case PacketList::JOINCONFIRM: {
|
||||
PacketData p;
|
||||
memcpy(&p, netUtil->GetInData(), sizeof(PacketData));
|
||||
*playerID = p.joinConfirm.playerID;
|
||||
netUtil->Bind(&netUtil->GetInPacket()->address, 0);
|
||||
SetNextScene(SceneList::INGAME);
|
||||
}
|
||||
break;
|
||||
// case PacketList::DISCONNECT:
|
||||
// //
|
||||
@@ -93,9 +93,12 @@ void Lobby::Receive() {
|
||||
// case PacketList::SYNCHRONIZE:
|
||||
// //
|
||||
// break;
|
||||
// case PacketList::NEWPLAYER:
|
||||
// //
|
||||
// break;
|
||||
#ifdef DEBUG
|
||||
//this might not be the end of the world; it only happens when the game ping is too low
|
||||
case PacketList::NEWPLAYER:
|
||||
cout << "WARNING: new player triggered unexpectedly" << endl;
|
||||
break;
|
||||
#endif
|
||||
// case PacketList::DELETEPLAYER:
|
||||
// //
|
||||
// break;
|
||||
@@ -195,6 +198,7 @@ void Lobby::PushServer(PacketData* packet) {
|
||||
|
||||
void Lobby::JoinRequest(ServerData* server) {
|
||||
if (!server) {
|
||||
//CAN receive null
|
||||
return;
|
||||
}
|
||||
PacketData p;
|
||||
|
||||
@@ -69,6 +69,7 @@ struct NewPlayer {
|
||||
char handle[PACKET_STRING_SIZE];
|
||||
char avatar[PACKET_STRING_SIZE];
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
};
|
||||
|
||||
struct DeletePlayer {
|
||||
|
||||
@@ -50,3 +50,25 @@ Receive:
|
||||
player update:
|
||||
PlayerManager.Update(message)
|
||||
end
|
||||
|
||||
-------------------------
|
||||
|
||||
--send info about the specified client to all clients
|
||||
--use this for new connections, movement, etc.
|
||||
SendClientData(int playerID):
|
||||
for (clientMap):
|
||||
Send(it.channel, clientMap[playerID])
|
||||
end
|
||||
end
|
||||
|
||||
NewClientData := SendClientData
|
||||
|
||||
|
||||
--send all info about the server to the specified client
|
||||
--send this to new connections
|
||||
SynchronizeClient(int playerID):
|
||||
for (clientMap):
|
||||
Send(clientMap[playerID].channel, it)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -25,8 +25,7 @@ struct ClientData {
|
||||
std::string avatar;
|
||||
enum class Command {
|
||||
NORMAL,
|
||||
SYNCHRONIZE,
|
||||
PING,
|
||||
CHANGED,
|
||||
}command = Command::NORMAL;
|
||||
};
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ void ServerApplication::Quit() {
|
||||
void ServerApplication::Ping(PacketData* packet) {
|
||||
//respond to pings with the server name
|
||||
if (!packet) {
|
||||
return;
|
||||
throw(runtime_error("Ping() received null"));
|
||||
}
|
||||
packet->type = PacketList::PONG;
|
||||
snprintf(packet->pong.metadata,PACKET_STRING_SIZE, "%s",configUtil.CString("servername"));
|
||||
@@ -114,7 +114,8 @@ void ServerApplication::JoinRequest(PacketData* packet) {
|
||||
p.joinConfirm.playerID = clientMap[playerID].playerID;
|
||||
netUtil.Send(clientMap[playerID].channel, &p, sizeof(PacketData));
|
||||
|
||||
//TODO: NewPlayer()
|
||||
//send it out to the clients
|
||||
NewClientData(playerID);
|
||||
|
||||
#ifdef DEBUG
|
||||
cout << "current players: " << clientMap.size() << endl;
|
||||
@@ -138,12 +139,40 @@ void ServerApplication::Disconnect(int playerID) {
|
||||
|
||||
void ServerApplication::Movement(PacketData* packet) {
|
||||
if (!packet) {
|
||||
return;
|
||||
throw(runtime_error("Movement() received null"));
|
||||
}
|
||||
clientMap[packet->movement.playerID].position = packet->movement.position;
|
||||
clientMap[packet->movement.playerID].motion = packet->movement.motion;
|
||||
//simple relay
|
||||
//TODO: SendClientData(packet->movement.playerID);
|
||||
for (auto it : clientMap) {
|
||||
netUtil.Send(it.second.channel, packet, sizeof(PacketData));
|
||||
}
|
||||
}
|
||||
|
||||
void ServerApplication::NewClientData(int playerID) {
|
||||
if (clientMap.find(playerID) == clientMap.end()) {
|
||||
throw(runtime_error("NewClientData() Failed to find the client"));
|
||||
}
|
||||
//create the packet to send
|
||||
PacketData p;
|
||||
p.type = PacketList::NEWPLAYER;
|
||||
p.newPlayer.playerID = clientMap[playerID].playerID;
|
||||
snprintf(p.newPlayer.handle, PACKET_STRING_SIZE, "%s", clientMap[playerID].handle.c_str());
|
||||
snprintf(p.newPlayer.avatar, PACKET_STRING_SIZE, "%s", clientMap[playerID].avatar.c_str());
|
||||
p.newPlayer.position = clientMap[playerID].position;
|
||||
p.newPlayer.motion = clientMap[playerID].motion;
|
||||
|
||||
//send the packet
|
||||
for (auto it : clientMap) {
|
||||
netUtil.Send(it.second.channel, &p, sizeof(PacketData));
|
||||
}
|
||||
}
|
||||
|
||||
void ServerApplication::SendClientData(int playerID) {
|
||||
//TODO
|
||||
}
|
||||
|
||||
void ServerApplication::SynchronizeClient(int playerID) {
|
||||
//TODO
|
||||
}
|
||||
|
||||
@@ -27,14 +27,17 @@ private:
|
||||
void Disconnect(int playerID);
|
||||
void Movement(PacketData*);
|
||||
|
||||
bool running = false;
|
||||
Delta delta;
|
||||
void NewClientData(int playerID);
|
||||
void SendClientData(int playerID);
|
||||
void SynchronizeClient(int playerID);
|
||||
|
||||
//globals
|
||||
ConfigUtility configUtil;
|
||||
UDPNetworkUtility netUtil;
|
||||
|
||||
//members
|
||||
bool running = false;
|
||||
Delta delta;
|
||||
std::map<int, ClientData> clientMap;
|
||||
int maxClients = SDLNET_MAX_UDPCHANNELS;
|
||||
int uniqueIndex = 0;
|
||||
|
||||
Reference in New Issue
Block a user