remove mobs on player leave

This commit is contained in:
Simon Pribylski
2023-08-26 10:46:20 +02:00
parent 8e7b446003
commit 5631efcf9e
5 changed files with 39 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ public:
virtual void HandlePacket(const protocol::SpawnMobPacket* packet) override; virtual void HandlePacket(const protocol::SpawnMobPacket* packet) override;
virtual void HandlePacket(const protocol::UpgradeTowerPacket* packet) override; virtual void HandlePacket(const protocol::UpgradeTowerPacket* packet) override;
virtual void HandlePacket(const protocol::WorldAddTowerPacket* packet) override; virtual void HandlePacket(const protocol::WorldAddTowerPacket* packet) override;
virtual void HandlePacket(const protocol::RemoveMobPacket* packet) override;
virtual void HandlePacket(const protocol::RemoveTowerPacket* packet) override; virtual void HandlePacket(const protocol::RemoveTowerPacket* packet) override;
virtual void HandlePacket(const protocol::UpdateMobStatesPacket* packet) override; virtual void HandlePacket(const protocol::UpdateMobStatesPacket* packet) override;
virtual void HandlePacket(const protocol::UpdateCastleLifePacket* packet) override; virtual void HandlePacket(const protocol::UpdateCastleLifePacket* packet) override;

View File

@@ -174,6 +174,7 @@ public:
void Reset(); // clear mobs and towers void Reset(); // clear mobs and towers
void SpawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir); void SpawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir);
MobPtr RemoveMob(MobID id);
TowerPtr PlaceTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder); TowerPtr PlaceTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder);
TowerPtr RemoveTower(TowerID id); TowerPtr RemoveTower(TowerID id);

View File

@@ -8,6 +8,7 @@
#include "td/protocol/packets/WorldDataPacket.h" #include "td/protocol/packets/WorldDataPacket.h"
#include "td/protocol/packets/SpawnMobPacket.h" #include "td/protocol/packets/SpawnMobPacket.h"
#include "td/protocol/packets/UpgradeTowerPacket.h" #include "td/protocol/packets/UpgradeTowerPacket.h"
#include "td/protocol/packets/RemoveMobPacket.h"
#include "td/protocol/packets/RemoveTowerPacket.h" #include "td/protocol/packets/RemoveTowerPacket.h"
#include "td/protocol/packets/UpdateCastleLifePacket.h" #include "td/protocol/packets/UpdateCastleLifePacket.h"
#include "td/protocol/packets/UpdateMobStatesPacket.h" #include "td/protocol/packets/UpdateMobStatesPacket.h"
@@ -21,6 +22,7 @@ WorldClient::WorldClient(ClientGame* game) : game::World(game), protocol::Packet
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldData, this); GetDispatcher()->RegisterHandler(protocol::PacketType::WorldData, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::SpawnMob, this); GetDispatcher()->RegisterHandler(protocol::PacketType::SpawnMob, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::UpgradeTower, this); GetDispatcher()->RegisterHandler(protocol::PacketType::UpgradeTower, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::RemoveMob, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::RemoveTower, this); GetDispatcher()->RegisterHandler(protocol::PacketType::RemoveTower, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateCastleLife, this); GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateCastleLife, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateMobStates, this); GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateMobStates, this);
@@ -70,6 +72,14 @@ void WorldClient::HandlePacket(const protocol::RemoveTowerPacket* packet) {
GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerRemove, tower); GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerRemove, tower);
} }
void WorldClient::HandlePacket(const protocol::RemoveMobPacket* packet) {
game::MobPtr mob = RemoveMob(packet->GetMobID());
SAFE_CHECK(mob);
//GetWorldNotifier().NotifyListeners(&MobListener::OnMobDie, mob.get());
}
void WorldClient::HandlePacket(const protocol::UpdateMobStatesPacket* packet) { void WorldClient::HandlePacket(const protocol::UpdateMobStatesPacket* packet) {
for (auto mobState : packet->GetMobStates()) { for (auto mobState : packet->GetMobStates()) {
game::MobID mobId = mobState.GetMobId(); game::MobID mobId = mobState.GetMobId();

View File

@@ -2,6 +2,7 @@
#include "server/Server.h" #include "server/Server.h"
#include "td/protocol/packets/DisconnectPacket.h" #include "td/protocol/packets/DisconnectPacket.h"
#include "td/protocol/packets/RemoveMobPacket.h"
#include "td/protocol/packets/RemoveTowerPacket.h" #include "td/protocol/packets/RemoveTowerPacket.h"
#include "td/protocol/packets/UpdatePlayerTeamPacket.h" #include "td/protocol/packets/UpdatePlayerTeamPacket.h"
#include "td/protocol/packets/UpdateGameStatePacket.h" #include "td/protocol/packets/UpdateGameStatePacket.h"
@@ -142,6 +143,19 @@ void ServerGame::OnPlayerJoin(game::PlayerID id){
void ServerGame::OnPlayerLeave(game::PlayerID playerId) { void ServerGame::OnPlayerLeave(game::PlayerID playerId) {
// temporary fix // temporary fix
auto& mobList = GetWorld()->GetMobList();
for(std::size_t i = 0; i < mobList.size(); i++) {
auto mob = mobList.at(i);
if(mob->GetSender() == playerId) {
protocol::RemoveMobPacket packet(mob->GetMobID());
m_Server->BroadcastPacket(&packet);
mobList.erase(mobList.begin() + i);
}
}
auto& towerList = GetWorld()->GetTowers(); auto& towerList = GetWorld()->GetTowers();
for(std::size_t i = 0; i < towerList.size(); i++) { for(std::size_t i = 0; i < towerList.size(); i++) {
auto tower = towerList.at(i); auto tower = towerList.at(i);

View File

@@ -123,6 +123,17 @@ void World::SpawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID send
GetMobNotifier().NotifyListeners(&MobListener::OnMobSpawn, mob.get()); GetMobNotifier().NotifyListeners(&MobListener::OnMobSpawn, mob.get());
} }
MobPtr World::RemoveMob(MobID mobId) {
auto it = std::find_if(m_Mobs.begin(), m_Mobs.end(), [mobId](MobPtr mob) { return mob->GetMobID() == mobId;});
if (it == m_Mobs.end()) return nullptr;
MobPtr mob = *it;
m_Mobs.erase(it);
return mob;
}
TowerPtr World::PlaceTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder) { TowerPtr World::PlaceTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder) {
TowerPtr tower = TowerFactory::CreateTower(type, id, x, y, builder); TowerPtr tower = TowerFactory::CreateTower(type, id, x, y, builder);
m_Towers.push_back(tower); m_Towers.push_back(tower);