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.

165 lines
4.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Contains all world state--the main game database
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "worldmanager.h"
  9. #include "legion.h"
  10. #include "heightfield.h"
  11. #include "rendermanager.h"
  12. //-----------------------------------------------------------------------------
  13. // Singleton accessor
  14. //-----------------------------------------------------------------------------
  15. static CWorldManager s_WorldManager;
  16. extern CWorldManager *g_pWorldManager = &s_WorldManager;
  17. //-----------------------------------------------------------------------------
  18. // ConVars
  19. //-----------------------------------------------------------------------------
  20. static ConVar cam_forwardspeed( "cam_forwardspeed", "100", FCVAR_CHEAT, "Sets the camera forward speed" );
  21. static ConVar cam_backwardspeed( "cam_backwardspeed", "100", FCVAR_CHEAT, "Sets the camera backward speed" );
  22. //-----------------------------------------------------------------------------
  23. // Constructor, destructor
  24. //-----------------------------------------------------------------------------
  25. CWorldManager::CWorldManager()
  26. {
  27. m_pHeightField = NULL;
  28. }
  29. CWorldManager::~CWorldManager()
  30. {
  31. Assert( m_pHeightField == NULL );
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Level init, shutdown
  35. //-----------------------------------------------------------------------------
  36. LevelRetVal_t CWorldManager::LevelInit( bool bFirstCall )
  37. {
  38. if ( !bFirstCall )
  39. return FINISHED;
  40. Assert( !m_pHeightField );
  41. m_pHeightField = new CHeightField( 6, 6, 4 );
  42. if ( !m_pHeightField->LoadHeightFromFile( "maps/testheight.psd" ) )
  43. return FAILED;
  44. CreateEntities();
  45. SetInitialLocalPlayerPosition();
  46. return FINISHED;
  47. }
  48. LevelRetVal_t CWorldManager::LevelShutdown( bool bFirstCall )
  49. {
  50. if ( !bFirstCall )
  51. return FINISHED;
  52. DestroyEntities();
  53. if ( m_pHeightField )
  54. {
  55. delete m_pHeightField;
  56. m_pHeightField = NULL;
  57. }
  58. return FINISHED;
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Create/ destroy entities
  62. //-----------------------------------------------------------------------------
  63. void CWorldManager::CreateEntities()
  64. {
  65. m_PlayerEntity.m_pCameraProperty = g_pRenderManager->CreateCameraProperty();
  66. }
  67. void CWorldManager::DestroyEntities()
  68. {
  69. g_pRenderManager->DestroyCameraProperty( m_PlayerEntity.m_pCameraProperty );
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Gets the camera to world matrix
  73. //-----------------------------------------------------------------------------
  74. CPlayerEntity* CWorldManager::GetLocalPlayer()
  75. {
  76. return &m_PlayerEntity;
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Sets the initial camera position
  80. //-----------------------------------------------------------------------------
  81. void CWorldManager::SetInitialLocalPlayerPosition()
  82. {
  83. float flDistance = 1024.0;
  84. Vector vecCameraDirection( 1.0f, 1.0f, -0.5f );
  85. VectorNormalize( vecCameraDirection );
  86. VectorMA( Vector( 512, 512, 0 ), -flDistance, vecCameraDirection, m_PlayerEntity.m_pCameraProperty->m_Origin );
  87. QAngle angles;
  88. VectorAngles( vecCameraDirection, m_PlayerEntity.m_pCameraProperty->m_Angles );
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Draws the UI
  92. //-----------------------------------------------------------------------------
  93. void CWorldManager::DrawWorld()
  94. {
  95. m_pHeightField->Draw( );
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Commands
  99. //-----------------------------------------------------------------------------
  100. void CWorldManager::ForwardStart( const CCommand &args )
  101. {
  102. CCameraProperty *pCamera = m_PlayerEntity.m_pCameraProperty;
  103. Vector vecForward;
  104. pCamera->GetForward( &vecForward );
  105. VectorMA( pCamera->m_Velocity, cam_forwardspeed.GetFloat(), vecForward, pCamera->m_Velocity );
  106. }
  107. void CWorldManager::ForwardStop( const CCommand &args )
  108. {
  109. CCameraProperty *pCamera = m_PlayerEntity.m_pCameraProperty;
  110. Vector vecForward;
  111. pCamera->GetForward( &vecForward );
  112. VectorMA( pCamera->m_Velocity, -cam_forwardspeed.GetFloat(), vecForward, pCamera->m_Velocity );
  113. }
  114. void CWorldManager::BackwardStart( const CCommand &args )
  115. {
  116. CCameraProperty *pCamera = m_PlayerEntity.m_pCameraProperty;
  117. Vector vecForward;
  118. pCamera->GetForward( &vecForward );
  119. VectorMA( pCamera->m_Velocity, -cam_backwardspeed.GetFloat(), vecForward, pCamera->m_Velocity );
  120. }
  121. void CWorldManager::BackwardStop( const CCommand &args )
  122. {
  123. CCameraProperty *pCamera = m_PlayerEntity.m_pCameraProperty;
  124. Vector vecForward;
  125. pCamera->GetForward( &vecForward );
  126. VectorMA( pCamera->m_Velocity, cam_backwardspeed.GetFloat(), vecForward, pCamera->m_Velocity );
  127. }