diff --git a/client/entities/base_character.hpp b/client/entities/base_character.hpp index c3d1b1e..ba699d9 100644 --- a/client/entities/base_character.hpp +++ b/client/entities/base_character.hpp @@ -45,7 +45,7 @@ public: std::string SetAvatar(std::string s); std::string GetAvatar() const; -private: +protected: //metadata int owner; std::string handle; diff --git a/client/entities/base_monster.hpp b/client/entities/base_monster.hpp index d7d1b6b..6fd91e9 100644 --- a/client/entities/base_monster.hpp +++ b/client/entities/base_monster.hpp @@ -29,7 +29,7 @@ public: BaseMonster() = default; virtual ~BaseMonster() = default; -private: +protected: // }; diff --git a/client/entities/entity.cpp b/client/entities/entity.cpp index 98b9483..34cc6cf 100644 --- a/client/entities/entity.cpp +++ b/client/entities/entity.cpp @@ -38,10 +38,6 @@ SpriteSheet* Entity::GetSprite() { //accessors & mutators //------------------------- -int Entity::SetEntityIndex(int i) { - return entityIndex = i; -} - Vector2 Entity::SetOrigin(Vector2 v) { return origin = v; } @@ -54,10 +50,6 @@ BoundingBox Entity::SetBounds(BoundingBox b) { return bounds = b; } -int Entity::GetEntityIndex() { - return entityIndex; -} - Vector2 Entity::GetOrigin() { return origin; } diff --git a/client/entities/entity.hpp b/client/entities/entity.hpp index c12fb71..a40f94f 100644 --- a/client/entities/entity.hpp +++ b/client/entities/entity.hpp @@ -35,12 +35,10 @@ public: SpriteSheet* GetSprite(); //accessors & mutators - int SetEntityIndex(int i); Vector2 SetOrigin(Vector2 v); Vector2 SetMotion(Vector2 v); BoundingBox SetBounds(BoundingBox b); - int GetEntityIndex(); Vector2 GetOrigin(); Vector2 GetMotion(); BoundingBox GetBounds(); @@ -50,7 +48,6 @@ protected: virtual ~Entity() = default; SpriteSheet sprite; - int entityIndex = -1; Vector2 origin; Vector2 motion; BoundingBox bounds; diff --git a/client/entities/local_character.cpp b/client/entities/local_character.cpp new file mode 100644 index 0000000..211a6bc --- /dev/null +++ b/client/entities/local_character.cpp @@ -0,0 +1,23 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "local_character.hpp" + diff --git a/client/entities/local_character.hpp b/client/entities/local_character.hpp new file mode 100644 index 0000000..bef4edc --- /dev/null +++ b/client/entities/local_character.hpp @@ -0,0 +1,34 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef LOCALCHARACTER_HPP_ +#define LOCALCHARACTER_HPP_ + +#include "base_character.hpp" + +class LocalCharacter: public BaseCharacter { +public: + // +private: + //NOTE: NO MEMBERS +}; + +#endif \ No newline at end of file diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index bb2c440..6e2aa76 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -91,6 +91,10 @@ InWorld::InWorld(int* const argClientIndex, int* const argAccountIndex): newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; network.SendTo(Channels::SERVER, &newPacket); + //set the camera's values + camera.width = GetScreen()->w; + camera.height = GetScreen()->h; + //debug // } @@ -133,6 +137,14 @@ void InWorld::Update() { //update the map UpdateMap(); + //update all entities + for (auto& it : characterMap) { + it.second.Update(); + } + for (auto& it : monsterMap) { + it.second.Update(); + } + //check the connection (heartbeat) if (Clock::now() - lastBeat > std::chrono::seconds(3)) { if (attemptedBeats > 2) { @@ -342,6 +354,10 @@ void InWorld::HandleLogoutResponse(ClientPacket* const argPacket) { accountIndex = -1; characterIndex = -1; + //reset the camera + camera.x = camera .y = 0; + camera.marginX = camera.marginY = 0; + SendDisconnectRequest(); } @@ -355,6 +371,10 @@ void InWorld::HandleDisconnectForced(ClientPacket* const argPacket) { accountIndex = -1; characterIndex = -1; + //reset the camera + camera.x = camera .y = 0; + camera.marginX = camera.marginY = 0; + SetNextScene(SceneList::DISCONNECTEDSCREEN); ConfigUtility::GetSingleton()["client.disconnectMessage"] = "You have been forcibly disconnected by the server"; } @@ -414,7 +434,7 @@ void InWorld::UpdateMap() { //NOTE: preexisting characters will result in query responses //NOTE: new characters will result in create messages -//NOTE: this client's character will exist in both +//NOTE: this client's character will exist in both (skipped) void InWorld::HandleCharacterCreate(CharacterPacket* const argPacket) { //prevent double message @@ -435,10 +455,16 @@ void InWorld::HandleCharacterCreate(CharacterPacket* const argPacket) { character->SetBounds({CHARACTER_BOUNDS_X, CHARACTER_BOUNDS_Y, CHARACTER_BOUNDS_WIDTH, CHARACTER_BOUNDS_HEIGHT}); character->SetHandle(argPacket->handle); character->SetAvatar(argPacket->avatar); + character->SetOwner(argPacket->accountIndex); - //TODO: check for this player's character + //check for this player's character + if (character->GetOwner() == accountIndex) { + localCharacter = static_cast(character); - //TODO: setup the camera + //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); + } //debug std::cout << "Create, total: " << characterMap.size() << std::endl; @@ -453,7 +479,14 @@ void InWorld::HandleCharacterDelete(CharacterPacket* const argPacket) { return; } - //TODO: check for this player's character + //check for this player's character + if ((*characterIt).second.GetOwner() == accountIndex) { + localCharacter = nullptr; + + //clear the camera + camera.marginX = 0; + camera.marginY = 0; + } //remove this character characterMap.erase(characterIt); @@ -463,6 +496,11 @@ void InWorld::HandleCharacterDelete(CharacterPacket* const argPacket) { } void InWorld::HandleCharacterQueryExists(CharacterPacket* const argPacket) { + //prevent a double message about this player's character + if (argPacket->accountIndex == accountIndex) { + return; + } + //implicitly construct the character if it doesn't exist BaseCharacter* character = &characterMap[argPacket->characterIndex]; @@ -472,6 +510,7 @@ void InWorld::HandleCharacterQueryExists(CharacterPacket* const argPacket) { character->SetBounds({CHARACTER_BOUNDS_X, CHARACTER_BOUNDS_Y, CHARACTER_BOUNDS_WIDTH, CHARACTER_BOUNDS_HEIGHT}); character->SetHandle(argPacket->handle); character->SetAvatar(argPacket->avatar); + character->SetOwner(argPacket->accountIndex); //debug std::cout << "Query, total: " << characterMap.size() << std::endl; diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 65e3ae6..2348cf9 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -39,13 +39,10 @@ //common #include "frame_rate.hpp" -#include "base_character.hpp" -#include "base_monster.hpp" - //client #include "base_scene.hpp" -#include "base_character.hpp" #include "base_monster.hpp" +#include "local_character.hpp" //STL #include @@ -126,6 +123,7 @@ protected: //entities std::map characterMap; std::map monsterMap; + LocalCharacter* localCharacter = nullptr; //heartbeat //TODO: Heartbeat needs it's own utility diff --git a/server/entities/entity.cpp b/server/entities/entity.cpp index d22a5d3..ebada62 100644 --- a/server/entities/entity.cpp +++ b/server/entities/entity.cpp @@ -21,10 +21,6 @@ */ #include "entity.hpp" -int Entity::SetEntityIndex(int i) { - return entityIndex = i; -} - int Entity::SetRoomIndex(int i) { return roomIndex = i; } @@ -36,9 +32,6 @@ Vector2 Entity::SetOrigin(Vector2 v) { Vector2 Entity::SetMotion(Vector2 v) { return motion = v; } -int Entity::GetEntityIndex() { - return entityIndex; -} int Entity::GetRoomIndex() { return roomIndex; diff --git a/server/entities/entity.hpp b/server/entities/entity.hpp index 8933c38..bad1627 100644 --- a/server/entities/entity.hpp +++ b/server/entities/entity.hpp @@ -28,12 +28,10 @@ class Entity { public: //accessors & mutators - int SetEntityIndex(int i); int SetRoomIndex(int i); Vector2 SetOrigin(Vector2 v); Vector2 SetMotion(Vector2 v); - int GetEntityIndex(); int GetRoomIndex(); Vector2 GetOrigin(); Vector2 GetMotion(); @@ -42,7 +40,6 @@ protected: Entity() = default; ~Entity() = default; - int entityIndex = -1; int roomIndex = -1; Vector2 origin; Vector2 motion;