rotdoor.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //Rotating door.
  2. //Based on The Dark Mod's CBinaryFrobMover.
  3. #include "../idlib/precompiled.h"
  4. #pragma hdrstop
  5. #include "Game_local.h"
  6. const idEventDef EV_rotdoor_open( "open", NULL );
  7. const idEventDef EV_rotdoor_close( "close", NULL );
  8. const idEventDef EV_rotdoor_isOpen( "isOpen", NULL, 'f' );
  9. CLASS_DECLARATION( idMover, idRotDoor )
  10. EVENT( EV_rotdoor_open, idRotDoor::Event_Open )
  11. EVENT( EV_rotdoor_close, idRotDoor::Event_Close )
  12. EVENT( EV_rotdoor_isOpen, idRotDoor::Event_isOpen )
  13. END_CLASS
  14. void idRotDoor::Save( idSaveGame *savefile ) const
  15. {
  16. savefile->WriteBool(shouldOpen);
  17. savefile->WriteAngles(m_Rotate);
  18. savefile->WriteVec3(m_ClosedPos);
  19. savefile->WriteVec3(m_OpenPos);
  20. savefile->WriteVec3(m_OpenDir);
  21. savefile->WriteAngles(m_ClosedAngles);
  22. savefile->WriteAngles(m_OpenAngles);
  23. }
  24. void idRotDoor::Restore( idRestoreGame *savefile )
  25. {
  26. savefile->ReadBool(shouldOpen);
  27. savefile->ReadAngles(m_Rotate);
  28. savefile->ReadVec3(m_ClosedPos);
  29. savefile->ReadVec3(m_OpenPos);
  30. savefile->ReadVec3(m_OpenDir);
  31. savefile->ReadAngles(m_ClosedAngles);
  32. savefile->ReadAngles(m_OpenAngles);
  33. }
  34. void idRotDoor::Spawn( void )
  35. {
  36. m_Rotate = spawnArgs.GetAngles("rotate", "0 90 0");
  37. m_ClosedAngles = physicsObj.GetLocalAngles();
  38. m_ClosedAngles.Normalize180();
  39. m_OpenAngles = (m_ClosedAngles + m_Rotate).Normalize180();
  40. idRotation rot = m_Rotate.ToRotation();
  41. idVec3 rotationAxis = rot.GetVec();
  42. idVec3 normal = rotationAxis.Cross(m_ClosedPos);
  43. m_OpenDir = (m_OpenPos * normal) * normal;
  44. m_OpenDir.Normalize();
  45. this->shouldOpen = true;
  46. }
  47. void idRotDoor::Event_isOpen( void )
  48. {
  49. idThread::ReturnInt( IsOpen() );
  50. }
  51. bool idRotDoor::IsOpen( void )
  52. {
  53. return ( !this->shouldOpen );
  54. }
  55. void idRotDoor::Event_Activate( idEntity *activator )
  56. {
  57. idAngles targetAngles = this->shouldOpen ? m_OpenAngles : m_ClosedAngles;
  58. idAngles angleDelta = (targetAngles - physicsObj.GetLocalAngles()).Normalize180();
  59. if (!angleDelta.Compare(idAngles(0,0,0)))
  60. {
  61. Event_RotateOnce(angleDelta);
  62. if (this->shouldOpen)
  63. {
  64. StartSound( "snd_open", SND_CHANNEL_ANY, 0, false, NULL );
  65. Signal( SIG_MOVER_1TO2 );
  66. }
  67. else
  68. StartSound( "snd_close", SND_CHANNEL_ANY, 0, false, NULL );
  69. }
  70. this->shouldOpen = !this->shouldOpen;
  71. }
  72. void idRotDoor::Event_Open( void )
  73. {
  74. this->shouldOpen = true;
  75. this->Event_Activate( NULL );
  76. }
  77. void idRotDoor::Event_Close( void )
  78. {
  79. this->shouldOpen = false;
  80. this->Event_Activate( NULL );
  81. }