Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
6.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "igamesystem.h"
  9. #include "point_camera.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. #define CAM_THINK_INTERVAL 0.05
  13. // Spawnflags
  14. #define SF_CAMERA_START_OFF 0x01
  15. // UNDONE: Share properly with the client code!!!
  16. #define POINT_CAMERA_MSG_SETACTIVE 1
  17. CEntityClassList<CPointCamera> g_PointCameraList;
  18. template <> CPointCamera *CEntityClassList<CPointCamera>::m_pClassList = NULL;
  19. CPointCamera* GetPointCameraList()
  20. {
  21. return g_PointCameraList.m_pClassList;
  22. }
  23. // These are already built into CBaseEntity
  24. // DEFINE_KEYFIELD( m_iName, FIELD_STRING, "targetname" ),
  25. // DEFINE_KEYFIELD( m_iParent, FIELD_STRING, "parentname" ),
  26. // DEFINE_KEYFIELD( m_target, FIELD_STRING, "target" ),
  27. LINK_ENTITY_TO_CLASS( point_camera, CPointCamera );
  28. //-----------------------------------------------------------------------------
  29. // Purpose:
  30. //-----------------------------------------------------------------------------
  31. CPointCamera::~CPointCamera()
  32. {
  33. g_PointCameraList.Remove( this );
  34. }
  35. CPointCamera::CPointCamera()
  36. {
  37. // Set these to opposites so that it'll be sent the first time around.
  38. m_bActive = false;
  39. m_bIsOn = false;
  40. m_bFogEnable = false;
  41. g_PointCameraList.Insert( this );
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Purpose:
  45. //-----------------------------------------------------------------------------
  46. void CPointCamera::Spawn( void )
  47. {
  48. BaseClass::Spawn();
  49. if ( m_spawnflags & SF_CAMERA_START_OFF )
  50. {
  51. m_bIsOn = false;
  52. }
  53. else
  54. {
  55. m_bIsOn = true;
  56. }
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose: Override ShouldTransmit since we want to be sent even though we don't have a model, etc.
  60. // All that matters is if we are in the pvs.
  61. //-----------------------------------------------------------------------------
  62. int CPointCamera::UpdateTransmitState()
  63. {
  64. if ( m_bActive )
  65. {
  66. return SetTransmitState( FL_EDICT_ALWAYS );
  67. }
  68. else
  69. {
  70. return SetTransmitState( FL_EDICT_DONTSEND );
  71. }
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose:
  75. //-----------------------------------------------------------------------------
  76. void CPointCamera::SetActive( bool bActive )
  77. {
  78. // If the mapmaker's told the camera it's off, it enforces inactive state
  79. if ( !m_bIsOn )
  80. {
  81. bActive = false;
  82. }
  83. if ( m_bActive != bActive )
  84. {
  85. m_bActive = bActive;
  86. DispatchUpdateTransmitState();
  87. }
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose:
  91. //-----------------------------------------------------------------------------
  92. void CPointCamera::InputChangeFOV( inputdata_t &inputdata )
  93. {
  94. // Parse the keyvalue data
  95. char parseString[255];
  96. Q_strncpy(parseString, inputdata.value.String(), sizeof(parseString));
  97. // Get FOV
  98. char *pszParam = strtok(parseString," ");
  99. if(pszParam)
  100. {
  101. m_TargetFOV = atof( pszParam );
  102. }
  103. else
  104. {
  105. // Assume no change
  106. m_TargetFOV = m_FOV;
  107. }
  108. // Get Time
  109. float flChangeTime;
  110. pszParam = strtok(NULL," ");
  111. if(pszParam)
  112. {
  113. flChangeTime = atof( pszParam );
  114. }
  115. else
  116. {
  117. // Assume 1 second.
  118. flChangeTime = 1.0;
  119. }
  120. m_DegreesPerSecond = ( m_TargetFOV - m_FOV ) / flChangeTime;
  121. SetThink( &CPointCamera::ChangeFOVThink );
  122. SetNextThink( gpGlobals->curtime );
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Purpose:
  126. //-----------------------------------------------------------------------------
  127. void CPointCamera::ChangeFOVThink( void )
  128. {
  129. SetNextThink( gpGlobals->curtime + CAM_THINK_INTERVAL );
  130. float newFOV = m_FOV;
  131. newFOV += m_DegreesPerSecond * CAM_THINK_INTERVAL;
  132. if( m_DegreesPerSecond < 0 )
  133. {
  134. if( newFOV <= m_TargetFOV )
  135. {
  136. newFOV = m_TargetFOV;
  137. SetThink( NULL );
  138. }
  139. }
  140. else
  141. {
  142. if( newFOV >= m_TargetFOV )
  143. {
  144. newFOV = m_TargetFOV;
  145. SetThink( NULL );
  146. }
  147. }
  148. m_FOV = newFOV;
  149. }
  150. //-----------------------------------------------------------------------------
  151. // Purpose: Turn this camera on, and turn all other cameras off
  152. //-----------------------------------------------------------------------------
  153. void CPointCamera::InputSetOnAndTurnOthersOff( inputdata_t &inputdata )
  154. {
  155. CBaseEntity *pEntity = NULL;
  156. while ((pEntity = gEntList.FindEntityByClassname( pEntity, "point_camera" )) != NULL)
  157. {
  158. CPointCamera *pCamera = (CPointCamera*)pEntity;
  159. pCamera->InputSetOff( inputdata );
  160. }
  161. // Now turn myself on
  162. InputSetOn( inputdata );
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Purpose:
  166. //-----------------------------------------------------------------------------
  167. void CPointCamera::InputSetOn( inputdata_t &inputdata )
  168. {
  169. m_bIsOn = true;
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Purpose:
  173. //-----------------------------------------------------------------------------
  174. void CPointCamera::InputSetOff( inputdata_t &inputdata )
  175. {
  176. m_bIsOn = false;
  177. SetActive( false );
  178. }
  179. BEGIN_DATADESC( CPointCamera )
  180. // Save/restore Keyvalue fields
  181. DEFINE_KEYFIELD( m_FOV, FIELD_FLOAT, "FOV" ),
  182. DEFINE_KEYFIELD( m_Resolution, FIELD_FLOAT, "resolution" ),
  183. DEFINE_KEYFIELD( m_bFogEnable, FIELD_BOOLEAN, "fogEnable" ),
  184. DEFINE_KEYFIELD( m_FogColor, FIELD_COLOR32, "fogColor" ),
  185. DEFINE_KEYFIELD( m_flFogStart, FIELD_FLOAT, "fogStart" ),
  186. DEFINE_KEYFIELD( m_flFogEnd, FIELD_FLOAT, "fogEnd" ),
  187. DEFINE_KEYFIELD( m_flFogMaxDensity, FIELD_FLOAT, "fogMaxDensity" ),
  188. DEFINE_KEYFIELD( m_bUseScreenAspectRatio, FIELD_BOOLEAN, "UseScreenAspectRatio" ),
  189. DEFINE_FIELD( m_bActive, FIELD_BOOLEAN ),
  190. DEFINE_FIELD( m_bIsOn, FIELD_BOOLEAN ),
  191. DEFINE_FIELD( m_TargetFOV, FIELD_FLOAT ),
  192. DEFINE_FIELD( m_DegreesPerSecond, FIELD_FLOAT ),
  193. // This is re-set up in the constructor
  194. //DEFINE_FIELD( m_pNext, FIELD_CLASSPTR ),
  195. DEFINE_FUNCTION( ChangeFOVThink ),
  196. // Input
  197. DEFINE_INPUTFUNC( FIELD_STRING, "ChangeFOV", InputChangeFOV ),
  198. DEFINE_INPUTFUNC( FIELD_VOID, "SetOnAndTurnOthersOff", InputSetOnAndTurnOthersOff ),
  199. DEFINE_INPUTFUNC( FIELD_VOID, "SetOn", InputSetOn ),
  200. DEFINE_INPUTFUNC( FIELD_VOID, "SetOff", InputSetOff ),
  201. END_DATADESC()
  202. IMPLEMENT_SERVERCLASS_ST( CPointCamera, DT_PointCamera )
  203. SendPropFloat( SENDINFO( m_FOV ), 0, SPROP_NOSCALE ),
  204. SendPropFloat( SENDINFO( m_Resolution ), 0, SPROP_NOSCALE ),
  205. SendPropInt( SENDINFO( m_bFogEnable ), 1, SPROP_UNSIGNED ),
  206. SendPropInt( SENDINFO_STRUCTELEM( m_FogColor ), 32, SPROP_UNSIGNED ),
  207. SendPropFloat( SENDINFO( m_flFogStart ), 0, SPROP_NOSCALE ),
  208. SendPropFloat( SENDINFO( m_flFogEnd ), 0, SPROP_NOSCALE ),
  209. SendPropFloat( SENDINFO( m_flFogMaxDensity ), 0, SPROP_NOSCALE ),
  210. SendPropInt( SENDINFO( m_bActive ), 1, SPROP_UNSIGNED ),
  211. SendPropInt( SENDINFO( m_bUseScreenAspectRatio ), 1, SPROP_UNSIGNED ),
  212. END_SEND_TABLE()