Entity.cpp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. #include "core/Entity.h"
  2. #include <OgreSceneNode.h>
  3. #include "core/ConfigVar.h"
  4. #include "core/DebugDraw.h"
  5. #include "core/Logger.h"
  6. ConfigVar cv_debug_entity( "debug_entity", "Draw entity debug info", "0" );
  7. Entity::Entity( const Ogre::String& name, Ogre::SceneNode* node ):
  8. m_Name( name ),
  9. m_SceneNode( node ),
  10. m_Height( 1.0f ),
  11. m_SolidRadius( 0.24f ),
  12. m_Solid( false ),
  13. m_TalkRadius( 0.45f ),
  14. m_Talkable( false ),
  15. m_State( Entity::NONE ),
  16. m_MoveAutoSpeed( 0.7f ),
  17. m_MoveWalkSpeed( 0.7f ),
  18. m_MoveRunSpeed( 0.8f ),
  19. m_MovePosition( Ogre::Vector3( 0, 0, 0 ) ),
  20. m_MoveEntity( NULL ),
  21. m_MoveStopDistance( 0.0f ),
  22. m_MoveTriangleId( -1 ),
  23. m_MoveAutoRotation( true ),
  24. m_MoveAutoAnimation( true ),
  25. m_MoveAnimationWalk( "Walk" ),
  26. m_MoveAnimationRun( "Run" ),
  27. m_LinearMovement( LM_UP_TO_DOWN ),
  28. m_LinearStart( 0.0f, 0.0f, 0.0f ),
  29. m_LinearEnd( 0.0f, 0.0f, 0.0f ),
  30. m_JumpStart( 0.0f, 0.0f, 0.0f ),
  31. m_JumpEnd( 0.0f, 0.0f, 0.0f ),
  32. m_JumpSeconds( 0.0f ),
  33. m_JumpCurrentSeconds( 0.0f ),
  34. m_OffsetPositionStart( 0.0f, 0.0f, 0.0f ),
  35. m_OffsetPositionEnd( 0.0f, 0.0f, 0.0f ),
  36. m_OffsetType( AT_NONE ),
  37. m_OffsetSeconds( 0.0f ),
  38. m_OffsetCurrentSeconds( 0.0f ),
  39. m_TurnDirection( TD_CLOSEST ),
  40. m_TurnDirectionStart( 0 ),
  41. m_TurnDirectionEnd( 0 ),
  42. m_TurnEntity( NULL ),
  43. m_TurnType( AT_NONE ),
  44. m_TurnSeconds( 0.0f ),
  45. m_TurnCurrentSeconds( 0.0f ),
  46. m_AnimationSpeed( 1.0f ),
  47. m_AnimationDefault( "Idle" ),
  48. m_AnimationCurrentName( "" ),
  49. m_AnimationAutoPlay( true )
  50. {
  51. m_model_root_node = m_SceneNode->createChildSceneNode();
  52. m_ModelNode = m_model_root_node->createChildSceneNode();
  53. m_Direction = new EntityDirection();
  54. m_Direction->setMaterial( "entity/direction" );
  55. m_DirectionNode = m_SceneNode->createChildSceneNode();
  56. m_DirectionNode->attachObject( m_Direction );
  57. m_SolidCollision = new EntityCollision();
  58. m_SolidCollision->setMaterial( "entity/solid_collision" );
  59. m_SolidCollisionNode = m_SceneNode->createChildSceneNode();
  60. m_SolidCollisionNode->setScale( m_SolidRadius, m_SolidRadius, m_Height );
  61. m_SolidCollisionNode->attachObject( m_SolidCollision );
  62. m_TalkCollision = new EntityCollision();
  63. m_TalkCollision->setMaterial( "entity/talk_collision" );
  64. m_TalkCollisionNode = m_SceneNode->createChildSceneNode();
  65. m_TalkCollisionNode->setScale( m_TalkRadius, m_TalkRadius, m_Height );
  66. m_TalkCollisionNode->attachObject( m_TalkCollision );
  67. m_model_root_node->setPosition( Ogre::Vector3::ZERO );
  68. m_ModelNode->setPosition( Ogre::Vector3::ZERO );
  69. m_SceneNode->setPosition( Ogre::Vector3::ZERO );
  70. LOG_TRIVIAL( "Entity \"" + m_Name + "\" created." );
  71. }
  72. Entity::~Entity()
  73. {
  74. delete m_SolidCollision;
  75. delete m_TalkCollision;
  76. m_SceneNode->removeAndDestroyAllChildren();
  77. LOG_TRIVIAL( "Entity \"" + m_Name + "\" destroyed." );
  78. }
  79. void
  80. Entity::Update()
  81. {
  82. if( m_MoveEntity != NULL )
  83. {
  84. m_MovePosition = m_MoveEntity->GetPosition();
  85. m_MoveStopDistance = GetSolidRadius() + m_MoveEntity->GetSolidRadius();
  86. }
  87. if( m_TurnEntity != NULL )
  88. {
  89. Ogre::Degree angle = GetDirectionToEntity( m_TurnEntity );
  90. angle = CalculateTurnAngle( m_TurnDirectionStart, angle );
  91. m_TurnDirectionEnd = angle;
  92. }
  93. }
  94. void
  95. Entity::UpdateDebug()
  96. {
  97. int debug = cv_debug_entity.GetI();
  98. m_DirectionNode->setVisible( debug > 0 );
  99. m_SolidCollisionNode->setVisible( debug > 0 && m_Solid );
  100. m_TalkCollisionNode->setVisible( debug > 0 && m_Talkable );
  101. // debug output
  102. if( debug > 0 )
  103. {
  104. DEBUG_DRAW.SetColour( Ogre::ColourValue::White );
  105. DEBUG_DRAW.SetScreenSpace( true );
  106. DEBUG_DRAW.SetTextAlignment( DEBUG_DRAW.CENTER );
  107. DEBUG_DRAW.SetFadeDistance( 40, 50 );
  108. Ogre::Vector3 entity_pos = GetPosition();
  109. DEBUG_DRAW.Text( entity_pos, 0, 0, m_Name );
  110. DEBUG_DRAW.Text( entity_pos, 0, 12, m_AnimationCurrentName );
  111. if( debug > 1 )
  112. {
  113. static Ogre::String move_state_string[] = { "NONE", "WALKMESH", "LINEAR", "JUMP" };
  114. DEBUG_DRAW.Text( entity_pos, 0, 24, Ogre::StringConverter::toString( entity_pos ) );
  115. DEBUG_DRAW.Text( entity_pos, 0, 36, "Triangle: " + Ogre::StringConverter::toString( m_MoveTriangleId ) );
  116. DEBUG_DRAW.Text( entity_pos, 0, 48, "Move state: " + move_state_string[ m_State ] );
  117. switch( m_State )
  118. {
  119. case Entity::WALKMESH:
  120. {
  121. DEBUG_DRAW.Line3d( entity_pos, m_MovePosition );
  122. }
  123. break;
  124. case Entity::LINEAR:
  125. {
  126. DEBUG_DRAW.Text( m_LinearStart, 0, 0, "Start: " + Ogre::StringConverter::toString( m_LinearStart ) );
  127. DEBUG_DRAW.Text( m_LinearEnd, 0, 0, "End: " + Ogre::StringConverter::toString( m_LinearEnd ) );
  128. DEBUG_DRAW.Line3d( m_LinearStart, m_LinearEnd );
  129. }
  130. break;
  131. case Entity::JUMP:
  132. {
  133. DEBUG_DRAW.Text( m_JumpStart, 0, 0, "Start: " + Ogre::StringConverter::toString( m_JumpStart ) );
  134. DEBUG_DRAW.Text( m_JumpEnd, 0, 0, "End: " + Ogre::StringConverter::toString( m_JumpEnd ) );
  135. Ogre::Vector3 distance = m_JumpEnd - m_JumpStart;
  136. Ogre::Vector3 pos1, pos2, pos3;
  137. pos1.x = m_JumpStart.x + distance.x * 0.25f;
  138. pos1.y = m_JumpStart.y + distance.y * 0.25f;
  139. float current1 = m_JumpSeconds / 4;
  140. pos1.z = current1 * current1 * -13.08f + current1 * ( ( distance.z ) / m_JumpSeconds + m_JumpSeconds * 13.08f ) + m_JumpStart.z;
  141. pos2.x = m_JumpStart.x + distance.x * 0.5f;
  142. pos2.y = m_JumpStart.y + distance.y * 0.5f;
  143. float current2 = current1 * 2;
  144. pos2.z = current2 * current2 * -13.08f + current2 * ( ( distance.z ) / m_JumpSeconds + m_JumpSeconds * 13.08f ) + m_JumpStart.z;
  145. pos3.x = m_JumpStart.x + distance.x * 0.75f;
  146. pos3.y = m_JumpStart.y + distance.y * 0.75f;
  147. float current3 = current1 * 3;
  148. pos3.z = current3 * current3 * -13.08f + current3 * ( ( distance.z ) / m_JumpSeconds + m_JumpSeconds * 13.08f ) + m_JumpStart.z;
  149. DEBUG_DRAW.Line3d( m_JumpStart, pos1 );
  150. DEBUG_DRAW.Line3d( pos1, pos2 );
  151. DEBUG_DRAW.Line3d( pos2, pos3 );
  152. DEBUG_DRAW.Line3d( pos3, m_JumpEnd );
  153. }
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. const Ogre::String&
  160. Entity::GetName() const
  161. {
  162. return m_Name;
  163. }
  164. void
  165. Entity::SetPosition( const Ogre::Vector3& position )
  166. {
  167. m_SceneNode->setPosition( position );
  168. }
  169. void
  170. Entity::ScriptSetPosition( const float x, const float y, const float z )
  171. {
  172. SetPosition( Ogre::Vector3( x, y, z ) );
  173. // if we set it from script - reset walkmesh triangle to reattach entity to walkmesh again if needed
  174. m_MoveTriangleId = -1;
  175. }
  176. const Ogre::Vector3
  177. Entity::GetPosition() const
  178. {
  179. return m_SceneNode->getPosition();
  180. }
  181. void
  182. Entity::ScriptGetPosition() const
  183. {
  184. Ogre::Vector3 position = m_SceneNode->getPosition();
  185. ScriptManager::getSingleton().AddValueToStack( position.x );
  186. ScriptManager::getSingleton().AddValueToStack( position.y );
  187. ScriptManager::getSingleton().AddValueToStack( position.z );
  188. }
  189. void
  190. Entity::SetOffset( const Ogre::Vector3& position )
  191. {
  192. assert( m_model_root_node );
  193. m_model_root_node->setPosition( position );
  194. }
  195. const Ogre::Vector3
  196. Entity::GetOffset() const
  197. {
  198. assert( m_model_root_node );
  199. return m_model_root_node->getPosition();
  200. }
  201. void
  202. Entity::SetRotation( const Ogre::Degree& rotation )
  203. {
  204. assert( m_model_root_node );
  205. float angle = rotation.valueDegrees() - Ogre::Math::Floor( rotation.valueDegrees() / 360.0f ) * 360.0f;
  206. if( angle < 0 )
  207. {
  208. angle = 360 + angle;
  209. }
  210. Ogre::Quaternion q;
  211. Ogre::Vector3 vec = Ogre::Vector3::UNIT_Z;
  212. q.FromAngleAxis( Ogre::Radian( Ogre::Degree( angle ) ), vec );
  213. m_model_root_node->setOrientation( q );
  214. m_DirectionNode->setOrientation( q );
  215. }
  216. void
  217. Entity::ScriptSetRotation( const float rotation )
  218. {
  219. SetRotation( Ogre::Degree( rotation ) );
  220. }
  221. Ogre::Degree
  222. Entity::GetRotation() const
  223. {
  224. assert( m_model_root_node );
  225. Ogre::Quaternion q = m_model_root_node->getOrientation();
  226. Ogre::Degree temp;
  227. Ogre::Vector3 vec = Ogre::Vector3::UNIT_Z;
  228. q.ToAngleAxis( temp, vec );
  229. return temp;
  230. }
  231. float
  232. Entity::ScriptGetRotation() const
  233. {
  234. return GetRotation().valueDegrees();
  235. }
  236. //--------------------------------------------------------------------------
  237. void Entity::setScale(const Ogre::Vector3 &scale)
  238. {
  239. assert(m_model_root_node);
  240. m_model_root_node->setScale( scale );
  241. }
  242. //--------------------------------------------------------------------------
  243. void Entity::setRootOrientation(const Ogre::Quaternion &root_orientation)
  244. {
  245. assert(m_ModelNode);
  246. m_ModelNode->setOrientation( root_orientation );
  247. }
  248. //--------------------------------------------------------------------------
  249. float
  250. Entity::GetHeight() const
  251. {
  252. return m_Height;
  253. }
  254. void
  255. Entity::SetSolidRadius( const float radius )
  256. {
  257. m_SolidRadius = radius;
  258. m_SolidCollisionNode->setScale( m_SolidRadius, m_SolidRadius, m_Height );
  259. }
  260. float
  261. Entity::GetSolidRadius() const
  262. {
  263. return m_SolidRadius;
  264. }
  265. void
  266. Entity::SetSolid( const bool solid )
  267. {
  268. m_Solid = solid;
  269. }
  270. bool
  271. Entity::IsSolid() const
  272. {
  273. return m_Solid;
  274. }
  275. void
  276. Entity::SetTalkRadius( const float radius )
  277. {
  278. m_TalkRadius = radius;
  279. m_TalkCollisionNode->setScale( m_TalkRadius, m_TalkRadius, m_Height );
  280. }
  281. float
  282. Entity::GetTalkRadius() const
  283. {
  284. return m_TalkRadius;
  285. }
  286. void
  287. Entity::SetTalkable( const bool talkable )
  288. {
  289. m_Talkable = talkable;
  290. }
  291. bool
  292. Entity::IsTalkable() const
  293. {
  294. return m_Talkable;
  295. }
  296. void
  297. Entity::SetState( const Entity::State state )
  298. {
  299. m_State = state;
  300. }
  301. Entity::State
  302. Entity::GetState() const
  303. {
  304. return m_State;
  305. }
  306. void
  307. Entity::SetMoveAutoSpeed( const float speed )
  308. {
  309. m_MoveAutoSpeed = speed;
  310. }
  311. float
  312. Entity::GetMoveAutoSpeed() const
  313. {
  314. return m_MoveAutoSpeed;
  315. }
  316. void
  317. Entity::SetMoveWalkSpeed( const float speed )
  318. {
  319. m_MoveWalkSpeed = speed;
  320. }
  321. float
  322. Entity::GetMoveWalkSpeed() const
  323. {
  324. return m_MoveWalkSpeed;
  325. }
  326. void
  327. Entity::SetMoveRunSpeed( const float speed )
  328. {
  329. m_MoveRunSpeed = speed;
  330. }
  331. float
  332. Entity::GetMoveRunSpeed() const
  333. {
  334. return m_MoveRunSpeed;
  335. }
  336. void
  337. Entity::SetMovePosition( const Ogre::Vector3& target )
  338. {
  339. m_MovePosition = target;
  340. }
  341. const Ogre::Vector3&
  342. Entity::GetMovePosition() const
  343. {
  344. return m_MovePosition;
  345. }
  346. float
  347. Entity::GetMoveStopDistance() const
  348. {
  349. return m_MoveStopDistance;
  350. }
  351. void
  352. Entity::SetMoveTriangleId( const int triangle )
  353. {
  354. m_MoveTriangleId = triangle;
  355. }
  356. int
  357. Entity::GetMoveTriangleId() const
  358. {
  359. return m_MoveTriangleId;
  360. }
  361. void
  362. Entity::SetMoveAutoRotation( const bool rotate )
  363. {
  364. m_MoveAutoRotation = rotate;
  365. }
  366. bool
  367. Entity::GetMoveAutoRotation() const
  368. {
  369. return m_MoveAutoRotation;
  370. }
  371. void
  372. Entity::SetMoveAutoAnimation( const bool animate )
  373. {
  374. m_MoveAutoAnimation = animate;
  375. }
  376. bool
  377. Entity::GetMoveAutoAnimation() const
  378. {
  379. return m_MoveAutoAnimation;
  380. }
  381. const Ogre::String&
  382. Entity::GetMoveAnimationWalkName() const
  383. {
  384. return m_MoveAnimationWalk;
  385. }
  386. const Ogre::String&
  387. Entity::GetMoveAnimationRunName() const
  388. {
  389. return m_MoveAnimationRun;
  390. }
  391. void
  392. Entity::ScriptMoveToPosition( const float x, const float y )
  393. {
  394. m_State = Entity::WALKMESH;
  395. m_MovePosition = Ogre::Vector3( x, y, 0 );
  396. m_MoveEntity = NULL;
  397. m_MoveStopDistance = 0;
  398. LOG_TRIVIAL( "[SCRIPT] Entity \"" + m_Name + "\" set move to walkmesh position \"" + Ogre::StringConverter::toString( m_MovePosition ) + "\"." );
  399. }
  400. void
  401. Entity::ScriptMoveToEntity( Entity* entity )
  402. {
  403. m_State = Entity::WALKMESH;
  404. m_MovePosition = entity->GetPosition();
  405. m_MoveEntity = entity;
  406. m_MoveStopDistance = GetSolidRadius() + entity->GetSolidRadius();
  407. LOG_TRIVIAL( "[SCRIPT] Entity \"" + m_Name + "\" set move to entity \"" + entity->GetName() + "\"." );
  408. }
  409. int
  410. Entity::ScriptMoveSync()
  411. {
  412. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  413. LOG_TRIVIAL( "[SCRIPT] Wait entity \"" + m_Name + "\" move for function \"" + script.function + "\" in script entity \"" + script.entity + "\"." );
  414. m_Sync.push_back( script );
  415. return -1;
  416. }
  417. void
  418. Entity::UnsetMove()
  419. {
  420. m_State = Entity::NONE;
  421. m_MoveEntity = NULL;
  422. m_MoveStopDistance = 0;
  423. for( size_t i = 0; i < m_Sync.size(); ++i)
  424. {
  425. ScriptManager::getSingleton().ContinueScriptExecution( m_Sync[ i ] );
  426. }
  427. m_Sync.clear();
  428. }
  429. void
  430. Entity::ScriptLinearToPosition( const float x, const float y, const float z, const LinearMovement movement, const char* animation )
  431. {
  432. Ogre::Vector3 pos = Ogre::Vector3( x, y, z );
  433. SetLinear( pos, movement, animation );
  434. LOG_TRIVIAL( "[SCRIPT] Entity \"" + m_Name + "\" set linear move to position \"" + Ogre::StringConverter::toString( pos ) + "\" with animation \"" + animation + "\"." );
  435. }
  436. int
  437. Entity::ScriptLinearSync()
  438. {
  439. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  440. LOG_TRIVIAL( "[SCRIPT] Wait entity \"" + m_Name + "\" linear move for function \"" + script.function + "\" in script entity \"" + script.entity + "\"." );
  441. m_Sync.push_back( script );
  442. return -1;
  443. }
  444. void
  445. Entity::SetLinear( const Ogre::Vector3& end, const LinearMovement movement, const Ogre::String& animation )
  446. {
  447. m_State = Entity::LINEAR;
  448. m_LinearMovement = movement;
  449. m_LinearStart = GetPosition();
  450. m_LinearEnd = end;
  451. // linear animation
  452. m_AnimationAutoPlay = false;
  453. PlayAnimation( animation, Entity::AUTO_ANIMATION, Entity::PLAY_LOOPED, 0, -1 );
  454. // after move we need reattach entity to walkmesh
  455. m_MoveTriangleId = -1;
  456. }
  457. void
  458. Entity::UnsetLinear()
  459. {
  460. m_State = Entity::NONE;
  461. m_AnimationAutoPlay = true;
  462. PlayAnimation( m_AnimationDefault, Entity::AUTO_ANIMATION, Entity::PLAY_LOOPED, 0, -1 );
  463. for( size_t i = 0; i < m_Sync.size(); ++i)
  464. {
  465. ScriptManager::getSingleton().ContinueScriptExecution( m_Sync[ i ] );
  466. }
  467. m_Sync.clear();
  468. }
  469. LinearMovement
  470. Entity::GetLinearMovement() const
  471. {
  472. return m_LinearMovement;
  473. }
  474. const Ogre::Vector3&
  475. Entity::GetLinearStart() const
  476. {
  477. return m_LinearStart;
  478. }
  479. const Ogre::Vector3&
  480. Entity::GetLinearEnd() const
  481. {
  482. return m_LinearEnd;
  483. }
  484. void
  485. Entity::ScriptJumpToPosition( const float x, const float y, const float z, const float seconds )
  486. {
  487. Ogre::Vector3 jump_to( x, y, z );
  488. SetJump( jump_to, seconds );
  489. LOG_TRIVIAL( "[SCRIPT] Entity \"" + m_Name + "\" set jump to position \"" + Ogre::StringConverter::toString( jump_to ) + "\" in " + Ogre::StringConverter::toString( seconds ) + " seconds." );
  490. }
  491. int
  492. Entity::ScriptJumpSync()
  493. {
  494. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  495. LOG_TRIVIAL( "[SCRIPT] Wait entity \"" + m_Name + "\" jump for function \"" + script.function + "\" in script entity \"" + script.entity + "\"." );
  496. m_Sync.push_back( script );
  497. return -1;
  498. }
  499. void
  500. Entity::SetJump( const Ogre::Vector3& jump_to, const float seconds )
  501. {
  502. m_State = Entity::JUMP;
  503. m_JumpStart = GetPosition();
  504. m_JumpEnd = jump_to;
  505. m_JumpSeconds = seconds;
  506. m_JumpCurrentSeconds = 0;
  507. // after move we need reattach entity to walkmesh
  508. m_MoveTriangleId = -1;
  509. }
  510. void
  511. Entity::UnsetJump()
  512. {
  513. m_State = Entity::NONE;
  514. for( size_t i = 0; i < m_Sync.size(); ++i)
  515. {
  516. ScriptManager::getSingleton().ContinueScriptExecution( m_Sync[ i ] );
  517. }
  518. m_Sync.clear();
  519. }
  520. const Ogre::Vector3&
  521. Entity::GetJumpStart() const
  522. {
  523. return m_JumpStart;
  524. }
  525. const Ogre::Vector3&
  526. Entity::GetJumpEnd() const
  527. {
  528. return m_JumpEnd;
  529. }
  530. float
  531. Entity::GetJumpSeconds() const
  532. {
  533. return m_JumpSeconds;
  534. }
  535. void
  536. Entity::SetJumpCurrentSeconds( const float seconds )
  537. {
  538. m_JumpCurrentSeconds = seconds;
  539. }
  540. float
  541. Entity::GetJumpCurrentSeconds() const
  542. {
  543. return m_JumpCurrentSeconds;
  544. }
  545. void
  546. Entity::ScriptOffsetToPosition( const float x, const float y, const float z, const ActionType type, const float seconds )
  547. {
  548. LOG_TRIVIAL( "[SCRIPT] Entity \"" + m_Name + "\" set offset to position \"" + Ogre::StringConverter::toString( Ogre::Vector3( x, y, z ) ) + "'." );
  549. Ogre::Vector3 position = Ogre::Vector3( x, y, z );
  550. if( type == AT_NONE )
  551. {
  552. this->SetOffset( position );
  553. return;
  554. }
  555. m_OffsetPositionStart = GetOffset();
  556. m_OffsetPositionEnd = position;
  557. m_OffsetType = type;
  558. m_OffsetSeconds = seconds;
  559. m_OffsetCurrentSeconds = 0;
  560. }
  561. int
  562. Entity::ScriptOffsetSync()
  563. {
  564. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  565. LOG_TRIVIAL( "[SCRIPT] Wait entity \"" + m_Name + "\" offset for function \"" + script.function + "\" in script entity \"" + script.entity + "\"." );
  566. m_OffsetSync.push_back( script );
  567. return -1;
  568. }
  569. void
  570. Entity::UnsetOffset()
  571. {
  572. m_OffsetType = AT_NONE;
  573. for( unsigned int i = 0; i < m_OffsetSync.size(); ++i )
  574. {
  575. ScriptManager::getSingleton().ContinueScriptExecution( m_OffsetSync[ i ] );
  576. }
  577. m_OffsetSync.clear();
  578. }
  579. const Ogre::Vector3&
  580. Entity::GetOffsetPositionStart() const
  581. {
  582. return m_OffsetPositionStart;
  583. }
  584. const Ogre::Vector3&
  585. Entity::GetOffsetPositionEnd() const
  586. {
  587. return m_OffsetPositionEnd;
  588. }
  589. ActionType
  590. Entity::GetOffsetType() const
  591. {
  592. return m_OffsetType;
  593. }
  594. float
  595. Entity::GetOffsetSeconds() const
  596. {
  597. return m_OffsetSeconds;
  598. }
  599. void
  600. Entity::SetOffsetCurrentSeconds( const float seconds )
  601. {
  602. m_OffsetCurrentSeconds = seconds;
  603. }
  604. float
  605. Entity::GetOffsetCurrentSeconds() const
  606. {
  607. return m_OffsetCurrentSeconds;
  608. }
  609. void
  610. Entity::ScriptTurnToDirection( const float direction, const TurnDirection turn_direction, const ActionType turn_type, const float seconds )
  611. {
  612. SetTurn( Ogre::Degree( direction ), NULL, turn_direction, turn_type, seconds );
  613. LOG_TRIVIAL("[SCRIPT] Entity \"" + m_Name + "\" turn to angle \"" + Ogre::StringConverter::toString( direction ) + "\".");
  614. }
  615. void
  616. Entity::ScriptTurnToEntity( Entity* entity, const TurnDirection turn_direction, const float seconds )
  617. {
  618. if( entity == NULL || entity == this )
  619. {
  620. LOG_ERROR("[SCRIPT] Turn to entity: Invalid entity pointer (NUUL or this).");
  621. return;
  622. }
  623. Ogre::Degree angle = GetDirectionToEntity( entity );
  624. SetTurn( angle, entity, turn_direction, AT_SMOOTH, seconds );
  625. LOG_TRIVIAL( "[SCRIPT] Entity \"" + m_Name + "\" turn to entity \"" + entity->GetName() + ".");
  626. }
  627. int
  628. Entity::ScriptTurnSync()
  629. {
  630. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  631. LOG_TRIVIAL( "[SCRIPT] Wait entity \"" + m_Name + "\" turn for function \"" + script.function + "\" in script entity \"" + script.entity + "\"." );
  632. m_TurnSync.push_back( script );
  633. return -1;
  634. }
  635. void
  636. Entity::SetTurn( const Ogre::Degree& direction_to, Entity* entity, const TurnDirection turn_direction, const ActionType turn_type, const float seconds )
  637. {
  638. if( turn_type == AT_NONE )
  639. {
  640. SetRotation( direction_to );
  641. return;
  642. }
  643. m_TurnDirection = turn_direction;
  644. Ogre::Degree angle_start = GetRotation();
  645. Ogre::Degree angle_end = CalculateTurnAngle( angle_start, direction_to );
  646. m_TurnEntity = entity;
  647. m_TurnDirectionStart = angle_start;
  648. m_TurnDirectionEnd = angle_end;
  649. m_TurnType = turn_type;
  650. m_TurnSeconds = seconds;
  651. m_TurnCurrentSeconds = 0;
  652. }
  653. void
  654. Entity::UnsetTurn()
  655. {
  656. m_TurnType = AT_NONE;
  657. for( unsigned int i = 0; i < m_TurnSync.size(); ++i )
  658. {
  659. ScriptManager::getSingleton().ContinueScriptExecution( m_TurnSync[ i ] );
  660. }
  661. m_TurnSync.clear();
  662. }
  663. Ogre::Degree
  664. Entity::CalculateTurnAngle( const Ogre::Degree& start, const Ogre::Degree& end ) const
  665. {
  666. Ogre::Degree ret = end;
  667. switch( m_TurnDirection )
  668. {
  669. case TD_CLOCKWISE:
  670. {
  671. if( end <= start )
  672. {
  673. ret = end + Ogre::Degree( 360 );
  674. }
  675. }
  676. break;
  677. case TD_ANTICLOCKWISE:
  678. {
  679. if( end >= start )
  680. {
  681. ret = end - Ogre::Degree( 360 );
  682. }
  683. }
  684. break;
  685. case TD_CLOSEST:
  686. {
  687. Ogre::Degree delta = end - start;
  688. delta = ( delta < Ogre::Degree( 0 ) ) ? -delta : delta;
  689. if( delta > Ogre::Degree( 180 ) )
  690. {
  691. if( start < end )
  692. {
  693. ret = end - Ogre::Degree( 360 );
  694. }
  695. else
  696. {
  697. ret = end + Ogre::Degree( 360 );
  698. }
  699. }
  700. }
  701. break;
  702. }
  703. return ret;
  704. }
  705. Ogre::Degree
  706. Entity::GetTurnDirectionStart() const
  707. {
  708. return m_TurnDirectionStart;
  709. }
  710. Ogre::Degree
  711. Entity::GetTurnDirectionEnd() const
  712. {
  713. return m_TurnDirectionEnd;
  714. }
  715. ActionType
  716. Entity::GetTurnType() const
  717. {
  718. return m_TurnType;
  719. }
  720. float
  721. Entity::GetTurnSeconds() const
  722. {
  723. return m_TurnSeconds;
  724. }
  725. void
  726. Entity::SetTurnCurrentSeconds( const float seconds )
  727. {
  728. m_TurnCurrentSeconds = seconds;
  729. }
  730. float
  731. Entity::GetTurnCurrentSeconds() const
  732. {
  733. return m_TurnCurrentSeconds;
  734. }
  735. void
  736. Entity::ScriptSetAnimationSpeed( const float speed )
  737. {
  738. m_AnimationSpeed = speed;
  739. }
  740. const Ogre::String&
  741. Entity::GetDefaultAnimationName() const
  742. {
  743. return m_AnimationDefault;
  744. }
  745. const Ogre::String&
  746. Entity::GetCurrentAnimationName() const
  747. {
  748. return m_AnimationCurrentName;
  749. }
  750. Entity::AnimationState
  751. Entity::GetAnimationState() const
  752. {
  753. return m_AnimationState;
  754. }
  755. void
  756. Entity::ScriptPlayAnimation( const char* name )
  757. {
  758. PlayAnimation( Ogre::String( name ), Entity::REQUESTED_ANIMATION, Entity::PLAY_DEFAULT, 0, -1 );
  759. }
  760. void
  761. Entity::ScriptPlayAnimationStop( const char* name )
  762. {
  763. PlayAnimation( Ogre::String( name ), Entity::REQUESTED_ANIMATION, Entity::PLAY_ONCE, 0, -1 );
  764. }
  765. void
  766. Entity::ScriptPlayAnimation( const char* name, const float start, const float end )
  767. {
  768. PlayAnimation( Ogre::String( name ), Entity::REQUESTED_ANIMATION, Entity::PLAY_DEFAULT, start, end );
  769. }
  770. void
  771. Entity::ScriptPlayAnimationStop( const char* name, const float start, const float end )
  772. {
  773. PlayAnimation( Ogre::String( name ), Entity::REQUESTED_ANIMATION, Entity::PLAY_ONCE, start, end );
  774. }
  775. void
  776. Entity::ScriptSetDefaultAnimation( const char* animation )
  777. {
  778. m_AnimationDefault = Ogre::String( animation );
  779. }
  780. int
  781. Entity::ScriptAnimationSync()
  782. {
  783. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  784. LOG_TRIVIAL( "[SCRIPT] Wait entity \"" + m_Name + "\" animation for function \"" + script.function + "\" in script entity \"" + script.entity + "\"." );
  785. m_AnimationSync.push_back( script );
  786. return -1;
  787. }
  788. Ogre::Degree
  789. Entity::GetDirectionToEntity( Entity* entity ) const
  790. {
  791. Ogre::Vector3 current_point = GetPosition();
  792. Ogre::Vector3 direction_point = entity->GetPosition();
  793. Ogre::Vector2 up(0.0f, -1.0f);
  794. Ogre::Vector2 dir( direction_point.x - current_point.x, direction_point.y - current_point.y );
  795. // angle between vectors
  796. Ogre::Degree angle( Ogre::Radian( acosf( dir.dotProduct( up ) / ( dir.length() * up.length() ) ) ) );
  797. return ( dir.x < 0 ) ? Ogre::Degree( 360 ) - angle : angle;
  798. }