//---------------------------------------------------// // [8/5/2009 Albert] // Description: 移动定时器 //---------------------------------------------------// bool CDynamicObject::UpdateTimer( unsigned int handle, unsigned short& repeat, unsigned int& timer ) { repeat = 1; timer = TIMER_SECONDS(1.0f); // 先移动到当前所在的位置. CGameMap* pScene = static_cast< CGameMap* >( CXObjectPool::GetInstance().GetObj( GetParent(), TypeGameMap ) ); if( pScene ) { if( pScene->DynamicMoveTo( this, m_vNextPosition, false ) ) { //CLogger::GetInstance(_T("Log"))->WriteLog( _T("角色[%08x]移动到路点[%f,%f,%f]") // , GetObjID() // , m_vNextPosition[0], m_vNextPosition[1], m_vNextPosition[2] ); OnStep( m_vNextPosition ); } else { OnArrived( m_vNextPosition ); return true; } } else { m_hUpdateTimer = INVALID_TIMER_HANDLE; return true; } // 计算下一个点 if( m_PointList.empty() ) { m_hUpdateTimer = INVALID_TIMER_HANDLE; OnArrived( m_vNextPosition ); return true; } const PathPoint& target = m_PointList.front(); XVector3 Vec = target.vPosition - m_vNextPosition; float distance = Vec.SqurLength(); float fSpeed = target.fSpeed; if( distance < fSpeed*fSpeed ) { // 当前坐标到目标点的距离小于速度值 m_vNextPosition = target.vPosition; float t = XMath::Sqrt( distance )/fSpeed; timer = TIMER_SECONDS(t); m_PointList.pop_front(); } else { Vec.NormalizeFast(); Vec *= fSpeed; m_vNextPosition += Vec; } return false; }
//---------------------------------------------------// // [8/4/2009 Albert] // Description: 完成添加路点 //---------------------------------------------------// void CDynamicObject::FinishPathPoint( float fDelayTime ) { // timer 参数为 -1 则表示立即被执行 SetUpdate( _tfunction( bind( &CDynamicObject::UpdateTimer, this, _1, _2, _3 ) ), 1, TIMER_SECONDS( 1.0f ), TIMER_SECONDS( fDelayTime ) ); }
int16_t timer_start(int16_t id, uint32_t us) { uint32_t ticks; if (id < 0) return -EINVAL; if (!timers[id].enabled) return -EINVAL; if (timers[id].active) return -EALREADY; ticks = ROUNDED_DIV((uint64_t)us * HFCLK, TIMER_SECONDS(1) * ROUNDED_DIV(2 << TIMER_PRESCALER, 2)); timers[id].active = 1; timers[id].ticks = ticks; update_cc(id, (uint64_t) ticks); if (active == 0) { NRF_TIMER0->TASKS_START = 1UL; } active++; return 0; }
//---------------------------------------------------// // [9/9/2009 Albert] // Description: 移动到指定位置 //---------------------------------------------------// void CDynamicObject::MoveTo( XVector3 vPosition, float fSpeed, float fDelayTime ) { if( m_hUpdateTimer != INVALID_TIMER_HANDLE ) { ThisTimer()->remove_event( m_hUpdateTimer ); m_hUpdateTimer = INVALID_TIMER_HANDLE; } m_PointList.clear(); PathPoint p = { vPosition, fSpeed }; m_PointList.push_back( p ); SetUpdate( _tfunction( bind( &CDynamicObject::UpdateTimer, this, _1, _2, _3 ) ), 1, TIMER_SECONDS( 1.0f ), TIMER_SECONDS( fDelayTime ) ); }