diff --git a/client/in_world.cpp b/client/in_world.cpp index 9992d7c..c2715f6 100644 --- a/client/in_world.cpp +++ b/client/in_world.cpp @@ -43,8 +43,8 @@ InWorld::InWorld() { snprintf(p.playerInfo.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); snprintf(p.playerInfo.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); - p.playerInfo.position = {0, 50}; - p.playerInfo.motion = {60, 0}; + p.playerInfo.position = {50, 50}; + p.playerInfo.motion = {0, 0}; netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet)); @@ -115,39 +115,56 @@ void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) { } void InWorld::KeyDown(SDL_KeyboardEvent const& key) { + //general switch(key.keysym.sym) { case SDLK_ESCAPE: ExitGame(); - break; + break; + } + + //player movement + if (infoMgr->GetPlayerIndex() == -1) { + return; + } + + switch(key.keysym.sym) { case SDLK_w: - //up + playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::NORTH); + SendState(); break; case SDLK_s: - //down + playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::SOUTH); + SendState(); break; case SDLK_a: - //left + playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::WEST); + SendState(); break; case SDLK_d: - //right + playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::EAST); + SendState(); break; } } void InWorld::KeyUp(SDL_KeyboardEvent const& key) { - //reversed + //player movement reversed switch(key.keysym.sym) { case SDLK_w: - // + playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::SOUTH); + SendState(); break; case SDLK_s: - // + playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::NORTH); + SendState(); break; case SDLK_a: - // + playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::EAST); + SendState(); break; case SDLK_d: - // + playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::WEST); + SendState(); break; } } @@ -244,13 +261,14 @@ void InWorld::AddPlayer(Packet& p) { throw(runtime_error("Duplicate players detected")); } - //sprite - playerCharacters[p.playerInfo.index].GetSprite()->SetSurface(surfaceMgr->Get(p.playerInfo.avatar), 32, 48); - - //pos + //position playerCharacters[p.playerInfo.index].SetPosition(p.playerInfo.position); playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion); + //sprite + playerCharacters[p.playerInfo.index].GetSprite()->SetSurface(surfaceMgr->Get(p.playerInfo.avatar), 32, 48); + playerCharacters[p.playerInfo.index].FaceDirection(); + //is it this player? if (p.meta.clientIndex == infoMgr->GetClientIndex()) { infoMgr->SetPlayerIndex(p.playerInfo.index); @@ -276,4 +294,21 @@ void InWorld::UpdatePlayer(Packet& p) { playerCharacters[p.playerInfo.index].SetPosition(p.playerInfo.position); playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion); + playerCharacters[p.playerInfo.index].FaceDirection(); +} + +void InWorld::SendState() { + //send the state of this player's character + if (infoMgr->GetPlayerIndex() == -1) { + return; + } + + Packet p; + p.meta.type = Packet::Type::PLAYER_UPDATE; + p.meta.clientIndex = infoMgr->GetClientIndex(); + p.playerInfo.index = infoMgr->GetPlayerIndex(); + p.playerInfo.position = playerCharacters[infoMgr->GetPlayerIndex()].GetPosition(); + p.playerInfo.motion = playerCharacters[infoMgr->GetPlayerIndex()].GetMotion(); + + netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet)); } diff --git a/client/in_world.hpp b/client/in_world.hpp index 44a1a80..bf707db 100644 --- a/client/in_world.hpp +++ b/client/in_world.hpp @@ -73,6 +73,8 @@ protected: void RemovePlayer(Packet&); void UpdatePlayer(Packet&); + void SendState(); + //services ConfigUtility* configUtil = Singleton::Get(); SurfaceManager* surfaceMgr = Singleton::Get(); diff --git a/client/player_character.cpp b/client/player_character.cpp index f3894f0..aaa3922 100644 --- a/client/player_character.cpp +++ b/client/player_character.cpp @@ -45,31 +45,17 @@ void PlayerCharacter::MoveDirection(CardinalDirection cd) { motion.y += WALKING_SPEED; } break; - case CardinalDirection::EAST: + case CardinalDirection::WEST: if (motion.x >= 0) { motion.x -= WALKING_SPEED; } break; - case CardinalDirection::WEST: + case CardinalDirection::EAST: if (motion.x <= 0) { motion.x += WALKING_SPEED; } break; } - //short cut - if (motion.x != 0 && motion.y != 0) { - sprite.SetInterval(0.1); - limitSpeed = true; - } - else if (motion.x != 0 || motion.y != 0) { - sprite.SetInterval(0.1); - limitSpeed = false; - } - else { - sprite.SetInterval(0); - sprite.SetCurrentFrame(0); - limitSpeed = false; - } //face the correct direction FaceDirection(); } @@ -82,10 +68,10 @@ void PlayerCharacter::FaceDirection(CardinalDirection cd) { case CardinalDirection::SOUTH: sprite.SetCurrentStrip(0); break; - case CardinalDirection::EAST: + case CardinalDirection::WEST: sprite.SetCurrentStrip(2); break; - case CardinalDirection::WEST: + case CardinalDirection::EAST: sprite.SetCurrentStrip(3); break; } @@ -93,16 +79,36 @@ void PlayerCharacter::FaceDirection(CardinalDirection cd) { void PlayerCharacter::FaceDirection() { //base the direction on the character's movement + if (motion.y < 0) { + FaceDirection(CardinalDirection::NORTH); + } if (motion.y > 0) { FaceDirection(CardinalDirection::SOUTH); } - else if (motion.y < 0) { - FaceDirection(CardinalDirection::NORTH); - } - else if (motion.x < 0) { - FaceDirection(CardinalDirection::EAST); - } - else if (motion.x > 0) { + if (motion.x < 0) { FaceDirection(CardinalDirection::WEST); } + if (motion.x > 0) { + FaceDirection(CardinalDirection::EAST); + } + CheckSpeed(); +} + +void PlayerCharacter::CheckSpeed() { + //diagonal + if (motion.x != 0 && motion.y != 0) { + sprite.SetInterval(0.1); + limitSpeed = true; + } + //cardinal + else if (motion != 0) { + sprite.SetInterval(0.1); + limitSpeed = false; + } + //not moving + else { + sprite.SetInterval(0); + sprite.SetCurrentFrame(0); + limitSpeed = false; + } } diff --git a/client/player_character.hpp b/client/player_character.hpp index 9cfa21e..9234dd8 100644 --- a/client/player_character.hpp +++ b/client/player_character.hpp @@ -35,6 +35,7 @@ public: void MoveDirection(CardinalDirection); void FaceDirection(CardinalDirection); + void FaceDirection(); void DrawTo(SDL_Surface* const dest) { sprite.DrawTo(dest, position.x, position.y); } @@ -49,7 +50,7 @@ public: SpriteSheet* GetSprite() { return &sprite; } private: - void FaceDirection(); + void CheckSpeed(); Vector2 position; Vector2 motion;