//------------------------------------------------------------------------------------- void FMH_Baseapp_onEntityGetCellFrom_onCreateInNewSpaceFromBaseapp::process() { KBE_ASSERT(_e != NULL); Space* space = Spaces::findSpace(_spaceID); if(space == NULL || !space->isGood()) { ERROR_MSG(fmt::format("FMH_Baseapp_onEntityGetCell::process: not found space({}), {} {}.\n", _spaceID, _e->scriptName(), _e->id())); return; } _e->spaceID(space->id()); _e->initializeEntity(_params); Py_XDECREF(_params); _params = NULL; // Ìí¼Óµ½space space->addEntityToNode(_e); if (_e->clientMailbox()) { _e->onGetWitness(); } else { space->onEnterWorld(_e); } }
//------------------------------------------------------------------------------------- thread::TPTask::TPTaskState LoadNavmeshTask::presentMainThread() { NavigationHandlePtr pNavigationHandle = Navigation::getSingleton().findNavigation(resPath_); Space* pSpace = Spaces::findSpace(spaceID_); if(pSpace == NULL || !pSpace->isGood()) { ERROR_MSG(fmt::format("LoadNavmeshTask::presentMainThread(): not found space({})\n", spaceID_)); } else { pSpace->onLoadedSpaceGeometryMapping(pNavigationHandle); } return thread::TPTask::TPTASK_STATE_COMPLETED; }
//------------------------------------------------------------------------------------- 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_; }