trembler.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "../idlib/precompiled.h"
  2. #pragma hdrstop
  3. #include "Game_local.h"
  4. #define HEIGHT_MAX 32
  5. #define FLYRADIUS 45
  6. const idEventDef EV_TremblerReset( "tremblerreset" );
  7. const idEventDef EV_TremblerArm( "<TremblerArm>" );
  8. CLASS_DECLARATION( idAnimatedEntity, idTrembler )
  9. EVENT( EV_Touch, idTrembler::Event_Touch )
  10. EVENT( EV_TremblerReset, idTrembler::Reset)
  11. EVENT( EV_TremblerArm, idTrembler::Arm)
  12. END_CLASS
  13. void idTrembler::Save( idSaveGame *savefile ) const
  14. {
  15. savefile->WriteInt(state);
  16. savefile->WriteVec3(itemOriginalPosition);
  17. itemEnt.Save(savefile);
  18. savefile->WriteInt(ticksoundTime);
  19. }
  20. void idTrembler::Restore( idRestoreGame *savefile )
  21. {
  22. savefile->ReadInt(state);
  23. savefile->ReadVec3(itemOriginalPosition);
  24. itemEnt.Restore(savefile);
  25. savefile->ReadInt(ticksoundTime);
  26. }
  27. //state
  28. //-1 = waiting for item to settle.
  29. // 0 = armed.
  30. // 1 = alarm tripped.
  31. void idTrembler::Spawn( void )
  32. {
  33. itemEnt = NULL;
  34. itemOriginalPosition = idVec3(0,0,0);
  35. state = -1;
  36. ticksoundTime = 0;
  37. Reset();
  38. BecomeActive( TH_THINK );
  39. }
  40. void idTrembler::Reset( void )
  41. {
  42. StopSound( SND_CHANNEL_ANY, false );
  43. renderEntity.shaderParms[ SHADERPARM_RED ] = 0;
  44. renderEntity.shaderParms[ SHADERPARM_GREEN ] = 0.7f;
  45. renderEntity.shaderParms[ SHADERPARM_BLUE ] = 1;
  46. UpdateVisuals();
  47. state = -1;
  48. int resettime = spawnArgs.GetInt("armtime");
  49. Event_PlayAnim("idle", 1);
  50. PostEventMS( &EV_TremblerArm, resettime ); //give a little delay to let physics settle the objects down.
  51. }
  52. void idTrembler::Arm( void )
  53. {
  54. if ( !itemEnt.IsValid() )
  55. {
  56. idStr itemName = spawnArgs.GetString( "itemname", "" );
  57. if ( itemName.Length() )
  58. {
  59. itemEnt = gameLocal.FindEntity( itemName );
  60. if (!itemEnt.IsValid())
  61. {
  62. common->Warning("idTrembler %s couldn't find item '%s'.", name.c_str(), itemName.c_str());
  63. }
  64. }
  65. else
  66. {
  67. common->Warning("idTrembler %s has no 'itemname' value.", name.c_str());
  68. }
  69. }
  70. if (itemEnt.IsValid())
  71. {
  72. itemOriginalPosition = itemEnt.GetEntity()->GetPhysics()->GetOrigin();
  73. }
  74. state = 0;
  75. renderEntity.shaderParms[ SHADERPARM_RED ] = 1;
  76. renderEntity.shaderParms[ SHADERPARM_GREEN ] = 1;
  77. renderEntity.shaderParms[ SHADERPARM_BLUE ] = 0;
  78. UpdateVisuals();
  79. }
  80. void idTrembler::Think( void )
  81. {
  82. if (state == 0 && itemEnt.IsValid())
  83. {
  84. idVec3 currentPosition = itemEnt.GetEntity()->GetPhysics()->GetOrigin();
  85. if (currentPosition.x != itemOriginalPosition.x || currentPosition.y != itemOriginalPosition.y
  86. || currentPosition.z != itemOriginalPosition.z || itemEnt.GetEntity()->IsHidden())
  87. {
  88. Event_Touch(NULL,NULL);
  89. }
  90. }
  91. if (state == 1)
  92. {
  93. if (gameLocal.time > ticksoundTime)
  94. {
  95. StopSound( SND_CHANNEL_BODY2, false );
  96. ticksoundTime = gameLocal.time + 1000000;
  97. }
  98. }
  99. //Render the polys.
  100. idAnimatedEntity::Present();
  101. }
  102. void idTrembler::Event_Touch( idEntity *other, trace_t *trace )
  103. {
  104. if (state == 0)
  105. {
  106. idEntity *entityList[ MAX_GENTITIES ];
  107. int i;
  108. int flyforce;
  109. state = 1;
  110. Event_PlayAnim("bounce", 1);
  111. StartSound( "snd_trigger", SND_CHANNEL_BODY, 0, false, NULL );
  112. StartSound( "snd_trigger_idle", SND_CHANNEL_BODY2, 0, false, NULL );
  113. ticksoundTime = gameLocal.time + (spawnArgs.GetInt("ticktime", "10") * 1000);
  114. if (g_skill.GetInteger() > 0)
  115. {
  116. ActivateTargets(this); //activate alarm.
  117. }
  118. int listedEntities = gameLocal.EntitiesWithinRadius( GetPhysics()->GetOrigin(), FLYRADIUS, entityList, MAX_GENTITIES );
  119. flyforce = spawnArgs.GetInt("force", "256");
  120. renderEntity.shaderParms[ SHADERPARM_RED ] = 1;
  121. renderEntity.shaderParms[ SHADERPARM_GREEN ] = 0;
  122. renderEntity.shaderParms[ SHADERPARM_BLUE ] = 0;
  123. UpdateVisuals();
  124. //KA BOIIIIINNNGGGGGG
  125. for (i = 0; i < listedEntities; i++)
  126. {
  127. idEntity *ent = entityList[ i ];
  128. if (!ent)
  129. {
  130. continue;
  131. }
  132. float ent_zpos = ent->GetPhysics()->GetOrigin().z;
  133. float trembler_zpos = this->GetPhysics()->GetOrigin().z;
  134. //only affect things above the trembler.
  135. if (ent_zpos < trembler_zpos)
  136. continue;
  137. //only affect things within X height of trembler.
  138. if (ent_zpos > trembler_zpos + HEIGHT_MAX)
  139. continue;
  140. if ( !ent->IsType( idMoveableItem::Type ) && !ent->IsType( idMoveable::Type ) )
  141. continue;
  142. ent->GetPhysics()->SetLinearVelocity(idVec3(0,0,1) * flyforce);
  143. }
  144. }
  145. }