PageRenderTime 99ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/GameSDK/GameDll/Effects/GameEffects/GameEffect.cpp

https://gitlab.com/blackbird91/CS188_AI_Game
C++ | 491 lines | 337 code | 49 blank | 105 comment | 64 complexity | 9f829dd05f46899cc6e0c8d1d50da036 MD5 | raw file
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. // Original file Copyright Crytek GMBH or its affiliates, used under license.
  13. // Description : Game effect - Ideal for handling a specific visual game feature
  14. #include "StdAfx.h"
  15. #include "GameEffect.h"
  16. #include "Effects/GameEffectsSystem.h"
  17. #include "ICryAnimation.h"
  18. #include "Actor.h"
  19. //--------------------------------------------------------------------------------------------------
  20. // Name: GameSDKCGameEffect
  21. // Desc: Constructor
  22. //--------------------------------------------------------------------------------------------------
  23. GameSDKCGameEffect::GameSDKCGameEffect()
  24. {
  25. m_prev = NULL;
  26. m_next = NULL;
  27. m_flags = 0;
  28. }//-------------------------------------------------------------------------------------------------
  29. //--------------------------------------------------------------------------------------------------
  30. // Name: ~GameSDKCGameEffect
  31. // Desc: Destructor
  32. //--------------------------------------------------------------------------------------------------
  33. GameSDKCGameEffect::~GameSDKCGameEffect()
  34. {
  35. #if DEBUG_GAME_FX_SYSTEM
  36. // Output message if effect hasn't been released before being deleted
  37. const bool bEffectIsReleased = (m_flags & GAME_EFFECT_RELEASED) || // -> Needs to be released before deleted
  38. !(m_flags & GAME_EFFECT_INITIALISED) || // -> Except when not initialised
  39. (gEnv->IsEditor()); // -> Or the editor (memory safely released by editor)
  40. if(!bEffectIsReleased)
  41. {
  42. string dbgMessage = m_debugName + " being destroyed without being released first";
  43. FX_ASSERT_MESSAGE(bEffectIsReleased,dbgMessage.c_str());
  44. }
  45. #endif
  46. if (GameSDKCGameEffectsSystem::Exists())
  47. {
  48. GAME_FX_SYSTEM.UnRegisterEffect(this); // -> Effect should have been released and been unregistered, but to avoid
  49. // crashes call unregister here too
  50. }
  51. }//-------------------------------------------------------------------------------------------------
  52. //--------------------------------------------------------------------------------------------------
  53. // Name: Initialise
  54. // Desc: Initializes game effect
  55. //--------------------------------------------------------------------------------------------------
  56. void GameSDKCGameEffect::Initialise(const SGameEffectParams* gameEffectParams)
  57. {
  58. #if DEBUG_GAME_FX_SYSTEM
  59. m_debugName = GetName(); // Store name so it can be accessed in destructor and debugging
  60. #endif
  61. if(!IsFlagSet(GAME_EFFECT_INITIALISED))
  62. {
  63. SGameEffectParams params;
  64. if(gameEffectParams)
  65. {
  66. params = *gameEffectParams;
  67. }
  68. SetFlag(GAME_EFFECT_AUTO_UPDATES_WHEN_ACTIVE,params.autoUpdatesWhenActive);
  69. SetFlag(GAME_EFFECT_AUTO_UPDATES_WHEN_NOT_ACTIVE,params.autoUpdatesWhenNotActive);
  70. SetFlag(GAME_EFFECT_AUTO_RELEASE,params.autoRelease);
  71. SetFlag(GAME_EFFECT_AUTO_DELETE,params.autoDelete);
  72. GAME_FX_SYSTEM.RegisterEffect(this);
  73. SetFlag(GAME_EFFECT_INITIALISED,true);
  74. SetFlag(GAME_EFFECT_RELEASED,false);
  75. }
  76. }//-------------------------------------------------------------------------------------------------
  77. //--------------------------------------------------------------------------------------------------
  78. // Name: Release
  79. // Desc: Releases game effect
  80. //--------------------------------------------------------------------------------------------------
  81. void GameSDKCGameEffect::Release()
  82. {
  83. SetFlag(GAME_EFFECT_RELEASING, true);
  84. if(IsFlagSet(GAME_EFFECT_ACTIVE))
  85. {
  86. SetActive(false);
  87. }
  88. GAME_FX_SYSTEM.UnRegisterEffect(this);
  89. SetFlag(GAME_EFFECT_INITIALISED,false);
  90. SetFlag(GAME_EFFECT_RELEASING, false);
  91. SetFlag(GAME_EFFECT_RELEASED,true);
  92. }//-------------------------------------------------------------------------------------------------
  93. //--------------------------------------------------------------------------------------------------
  94. // Name: SetActive
  95. // Desc: Sets active status
  96. //--------------------------------------------------------------------------------------------------
  97. void GameSDKCGameEffect::SetActive(bool isActive)
  98. {
  99. FX_ASSERT_MESSAGE(IsFlagSet(GAME_EFFECT_INITIALISED),"Effect changing active status without being initialised first");
  100. FX_ASSERT_MESSAGE((IsFlagSet(GAME_EFFECT_RELEASED)==false),"Effect changing active status after being released");
  101. SetFlag(GAME_EFFECT_ACTIVE,isActive);
  102. GAME_FX_SYSTEM.RegisterEffect(this); // Re-register effect with game effects system
  103. }//-------------------------------------------------------------------------------------------------
  104. //--------------------------------------------------------------------------------------------------
  105. // Name: Update
  106. // Desc: Updates game effect
  107. //--------------------------------------------------------------------------------------------------
  108. void GameSDKCGameEffect::Update(float frameTime)
  109. {
  110. FX_ASSERT_MESSAGE(IsFlagSet(GAME_EFFECT_INITIALISED),"Effect being updated without being initialised first");
  111. FX_ASSERT_MESSAGE((IsFlagSet(GAME_EFFECT_RELEASED)==false),"Effect being updated after being released");
  112. }//-------------------------------------------------------------------------------------------------
  113. //--------------------------------------------------------------------------------------------------
  114. // Name: Next
  115. // Desc: Gets next effect
  116. //--------------------------------------------------------------------------------------------------
  117. GameSDKIGameEffect* GameSDKCGameEffect::Next() const
  118. {
  119. return m_next;
  120. }//-------------------------------------------------------------------------------------------------
  121. //--------------------------------------------------------------------------------------------------
  122. // Name: Prev
  123. // Desc: Gets previous effect
  124. //--------------------------------------------------------------------------------------------------
  125. GameSDKIGameEffect* GameSDKCGameEffect::Prev() const
  126. {
  127. return m_prev;
  128. }//-------------------------------------------------------------------------------------------------
  129. //--------------------------------------------------------------------------------------------------
  130. // Name: SetNext
  131. // Desc: Sets the next effect
  132. //--------------------------------------------------------------------------------------------------
  133. void GameSDKCGameEffect::SetNext(GameSDKIGameEffect* newNext)
  134. {
  135. m_next = newNext;
  136. }//-------------------------------------------------------------------------------------------------
  137. //--------------------------------------------------------------------------------------------------
  138. // Name: SetPrev
  139. // Desc: Sets the previous effect
  140. //--------------------------------------------------------------------------------------------------
  141. void GameSDKCGameEffect::SetPrev(GameSDKIGameEffect* newPrev)
  142. {
  143. m_prev = newPrev;
  144. }//-------------------------------------------------------------------------------------------------
  145. //--------------------------------------------------------------------------------------------------
  146. // Name: SetFlag
  147. // Desc: Sets a game effect flag
  148. //--------------------------------------------------------------------------------------------------
  149. void GameSDKCGameEffect::SetFlag(uint32 flag, bool state)
  150. {
  151. SET_FLAG(m_flags,flag,state);
  152. }//-------------------------------------------------------------------------------------------------
  153. //--------------------------------------------------------------------------------------------------
  154. // Name: IsFlagSet
  155. // Desc: Checks a game effect flag
  156. //--------------------------------------------------------------------------------------------------
  157. bool GameSDKCGameEffect::IsFlagSet(uint32 flag) const
  158. {
  159. return IS_FLAG_SET(m_flags,flag);
  160. }//-------------------------------------------------------------------------------------------------
  161. //--------------------------------------------------------------------------------------------------
  162. // Name: GetFlags
  163. // Desc: Gets effect's flags
  164. //--------------------------------------------------------------------------------------------------
  165. uint32 GameSDKCGameEffect::GetFlags() const
  166. {
  167. return m_flags;
  168. }//-------------------------------------------------------------------------------------------------
  169. //--------------------------------------------------------------------------------------------------
  170. // Name: SetFlags
  171. // Desc: Sets effect's flags
  172. //--------------------------------------------------------------------------------------------------
  173. void GameSDKCGameEffect::SetFlags(uint32 flags)
  174. {
  175. m_flags = flags;
  176. }//-------------------------------------------------------------------------------------------------
  177. //--------------------------------------------------------------------------------------------------
  178. // Name: SpawnParticlesOnSkeleton
  179. // Desc: Spawn particles on Skeleton
  180. //--------------------------------------------------------------------------------------------------
  181. void GameSDKCGameEffect::SpawnParticlesOnSkeleton(IEntity* pEntity, IParticleEmitter* pParticleEmitter, uint32 numParticles, float maxHeightScale) const
  182. {
  183. if((pEntity) && (numParticles>0) && (pParticleEmitter) && (maxHeightScale>0.0f))
  184. {
  185. ICharacterInstance* pCharacter = pEntity->GetCharacter(0);
  186. if(pCharacter)
  187. {
  188. IDefaultSkeleton& rIDefaultSkeleton = pCharacter->GetIDefaultSkeleton();
  189. ISkeletonPose* pPose = pCharacter->GetISkeletonPose();
  190. if(pPose)
  191. {
  192. Vec3 animPos;
  193. Quat animRot;
  194. IActor* pActor = gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(pEntity->GetId());
  195. if(pActor) // First try to get animation data
  196. {
  197. QuatT animLoc = pActor->GetAnimatedCharacter()->GetAnimLocation();
  198. animPos = animLoc.t;
  199. animRot = animLoc.q;
  200. }
  201. else // If no actor, then use entity data
  202. {
  203. animPos = pEntity->GetWorldPos();
  204. animRot = pEntity->GetWorldRotation();
  205. }
  206. animRot.Invert();
  207. AABB bbox;
  208. pEntity->GetLocalBounds(bbox);
  209. float bbHeight = bbox.max.z - bbox.min.z;
  210. // Avoid division by 0
  211. if(bbHeight == 0)
  212. {
  213. bbHeight = 0.0001f;
  214. }
  215. const uint32 numJoints = rIDefaultSkeleton.GetJointCount();
  216. for (uint32 i = 0; i < numParticles; ++i)
  217. {
  218. int id = cry_random(0U, numJoints - 1);
  219. int parentId = rIDefaultSkeleton.GetJointParentIDByID(id);
  220. if(parentId>0)
  221. {
  222. QuatT boneQuat = pPose->GetAbsJointByID(id);
  223. QuatT parentBoneQuat= pPose->GetAbsJointByID(parentId);
  224. float lerpScale = cry_random(0.0f, 1.0f);
  225. QuatTS loc(IDENTITY);
  226. loc.t = LERP(boneQuat.t,parentBoneQuat.t,lerpScale);
  227. float heightScale = ((loc.t.z - bbox.min.z) / bbHeight);
  228. if(heightScale < maxHeightScale)
  229. {
  230. loc.t = loc.t * animRot;
  231. loc.t = loc.t + animPos;
  232. pParticleEmitter->EmitParticle(NULL, NULL, &loc);
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }//-------------------------------------------------------------------------------------------------
  240. //--------------------------------------------------------------------------------------------------
  241. // Name: SetMaterialOnEntity
  242. // Desc: Sets material on entity
  243. //--------------------------------------------------------------------------------------------------
  244. void GameSDKCGameEffect::SetMaterialOnEntity(IMaterial* pMaterial, EntityId entityId, PodArray<int>* pBodyAttachmentIndexArray)
  245. {
  246. if(pMaterial && pBodyAttachmentIndexArray)
  247. {
  248. IEntity* pEntity = gEnv->pEntitySystem->GetEntity(entityId);
  249. if(pEntity)
  250. {
  251. SEntitySlotInfo slotInfo;
  252. bool gotSlotInfo = pEntity->GetSlotInfo(0, slotInfo);
  253. if(gotSlotInfo && slotInfo.pCharacter)
  254. {
  255. IAttachmentManager* pAttachmentManager = slotInfo.pCharacter->GetIAttachmentManager();
  256. if(pAttachmentManager)
  257. {
  258. int attachmentCount = pBodyAttachmentIndexArray->size();
  259. for(int i=0; i<attachmentCount; i++)
  260. {
  261. IAttachment* attachment = pAttachmentManager->GetInterfaceByIndex((*pBodyAttachmentIndexArray)[i]);
  262. if(attachment)
  263. {
  264. IAttachmentObject* attachmentObj = attachment->GetIAttachmentObject();
  265. if(attachmentObj)
  266. {
  267. attachmentObj->SetReplacementMaterial(pMaterial);
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }//-------------------------------------------------------------------------------------------------
  276. //--------------------------------------------------------------------------------------------------
  277. // Name: SetMaterialOnEntity
  278. // Desc: Sets material on entity
  279. //--------------------------------------------------------------------------------------------------
  280. void GameSDKCGameEffect::SetMaterialOnEntity(IMaterial* pMaterial, EntityId entityId, PodArray<ItemString>* bodyAttachmentNameArray)
  281. {
  282. if(pMaterial && bodyAttachmentNameArray)
  283. {
  284. IEntity* pEntity = gEnv->pEntitySystem->GetEntity(entityId);
  285. if(pEntity)
  286. {
  287. SEntitySlotInfo slotInfo;
  288. bool gotSlotInfo = pEntity->GetSlotInfo(0, slotInfo);
  289. if(gotSlotInfo && slotInfo.pCharacter)
  290. {
  291. IAttachmentManager* pAttachmentManager = slotInfo.pCharacter->GetIAttachmentManager();
  292. if(pAttachmentManager)
  293. {
  294. int attachmentCount = bodyAttachmentNameArray->size();
  295. for(int i=0; i<attachmentCount; i++)
  296. {
  297. IAttachment* attachment = pAttachmentManager->GetInterfaceByName(((*bodyAttachmentNameArray)[i]).c_str());
  298. if(attachment)
  299. {
  300. IAttachmentObject* attachmentObj = attachment->GetIAttachmentObject();
  301. if(attachmentObj)
  302. {
  303. attachmentObj->SetReplacementMaterial(pMaterial);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }//-------------------------------------------------------------------------------------------------
  312. //--------------------------------------------------------------------------------------------------
  313. // Name: ReadAttachmentNames
  314. // Desc: Reads attachment names
  315. //--------------------------------------------------------------------------------------------------
  316. void GameSDKCGameEffect::ReadAttachmentNames(const IItemParamsNode* pParamNode, PodArray<ItemString>* pAttachmentNameArray)
  317. {
  318. if(pParamNode && pAttachmentNameArray)
  319. {
  320. int attachmentCount = pParamNode->GetChildCount();
  321. pAttachmentNameArray->resize(attachmentCount);
  322. for(int i=0; i<attachmentCount; i++)
  323. {
  324. const IItemParamsNode* attachmentNode = pParamNode->GetChild(i);
  325. if(attachmentNode)
  326. {
  327. (*pAttachmentNameArray)[i] = attachmentNode->GetAttribute("name");
  328. }
  329. }
  330. }
  331. }//-------------------------------------------------------------------------------------------------
  332. //--------------------------------------------------------------------------------------------------
  333. // Name: InitAttachmentIndexArray
  334. // Desc: Initialises attachment index array
  335. //--------------------------------------------------------------------------------------------------
  336. void GameSDKCGameEffect::InitAttachmentIndexArray(PodArray<int> *attachmentIndexArray, PodArray<ItemString>* pAttachmentNameArray, EntityId entityId) const
  337. {
  338. if(attachmentIndexArray && pAttachmentNameArray && (entityId!=0))
  339. {
  340. if(attachmentIndexArray->size() == 0)
  341. {
  342. // Store attachment index array
  343. const int attachmentNameCount = pAttachmentNameArray->size();
  344. SEntitySlotInfo slotInfo;
  345. IEntity* pEntity = gEnv->pEntitySystem->GetEntity(entityId);
  346. if(pEntity)
  347. {
  348. bool gotEntitySlotInfo = pEntity->GetSlotInfo(0, slotInfo);
  349. if(gotEntitySlotInfo)
  350. {
  351. attachmentIndexArray->reserve(attachmentNameCount);
  352. if(slotInfo.pCharacter)
  353. {
  354. IAttachmentManager* attachmentManager = slotInfo.pCharacter->GetIAttachmentManager();
  355. if(attachmentManager)
  356. {
  357. const int attachmentCount = attachmentManager->GetAttachmentCount();
  358. for(int n=0; n<attachmentNameCount; n++)
  359. {
  360. for(int a=0; a<attachmentCount; a++)
  361. {
  362. IAttachment* attachment = attachmentManager->GetInterfaceByIndex(a);
  363. if(attachment)
  364. {
  365. const char* currentAttachmentName = attachment->GetName();
  366. if(strcmp(currentAttachmentName,(*pAttachmentNameArray)[n].c_str())==0)
  367. {
  368. attachmentIndexArray->push_back(a);
  369. break;
  370. }
  371. }
  372. }
  373. }
  374. }
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }//-------------------------------------------------------------------------------------------------
  381. //--------------------------------------------------------------------------------------------------
  382. // Name: LoadMaterial
  383. // Desc: Loads and calls AddRef on material
  384. //--------------------------------------------------------------------------------------------------
  385. IMaterial* GameSDKCGameEffect::LoadMaterial(const char* pMaterialName)
  386. {
  387. IMaterial* pMaterial = NULL;
  388. I3DEngine* p3DEngine = gEnv->p3DEngine;
  389. if(pMaterialName && p3DEngine)
  390. {
  391. IMaterialManager* pMaterialManager = p3DEngine->GetMaterialManager();
  392. if(pMaterialManager)
  393. {
  394. pMaterial = pMaterialManager->LoadMaterial(pMaterialName);
  395. if(pMaterial)
  396. {
  397. pMaterial->AddRef();
  398. }
  399. }
  400. }
  401. return pMaterial;
  402. }//-------------------------------------------------------------------------------------------------
  403. //--------------------------------------------------------------------------------------------------
  404. // Name: LoadParticleEffect
  405. // Desc: Loads and calls AddRef on a particle effect
  406. //--------------------------------------------------------------------------------------------------
  407. IParticleEffect* GameSDKCGameEffect::LoadParticleEffect(const char* pParticleEffectName)
  408. {
  409. IParticleEffect* pParticleEffect = NULL;
  410. IParticleManager* pParticleManager = gEnv->pParticleManager;
  411. if(pParticleEffectName && pParticleManager)
  412. {
  413. pParticleEffect = gEnv->pParticleManager->FindEffect(pParticleEffectName);
  414. if(pParticleEffect)
  415. {
  416. pParticleEffect->AddRef();
  417. }
  418. }
  419. return pParticleEffect;
  420. }//-------------------------------------------------------------------------------------------------
  421. //--------------------------------------------------------------------------------------------------
  422. // Name: IsEntity3rdPerson
  423. // Desc: Returns 3rd person state
  424. //--------------------------------------------------------------------------------------------------
  425. bool GameSDKCGameEffect::IsEntity3rdPerson(EntityId entityId)
  426. {
  427. bool bIs3rdPerson = true;
  428. IGame* pGame = gEnv->pGame;
  429. if(pGame)
  430. {
  431. IGameFramework* pGameFramework = pGame->GetIGameFramework();
  432. if(pGameFramework)
  433. {
  434. EntityId clientEntityId = pGameFramework->GetClientActorId();
  435. if(clientEntityId == entityId)
  436. {
  437. bIs3rdPerson = pGameFramework->GetClientActor()->IsThirdPerson();
  438. }
  439. }
  440. }
  441. return bIs3rdPerson;
  442. }//-------------------------------------------------------------------------------------------------