void Display::updateDisplay(Entities &entities){ al_clear_to_color(al_map_rgba(0, 0, 0, 255)); std::list<Entity*> entitiesTemp = entities.getEntities(); for (std::list<Entity*>::iterator it = entitiesTemp.begin(); it != entitiesTemp.end(); it++){ (*it)->draw(); } displayText(score, Coordinates(80, 10), font2); al_flip_display(); }
PyObject * Entities<T>::mp_subscript(PyObject* self, PyObject* key /*entityID*/) { Entities* lpEntities = static_cast<Entities*>(self); ENTITY_ID entityID = PyLong_AsLong(key); if (PyErr_Occurred()) return NULL; PyObject * pyEntity = NULL; ENTITYS_MAP& entities = lpEntities->getEntities(); ENTITYS_MAP::const_iterator iter = entities.find(entityID); if (iter != entities.end()) pyEntity = iter->second.get(); if(pyEntity == NULL) { PyErr_Format(PyExc_KeyError, "%d", entityID); PyErr_PrintEx(0); return NULL; } Py_INCREF(pyEntity); return pyEntity; }
//------------------------------------------------------------------------------------- void SpaceViewer::updateClient() { if (spaceID_ == 0) return; Space* space = Spaces::findSpace(spaceID_); if (space == NULL || !space->isGood()) { return; } // 最多每次更新500个实体 const int MAX_UPDATE_COUNT = 100; int updateCount = 0; // 获取本次与上次结果的差值,将差值放入stream中更新到客户端 // 差值包括新增的实体,以及已经有的实体的位置变化 MemoryStream s; Entities<Entity>* pEntities = Cellapp::getSingleton().pEntities(); Entities<Entity>::ENTITYS_MAP& entitiesMap = pEntities->getEntities(); // 先检查已经监视的实体,对于版本号较低的优先更新 if (updateCount < MAX_UPDATE_COUNT) { std::map< ENTITY_ID, ViewEntity >::iterator viewerIter = viewedEntities.begin(); for (; viewerIter != viewedEntities.end(); ) { if (updateCount >= MAX_UPDATE_COUNT) break; ViewEntity& viewEntity = viewerIter->second; if (viewEntity.updateVersion > lastUpdateVersion_) continue; Entities<Entity>::ENTITYS_MAP::iterator iter = entitiesMap.find(viewerIter->first); // 找不到实体, 说明已经销毁或者跑到其他进程了 // 如果在其他进程, 其他进程会将其更新到客户端 if (iter == entitiesMap.end()) { s << viewerIter->first; s << false; // true为更新, false为销毁 // 将其从viewedEntities删除 viewedEntities.erase(viewerIter++); } else { Entity* pEntity = static_cast<Entity*>(iter->second.get()); if (pEntity->spaceID() != spaceID_) { // 将其从viewedEntities删除 viewedEntities.erase(viewerIter++); continue; } /* if (pEntity->cellID() != cellID_) { // 将其从viewedEntities删除 viewedEntities.erase(viewerIter++); continue; } */ // 有新增的实体或者已经观察到的实体,检查位置变化 // 如果没有变化则pass if ((viewEntity.position - pEntity->position()).length() <= 0.0004f && (viewEntity.direction.dir - pEntity->direction().dir).length() <= 0.0004f) { ++viewerIter; continue; } viewEntity.entityID = pEntity->id(); viewEntity.position = pEntity->position(); viewEntity.direction = pEntity->direction(); ++viewEntity.updateVersion; s << viewEntity.entityID; s << true; // true为更新, false为销毁 s << pEntity->pScriptModule()->getUType(); s << viewEntity.position.x << viewEntity.position.y << viewEntity.position.z; s << viewEntity.direction.roll() << viewEntity.direction.pitch() << viewEntity.direction.yaw(); ++updateCount; ++viewerIter; } } } // 再检查是否有新增的实体 if (updateCount < MAX_UPDATE_COUNT) { Entities<Entity>::ENTITYS_MAP::iterator iter = entitiesMap.begin(); for (; iter != entitiesMap.end(); ++iter) { if (updateCount >= MAX_UPDATE_COUNT) break; Entity* pEntity = static_cast<Entity*>(iter->second.get()); if (pEntity->spaceID() != spaceID_) continue; /* if (pEntity->cellID() != cellID_) continue; */ std::map< ENTITY_ID, ViewEntity >::iterator findIter = viewedEntities.find(pEntity->id()); ViewEntity& viewEntity = viewedEntities[pEntity->id()]; if (findIter != viewedEntities.end()) continue; viewEntity.entityID = pEntity->id(); viewEntity.position = pEntity->position(); viewEntity.direction = pEntity->direction(); viewEntity.updateVersion = lastUpdateVersion_ + 1; ++updateCount; s << viewEntity.entityID; s << true; // true为更新, false为销毁 s << pEntity->pScriptModule()->getUType(); s << viewEntity.position.x << viewEntity.position.y << viewEntity.position.z; s << viewEntity.direction.roll() << viewEntity.direction.pitch() << viewEntity.direction.yaw(); } } sendStream(&s, updateType_); // 如果全部更新完毕,更换版本号 if (updateCount < MAX_UPDATE_COUNT) ++lastUpdateVersion_; }