lever.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "../idlib/precompiled.h"
  2. #pragma hdrstop
  3. #include "Game_local.h"
  4. const idEventDef EV_frobscript( "<frobscript>" );
  5. CLASS_DECLARATION( idAnimated, idLever )
  6. EVENT( EV_frobscript, idLever::OnFrobScript)
  7. END_CLASS
  8. void idLever::Save( idSaveGame *savefile ) const
  9. {
  10. savefile->WriteBool(toggleable);
  11. savefile->WriteInt(toggleState);
  12. savefile->WriteInt(state);
  13. savefile->WriteInt(nextStateTime);
  14. }
  15. void idLever::Restore( idRestoreGame *savefile )
  16. {
  17. savefile->ReadBool(toggleable);
  18. savefile->ReadInt(toggleState);
  19. savefile->ReadInt(state);
  20. savefile->ReadInt(nextStateTime);
  21. }
  22. void idLever::Spawn( void )
  23. {
  24. //make frobbable and no collision.
  25. //this->GetPhysics()->SetContents(CONTENTS_CORPSE);
  26. GetPhysics()->SetContents( CONTENTS_RENDERMODEL );
  27. GetPhysics()->SetClipMask( MASK_SOLID | CONTENTS_MOVEABLECLIP );
  28. this->isFrobbable = true;
  29. this->noGrab = true;
  30. renderEntity.shaderParms[ SHADERPARM_RED ] = 0;
  31. renderEntity.shaderParms[ SHADERPARM_GREEN ] = 1;
  32. renderEntity.shaderParms[ SHADERPARM_BLUE ] = 0;
  33. nextStateTime = 0;
  34. toggleable = spawnArgs.GetBool("toggleable", "0");
  35. toggleState = 0;
  36. state = IDLE;
  37. if (spawnArgs.GetBool("start_on", "0"))
  38. {
  39. toggleState = !toggleState;
  40. }
  41. BecomeActive(TH_THINK);
  42. }
  43. void idLever::Reset( void )
  44. {
  45. if (spawnArgs.GetBool("start_on", "0"))
  46. {
  47. toggleState = 1;
  48. }
  49. else
  50. {
  51. toggleState = 0;
  52. }
  53. }
  54. void idLever::UpdateStates( void )
  55. {
  56. if (state == PRESSED)
  57. {
  58. if (gameLocal.time > nextStateTime)
  59. {
  60. renderEntity.shaderParms[ SHADERPARM_RED ] = 0;
  61. renderEntity.shaderParms[ SHADERPARM_GREEN ] = 1;
  62. renderEntity.shaderParms[ SHADERPARM_BLUE ] = 0;
  63. this->isFrobbable = true;
  64. state = IDLE;
  65. }
  66. }
  67. }
  68. void idLever::OnFrobScript( void )
  69. {
  70. idStr scriptName = spawnArgs.GetString( "call", "" );
  71. if (scriptName.Length() <= 0)
  72. return;
  73. if (toggleable)
  74. {
  75. if (toggleState > 0)
  76. {
  77. scriptName = scriptName + "_on";
  78. }
  79. else
  80. {
  81. scriptName = scriptName + "_off";
  82. }
  83. }
  84. const function_t *scriptFunction;
  85. scriptFunction = gameLocal.program.FindFunction( scriptName );
  86. if ( scriptFunction )
  87. {
  88. idThread *thread;
  89. thread = new idThread( scriptFunction );
  90. thread->DelayedStart( 0 );
  91. }
  92. }
  93. void idLever::OnFrob( idEntity* activator )
  94. {
  95. float waitTime;
  96. if (state == PRESSED)
  97. {
  98. return;
  99. }
  100. state = PRESSED;
  101. StartSound( "snd_press", SND_CHANNEL_BODY, 0, false, NULL );
  102. if (!toggleable)
  103. {
  104. Event_PlayAnim( spawnArgs.GetString("pushanim", "push"), 4 );
  105. }
  106. else
  107. {
  108. if (toggleState)
  109. {
  110. Event_PlayAnim( spawnArgs.GetString("pullanim", "pull"), 4 );
  111. }
  112. else
  113. {
  114. Event_PlayAnim( spawnArgs.GetString("pushanim", "push"), 4 );
  115. }
  116. toggleState = !toggleState;
  117. }
  118. renderEntity.shaderParms[ SHADERPARM_RED ] = 1;
  119. renderEntity.shaderParms[ SHADERPARM_GREEN ] = .75f;
  120. renderEntity.shaderParms[ SHADERPARM_BLUE ] = 0;
  121. waitTime = spawnArgs.GetFloat("animtime", "0.3");
  122. if (waitTime <= 0)
  123. {
  124. waitTime = 0.1f;
  125. }
  126. nextStateTime = gameLocal.time + (waitTime * 1000);
  127. ActivateTargets(this);
  128. this->isFrobbable = false;
  129. float delay = spawnArgs.GetFloat("delay", "0");
  130. PostEventSec( &EV_frobscript, delay );
  131. }
  132. void idLever::Think( void )
  133. {
  134. UpdateStates();
  135. idAnimatedEntity::Think();
  136. idAnimatedEntity::Present();
  137. }