feat: sync game
This commit is contained in:
@@ -13,13 +13,15 @@ WorldClient::WorldClient(ClientGame* game) : game::World(game), protocol::Packet
|
||||
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 game::Color& backgroundColor = getBackgroundColor();
|
||||
m_Game->getRenderer()->setBackgroundColor({ static_cast<float>(backgroundColor.r / 255.0f), static_cast<float>(backgroundColor.g / 255.0f),
|
||||
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) });
|
||||
}
|
||||
}
|
||||
@@ -53,5 +55,22 @@ void WorldClient::HandlePacket(const protocol::RemoveTowerPacket* packet) {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -4,13 +4,17 @@
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
ServerGame::ServerGame(server::Server* server) : game::Game(&m_ServerWorld), m_Server(server), m_ServerWorld(server, this) {
|
||||
ServerGame::ServerGame(server::Server* server) : game::Game(&m_ServerWorld), m_Server(server), m_ServerWorld(server, this),
|
||||
m_GoldMineTimer{ 1000, std::bind(&ServerGame::updateGoldMines, this) },
|
||||
m_MobStatesTimer{ 5000, std::bind(&ServerGame::updateMobStates, this) },
|
||||
m_EndGameCooldown{ 1000 * 10 } {
|
||||
bindListener(this);
|
||||
}
|
||||
|
||||
void ServerGame::tick(std::uint64_t delta) {
|
||||
if (m_GameState == game::GameState::Game) {
|
||||
Game::tick(delta);
|
||||
m_MobStatesTimer.update(delta);
|
||||
updatePlayerStats();
|
||||
} else if (m_GameState == game::GameState::EndGame) {
|
||||
if (m_EndGameCooldown.update(delta)) {
|
||||
@@ -55,6 +59,14 @@ void ServerGame::updateGoldMines() {
|
||||
}
|
||||
}
|
||||
|
||||
void ServerGame::updateMobStates() {
|
||||
protocol::UpdateMobStatesPacket packet;
|
||||
for (auto mob : m_World->getMobList()) {
|
||||
packet.addMobState({ mob->getMobID(), mob->getCenter(), mob->getHealth(), mob->getDirection() });
|
||||
}
|
||||
m_Server->broadcastPacket(&packet);
|
||||
}
|
||||
|
||||
void ServerGame::balanceTeams() {
|
||||
for (auto& playerInfo : Game::m_Players) {
|
||||
game::Player& player = playerInfo.second;
|
||||
|
||||
@@ -66,5 +66,13 @@ void ServerWorld::OnMobDie(game::Mob* mob) {
|
||||
}
|
||||
}
|
||||
|
||||
void ServerWorld::OnMobCastleDamage(game::Mob* damager, game::TeamCastle* enemyCastle, float damage) {
|
||||
// calling base class event
|
||||
World::OnMobCastleDamage(damager, enemyCastle, damage);
|
||||
|
||||
protocol::UpdateCastleLifePacket packet(enemyCastle->getLife(), enemyCastle->getTeam()->getColor());
|
||||
m_Server->broadcastPacket(&packet);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
|
||||
Reference in New Issue
Block a user