diff --git a/client/player_character.hpp b/client/player_character.hpp index b33baca..1f6bc7a 100644 --- a/client/player_character.hpp +++ b/client/player_character.hpp @@ -36,7 +36,7 @@ public: void Update(double delta); - void DrawTo(SDL_Surface* const dest) { sprite.DrawTo(dest, position.x, position.y); } + void DrawTo(SDL_Surface* const dest, int camX, int camY) { sprite.DrawTo(dest, position.x - camX, position.y - camY); } //clunky code results in smooth movement and controls void AdjustDirection(Direction); diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 1c25bf6..cc8776c 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -88,8 +88,20 @@ void InWorld::FrameStart() { } void InWorld::Update(double delta) { - //suck in all waiting packets NetworkPacket packet; + + //update the camera + if(localCharacter) { + int marginX = (GetScreen()->w / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2); + int marginY = (GetScreen()->h / 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2); + camera.x = localCharacter->GetPosition().x - marginX; + camera.y = localCharacter->GetPosition().y - marginY; + } + + //check the map + UpdateMap(); + + //suck in all waiting packets while(network.Receive()) { deserialize(&packet, network.GetInData()); packet.meta.srcAddress = network.GetInPacket()->address; @@ -107,7 +119,7 @@ void InWorld::FrameEnd() { void InWorld::Render(SDL_Surface* const screen) { for (auto& it : playerCharacters) { - it.second.DrawTo(screen); + it.second.DrawTo(screen, camera.x, camera.y); } disconnectButton.DrawTo(screen); shutDownButton.DrawTo(screen); @@ -212,24 +224,24 @@ void InWorld::KeyUp(SDL_KeyboardEvent const& key) { } } +//------------------------- +//Network handlers +//------------------------- + void InWorld::HandlePacket(NetworkPacket packet) { switch(packet.meta.type) { case NetworkPacket::Type::DISCONNECT: HandleDisconnect(packet); break; - case NetworkPacket::Type::PLAYER_NEW: HandlePlayerNew(packet); break; - case NetworkPacket::Type::PLAYER_DELETE: HandlePlayerDelete(packet); break; - case NetworkPacket::Type::PLAYER_UPDATE: HandlePlayerUpdate(packet); break; - //handle errors default: throw(std::runtime_error("Unknown NetworkPacket::Type encountered")); @@ -288,6 +300,10 @@ void InWorld::HandlePlayerUpdate(NetworkPacket packet) { playerCharacters[packet.playerInfo.playerIndex].ResetDirection(); } +//------------------------- +//Server control +//------------------------- + void InWorld::SendState() { NetworkPacket packet; char buffer[PACKET_BUFFER_SIZE]; @@ -325,4 +341,12 @@ void InWorld::RequestShutDown() { packet.clientInfo.index = clientIndex; serialize(&packet, buffer); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); +} + +void InWorld::UpdateMap() { + // +} + +void InWorld::RequestRegion(int x, int y) { + // } \ No newline at end of file diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index c6ecd72..b50850c 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -68,15 +68,19 @@ protected: void KeyDown(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&); + //Network handlers void HandlePacket(NetworkPacket); void HandleDisconnect(NetworkPacket); void HandlePlayerNew(NetworkPacket); void HandlePlayerDelete(NetworkPacket); void HandlePlayerUpdate(NetworkPacket); + //Server control void SendState(); void RequestDisconnect(); void RequestShutDown(); + void UpdateMap(); + void RequestRegion(int x, int y); //globals ConfigUtility& config;