commit 497a601c424e8e728ef0a8e61f049982f2d4af16 Author: Persson-dev <sim16.prib@gmail.com> Date: Sat Aug 12 10:32:52 2023 +0200 fix warning commit 1bfd019a1ea00dcdb6323d1f285e2cdd3ebb4020 Author: Persson-dev <sim16.prib@gmail.com> Date: Tue Jul 25 19:05:13 2023 +0200 refactor: update cast commit 5bbc23a7d37e53eb74a885685f18e714f9448fd9 Author: Persson-dev <sim16.prib@gmail.com> Date: Tue Jul 25 19:04:15 2023 +0200 moved GetImguiTeamColor commit fd0e2d2470ea5cca3553acf280aa371de5c06f4c Author: Persson-dev <sim16.prib@gmail.com> Date: Tue Jul 25 19:03:37 2023 +0200 packets forward declaration commit 06eb9b99a96731f4b9a2167c00ed0bcd03702e3b Author: Persson-dev <sim16.prib@gmail.com> Date: Tue Jul 25 18:30:55 2023 +0200 remove Protocol.h includes commit 165f63d2e8b468f3e38992baddc221270010f801 Author: Persson-dev <sim16.prib@gmail.com> Date: Tue Jul 25 18:30:30 2023 +0200 split packets into separate source files commit f247f146c6c1e804a44b86f65cf3059859c07c6c Author: Persson-dev <sim16.prib@gmail.com> Date: Tue Jul 25 17:45:24 2023 +0200 split packets into separate headers
86 lines
3.6 KiB
C++
86 lines
3.6 KiB
C++
#include "game/client/WorldClient.h"
|
|
#include "protocol/PacketDispatcher.h"
|
|
#include "game/client/ClientGame.h"
|
|
#include "render/WorldRenderer.h"
|
|
|
|
#include "protocol/packets/WorldAddTowerPacket.h"
|
|
#include "protocol/packets/WorldBeginDataPacket.h"
|
|
#include "protocol/packets/WorldDataPacket.h"
|
|
#include "protocol/packets/SpawnMobPacket.h"
|
|
#include "protocol/packets/UpgradeTowerPacket.h"
|
|
#include "protocol/packets/RemoveTowerPacket.h"
|
|
#include "protocol/packets/UpdateCastleLifePacket.h"
|
|
#include "protocol/packets/UpdateMobStatesPacket.h"
|
|
|
|
namespace td {
|
|
namespace client {
|
|
|
|
WorldClient::WorldClient(ClientGame* game) : game::World(game), protocol::PacketHandler(game->GetDispatcher()), m_Game(game) {
|
|
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldAddTower, this);
|
|
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldBeginData, this);
|
|
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldData, this);
|
|
GetDispatcher()->RegisterHandler(protocol::PacketType::SpawnMob, this);
|
|
GetDispatcher()->RegisterHandler(protocol::PacketType::UpgradeTower, this);
|
|
GetDispatcher()->RegisterHandler(protocol::PacketType::RemoveTower, this);
|
|
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateCastleLife, this);
|
|
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateMobStates, this);
|
|
}
|
|
|
|
void WorldClient::HandlePacket(const protocol::WorldBeginDataPacket* packet) {
|
|
LoadMap(packet);
|
|
if (m_Game->GetGameState() == game::GameState::Game) {
|
|
const Color& backgroundColor = GetBackgroundColor();
|
|
m_Game->GetRenderer()->SetBackgroundColor({ static_cast<float>(backgroundColor.r / 255.0f), static_cast<float>(backgroundColor.g / 255.0f),
|
|
static_cast<float>(backgroundColor.b / 255.0f) });
|
|
}
|
|
}
|
|
|
|
void WorldClient::HandlePacket(const protocol::WorldDataPacket* packet) {
|
|
LoadMap(packet);
|
|
}
|
|
|
|
void WorldClient::HandlePacket(const protocol::SpawnMobPacket* packet) {
|
|
SpawnMobAt(packet->GetMobID(), packet->GetMobType(), packet->GetMobLevel(), packet->GetSender(),
|
|
packet->GetMobX(), packet->GetMobY(), packet->GetMobDirection());
|
|
}
|
|
|
|
void WorldClient::HandlePacket(const protocol::UpgradeTowerPacket* packet) {
|
|
game::TowerPtr tower = GetTowerById(packet->GetTowerID());
|
|
if (tower == nullptr) return; // this should not happen but who knows ?
|
|
tower->Upgrade(packet->GetTowerLevel().GetLevel(), packet->GetTowerLevel().GetPath());
|
|
}
|
|
|
|
void WorldClient::HandlePacket(const protocol::WorldAddTowerPacket* packet) {
|
|
game::TowerPtr newTower = PlaceTowerAt(packet->GetTowerID(), packet->GetTowerType(), packet->GetTowerX(), packet->GetTowerY(), packet->GetBuilder());
|
|
|
|
GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerAdd, newTower);
|
|
}
|
|
|
|
void WorldClient::HandlePacket(const protocol::RemoveTowerPacket* packet) {
|
|
game::TowerPtr tower = RemoveTower(packet->GetTowerID());
|
|
|
|
if (tower != nullptr) {
|
|
GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerRemove, tower);
|
|
}
|
|
}
|
|
|
|
void WorldClient::HandlePacket(const protocol::UpdateMobStatesPacket* packet) {
|
|
for (auto mobState : packet->GetMobStates()) {
|
|
game::MobID mobId = mobState.GetMobId();
|
|
auto it = std::find_if(GetMobList().begin(), GetMobList().end(), [mobId](game::MobPtr mob) { return mob->GetMobID() == mobId; });
|
|
if (it != GetMobList().end()) {
|
|
game::MobPtr& mob = *it;
|
|
mob->SetCenter(mobState.GetMobPosition());
|
|
mob->SetDirection(mobState.GetMobDirection());
|
|
mob->SetHealth(mobState.GetMobLife());
|
|
}
|
|
}
|
|
}
|
|
|
|
void WorldClient::HandlePacket(const protocol::UpdateCastleLifePacket* packet) {
|
|
GetTeam(packet->GetTeamColor()).GetCastle().SetLife(packet->GetCastleLife());
|
|
}
|
|
|
|
} // namespace client
|
|
} // namespace td
|