diff --git a/client/entities/base_monster.cpp b/client/entities/base_monster.cpp index 2eaa513..788dbb1 100644 --- a/client/entities/base_monster.cpp +++ b/client/entities/base_monster.cpp @@ -21,3 +21,26 @@ */ #include "base_monster.hpp" +#include "config_utility.hpp" + +void BaseMonster::CorrectSprite() { + //TODO +} + +std::string BaseMonster::SetHandle(std::string s) { + return handle = s; +} + +std::string BaseMonster::GetHandle() const { + return handle; +} + +std::string BaseMonster::SetAvatar(std::string s) { + avatar = s; + sprite.LoadSurface(ConfigUtility::GetSingleton()["dir.sprites"] + avatar, 4, 1); + return avatar; +} + +std::string BaseMonster::GetAvatar() const { + return avatar; +} \ No newline at end of file diff --git a/client/entities/base_monster.hpp b/client/entities/base_monster.hpp index c9b7bb3..328008a 100644 --- a/client/entities/base_monster.hpp +++ b/client/entities/base_monster.hpp @@ -29,8 +29,17 @@ public: BaseMonster() = default; virtual ~BaseMonster() = default; + void CorrectSprite(); + + std::string SetHandle(std::string s); + std::string GetHandle() const; + std::string SetAvatar(std::string s); + std::string GetAvatar() const; + protected: - // + //metadata + std::string handle; + std::string avatar; }; #endif \ No newline at end of file diff --git a/client/gameplay_scenes/in_world_entities.cpp b/client/gameplay_scenes/in_world_entities.cpp index 7fd7155..c1df09a 100644 --- a/client/gameplay_scenes/in_world_entities.cpp +++ b/client/gameplay_scenes/in_world_entities.cpp @@ -51,7 +51,7 @@ void InWorld::HandleCharacterCreate(CharacterPacket* const argPacket) { //fill the character's info character->SetOrigin(argPacket->origin); character->SetMotion(argPacket->motion); - character->SetBounds({CHARACTER_BOUNDS_X, CHARACTER_BOUNDS_Y, CHARACTER_BOUNDS_WIDTH, CHARACTER_BOUNDS_HEIGHT}); + character->SetBounds({CHARACTER_BOUNDS_X, CHARACTER_BOUNDS_Y, CHARACTER_BOUNDS_WIDTH, CHARACTER_BOUNDS_HEIGHT}); //TODO: send the bounds from the server character->SetHandle(argPacket->handle); character->SetAvatar(argPacket->avatar); character->SetOwner(argPacket->accountIndex); @@ -71,7 +71,7 @@ void InWorld::HandleCharacterCreate(CharacterPacket* const argPacket) { } //debug - std::cout << "Create, total: " << characterMap.size() << std::endl; + std::cout << "Character Create, total: " << characterMap.size() << std::endl; } void InWorld::HandleCharacterDelete(CharacterPacket* const argPacket) { @@ -97,7 +97,7 @@ void InWorld::HandleCharacterDelete(CharacterPacket* const argPacket) { characterMap.erase(characterIt); //debug - std::cout << "Delete, total: " << characterMap.size() << std::endl; + std::cout << "Character Delete, total: " << characterMap.size() << std::endl; } void InWorld::HandleCharacterQueryExists(CharacterPacket* const argPacket) { @@ -124,7 +124,7 @@ void InWorld::HandleCharacterQueryExists(CharacterPacket* const argPacket) { character->CorrectSprite(); //debug - std::cout << "Query, total: " << characterMap.size() << std::endl; + std::cout << "Character Query, total: " << characterMap.size() << std::endl; } void InWorld::HandleCharacterMovement(CharacterPacket* const argPacket) { @@ -152,15 +152,71 @@ void InWorld::HandleCharacterAttack(CharacterPacket* const argPacket) { //------------------------- void InWorld::HandleMonsterCreate(MonsterPacket* const argPacket) { - //TODO + //check for logic errors + if (monsterMap.find(argPacket->monsterIndex) != monsterMap.end()) { + std::ostringstream msg; + msg << "Double monster creation event; "; + msg << "Index: " << argPacket->monsterIndex << "; "; + msg << "Handle: " << argPacket->handle; + throw(std::runtime_error(msg.str())); + } + + //ignore monsters from other rooms + if (roomIndex != argPacket->roomIndex) { + //temporary error checking + std::ostringstream msg; + msg << "Monster from the wrong room received: "; + msg << "monsterIndex: " << argPacket->monsterIndex << ", roomIndex: " << argPacket->roomIndex; + throw(std::runtime_error(msg.str())); + } + + //implicitly create the element + BaseMonster* monster = &monsterMap[argPacket->monsterIndex]; + + //fill the monster's info + monster->SetHandle(argPacket->handle); + monster->SetAvatar(argPacket->avatar); + monster->SetBounds(argPacket->bounds); + monster->SetOrigin(argPacket->origin); + monster->SetMotion(argPacket->motion); + + //debug + std::cout << "Monster Create, total: " << monsterMap.size() << std::endl; } void InWorld::HandleMonsterDelete(MonsterPacket* const argPacket) { - //TODO + //ignore if this monster doesn't exist + std::map::iterator monsterIt = monsterMap.find(argPacket->monsterIndex); + if (monsterIt == monsterMap.end()) { + return; + } + + + //remove this monster + monsterMap.erase(monsterIt); + + //debug + std::cout << "Monster Delete, total: " << monsterMap.size() << std::endl; } void InWorld::HandleMonsterQueryExists(MonsterPacket* const argPacket) { - //TODO + //ignore monsters in a different room (sub-optimal) + if (argPacket->roomIndex != roomIndex) { + return; + } + + //implicitly create the element + BaseMonster* monster = &monsterMap[argPacket->monsterIndex]; + + //fill the monster's info + monster->SetHandle(argPacket->handle); + monster->SetAvatar(argPacket->avatar); + monster->SetBounds(argPacket->bounds); + monster->SetOrigin(argPacket->origin); + monster->SetMotion(argPacket->motion); + + //debug + std::cout << "Monster Query, total: " << monsterMap.size() << std::endl; } void InWorld::HandleMonsterMovement(MonsterPacket* const argPacket) { @@ -175,7 +231,6 @@ void InWorld::HandleMonsterAttack(MonsterPacket* const argPacket) { //player movement //------------------------- -//TODO: add a "movement" packet type void InWorld::SendLocalCharacterMovement() { CharacterPacket newPacket; newPacket.type = SerialPacketType::CHARACTER_MOVEMENT; diff --git a/client/gameplay_scenes/in_world_scene.cpp b/client/gameplay_scenes/in_world_scene.cpp index 284d892..d5d47ea 100644 --- a/client/gameplay_scenes/in_world_scene.cpp +++ b/client/gameplay_scenes/in_world_scene.cpp @@ -77,6 +77,8 @@ InWorld::InWorld(int* const argClientIndex, int* const argAccountIndex): memset(&newPacket, 0, MAX_PACKET_SIZE); newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; network.SendTo(Channels::SERVER, &newPacket); +// newPacket.type = SerialPacketType::QUERY_MONSTER_EXISTS; +// network.SendTo(Channels::SERVER, &newPacket); //set the camera's values camera.width = GetScreen()->w;