Creatures are nearly ready
This commit is contained in:
+32
-13
@@ -186,11 +186,6 @@ void World::RenderFrame(SDL_Renderer* renderer) {
|
||||
//draw the map
|
||||
for (std::list<Region>::iterator it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) {
|
||||
tileSheet.DrawRegionTo(renderer, &(*it), camera.x, camera.y);
|
||||
|
||||
//debugging
|
||||
// std::ostringstream msg;
|
||||
// msg << it->GetX() << ", " << it->GetY();
|
||||
// font.DrawStringTo(msg.str(), screen, it->GetX() * tileSheet.GetImage()->GetClipW() - camera.x, it->GetY() * tileSheet.GetImage()->GetClipH() - camera.y);
|
||||
}
|
||||
|
||||
//draw the entities
|
||||
@@ -398,6 +393,10 @@ void World::HandlePacket(SerialPacket* const argPacket) {
|
||||
break;
|
||||
|
||||
//creature management
|
||||
case SerialPacketType::CREATURE_UPDATE:
|
||||
hCreatureUpdate(static_cast<CreaturePacket*>(argPacket));
|
||||
break;
|
||||
|
||||
case SerialPacketType::CREATURE_CREATE:
|
||||
hCreatureCreate(static_cast<CreaturePacket*>(argPacket));
|
||||
break;
|
||||
@@ -776,6 +775,26 @@ void World::hCharacterMovement(CharacterPacket* const argPacket) {
|
||||
//creature management
|
||||
//-------------------------
|
||||
|
||||
void World::hCreatureUpdate(CreaturePacket* const argPacket) {
|
||||
//TODO: (1) Authentication
|
||||
|
||||
//check that this character exists
|
||||
std::map<int, BaseCreature>::iterator creatureIt = creatureMap.find(argPacket->creatureIndex);
|
||||
if (creatureIt != creatureMap.end()) {
|
||||
//update the origin and motion, if there's a difference
|
||||
if (creatureIt->second.GetOrigin() != argPacket->origin) {
|
||||
creatureIt->second.SetOrigin(argPacket->origin);
|
||||
}
|
||||
if (creatureIt->second.GetMotion() != argPacket->motion) {
|
||||
creatureIt->second.SetMotion(argPacket->motion);
|
||||
creatureIt->second.CorrectSprite(); //only correct the sprite if the motion changes
|
||||
}
|
||||
}
|
||||
else {
|
||||
hCreatureCreate(argPacket);
|
||||
}
|
||||
}
|
||||
|
||||
void World::hCreatureCreate(CreaturePacket* const argPacket) {
|
||||
//check for logic errors
|
||||
if (creatureMap.find(argPacket->creatureIndex) != creatureMap.end()) {
|
||||
@@ -786,14 +805,14 @@ void World::hCreatureCreate(CreaturePacket* const argPacket) {
|
||||
throw(std::runtime_error(msg.str()));
|
||||
}
|
||||
|
||||
//ignore creatures from other rooms
|
||||
if (roomIndex != argPacket->roomIndex) {
|
||||
//temporary error checking
|
||||
std::ostringstream msg;
|
||||
msg << "Creature from the wrong room received: ";
|
||||
msg << "creatureIndex: " << argPacket->creatureIndex << ", roomIndex: " << argPacket->roomIndex;
|
||||
throw(std::runtime_error(msg.str()));
|
||||
}
|
||||
// //ignore creatures from other rooms
|
||||
// if (roomIndex != argPacket->roomIndex) {
|
||||
// //temporary error checking
|
||||
// std::ostringstream msg;
|
||||
// msg << "Creature from the wrong room received: ";
|
||||
// msg << "creatureIndex: " << argPacket->creatureIndex << ", roomIndex: " << argPacket->roomIndex;
|
||||
// throw(std::runtime_error(msg.str()));
|
||||
// }
|
||||
|
||||
//implicitly create the element
|
||||
BaseCreature* creature = &creatureMap[argPacket->creatureIndex];
|
||||
|
||||
@@ -107,6 +107,7 @@ private:
|
||||
void hCharacterMovement(CharacterPacket* const);
|
||||
|
||||
//creature management
|
||||
void hCreatureUpdate(CreaturePacket* const);
|
||||
void hCreatureCreate(CreaturePacket* const);
|
||||
void hCreatureUnload(CreaturePacket* const);
|
||||
void hQueryCreatureExists(CreaturePacket* const);
|
||||
|
||||
@@ -109,7 +109,8 @@ void RoomData::RunFrame() {
|
||||
for (auto& it : creatureList) {
|
||||
CreaturePacket packet;
|
||||
copyCreatureToPacket(&packet, it.second, it.first);
|
||||
//TODO: send
|
||||
packet.type = SerialPacketType::CREATURE_UPDATE;
|
||||
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, it.second->GetOrigin(), 320);
|
||||
}
|
||||
|
||||
//TODO: creature/character collisions
|
||||
@@ -131,6 +132,14 @@ std::string RoomData::GetTileset() {
|
||||
return tilesetName;
|
||||
}
|
||||
|
||||
int RoomData::SetRoomIndex(int i) {
|
||||
return roomIndex = i;
|
||||
}
|
||||
|
||||
int RoomData::GetRoomIndex() {
|
||||
return roomIndex;
|
||||
}
|
||||
|
||||
std::list<CharacterData*>* RoomData::GetCharacterList() {
|
||||
return &characterList;
|
||||
}
|
||||
|
||||
@@ -48,6 +48,9 @@ public:
|
||||
std::string SetTileset(std::string);
|
||||
std::string GetTileset();
|
||||
|
||||
int SetRoomIndex(int i);
|
||||
int GetRoomIndex();
|
||||
|
||||
std::list<CharacterData*>* GetCharacterList();
|
||||
CreatureManager* GetCreatureMgr();
|
||||
RegionPagerLua* GetPager();
|
||||
@@ -70,6 +73,7 @@ private:
|
||||
std::string tilesetName;
|
||||
|
||||
//members
|
||||
int roomIndex = 0;
|
||||
std::list<CharacterData*> characterList;
|
||||
CreatureManager creatureMgr;
|
||||
RegionPagerLua pager;
|
||||
|
||||
@@ -34,6 +34,7 @@ int RoomManager::Create(std::string roomName, std::string tileset) {
|
||||
RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element
|
||||
newRoom->SetName(roomName);
|
||||
newRoom->SetTileset(tileset);
|
||||
newRoom->SetRoomIndex(counter);
|
||||
|
||||
newRoom->SetLuaState(lua);
|
||||
newRoom->SetDatabase(database);
|
||||
|
||||
@@ -281,11 +281,11 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
|
||||
case SerialPacketType::REGION_REQUEST:
|
||||
hRegionRequest(static_cast<RegionPacket*>(argPacket));
|
||||
break;
|
||||
|
||||
//character management
|
||||
case SerialPacketType::QUERY_CHARACTER_EXISTS:
|
||||
hQueryCharacterExists(static_cast<CharacterPacket*>(argPacket));
|
||||
break;
|
||||
|
||||
//character management
|
||||
case SerialPacketType::CHARACTER_CREATE:
|
||||
hCharacterCreate(static_cast<CharacterPacket*>(argPacket));
|
||||
break;
|
||||
@@ -298,14 +298,13 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
|
||||
case SerialPacketType::CHARACTER_UNLOAD:
|
||||
hCharacterUnload(static_cast<CharacterPacket*>(argPacket));
|
||||
break;
|
||||
|
||||
//character movement
|
||||
case SerialPacketType::CHARACTER_MOVEMENT:
|
||||
hCharacterMovement(static_cast<CharacterPacket*>(argPacket));
|
||||
break;
|
||||
|
||||
//creature management
|
||||
case SerialPacketType::QUERY_CREATURE_EXISTS:
|
||||
//TODO: creature queries
|
||||
hQueryCreatureExists(static_cast<CreaturePacket*>(argPacket));
|
||||
break;
|
||||
|
||||
//chat
|
||||
@@ -583,6 +582,10 @@ void ServerApplication::hRegionRequest(RegionPacket* const argPacket) {
|
||||
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Character Management
|
||||
//-------------------------
|
||||
|
||||
void ServerApplication::hQueryCharacterExists(CharacterPacket* const argPacket) {
|
||||
//respond with all character data
|
||||
CharacterPacket newPacket;
|
||||
@@ -597,10 +600,6 @@ void ServerApplication::hQueryCharacterExists(CharacterPacket* const argPacket)
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Character Management
|
||||
//-------------------------
|
||||
|
||||
void ServerApplication::hCharacterCreate(CharacterPacket* const argPacket) {
|
||||
int characterIndex = characterMgr.Create(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
|
||||
|
||||
@@ -747,12 +746,6 @@ void ServerApplication::hCharacterUnload(CharacterPacket* const argPacket) {
|
||||
characterMgr.Unload(argPacket->characterIndex);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//character movement
|
||||
//-------------------------
|
||||
|
||||
//TODO: (2) Could replace this verbosity with a "verify" method, taking a client, account and character ptr as arguments
|
||||
|
||||
void ServerApplication::hCharacterMovement(CharacterPacket* const argPacket) {
|
||||
//get the specified objects
|
||||
AccountData* accountData = accountMgr.Get(argPacket->accountIndex);
|
||||
@@ -806,6 +799,25 @@ void ServerApplication::hCharacterMovement(CharacterPacket* const argPacket) {
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//creature management
|
||||
//-------------------------
|
||||
|
||||
//TODO: On creature create, etc.
|
||||
|
||||
void ServerApplication::hQueryCreatureExists(CreaturePacket* const argPacket) {
|
||||
CreatureManager* creatureMgr = roomMgr.Get(argPacket->roomIndex)->GetCreatureMgr();
|
||||
|
||||
CreaturePacket newPacket;
|
||||
for ( auto& it : *(creatureMgr->GetContainer()) ) {
|
||||
if (distance(argPacket->origin, it.second.GetOrigin()) < 1000) {
|
||||
copyCreatureToPacket(&newPacket, &(it.second), it.first);
|
||||
newPacket.type = SerialPacketType::QUERY_CREATURE_EXISTS;
|
||||
network.SendTo(argPacket->srcAddress, reinterpret_cast<void*>(&newPacket), MAX_PACKET_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//chat
|
||||
//-------------------------
|
||||
|
||||
@@ -88,23 +88,17 @@ private:
|
||||
|
||||
//data management
|
||||
void hRegionRequest(RegionPacket* const);
|
||||
void hQueryCharacterExists(CharacterPacket* const);
|
||||
// void hQueryCharacterStats(CharacterPacket* const);
|
||||
// void hQueryCharacterLocation(CharacterPacket* const);
|
||||
// void hQueryMonsterExists(MonsterPacket* const);
|
||||
// void hQueryMonsterStats(MonsterPacket* const);
|
||||
// void hQueryMonsterLocation(MonsterPacket* const);
|
||||
|
||||
//character management
|
||||
void hQueryCharacterExists(CharacterPacket* const);
|
||||
void hCharacterCreate(CharacterPacket* const);
|
||||
void hCharacterDelete(CharacterPacket* const);
|
||||
void hCharacterLoad(CharacterPacket* const);
|
||||
void hCharacterUnload(CharacterPacket* const);
|
||||
|
||||
//character movement
|
||||
void hCharacterMovement(CharacterPacket* const);
|
||||
// void hCharacterAttack(CharacterPacket* const);
|
||||
// void hCharacterDamage(CharacterPacket* const);
|
||||
|
||||
//creature management
|
||||
void hQueryCreatureExists(CreaturePacket* const);
|
||||
|
||||
//chat
|
||||
void hTextBroadcast(TextPacket* const);
|
||||
|
||||
@@ -187,7 +187,7 @@ void pumpAndChangeRooms(int characterIndex, int newRoomIndex) {
|
||||
|
||||
//TODO: (0) refactor this
|
||||
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex) {
|
||||
//delete from the old room
|
||||
//delete the character from the old room
|
||||
CharacterPacket newPacket;
|
||||
copyCharacterToPacket(&newPacket, characterData, characterIndex);
|
||||
newPacket.type = SerialPacketType::CHARACTER_UNLOAD;
|
||||
@@ -198,8 +198,12 @@ void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, in
|
||||
characterData->SetRoomIndex(newRoomIndex);
|
||||
RoomManager::GetSingleton().PushCharacter(characterData);
|
||||
|
||||
//create in the new room
|
||||
//create character in the new room
|
||||
copyCharacterToPacket(&newPacket, characterData, characterIndex);
|
||||
newPacket.type = SerialPacketType::CHARACTER_CREATE;
|
||||
pumpPacketProximity(&newPacket, characterData->GetRoomIndex());
|
||||
}
|
||||
|
||||
double distance(Vector2 lhs, Vector2 rhs) {
|
||||
return abs((lhs - rhs).Length());
|
||||
}
|
||||
@@ -40,3 +40,5 @@ void copyCreatureToPacket(CreaturePacket* const packet, CreatureData* const crea
|
||||
|
||||
void pumpAndChangeRooms(int characterIndex, int newRoomIndex);
|
||||
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex);
|
||||
|
||||
double distance(Vector2 lhs, Vector2 rhs);
|
||||
|
||||
Reference in New Issue
Block a user