110 lines
2.8 KiB
C++
110 lines
2.8 KiB
C++
#include "precompile.h"
|
|
|
|
#include "batchsync.h"
|
|
|
|
#include "creature.h"
|
|
#include "team.h"
|
|
#include "human.h"
|
|
#include "room.h"
|
|
#include "player.h"
|
|
|
|
#include "cs_proto.pb.h"
|
|
#include "cs_msgid.pb.h"
|
|
|
|
BatchSync::BatchSync(Room* room)
|
|
{
|
|
room_ = room;
|
|
}
|
|
|
|
BatchSync::~BatchSync()
|
|
{
|
|
|
|
}
|
|
|
|
void BatchSync::AddGlobalObject(Creature* c)
|
|
{
|
|
if (global_object_hash_.find(c->GetUniId()) != global_object_hash_.end()) {
|
|
abort();
|
|
}
|
|
auto tuple = std::make_shared<std::tuple<f8::TimerWp, SyncObject>>();
|
|
SyncObject& sync_obj = std::get<1>(*tuple);
|
|
sync_obj.obj_uniid = c->GetUniId();
|
|
sync_obj.c = c->GetWeakPtrRef();
|
|
sync_obj.pos = c->GetPos().ToGlmVec3();
|
|
sync_obj.dir = c->GetAttackDir();
|
|
sync_obj.last_sync_frameno = 0;
|
|
global_object_hash_[c->GetUniId()] = tuple;
|
|
std::get<0>(*tuple) = f8::Timer::Instance()->SetIntervalWpEx
|
|
(
|
|
1000,
|
|
[this, tuple] (int event, const a8::Args* args)
|
|
{
|
|
if (event == a8::TIMER_EXEC_EVENT) {
|
|
cs::SMSyncPosition sync_msg;
|
|
std::get<1>(*tuple).FillSMSyncPosition(sync_msg);
|
|
room_->TraversePlayerList
|
|
(
|
|
[&sync_msg] (Player* hum) mutable
|
|
{
|
|
hum->SendNotifyMsg(sync_msg);
|
|
});
|
|
}
|
|
},
|
|
&timer_attacher_);
|
|
}
|
|
|
|
void BatchSync::RemoveGlobalObject(int obj_uniid)
|
|
{
|
|
auto itr = global_object_hash_.find(obj_uniid);
|
|
if (itr != global_object_hash_.end()) {
|
|
f8::Timer::Instance()->Delete(std::get<0>(*itr->second));
|
|
global_object_hash_.erase(itr);
|
|
}
|
|
}
|
|
|
|
void BatchSync::AddTeam(Team* team)
|
|
{
|
|
if (team_hash_.find(team) != team_hash_.end()) {
|
|
abort();
|
|
}
|
|
auto tuple = std::make_shared<std::tuple<f8::TimerWp, std::vector<SyncObject>>>();
|
|
team->TraverseMembers
|
|
(
|
|
[&tuple] (Human* hum)
|
|
{
|
|
SyncObject& sync_obj = a8::FastAppend(std::get<1>(*tuple));
|
|
sync_obj.obj_uniid = hum->GetUniId();
|
|
sync_obj.c = hum->GetWeakPtrRef();
|
|
sync_obj.pos = hum->GetPos().ToGlmVec3();
|
|
sync_obj.dir = hum->GetAttackDir();
|
|
sync_obj.last_sync_frameno = 0;
|
|
return true;
|
|
});
|
|
team_hash_[team] = tuple;
|
|
std::get<0>(*tuple) = f8::Timer::Instance()->SetIntervalWpEx
|
|
(
|
|
1000,
|
|
[] (int event, const a8::Args* args)
|
|
{
|
|
if (event == a8::TIMER_EXEC_EVENT) {
|
|
|
|
}
|
|
},
|
|
&timer_attacher_);
|
|
}
|
|
|
|
void BatchSync::UpdateTeam(Team* team)
|
|
{
|
|
RemoveTeam(team);
|
|
AddTeam(team);
|
|
}
|
|
|
|
void BatchSync::RemoveTeam(Team* team)
|
|
{
|
|
auto itr = team_hash_.find(team);
|
|
if (itr != team_hash_.end()) {
|
|
f8::Timer::Instance()->Delete(std::get<0>(*itr->second));
|
|
team_hash_.erase(itr);
|
|
}
|
|
}
|