//------------------------------------------------------------------------------------- ScriptID ScriptCallbacks::addCallback( float initialOffset, TimerHandler * pHandler ) { if (initialOffset < 0.f) { WARNING_MSG(boost::format("ScriptCallbacks::addTimer: Negative timer offset (%1%)\n") % initialOffset ); initialOffset = 0.f; } int hertz = 0; if(g_componentType == BOTS_TYPE) hertz = g_kbeSrvConfig.gameUpdateHertz(); else hertz = Config::getSingleton().gameUpdateHertz(); int initialTicks = GameTime( g_kbetime + initialOffset * hertz ); TimerHandle timerHandle = timers_.add( initialTicks, 0, pHandler, NULL ); if (timerHandle.isSet()) { int id = this->getNewID(); map_[ id ] = timerHandle; return id; } return 0; }
//------------------------------------------------------------------------------------- void ScriptCallbacks::cancelAll() { Map::size_type size = map_.size(); for (Map::size_type i = 0; i < size; ++i) { KBE_ASSERT( i + map_.size() == size ); TimerHandle handle = map_.begin()->second; handle.cancel(); } }
void DeleteTimerQueue(TimerHandle& Handle) { if (Handle.GetPointer()) { TimerWrapper* pimpl=reinterpret_cast<TimerWrapper*>(Handle.GetPointer()); // msdn say call it again till DeleteTimerQueueTimer succeed or error is ERROR_IO_PENDING for(; DeleteTimerQueueTimer(0, pimpl->Handle, 0) == 0 && GetLastError() != ERROR_IO_PENDING;) { } delete pimpl; Handle=TimerHandle::Zero(); } }
//------------------------------------------------------------------------------------- bool ScriptCallbacks::delCallback(ScriptID timerID) { Map::iterator iter = map_.find( timerID ); if (iter != map_.end()) { TimerHandle handle = iter->second; handle.cancel(); return true; } return false; }
//------------------------------------------------------------------------------------- ScriptID ScriptTimers::addTimer( float initialOffset, float repeatOffset, int userArg, TimerHandler * pHandler ) { if (initialOffset < 0.f) { WARNING_MSG(boost::format("ScriptTimers::addTimer: Negative timer offset (%1%)\n") % initialOffset ); initialOffset = 0.f; } KBE_ASSERT( g_pApp ); int hertz = g_kbeSrvConfig.gameUpdateHertz(); int initialTicks = GameTime( g_pApp->time() + initialOffset * hertz ); int repeatTicks = 0; if (repeatOffset > 0.f) { repeatTicks = GameTime( repeatOffset * hertz ); if (repeatTicks < 1) { repeatTicks = 1; } } TimerHandle timerHandle = g_pApp->timers().add( initialTicks, repeatTicks, pHandler, (void *)(intptr_t)userArg ); if (timerHandle.isSet()) { int id = this->getNewID(); map_[ id ] = timerHandle; return id; } return 0; }
//------------------------------------------------------------------------------------- bool MoveToPointHandler::update(TimerHandle& handle) { if(pEntity_ == NULL) { handle.cancel(); return false; } Entity* pEntity = pEntity_; const Position3D& dstPos = destPos(); Position3D currpos = pEntity->position(); Position3D currpos_backup = currpos; Direction3D direction = pEntity->direction(); Vector3 movement = dstPos - currpos; if (!moveVertically_) movement.y = 0.f; bool ret = true; if(KBEVec3Length(&movement) < velocity_ + distance_) { float y = currpos.y; currpos = dstPos; if(distance_ > 0.0f) { // 单位化向量 KBEVec3Normalize(&movement, &movement); movement *= distance_; currpos -= movement; } if (!moveVertically_) currpos.y = y; ret = false; } else { // 单位化向量 KBEVec3Normalize(&movement, &movement); // 移动位置 movement *= velocity_; currpos += movement; } // 是否需要改变面向 if (faceMovement_ && (movement.x != 0.f || movement.z != 0.f)) direction.yaw(movement.yaw()); // 设置entity的新位置和面向 pEntity_->position(currpos); pEntity_->direction(direction); // 非navigate都不能确定其在地面上 pEntity_->isOnGound(false); // 通知脚本 pEntity->onMove(scriptCallbacks_.getIDForHandle(handle), layer_, currpos_backup, pyuserarg_); // 如果达到目的地则返回true if(!ret) { return !requestMoveOver(handle, currpos_backup); } return true; }
//------------------------------------------------------------------------------------- bool MoveToPointHandler::requestMoveOver(TimerHandle& handle, const Position3D& oldPos) { pEntity_->onMoveOver(scriptCallbacks_.getIDForHandle(handle), layer_, oldPos, pyuserarg_); handle.cancel(); return true; }