MissionParticles.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //============================================================================================
  2. // Spirenkov Maxim, 2003
  3. //============================================================================================
  4. // Mission objects
  5. //============================================================================================
  6. // MissionParticles
  7. //============================================================================================
  8. #include "MissionParticles.h"
  9. //============================================================================================
  10. MissionParticles::MissionParticles()
  11. {
  12. ps = null;
  13. model = null;
  14. noSwing = false;
  15. }
  16. MissionParticles::~MissionParticles()
  17. {
  18. if(model) model->Release();
  19. if(ps) ps->Release();
  20. }
  21. //============================================================================================
  22. //Инициализировать объект
  23. bool MissionParticles::Create(MOPReader & reader)
  24. {
  25. objectPtr.Reset();
  26. ConstString n = reader.String();
  27. if(name != n)
  28. {
  29. name = n;
  30. if(ps){ ps->Release(); ps = null; }
  31. ps = Particles().CreateParticleSystem(name.c_str());
  32. }
  33. Vector pos = reader.Position();
  34. Vector ang = reader.Angles();
  35. float scale = reader.Float();
  36. float timeScale = reader.Float();
  37. matrix.Build(ang, pos);
  38. if(ps)
  39. {
  40. ps->Teleport(matrix);
  41. ps->Restart(rand());
  42. ps->AutoDelete(false);
  43. ps->SetScale(scale);
  44. ps->SetTimeScale(timeScale);
  45. }
  46. connectToObject = reader.String();
  47. needUpdate = connectToObject.NotEmpty();
  48. needUpdate |= CreateSounds(reader, &pos);
  49. noSwing = reader.Bool();
  50. needUpdate |= noSwing;
  51. if(noSwing && ps)
  52. {
  53. ps->SetLocalMode(true);
  54. }
  55. return true;
  56. }
  57. //Показать/скрыть объект
  58. void MissionParticles::Show(bool isShow)
  59. {
  60. LogicDebug(isShow ? "Show" : "Hide");
  61. Activate(isShow);
  62. MissionSoundBase::Show(isShow);
  63. }
  64. //Активировать/деактивировать объект
  65. void MissionParticles::Activate(bool isActive)
  66. {
  67. MissionSoundBase::Activate(isActive);
  68. if(ps)
  69. {
  70. ps->Restart(rand());
  71. ps->PauseEmission(!isActive);
  72. }
  73. if(needUpdate)
  74. {
  75. if(isActive)
  76. {
  77. SetUpdate(&MissionParticles::Work, ML_EXECUTE1);
  78. }else{
  79. DelUpdate(&MissionParticles::Work);
  80. }
  81. }else{
  82. DelUpdate(&MissionParticles::Work);
  83. }
  84. }
  85. #ifndef MIS_STOP_EDIT_FUNCS
  86. //Перевести объект в спящий режим
  87. void MissionParticles::EditMode_Sleep(bool isSleep)
  88. {
  89. MissionObject::EditMode_Sleep(isSleep);
  90. if(EditMode_IsSleep() || !EditMode_IsVisible())
  91. {
  92. if(ps)
  93. {
  94. ps->Restart(rand());
  95. ps->PauseEmission(true);
  96. }
  97. bool curState = IsActive();
  98. Activate(false);
  99. MissionObject::Activate(curState);
  100. }else{
  101. Activate(IsActive());
  102. }
  103. }
  104. //Показать или скрыть объект в редакторе
  105. void MissionParticles::EditMode_Visible(bool isVisible)
  106. {
  107. MissionObject::EditMode_Visible(isVisible);
  108. EditMode_Sleep(EditMode_IsSleep());
  109. }
  110. #endif
  111. //Получить матрицу объекта
  112. Matrix & MissionParticles::GetMatrix(Matrix & mtx)
  113. {
  114. if(connectToObject.NotEmpty())
  115. {
  116. if(objectPtr.Validate())
  117. {
  118. mtx = matrix*objectPtr.Ptr()->GetMatrix(Matrix());
  119. return mtx;
  120. }else{
  121. if(FindObject(connectToObject, objectPtr))
  122. {
  123. mtx = matrix*objectPtr.Ptr()->GetMatrix(Matrix());
  124. return mtx;
  125. }
  126. }
  127. }
  128. //Компенсируем свинг, если активна опция
  129. if(noSwing)
  130. {
  131. //Видовая матрица с включёной свинг матрицей
  132. Matrix view = Render().GetView();
  133. //Чистая видовая матрица
  134. Matrix realView = Mission().GetInverseSwingMatrix()*Matrix(view).Inverse();
  135. realView.Inverse();
  136. //Разносная матрица
  137. Matrix diff = Matrix(Matrix(realView), Matrix(view).Inverse());
  138. mtx.EqMultiply(matrix, diff);
  139. return mtx;
  140. }
  141. return mtx = matrix;
  142. }
  143. //Инициализировать объект
  144. bool MissionParticles::EditMode_Create(MOPReader & reader)
  145. {
  146. IGMXScene * m = model;
  147. model = Geometry().CreateGMX("editor\\particles.gmx", &Mission().Animation() , &Mission().Particles(), &Mission().Sound());
  148. if(m)
  149. {
  150. m->Release();
  151. }
  152. Create(reader);
  153. SetUpdate(&MissionParticles::EditModeWork, ML_GEOMETRY5);
  154. return true;
  155. }
  156. //Обновить параметры
  157. bool MissionParticles::EditMode_Update(MOPReader & reader)
  158. {
  159. Create(reader);
  160. return true;
  161. }
  162. //Получить размеры описывающего ящика
  163. void MissionParticles::EditMode_GetSelectBox(Vector & min, Vector & max)
  164. {
  165. if(model)
  166. {
  167. min = model->GetLocalBound().vMin;
  168. max = model->GetLocalBound().vMax;
  169. return;
  170. }
  171. min = -0.2f;
  172. max = 0.2f;
  173. }
  174. //Работа партиклов в режиме игры
  175. void _cdecl MissionParticles::Work(float dltTime, long level)
  176. {
  177. Matrix mtx;
  178. GetMatrix(mtx);
  179. UpdatePosition(mtx.pos);
  180. if(ps)
  181. {
  182. ps->SetTransform(mtx);
  183. }
  184. Update(dltTime);
  185. }
  186. //Работа детектора в режиме редактирования
  187. void _cdecl MissionParticles::EditModeWork(float dltTime, long level)
  188. {
  189. if(!Mission().EditMode_IsAdditionalDraw()) return;
  190. if(ps && !ps->IsAlive() && EditMode_IsSelect()) ps->Restart(rand());
  191. Matrix mtx;
  192. GetMatrix(mtx);
  193. if(model)
  194. {
  195. model->SetTransform(mtx);
  196. model->Draw();
  197. }else Render().DrawSphere(mtx.pos, 0.1f, ps ? 0x80808080 : 0x80800000);
  198. }
  199. //Обработчик команд для объекта
  200. void MissionParticles::Command(const char * id, dword numParams, const char ** params)
  201. {
  202. if(!id || !id[0]) return;
  203. if(string::IsEqual(id, "pause") && ps)
  204. {
  205. ps->PauseEmission(true);
  206. return;
  207. }
  208. if(string::IsEqual(id, "play") && ps)
  209. {
  210. ps->PauseEmission(false);
  211. return;
  212. }
  213. if(string::IsEqual(id, "teleport"))
  214. {
  215. if(numParams < 1) return;
  216. MOSafePointer obj;
  217. if(FindObject(ConstString(params[0]), obj))
  218. {
  219. //Получаем конечные матрицы
  220. Matrix mtx, mtxCur;
  221. GetMatrix(mtxCur);
  222. obj.Ptr()->GetMatrix(mtx);
  223. Matrix mobj(Matrix(matrix).Inverse(), mtxCur);
  224. matrix = mtx*mobj.Inverse();
  225. Work(0.0f, 0);
  226. LogicDebug("Teleport to mission object \"%s\"", obj.Ptr()->GetObjectID().c_str());
  227. }else{
  228. LogicDebugError("Can't to mission object \"%s\", object not found...", params[0]);
  229. }
  230. }else{
  231. LogicDebugError("Unknown command \"%s\"", id);
  232. }
  233. }
  234. //============================================================================================
  235. //Параметры инициализации
  236. //============================================================================================
  237. const char * MissionParticles::comment =
  238. "Representation particle system in mission\n"
  239. " \n"
  240. "Commands list:\n"
  241. "----------------------------------------\n"
  242. " Teleport geometry to position of some\n"
  243. " mission object\n"
  244. "----------------------------------------\n"
  245. " command: teleport\n"
  246. " parm: name of mission object\n"
  247. " \n"
  248. " command: pause\n"
  249. " \n"
  250. " command: play\n"
  251. " \n"
  252. " ";
  253. MOP_BEGINLISTCG(MissionParticles, "Particles system", '1.00', 0x0fffffff, MissionParticles::comment, "Effects")
  254. MOP_STRING("Paticles name", "particles")
  255. MOP_POSITION("Position", Vector(0.0f))
  256. MOP_ANGLES("Angles", Vector(0.0f))
  257. MOP_FLOATEX("Scale", 1.0f, 0.001f, 10000000.0f)
  258. MOP_FLOATEX("Time scale", 1.0f, 0.001f, 10000000.0f)
  259. MOP_STRING("Connect to object", "")
  260. MISSION_SOUND_PARAMS
  261. MOP_BOOLC("No swing", false, "No swing particles in swing machine")
  262. MOP_ENDLIST(MissionParticles)