if (skel) {
skel->Instantiate(entity.GetModel());
}
- cout << "spawned entity " << entity.Name() << " at " << entity.AbsolutePosition() << endl;
+ cout << "spawned entity #" << entity_id << " (" << entity.Name()
+ << ") at " << entity.AbsolutePosition() << endl;
}
void MasterState::On(const Packet::DespawnEntity &pack) {
for (Entity &entity : state->GetWorld().Entities()) {
if (entity.ID() == entity_id) {
entity.Kill();
- cout << "despawned entity " << entity.Name() << " at " << entity.AbsolutePosition() << endl;
+ cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
return;
}
}
#include <deque>
#include <list>
#include <SDL_net.h>
+#include <vector>
namespace blank {
void SendSpawn(SpawnStatus &);
void SendDespawn(SpawnStatus &);
- void SendUpdate(SpawnStatus &);
+ void QueueUpdate(SpawnStatus &);
+ void SendUpdates();
void CheckPlayerFix();
std::list<SpawnStatus> spawns;
unsigned int confirm_wait;
+ std::vector<SpawnStatus *> entity_updates;
+
EntityState player_update_state;
std::uint16_t player_update_pack;
IntervalTimer player_update_timer;
, player(nullptr, nullptr)
, spawns()
, confirm_wait(0)
+, entity_updates()
, player_update_state()
, player_update_pack(0)
, player_update_timer(1500)
SendDespawn(*local_iter);
} else {
// update
- SendUpdate(*local_iter);
+ QueueUpdate(*local_iter);
}
++global_iter;
++local_iter;
SendDespawn(*local_iter);
++local_iter;
}
+ SendUpdates();
CheckPlayerFix();
CheckChunkQueue();
++confirm_wait;
}
-void ClientConnection::SendUpdate(SpawnStatus &status) {
+void ClientConnection::QueueUpdate(SpawnStatus &status) {
// don't send updates while spawn not ack'd or despawn sent
- if (status.spawn_pack != -1 || status.despawn_pack != -1) return;
+ if (status.spawn_pack == -1 && status.despawn_pack == -1) {
+ entity_updates.push_back(&status);
+ }
+}
- // TODO: pack entity updates
+void ClientConnection::SendUpdates() {
auto pack = Prepare<Packet::EntityUpdate>();
- pack.WriteEntityCount(1);
- pack.WriteEntity(*status.entity, 0);
- Send(Packet::EntityUpdate::GetSize(1));
+ int entity_pos = 0;
+ for (SpawnStatus *status : entity_updates) {
+ pack.WriteEntity(*status->entity, entity_pos);
+ ++entity_pos;
+ if (entity_pos == Packet::EntityUpdate::MAX_ENTITIES) {
+ pack.WriteEntityCount(entity_pos);
+ Send(Packet::EntityUpdate::GetSize(entity_pos));
+ pack = Prepare<Packet::EntityUpdate>();
+ entity_pos = 0;
+ }
+ }
+ if (entity_pos > 0) {
+ pack.WriteEntityCount(entity_pos);
+ Send(Packet::EntityUpdate::GetSize(entity_pos));
+ }
+ entity_updates.clear();
}
void ClientConnection::CheckPlayerFix() {