From 3e2d1a5a56e2c04148622268aa5cc999e9eb4471 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 22 Dec 2014 23:01:53 +1100 Subject: [PATCH] Implemented client-side HandleCharacterSet*(); untested --- client/scenes/in_world.cpp | 84 ++++++++++++++++++++++++++++- client/scenes/in_world.hpp | 4 ++ server/server_character_methods.cpp | 2 +- server/server_data.cpp | 1 + 4 files changed, 88 insertions(+), 3 deletions(-) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 30a2e2b..1b6e060 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -289,6 +289,17 @@ void InWorld::HandlePacket(SerialPacket* const argPacket) { HandleCharacterQueryExists(static_cast(argPacket)); break; + //character movement + case SerialPacketType::CHARACTER_SET_ROOM: + HandleCharacterSetRoom(static_cast(argPacket)); + break; + case SerialPacketType::CHARACTER_SET_ORIGIN: + HandleCharacterSetOrigin(static_cast(argPacket)); + break; + case SerialPacketType::CHARACTER_SET_MOTION: + HandleCharacterSetMotion(static_cast(argPacket)); + break; + //rejection messages case SerialPacketType::REGION_REJECTION: case SerialPacketType::CHARACTER_REJECTION: @@ -412,6 +423,10 @@ void InWorld::HandleRegionContent(RegionPacket* const argPacket) { } void InWorld::UpdateMap() { + if (roomIndex == -1) { + return; + } + //these represent the zone of regions that the client needs loaded, including the mandatory buffers (+1/-1) int xStart = snapToBase(REGION_WIDTH, camera.x/tileSheet.GetTileW()) - REGION_WIDTH; int xEnd = snapToBase(REGION_WIDTH, (camera.x+camera.width)/tileSheet.GetTileW()) + REGION_WIDTH; @@ -428,7 +443,7 @@ void InWorld::UpdateMap() { for (int i = xStart; i <= xEnd; i += REGION_WIDTH) { for (int j = yStart; j <= yEnd; j += REGION_HEIGHT) { if (!regionPager.FindRegion(i, j)) { - SendRegionRequest(0, i, j); + SendRegionRequest(roomIndex, i, j); } } } @@ -470,6 +485,9 @@ void InWorld::HandleCharacterCreate(CharacterPacket* const argPacket) { //focus the camera on this character camera.marginX = (camera.width / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2); camera.marginY = (camera.height/ 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2); + + //focus on this character's room + roomIndex = argPacket->roomIndex; } //debug @@ -492,6 +510,9 @@ void InWorld::HandleCharacterDelete(CharacterPacket* const argPacket) { //clear the camera camera.marginX = 0; camera.marginY = 0; + + //clear the room + roomIndex = -1; } //remove this character @@ -507,6 +528,11 @@ void InWorld::HandleCharacterQueryExists(CharacterPacket* const argPacket) { return; } + //ignore characters in a different room (sub-optimal) + if (argPacket->roomIndex != roomIndex) { + return; + } + //implicitly construct the character if it doesn't exist BaseCharacter* character = &characterMap[argPacket->characterIndex]; @@ -520,4 +546,58 @@ void InWorld::HandleCharacterQueryExists(CharacterPacket* const argPacket) { //debug std::cout << "Query, total: " << characterMap.size() << std::endl; -} \ No newline at end of file +} + +void InWorld::HandleCharacterSetRoom(CharacterPacket* const argPacket) { + //someone else's character + if (argPacket->characterIndex != characterIndex) { + characterMap.erase(argPacket->characterIndex); + return; + } + + //this character is moving between rooms + roomIndex = argPacket->roomIndex; + + //set the character's info + localCharacter->SetOrigin(argPacket->origin); + localCharacter->SetMotion(argPacket->motion); + + //clear the old room's data + regionPager.UnloadAll(); + monsterMap.clear(); + + //use the jenky pattern for std::map to skip this player's character + for (std::map::iterator it = characterMap.begin(); it != characterMap.end(); /* EMPTY */ ) { + if (it->first != characterIndex) { + it = characterMap.erase(it); + } + else { + ++it; + } + } + + //request the info on characters in this room + CharacterPacket newPacket; + newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; + network.SendTo(Channels::SERVER, &newPacket); +} + +void InWorld::HandleCharacterSetOrigin(CharacterPacket* const argPacket) { + //check that this character exists + std::map::iterator characterIt = characterMap.find(argPacket->characterIndex); + if (characterIt != characterMap.end()) { + //set the origin and motion + characterIt->second.SetOrigin(argPacket->origin); + characterIt->second.SetMotion(argPacket->motion); + } +} + +void InWorld::HandleCharacterSetMotion(CharacterPacket* const argPacket) { + //check that this character exists + std::map::iterator characterIt = characterMap.find(argPacket->characterIndex); + if (characterIt != characterMap.end()) { + //set the origin and motion + characterIt->second.SetOrigin(argPacket->origin); + characterIt->second.SetMotion(argPacket->motion); + } +} diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 2348cf9..e2fb9d9 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -94,11 +94,15 @@ protected: void HandleCharacterCreate(CharacterPacket* const); void HandleCharacterDelete(CharacterPacket* const); void HandleCharacterQueryExists(CharacterPacket* const); + void HandleCharacterSetRoom(CharacterPacket* const); + void HandleCharacterSetOrigin(CharacterPacket* const); + void HandleCharacterSetMotion(CharacterPacket* const); //indexes int& clientIndex; int& accountIndex; int characterIndex = -1; + int roomIndex = -1; //graphics Image buttonImage; diff --git a/server/server_character_methods.cpp b/server/server_character_methods.cpp index 8f4a22b..780bf24 100644 --- a/server/server_character_methods.cpp +++ b/server/server_character_methods.cpp @@ -192,7 +192,7 @@ void ServerApplication::HandleCharacterSetRoom(CharacterPacket* const argPacket) //set the character's room, zero it's origin, zero it's motion //TODO: Set the origin here - characterData->SetRoom(argPacket->roomIndex); + characterData->SetRoomIndex(argPacket->roomIndex); characterData->SetOrigin({0, 0}); characterData->SetMotion({0, 0}); diff --git a/server/server_data.cpp b/server/server_data.cpp index 4eadeba..b9098fa 100644 --- a/server/server_data.cpp +++ b/server/server_data.cpp @@ -73,6 +73,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) { void ServerApplication::HandleCharacterExists(CharacterPacket* const argPacket) { //respond with all character data + //TODO: handle room and location specifications CharacterPacket newPacket; for (auto& it : *characterMgr.GetContainer()) { CopyCharacterToPacket(&newPacket, it.first);