Creatures are nearly ready
This commit is contained in:
+32
-13
@@ -186,11 +186,6 @@ void World::RenderFrame(SDL_Renderer* renderer) {
|
|||||||
//draw the map
|
//draw the map
|
||||||
for (std::list<Region>::iterator it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) {
|
for (std::list<Region>::iterator it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) {
|
||||||
tileSheet.DrawRegionTo(renderer, &(*it), camera.x, camera.y);
|
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
|
//draw the entities
|
||||||
@@ -398,6 +393,10 @@ void World::HandlePacket(SerialPacket* const argPacket) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
//creature management
|
//creature management
|
||||||
|
case SerialPacketType::CREATURE_UPDATE:
|
||||||
|
hCreatureUpdate(static_cast<CreaturePacket*>(argPacket));
|
||||||
|
break;
|
||||||
|
|
||||||
case SerialPacketType::CREATURE_CREATE:
|
case SerialPacketType::CREATURE_CREATE:
|
||||||
hCreatureCreate(static_cast<CreaturePacket*>(argPacket));
|
hCreatureCreate(static_cast<CreaturePacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
@@ -776,6 +775,26 @@ void World::hCharacterMovement(CharacterPacket* const argPacket) {
|
|||||||
//creature management
|
//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) {
|
void World::hCreatureCreate(CreaturePacket* const argPacket) {
|
||||||
//check for logic errors
|
//check for logic errors
|
||||||
if (creatureMap.find(argPacket->creatureIndex) != creatureMap.end()) {
|
if (creatureMap.find(argPacket->creatureIndex) != creatureMap.end()) {
|
||||||
@@ -786,14 +805,14 @@ void World::hCreatureCreate(CreaturePacket* const argPacket) {
|
|||||||
throw(std::runtime_error(msg.str()));
|
throw(std::runtime_error(msg.str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
//ignore creatures from other rooms
|
// //ignore creatures from other rooms
|
||||||
if (roomIndex != argPacket->roomIndex) {
|
// if (roomIndex != argPacket->roomIndex) {
|
||||||
//temporary error checking
|
// //temporary error checking
|
||||||
std::ostringstream msg;
|
// std::ostringstream msg;
|
||||||
msg << "Creature from the wrong room received: ";
|
// msg << "Creature from the wrong room received: ";
|
||||||
msg << "creatureIndex: " << argPacket->creatureIndex << ", roomIndex: " << argPacket->roomIndex;
|
// msg << "creatureIndex: " << argPacket->creatureIndex << ", roomIndex: " << argPacket->roomIndex;
|
||||||
throw(std::runtime_error(msg.str()));
|
// throw(std::runtime_error(msg.str()));
|
||||||
}
|
// }
|
||||||
|
|
||||||
//implicitly create the element
|
//implicitly create the element
|
||||||
BaseCreature* creature = &creatureMap[argPacket->creatureIndex];
|
BaseCreature* creature = &creatureMap[argPacket->creatureIndex];
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ private:
|
|||||||
void hCharacterMovement(CharacterPacket* const);
|
void hCharacterMovement(CharacterPacket* const);
|
||||||
|
|
||||||
//creature management
|
//creature management
|
||||||
|
void hCreatureUpdate(CreaturePacket* const);
|
||||||
void hCreatureCreate(CreaturePacket* const);
|
void hCreatureCreate(CreaturePacket* const);
|
||||||
void hCreatureUnload(CreaturePacket* const);
|
void hCreatureUnload(CreaturePacket* const);
|
||||||
void hQueryCreatureExists(CreaturePacket* const);
|
void hQueryCreatureExists(CreaturePacket* const);
|
||||||
|
|||||||
@@ -109,7 +109,8 @@ void RoomData::RunFrame() {
|
|||||||
for (auto& it : creatureList) {
|
for (auto& it : creatureList) {
|
||||||
CreaturePacket packet;
|
CreaturePacket packet;
|
||||||
copyCreatureToPacket(&packet, it.second, it.first);
|
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
|
//TODO: creature/character collisions
|
||||||
@@ -131,6 +132,14 @@ std::string RoomData::GetTileset() {
|
|||||||
return tilesetName;
|
return tilesetName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int RoomData::SetRoomIndex(int i) {
|
||||||
|
return roomIndex = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
int RoomData::GetRoomIndex() {
|
||||||
|
return roomIndex;
|
||||||
|
}
|
||||||
|
|
||||||
std::list<CharacterData*>* RoomData::GetCharacterList() {
|
std::list<CharacterData*>* RoomData::GetCharacterList() {
|
||||||
return &characterList;
|
return &characterList;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,9 @@ public:
|
|||||||
std::string SetTileset(std::string);
|
std::string SetTileset(std::string);
|
||||||
std::string GetTileset();
|
std::string GetTileset();
|
||||||
|
|
||||||
|
int SetRoomIndex(int i);
|
||||||
|
int GetRoomIndex();
|
||||||
|
|
||||||
std::list<CharacterData*>* GetCharacterList();
|
std::list<CharacterData*>* GetCharacterList();
|
||||||
CreatureManager* GetCreatureMgr();
|
CreatureManager* GetCreatureMgr();
|
||||||
RegionPagerLua* GetPager();
|
RegionPagerLua* GetPager();
|
||||||
@@ -70,6 +73,7 @@ private:
|
|||||||
std::string tilesetName;
|
std::string tilesetName;
|
||||||
|
|
||||||
//members
|
//members
|
||||||
|
int roomIndex = 0;
|
||||||
std::list<CharacterData*> characterList;
|
std::list<CharacterData*> characterList;
|
||||||
CreatureManager creatureMgr;
|
CreatureManager creatureMgr;
|
||||||
RegionPagerLua pager;
|
RegionPagerLua pager;
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ int RoomManager::Create(std::string roomName, std::string tileset) {
|
|||||||
RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element
|
RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element
|
||||||
newRoom->SetName(roomName);
|
newRoom->SetName(roomName);
|
||||||
newRoom->SetTileset(tileset);
|
newRoom->SetTileset(tileset);
|
||||||
|
newRoom->SetRoomIndex(counter);
|
||||||
|
|
||||||
newRoom->SetLuaState(lua);
|
newRoom->SetLuaState(lua);
|
||||||
newRoom->SetDatabase(database);
|
newRoom->SetDatabase(database);
|
||||||
|
|||||||
@@ -281,11 +281,11 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
|
|||||||
case SerialPacketType::REGION_REQUEST:
|
case SerialPacketType::REGION_REQUEST:
|
||||||
hRegionRequest(static_cast<RegionPacket*>(argPacket));
|
hRegionRequest(static_cast<RegionPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
//character management
|
||||||
case SerialPacketType::QUERY_CHARACTER_EXISTS:
|
case SerialPacketType::QUERY_CHARACTER_EXISTS:
|
||||||
hQueryCharacterExists(static_cast<CharacterPacket*>(argPacket));
|
hQueryCharacterExists(static_cast<CharacterPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//character management
|
|
||||||
case SerialPacketType::CHARACTER_CREATE:
|
case SerialPacketType::CHARACTER_CREATE:
|
||||||
hCharacterCreate(static_cast<CharacterPacket*>(argPacket));
|
hCharacterCreate(static_cast<CharacterPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
@@ -298,14 +298,13 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
|
|||||||
case SerialPacketType::CHARACTER_UNLOAD:
|
case SerialPacketType::CHARACTER_UNLOAD:
|
||||||
hCharacterUnload(static_cast<CharacterPacket*>(argPacket));
|
hCharacterUnload(static_cast<CharacterPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//character movement
|
|
||||||
case SerialPacketType::CHARACTER_MOVEMENT:
|
case SerialPacketType::CHARACTER_MOVEMENT:
|
||||||
hCharacterMovement(static_cast<CharacterPacket*>(argPacket));
|
hCharacterMovement(static_cast<CharacterPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
//creature management
|
||||||
case SerialPacketType::QUERY_CREATURE_EXISTS:
|
case SerialPacketType::QUERY_CREATURE_EXISTS:
|
||||||
//TODO: creature queries
|
hQueryCreatureExists(static_cast<CreaturePacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//chat
|
//chat
|
||||||
@@ -583,6 +582,10 @@ void ServerApplication::hRegionRequest(RegionPacket* const argPacket) {
|
|||||||
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Character Management
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
void ServerApplication::hQueryCharacterExists(CharacterPacket* const argPacket) {
|
void ServerApplication::hQueryCharacterExists(CharacterPacket* const argPacket) {
|
||||||
//respond with all character data
|
//respond with all character data
|
||||||
CharacterPacket newPacket;
|
CharacterPacket newPacket;
|
||||||
@@ -597,10 +600,6 @@ void ServerApplication::hQueryCharacterExists(CharacterPacket* const argPacket)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Character Management
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void ServerApplication::hCharacterCreate(CharacterPacket* const argPacket) {
|
void ServerApplication::hCharacterCreate(CharacterPacket* const argPacket) {
|
||||||
int characterIndex = characterMgr.Create(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
|
int characterIndex = characterMgr.Create(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
|
||||||
|
|
||||||
@@ -747,12 +746,6 @@ void ServerApplication::hCharacterUnload(CharacterPacket* const argPacket) {
|
|||||||
characterMgr.Unload(argPacket->characterIndex);
|
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) {
|
void ServerApplication::hCharacterMovement(CharacterPacket* const argPacket) {
|
||||||
//get the specified objects
|
//get the specified objects
|
||||||
AccountData* accountData = accountMgr.Get(argPacket->accountIndex);
|
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
|
//chat
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|||||||
@@ -88,23 +88,17 @@ private:
|
|||||||
|
|
||||||
//data management
|
//data management
|
||||||
void hRegionRequest(RegionPacket* const);
|
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
|
//character management
|
||||||
|
void hQueryCharacterExists(CharacterPacket* const);
|
||||||
void hCharacterCreate(CharacterPacket* const);
|
void hCharacterCreate(CharacterPacket* const);
|
||||||
void hCharacterDelete(CharacterPacket* const);
|
void hCharacterDelete(CharacterPacket* const);
|
||||||
void hCharacterLoad(CharacterPacket* const);
|
void hCharacterLoad(CharacterPacket* const);
|
||||||
void hCharacterUnload(CharacterPacket* const);
|
void hCharacterUnload(CharacterPacket* const);
|
||||||
|
|
||||||
//character movement
|
|
||||||
void hCharacterMovement(CharacterPacket* const);
|
void hCharacterMovement(CharacterPacket* const);
|
||||||
// void hCharacterAttack(CharacterPacket* const);
|
|
||||||
// void hCharacterDamage(CharacterPacket* const);
|
//creature management
|
||||||
|
void hQueryCreatureExists(CreaturePacket* const);
|
||||||
|
|
||||||
//chat
|
//chat
|
||||||
void hTextBroadcast(TextPacket* const);
|
void hTextBroadcast(TextPacket* const);
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ void pumpAndChangeRooms(int characterIndex, int newRoomIndex) {
|
|||||||
|
|
||||||
//TODO: (0) refactor this
|
//TODO: (0) refactor this
|
||||||
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex) {
|
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex) {
|
||||||
//delete from the old room
|
//delete the character from the old room
|
||||||
CharacterPacket newPacket;
|
CharacterPacket newPacket;
|
||||||
copyCharacterToPacket(&newPacket, characterData, characterIndex);
|
copyCharacterToPacket(&newPacket, characterData, characterIndex);
|
||||||
newPacket.type = SerialPacketType::CHARACTER_UNLOAD;
|
newPacket.type = SerialPacketType::CHARACTER_UNLOAD;
|
||||||
@@ -198,8 +198,12 @@ void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, in
|
|||||||
characterData->SetRoomIndex(newRoomIndex);
|
characterData->SetRoomIndex(newRoomIndex);
|
||||||
RoomManager::GetSingleton().PushCharacter(characterData);
|
RoomManager::GetSingleton().PushCharacter(characterData);
|
||||||
|
|
||||||
//create in the new room
|
//create character in the new room
|
||||||
copyCharacterToPacket(&newPacket, characterData, characterIndex);
|
copyCharacterToPacket(&newPacket, characterData, characterIndex);
|
||||||
newPacket.type = SerialPacketType::CHARACTER_CREATE;
|
newPacket.type = SerialPacketType::CHARACTER_CREATE;
|
||||||
pumpPacketProximity(&newPacket, characterData->GetRoomIndex());
|
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(int characterIndex, int newRoomIndex);
|
||||||
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex);
|
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex);
|
||||||
|
|
||||||
|
double distance(Vector2 lhs, Vector2 rhs);
|
||||||
|
|||||||
Reference in New Issue
Block a user