Advertisement
dcomicboy

hitbox.cpp

Mar 29th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. // ----------------------------------------------------------------------- //
  2. //
  3. // MODULE : HitBox.cpp
  4. //
  5. // PURPOSE : Client side reresentation of the CCharacterHitBox object
  6. //
  7. // CREATED : 8/26/02
  8. //
  9. // (c) 2002 Monolith Productions, Inc. All Rights Reserved
  10. //
  11. // ----------------------------------------------------------------------- //
  12.  
  13. //
  14. // Includes...
  15. //
  16.  
  17. #include "stdafx.h"
  18. #include "HitBox.h"
  19.  
  20. //
  21. // Defines...
  22. //
  23.  
  24. #define HB_COLOR_R 0.5f
  25. #define HB_COLOR_G 0.5f
  26. #define HB_COLOR_B 0.5f
  27. #define HB_COLOR_A 1.0f
  28.  
  29. static VarTrack s_ShowClientHitBox;
  30.  
  31.  
  32. // ----------------------------------------------------------------------- //
  33. //
  34. // ROUTINE: CHitBox::CHitBox()
  35. //
  36. // PURPOSE: Constructor
  37. //
  38. // ----------------------------------------------------------------------- //
  39.  
  40. CHitBox::CHitBox()
  41. : m_hObject ( INVALID_HOBJECT ),
  42. m_hModel ( INVALID_HOBJECT ),
  43. m_vDims ( 0.0f, 0.0f, 0.0f ),
  44. m_vOffset ( 0.0f, 0.0f, 0.0f ),
  45. m_hBoundingBox ( INVALID_HOBJECT )
  46. {
  47.  
  48. }
  49.  
  50. // ----------------------------------------------------------------------- //
  51. //
  52. // ROUTINE: CHitBox::~CHitBox()
  53. //
  54. // PURPOSE: Destructor
  55. //
  56. // ----------------------------------------------------------------------- //
  57.  
  58. CHitBox::~CHitBox()
  59. {
  60. if( m_hObject != INVALID_HOBJECT )
  61. {
  62. g_pLTClient->RemoveObject( m_hObject );
  63. }
  64. m_hObject = INVALID_HOBJECT;
  65.  
  66. if( m_hBoundingBox != INVALID_HOBJECT )
  67. {
  68. g_pLTClient->RemoveObject( m_hBoundingBox );
  69. }
  70. m_hBoundingBox = INVALID_HOBJECT;
  71. }
  72.  
  73. // ----------------------------------------------------------------------- //
  74. //
  75. // ROUTINE: CHitBox::Init()
  76. //
  77. // PURPOSE: Create our object
  78. //
  79. // ----------------------------------------------------------------------- //
  80.  
  81. bool CHitBox::Init( HOBJECT hModel, const LTVector &vDims, const LTVector &vOffset )
  82. {
  83. if( hModel == INVALID_HOBJECT )
  84. return false;
  85.  
  86. if( !s_ShowClientHitBox.IsInitted() )
  87. {
  88. s_ShowClientHitBox.Init( g_pLTClient, "ShowClientHitBox", LTNULL, 0.0f );
  89. }
  90.  
  91. m_hModel = hModel;
  92. m_vDims = vDims;
  93. m_vOffset = vOffset;
  94.  
  95. // Create the hitbox at the propper offset from the models position...
  96.  
  97. LTVector vModelPos;
  98. g_pLTClient->GetObjectPos( m_hModel, &vModelPos );
  99.  
  100. LTRotation rModelRot;
  101. g_pLTClient->GetObjectRotation( m_hModel, &rModelRot );
  102.  
  103. LTMatrix mMat;
  104. rModelRot.ConvertToMatrix( mMat );
  105.  
  106. // Get rid of our object if it already exists...
  107.  
  108. if( m_hObject )
  109. {
  110. g_pLTClient->RemoveObject( m_hObject );
  111. m_hObject = LTNULL;
  112. }
  113.  
  114. ObjectCreateStruct ocs;
  115.  
  116. ocs.m_ObjectType = OT_NORMAL;
  117. ocs.m_Flags = FLAG_RAYHIT;
  118. ocs.m_Pos = vModelPos + (mMat * m_vOffset);
  119.  
  120. m_hObject = g_pLTClient->CreateObject( &ocs );
  121. if( m_hObject == INVALID_HOBJECT )
  122. {
  123. return false;
  124. }
  125.  
  126. g_pPhysicsLT->SetObjectDims( m_hObject, &m_vDims, 0 );
  127.  
  128. g_pCommonLT->SetObjectFlags( m_hObject, OFT_User, USRFLG_HITBOX | USRFLG_CHARACTER, USRFLG_HITBOX | USRFLG_CHARACTER );
  129.  
  130. return true;
  131. }
  132.  
  133. // ----------------------------------------------------------------------- //
  134. //
  135. // ROUTINE: CHitBox::Update()
  136. //
  137. // PURPOSE: Update the HitBox...
  138. //
  139. // ----------------------------------------------------------------------- //
  140.  
  141. void CHitBox::Update()
  142. {
  143. if( (m_hObject == INVALID_HOBJECT) ||
  144. (m_hModel == INVALID_HOBJECT) )
  145. {
  146. return;
  147. }
  148.  
  149. // Offset the HitBox from the position of the associated model...
  150.  
  151. LTVector vPos;
  152. g_pLTClient->GetObjectPos( m_hModel, &vPos );
  153.  
  154. LTRotation rRot;
  155. g_pLTClient->GetObjectRotation( m_hModel, &rRot );
  156.  
  157. LTMatrix mMat;
  158. rRot.ConvertToMatrix( mMat );
  159.  
  160. vPos += (mMat * m_vOffset);
  161.  
  162. g_pLTClient->SetObjectPos( m_hObject, &vPos );
  163.  
  164. // If the model object is not visible we should not be able to interact with the hitbox
  165.  
  166. uint32 dwFlags;
  167. g_pCommonLT->GetObjectFlags( m_hModel, OFT_Flags, dwFlags );
  168. g_pCommonLT->SetObjectFlags( m_hObject, OFT_Flags, ((dwFlags & FLAG_VISIBLE) ? FLAG_RAYHIT : 0), FLAG_RAYHIT );
  169.  
  170.  
  171. #ifndef _FINAL
  172. // Update the visual model...
  173. UpdateBoundingBox();
  174. #endif // _FINAL
  175.  
  176. }
  177.  
  178. // ----------------------------------------------------------------------- //
  179. //
  180. // ROUTINE: CHitBox::SetDims()
  181. //
  182. // PURPOSE: Set the dimensions of the hitbox...
  183. //
  184. // ----------------------------------------------------------------------- //
  185.  
  186. void CHitBox::SetDims( const LTVector &vDims )
  187. {
  188. if( m_hObject == INVALID_HOBJECT )
  189. return;
  190.  
  191. m_vDims = vDims;
  192. g_pPhysicsLT->SetObjectDims( m_hObject, &m_vDims, 0 );
  193. }
  194.  
  195. // ----------------------------------------------------------------------- //
  196. //
  197. // ROUTINE: CHitBox::SetOffset()
  198. //
  199. // PURPOSE: Set the offset from the models position...
  200. //
  201. // ----------------------------------------------------------------------- //
  202.  
  203. void CHitBox::SetOffset( const LTVector &vOffset )
  204. {
  205. if( m_hObject == INVALID_HOBJECT )
  206. return;
  207.  
  208. // Just set the offset, Update() will take care of setting the position...
  209.  
  210. m_vOffset = vOffset;
  211. }
  212.  
  213. // ----------------------------------------------------------------------- //
  214. //
  215. // ROUTINE: CHitBox::CreateBoundingBox()
  216. //
  217. // PURPOSE: Create a model for some visual feedback of the hitbox...
  218. //
  219. // ----------------------------------------------------------------------- //
  220.  
  221. void CHitBox::CreateBoundingBox()
  222. {
  223. if( (m_hObject == INVALID_HOBJECT) ||
  224. (m_hBoundingBox != INVALID_HOBJECT ))
  225. return;
  226.  
  227. ObjectCreateStruct ocs;
  228.  
  229. g_pLTClient->GetObjectPos( m_hObject, &ocs.m_Pos );
  230.  
  231. ocs.m_ObjectType = OT_MODEL;
  232. ocs.m_Flags = FLAG_VISIBLE | FLAG_NOLIGHT | FLAG_GOTHRUWORLD;
  233. ocs.m_Flags2 = FLAG2_FORCETRANSLUCENT;
  234. ocs.m_Scale = m_vDims * 2.0f;
  235.  
  236. LTStrCpy( ocs.m_Filename, "Models\\1x1_square.ltb", ARRAY_LEN( ocs.m_Filename ));
  237. LTStrCpy( ocs.m_SkinName, "Models\\1x1_square.dtx", ARRAY_LEN( ocs.m_SkinName ));
  238.  
  239. m_hBoundingBox = g_pLTClient->CreateObject( &ocs );
  240. if( m_hBoundingBox == INVALID_HOBJECT )
  241. return;
  242.  
  243. g_pLTClient->SetObjectColor( m_hBoundingBox, HB_COLOR_R, HB_COLOR_G, HB_COLOR_B, HB_COLOR_A);
  244. }
  245.  
  246. // ----------------------------------------------------------------------- //
  247. //
  248. // ROUTINE: CHitBox::UpdateBoundingBox()
  249. //
  250. // PURPOSE: Update the model for some visual feedback of the hitbox...
  251. //
  252. // ----------------------------------------------------------------------- //
  253.  
  254. void CHitBox::UpdateBoundingBox()
  255. {
  256. if( m_hObject == INVALID_HOBJECT )
  257. return;
  258.  
  259. if( s_ShowClientHitBox.GetFloat( 0.0f ) > 0.0f )
  260. {
  261. CreateBoundingBox();
  262.  
  263. if( m_hBoundingBox == INVALID_HOBJECT )
  264. return;
  265.  
  266. LTVector vPos;
  267.  
  268. g_pLTClient->GetObjectPos( m_hObject, &vPos );
  269. g_pLTClient->SetObjectPos( m_hBoundingBox, &vPos );
  270.  
  271. LTVector vScale = m_vDims * 2.0f;
  272. g_pLTClient->SetObjectScale( m_hBoundingBox, &vScale );
  273.  
  274. g_pLTClient->SetObjectColor( m_hBoundingBox, HB_COLOR_R, HB_COLOR_G, HB_COLOR_B, HB_COLOR_A );
  275. }
  276. else
  277. {
  278. if( m_hBoundingBox != INVALID_HOBJECT )
  279. {
  280. g_pLTClient->RemoveObject( m_hBoundingBox );
  281. }
  282. m_hBoundingBox = INVALID_HOBJECT;
  283. }
  284. }
  285.  
  286. // ----------------------------------------------------------------------- //
  287. //
  288. // ROUTINE: CHitBox::SetCanBeSearched()
  289. //
  290. // PURPOSE: Set the hitbox to be searchable...
  291. //
  292. // ----------------------------------------------------------------------- //
  293.  
  294. void CHitBox::SetCanBeSearched( bool bCanBeSearched )
  295. {
  296. if( m_hObject == INVALID_HOBJECT )
  297. return;
  298.  
  299. g_pCommonLT->SetObjectFlags( m_hObject, OFT_User, (bCanBeSearched ? USRFLG_CAN_SEARCH : 0), USRFLG_CAN_SEARCH );
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement