worldman_moveable.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "../idlib/precompiled.h"
  2. #pragma hdrstop
  3. #include "Game_local.h"
  4. const idEventDef EV_wmm_startplay( "worldmoveable_play" );
  5. const idEventDef EV_wmm_startrecord( "worldmoveable_record" );
  6. const idEventDef EV_wmm_stop( "worldmoveable_stop" );
  7. CLASS_DECLARATION( idEntity, idWorldManager_Moveable )
  8. EVENT( EV_wmm_startplay, idWorldManager_Moveable::Event_startplay)
  9. EVENT( EV_wmm_startrecord, idWorldManager_Moveable::Event_startrecord)
  10. EVENT( EV_wmm_stop, idWorldManager_Moveable::Event_stop)
  11. END_CLASS
  12. void idWorldManager_Moveable::Spawn( void )
  13. {
  14. if (spawnArgs.GetString("target0"))
  15. {
  16. targetEnt = gameLocal.FindEntity(spawnArgs.GetString("target0",""));
  17. }
  18. else if (spawnArgs.GetString("target"))
  19. {
  20. targetEnt = gameLocal.FindEntity(spawnArgs.GetString("target",""));
  21. }
  22. if (!targetEnt.IsValid())
  23. {
  24. common->Error("unable to find target ent for worldmanager_moveable: %s", this->GetName());
  25. return;
  26. }
  27. events.Clear();
  28. lastPosition.Zero();
  29. state = OFF;
  30. lastRecordTime = 0;
  31. startTime = 0;
  32. index = 0;
  33. lastTimestamp = 0;
  34. BecomeActive( TH_THINK );
  35. }
  36. void idWorldManager_Moveable::Event_startplay( void )
  37. {
  38. index = 0;
  39. state = PLAYING;
  40. startTime = gameLocal.time;
  41. }
  42. void idWorldManager_Moveable::Event_startrecord( bool continuation )
  43. {
  44. index = 0;
  45. state = RECORDING;
  46. startTime = gameLocal.time;
  47. if (!continuation)
  48. {
  49. events.Clear();
  50. }
  51. if (events.Num() > 0)
  52. {
  53. lastTimestamp = events[events.Num() - 1].timestamp + gameLocal.time;
  54. }
  55. }
  56. void idWorldManager_Moveable::Event_stop( void )
  57. {
  58. index = 0;
  59. state = OFF;
  60. }
  61. void idWorldManager_Moveable::UpdateRecord( void )
  62. {
  63. if (gameLocal.time < lastRecordTime)
  64. {
  65. return;
  66. }
  67. lastRecordTime = gameLocal.time + 200;
  68. idVec3 currentPosition = targetEnt.GetEntity()->GetPhysics()->GetOrigin();
  69. if ((int)lastPosition.x == (int)currentPosition.x && (int)lastPosition.y == (int)currentPosition.y && (int)lastPosition.z == (int)currentPosition.z)
  70. {
  71. return;
  72. }
  73. lastPosition = currentPosition;
  74. record_moveable_t newEvent;
  75. newEvent.timestamp = gameLocal.time - startTime;
  76. newEvent.position = currentPosition;
  77. newEvent.angle = targetEnt.GetEntity()->GetPhysics()->GetAxis();
  78. events.Append( newEvent );
  79. }
  80. void idWorldManager_Moveable::UpdatePlay( void )
  81. {
  82. if (index > events.Num() - 1)
  83. {
  84. // No more events! Get outta here.
  85. return;
  86. }
  87. if (events[index].timestamp > gameLocal.time - startTime)
  88. {
  89. return;
  90. }
  91. // Valid event. Execute it.
  92. targetEnt.GetEntity()->SetOrigin(events[index].position);
  93. targetEnt.GetEntity()->SetAngles(events[index].angle.ToAngles());
  94. index++;
  95. }
  96. void idWorldManager_Moveable::Think( void )
  97. {
  98. if (state == OFF)
  99. {
  100. return;
  101. }
  102. if (state == RECORDING)
  103. {
  104. if (gameLocal.time < lastTimestamp)
  105. {
  106. UpdatePlay();
  107. }
  108. else
  109. {
  110. UpdateRecord();
  111. }
  112. }
  113. else if (state == PLAYING)
  114. {
  115. UpdatePlay();
  116. }
  117. }