+void MasterState::On(const Packet::SpawnEntity &pack) {
+ if (!state) {
+ cout << "got entity spawn before world was created" << endl;
+ Quit();
+ return;
+ }
+ uint32_t entity_id;
+ pack.ReadEntityID(entity_id);
+ Entity *entity = state->GetWorld().AddEntity(entity_id);
+ if (!entity) {
+ cout << "entity ID inconsistency" << endl;
+ Quit();
+ return;
+ }
+ pack.ReadEntity(*entity);
+ cout << "spawned entity " << entity->Name() << " at " << entity->AbsolutePosition() << endl;
+}
+
+void MasterState::On(const Packet::DespawnEntity &pack) {
+ if (!state) {
+ cout << "got entity despawn before world was created" << endl;
+ Quit();
+ return;
+ }
+ uint32_t entity_id;
+ pack.ReadEntityID(entity_id);
+ for (Entity &entity : state->GetWorld().Entities()) {
+ if (entity.ID() == entity_id) {
+ entity.Kill();
+ cout << "despawned entity " << entity.Name() << " at " << entity.AbsolutePosition() << endl;
+ return;
+ }
+ }
+}
+
+void MasterState::On(const Packet::EntityUpdate &pack) {
+ if (!state) {
+ cout << "got entity update before world was created" << endl;
+ Quit();
+ return;
+ }
+
+ auto world_iter = state->GetWorld().Entities().begin();
+ auto world_end = state->GetWorld().Entities().end();
+
+ uint32_t count = 0;
+ pack.ReadEntityCount(count);
+
+ for (uint32_t i = 0; i < count; ++i) {
+ uint32_t entity_id = 0;
+ pack.ReadEntityID(entity_id, i);
+
+ while (world_iter != world_end && world_iter->ID() < entity_id) {
+ ++world_iter;
+ }
+ if (world_iter == world_end) {
+ // nothing can be done from here
+ return;
+ }
+ if (world_iter->ID() == entity_id) {
+ pack.ReadEntity(*world_iter, i);
+ }
+ }
+}
+