Characters are moving around in the world
This commit is contained in:
@@ -242,10 +242,59 @@ void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
|||||||
SendLogoutRequest();
|
SendLogoutRequest();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//character movement
|
||||||
|
if (!localCharacter) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Vector2 motion = localCharacter->GetMotion();
|
||||||
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_w:
|
||||||
|
motion.y -= CHARACTER_WALKING_SPEED;
|
||||||
|
break;
|
||||||
|
case SDLK_a:
|
||||||
|
motion.x -= CHARACTER_WALKING_SPEED;
|
||||||
|
break;
|
||||||
|
case SDLK_s:
|
||||||
|
motion.y += CHARACTER_WALKING_SPEED;
|
||||||
|
break;
|
||||||
|
case SDLK_d:
|
||||||
|
motion.x += CHARACTER_WALKING_SPEED;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
//handle diagonals
|
||||||
|
if (motion.x != 0 && motion.y != 0) {
|
||||||
|
motion *= CHARACTER_WALKING_MOD;
|
||||||
|
}
|
||||||
|
localCharacter->SetMotion(motion);
|
||||||
|
localCharacter->CorrectSprite();
|
||||||
|
SendLocalCharacterMotion();
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||||
//
|
//character movement
|
||||||
|
if (!localCharacter) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Vector2 motion = localCharacter->GetMotion();
|
||||||
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_w:
|
||||||
|
motion.y = std::min(0.0, motion.y += CHARACTER_WALKING_SPEED);
|
||||||
|
break;
|
||||||
|
case SDLK_a:
|
||||||
|
motion.x = std::min(0.0, motion.x += CHARACTER_WALKING_SPEED);
|
||||||
|
break;
|
||||||
|
case SDLK_s:
|
||||||
|
motion.y = std::max(0.0, motion.y -= CHARACTER_WALKING_SPEED);
|
||||||
|
break;
|
||||||
|
case SDLK_d:
|
||||||
|
motion.x = std::max(0.0, motion.x -= CHARACTER_WALKING_SPEED);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
//handle diagonals
|
||||||
|
localCharacter->SetMotion(motion);
|
||||||
|
localCharacter->CorrectSprite();
|
||||||
|
SendLocalCharacterMotion();
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -477,6 +526,7 @@ void InWorld::HandleCharacterCreate(CharacterPacket* const argPacket) {
|
|||||||
character->SetHandle(argPacket->handle);
|
character->SetHandle(argPacket->handle);
|
||||||
character->SetAvatar(argPacket->avatar);
|
character->SetAvatar(argPacket->avatar);
|
||||||
character->SetOwner(argPacket->accountIndex);
|
character->SetOwner(argPacket->accountIndex);
|
||||||
|
character->CorrectSprite();
|
||||||
|
|
||||||
//check for this player's character
|
//check for this player's character
|
||||||
if (character->GetOwner() == accountIndex) {
|
if (character->GetOwner() == accountIndex) {
|
||||||
@@ -486,7 +536,8 @@ void InWorld::HandleCharacterCreate(CharacterPacket* const argPacket) {
|
|||||||
camera.marginX = (camera.width / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2);
|
camera.marginX = (camera.width / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2);
|
||||||
camera.marginY = (camera.height/ 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2);
|
camera.marginY = (camera.height/ 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2);
|
||||||
|
|
||||||
//focus on this character's room
|
//focus on this character's info
|
||||||
|
characterIndex = argPacket->characterIndex;
|
||||||
roomIndex = argPacket->roomIndex;
|
roomIndex = argPacket->roomIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -543,6 +594,7 @@ void InWorld::HandleCharacterQueryExists(CharacterPacket* const argPacket) {
|
|||||||
character->SetHandle(argPacket->handle);
|
character->SetHandle(argPacket->handle);
|
||||||
character->SetAvatar(argPacket->avatar);
|
character->SetAvatar(argPacket->avatar);
|
||||||
character->SetOwner(argPacket->accountIndex);
|
character->SetOwner(argPacket->accountIndex);
|
||||||
|
character->CorrectSprite();
|
||||||
|
|
||||||
//debug
|
//debug
|
||||||
std::cout << "Query, total: " << characterMap.size() << std::endl;
|
std::cout << "Query, total: " << characterMap.size() << std::endl;
|
||||||
@@ -561,6 +613,7 @@ void InWorld::HandleCharacterSetRoom(CharacterPacket* const argPacket) {
|
|||||||
//set the character's info
|
//set the character's info
|
||||||
localCharacter->SetOrigin(argPacket->origin);
|
localCharacter->SetOrigin(argPacket->origin);
|
||||||
localCharacter->SetMotion(argPacket->motion);
|
localCharacter->SetMotion(argPacket->motion);
|
||||||
|
localCharacter->CorrectSprite();
|
||||||
|
|
||||||
//clear the old room's data
|
//clear the old room's data
|
||||||
regionPager.UnloadAll();
|
regionPager.UnloadAll();
|
||||||
@@ -590,6 +643,7 @@ void InWorld::HandleCharacterSetOrigin(CharacterPacket* const argPacket) {
|
|||||||
//set the origin and motion
|
//set the origin and motion
|
||||||
characterIt->second.SetOrigin(argPacket->origin);
|
characterIt->second.SetOrigin(argPacket->origin);
|
||||||
characterIt->second.SetMotion(argPacket->motion);
|
characterIt->second.SetMotion(argPacket->motion);
|
||||||
|
characterIt->second.CorrectSprite();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -600,5 +654,24 @@ void InWorld::HandleCharacterSetMotion(CharacterPacket* const argPacket) {
|
|||||||
//set the origin and motion
|
//set the origin and motion
|
||||||
characterIt->second.SetOrigin(argPacket->origin);
|
characterIt->second.SetOrigin(argPacket->origin);
|
||||||
characterIt->second.SetMotion(argPacket->motion);
|
characterIt->second.SetMotion(argPacket->motion);
|
||||||
|
characterIt->second.CorrectSprite();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//player movement
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
//TODO: add a "movement" packet type
|
||||||
|
void InWorld::SendLocalCharacterMotion() {
|
||||||
|
CharacterPacket newPacket;
|
||||||
|
newPacket.type = SerialPacketType::CHARACTER_SET_MOTION;
|
||||||
|
|
||||||
|
newPacket.accountIndex = accountIndex;
|
||||||
|
newPacket.characterIndex = characterIndex;
|
||||||
|
newPacket.roomIndex = roomIndex;
|
||||||
|
newPacket.origin = localCharacter->GetOrigin();
|
||||||
|
newPacket.motion = localCharacter->GetMotion();
|
||||||
|
|
||||||
|
network.SendTo(Channels::SERVER, &newPacket);
|
||||||
|
}
|
||||||
@@ -98,6 +98,9 @@ protected:
|
|||||||
void HandleCharacterSetOrigin(CharacterPacket* const);
|
void HandleCharacterSetOrigin(CharacterPacket* const);
|
||||||
void HandleCharacterSetMotion(CharacterPacket* const);
|
void HandleCharacterSetMotion(CharacterPacket* const);
|
||||||
|
|
||||||
|
//player movement
|
||||||
|
void SendLocalCharacterMotion();
|
||||||
|
|
||||||
//indexes
|
//indexes
|
||||||
int& clientIndex;
|
int& clientIndex;
|
||||||
int& accountIndex;
|
int& accountIndex;
|
||||||
|
|||||||
@@ -242,10 +242,21 @@ void ServerApplication::HandleCharacterSetOrigin(CharacterPacket* const argPacke
|
|||||||
void ServerApplication::HandleCharacterSetMotion(CharacterPacket* const argPacket) {
|
void ServerApplication::HandleCharacterSetMotion(CharacterPacket* const argPacket) {
|
||||||
//get the specified objects
|
//get the specified objects
|
||||||
AccountData* accountData = accountMgr.Get(argPacket->accountIndex);
|
AccountData* accountData = accountMgr.Get(argPacket->accountIndex);
|
||||||
|
|
||||||
|
if (!accountData) {
|
||||||
|
std::ostringstream msg;
|
||||||
|
msg << "Failed to set character motion, missing account: Index " << argPacket->accountIndex << "; ";
|
||||||
|
msg << "Number of accounts loaded: " << accountMgr.GetContainer()->size();
|
||||||
|
throw(std::runtime_error(msg.str()));
|
||||||
|
}
|
||||||
|
|
||||||
CharacterData* characterData = characterMgr.Get(argPacket->characterIndex);
|
CharacterData* characterData = characterMgr.Get(argPacket->characterIndex);
|
||||||
|
|
||||||
if (!accountData || !characterData) {
|
if (!characterData) {
|
||||||
throw(std::runtime_error("Failed to set character motion, missing data"));
|
std::ostringstream msg;
|
||||||
|
msg << "Failed to set character motion, missing character: Index " << argPacket->characterIndex << "; ";
|
||||||
|
msg << "Number of characters loaded: " << characterMgr.GetContainer()->size();
|
||||||
|
throw(std::runtime_error(msg.str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
//get this account's client
|
//get this account's client
|
||||||
|
|||||||
Reference in New Issue
Block a user