Added controls, it's now MP capable
This commit is contained in:
@@ -142,19 +142,78 @@ void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
|||||||
|
|
||||||
void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
||||||
switch(key.keysym.sym) {
|
switch(key.keysym.sym) {
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE: {
|
||||||
//send a disconnect request
|
//send a disconnect request
|
||||||
NetworkPacket packet;
|
NetworkPacket packet;
|
||||||
packet.meta.type = NetworkPacket::Type::DISCONNECT;
|
packet.meta.type = NetworkPacket::Type::DISCONNECT;
|
||||||
packet.clientInfo.index = clientIndex;
|
packet.clientInfo.index = clientIndex;
|
||||||
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||||
QuitEvent();
|
QuitEvent();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
//player movement
|
||||||
|
case SDLK_LEFT:
|
||||||
|
if (localCharacter) {
|
||||||
|
localCharacter->AdjustDirection(PlayerCharacter::Direction::WEST);
|
||||||
|
SendState();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_RIGHT:
|
||||||
|
if (localCharacter) {
|
||||||
|
localCharacter->AdjustDirection(PlayerCharacter::Direction::EAST);
|
||||||
|
SendState();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_UP:
|
||||||
|
if (localCharacter) {
|
||||||
|
localCharacter->AdjustDirection(PlayerCharacter::Direction::NORTH);
|
||||||
|
SendState();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_DOWN:
|
||||||
|
if (localCharacter) {
|
||||||
|
localCharacter->AdjustDirection(PlayerCharacter::Direction::SOUTH);
|
||||||
|
SendState();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||||
//
|
switch(key.keysym.sym) {
|
||||||
|
//player movement
|
||||||
|
case SDLK_LEFT:
|
||||||
|
if (localCharacter) {
|
||||||
|
localCharacter->AdjustDirection(PlayerCharacter::Direction::EAST);
|
||||||
|
SendState();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_RIGHT:
|
||||||
|
if (localCharacter) {
|
||||||
|
localCharacter->AdjustDirection(PlayerCharacter::Direction::WEST);
|
||||||
|
SendState();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_UP:
|
||||||
|
if (localCharacter) {
|
||||||
|
localCharacter->AdjustDirection(PlayerCharacter::Direction::SOUTH);
|
||||||
|
SendState();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_DOWN:
|
||||||
|
if (localCharacter) {
|
||||||
|
localCharacter->AdjustDirection(PlayerCharacter::Direction::NORTH);
|
||||||
|
SendState();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::HandlePacket(NetworkPacket packet) {
|
void InWorld::HandlePacket(NetworkPacket packet) {
|
||||||
@@ -198,7 +257,11 @@ void InWorld::HandlePlayerNew(NetworkPacket packet) {
|
|||||||
playerCharacters[packet.playerInfo.playerIndex].SetMotion(packet.playerInfo.motion);
|
playerCharacters[packet.playerInfo.playerIndex].SetMotion(packet.playerInfo.motion);
|
||||||
playerCharacters[packet.playerInfo.playerIndex].ResetDirection();
|
playerCharacters[packet.playerInfo.playerIndex].ResetDirection();
|
||||||
|
|
||||||
//TODO: catch this client's player object
|
//catch this client's player object
|
||||||
|
if (packet.playerInfo.clientIndex == clientIndex && !localCharacter) {
|
||||||
|
playerIndex = packet.playerInfo.playerIndex;
|
||||||
|
localCharacter = &playerCharacters[playerIndex];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::HandlePlayerDelete(NetworkPacket packet) {
|
void InWorld::HandlePlayerDelete(NetworkPacket packet) {
|
||||||
@@ -208,7 +271,11 @@ void InWorld::HandlePlayerDelete(NetworkPacket packet) {
|
|||||||
|
|
||||||
playerCharacters.erase(packet.playerInfo.playerIndex);
|
playerCharacters.erase(packet.playerInfo.playerIndex);
|
||||||
|
|
||||||
//TODO: catch this client's player object
|
//catch this client's player object
|
||||||
|
if (packet.playerInfo.clientIndex == clientIndex) {
|
||||||
|
playerIndex = -1;
|
||||||
|
localCharacter = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::HandlePlayerUpdate(NetworkPacket packet) {
|
void InWorld::HandlePlayerUpdate(NetworkPacket packet) {
|
||||||
@@ -221,3 +288,16 @@ void InWorld::HandlePlayerUpdate(NetworkPacket packet) {
|
|||||||
playerCharacters[packet.playerInfo.playerIndex].SetMotion(packet.playerInfo.motion);
|
playerCharacters[packet.playerInfo.playerIndex].SetMotion(packet.playerInfo.motion);
|
||||||
playerCharacters[packet.playerInfo.playerIndex].ResetDirection();
|
playerCharacters[packet.playerInfo.playerIndex].ResetDirection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InWorld::SendState() {
|
||||||
|
NetworkPacket packet;
|
||||||
|
packet.meta.type = NetworkPacket::Type::PLAYER_UPDATE;
|
||||||
|
packet.playerInfo.clientIndex = clientIndex;
|
||||||
|
packet.playerInfo.playerIndex = playerIndex;
|
||||||
|
// snprintf(packet.playerInfo.handle, PACKET_STRING_SIZE, "%s", config["player.handle"].c_str());
|
||||||
|
// snprintf(packet.playerInfo.avatar, PACKET_STRING_SIZE, "%s", config["player.avatar"].c_str());
|
||||||
|
packet.playerInfo.position = localCharacter->GetPosition();
|
||||||
|
packet.playerInfo.motion = localCharacter->GetMotion();
|
||||||
|
|
||||||
|
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||||
|
}
|
||||||
@@ -60,6 +60,8 @@ protected:
|
|||||||
void HandlePlayerDelete(NetworkPacket);
|
void HandlePlayerDelete(NetworkPacket);
|
||||||
void HandlePlayerUpdate(NetworkPacket);
|
void HandlePlayerUpdate(NetworkPacket);
|
||||||
|
|
||||||
|
void SendState();
|
||||||
|
|
||||||
//global
|
//global
|
||||||
ConfigUtility& config;
|
ConfigUtility& config;
|
||||||
UDPNetworkUtility& network;
|
UDPNetworkUtility& network;
|
||||||
@@ -71,6 +73,8 @@ protected:
|
|||||||
Button disconnectButton;
|
Button disconnectButton;
|
||||||
Button shutDownButton;
|
Button shutDownButton;
|
||||||
std::map<int, PlayerCharacter> playerCharacters;
|
std::map<int, PlayerCharacter> playerCharacters;
|
||||||
|
PlayerCharacter* localCharacter = nullptr;
|
||||||
|
int playerIndex = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user