MissionLocator.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //============================================================================================
  2. // Spirenkov Maxim, 2004
  3. //============================================================================================
  4. // Mission objects
  5. //============================================================================================
  6. // MissionLocator
  7. //============================================================================================
  8. #include "MissionLocator.h"
  9. //============================================================================================
  10. MissionLocator::MissionLocator()
  11. {
  12. sound = null;
  13. }
  14. MissionLocator::~MissionLocator()
  15. {
  16. ReleaseSound();
  17. }
  18. //============================================================================================
  19. //Инициализировать объект
  20. bool MissionLocator::Create(MOPReader & reader)
  21. {
  22. objectPtr.Reset();
  23. if(!MissionObject::Create(reader)) return false;
  24. //Позиция
  25. Vector pos = reader.Position();
  26. Vector ang = reader.Angles();
  27. matrix.Build(ang, pos);
  28. connectToObject = reader.String();
  29. if(reader.Bool())
  30. {
  31. SetUpdate(&MissionLocator::Draw, ML_ALPHA1);
  32. }
  33. return true;
  34. }
  35. //Инициализировать объект
  36. bool MissionLocator::EditMode_Create(MOPReader & reader)
  37. {
  38. SetUpdate(&MissionLocator::EditMode_Draw, ML_ALPHA1);
  39. Create(reader);
  40. return true;
  41. }
  42. //Обновить параметры
  43. bool MissionLocator::EditMode_Update(MOPReader & reader)
  44. {
  45. Create(reader);
  46. return true;
  47. }
  48. //Получить размеры описывающего ящика
  49. void MissionLocator::EditMode_GetSelectBox(Vector & min, Vector & max)
  50. {
  51. min = -0.5f;
  52. max = 0.5f;
  53. }
  54. //Получить матрицу объекта
  55. Matrix & MissionLocator::GetMatrix(Matrix & mtx)
  56. {
  57. if(connectToObject.NotEmpty())
  58. {
  59. if(objectPtr.Validate())
  60. {
  61. mtx = matrix*objectPtr.Ptr()->GetMatrix(Matrix());
  62. return mtx;
  63. }else{
  64. if(FindObject(connectToObject, objectPtr))
  65. {
  66. mtx = matrix*objectPtr.Ptr()->GetMatrix(Matrix());
  67. return mtx;
  68. }
  69. }
  70. }
  71. return (mtx = matrix);
  72. };
  73. //Нарисовать модельку
  74. void _cdecl MissionLocator::Draw(float dltTime, long level)
  75. {
  76. Matrix mtx;
  77. GetMatrix(mtx);
  78. Render().DrawMatrix(mtx);
  79. Render().DrawSphere(mtx.pos, 0.1f, 0x90ffff80);
  80. Render().Print(mtx.pos, 5.0f, 1.0f, 0xffffff80, "Mission locator");
  81. Render().Print(mtx.pos, 5.0f, 2.0f, 0xffffff80, "\"%s\"", GetObjectID().c_str());
  82. };
  83. //Нарисовать модельку
  84. void _cdecl MissionLocator::EditMode_Draw(float dltTime, long level)
  85. {
  86. if(!EditMode_IsVisible()) return;
  87. Draw(dltTime, level);
  88. };
  89. //Пересоздать объект
  90. void MissionLocator::Restart()
  91. {
  92. ReleaseSound();
  93. DelUpdate(&MissionLocator::UpdateSound);
  94. }
  95. //Обработчик команд для объекта
  96. void MissionLocator::Command(const char * id, dword numParams, const char ** params)
  97. {
  98. if(string::IsEqual(id, "RedirectSoundEvent"))
  99. {
  100. LogicDebug("RedirectSoundEvent");
  101. ReleaseSound();
  102. if(numParams > 0 && string::NotEmpty(params[0]))
  103. {
  104. Matrix mtx;
  105. GetMatrix(mtx);
  106. sound = Sound().Create3D(params[0], mtx.pos, _FL_, true, false);
  107. SetUpdate(&MissionLocator::UpdateSound, ML_ALPHA5);
  108. }else{
  109. LogicDebugError("Error sound name: %s", id);
  110. }
  111. }else{
  112. LogicDebugError("Unknown command: \"%s\"", numParams > 0 ? params[0] : "<no command params>");
  113. }
  114. }
  115. //Обновить позицию и состояние звука
  116. void _cdecl MissionLocator::UpdateSound(float dltTime, long level)
  117. {
  118. if(sound)
  119. {
  120. if(sound->IsPlay())
  121. {
  122. if(objectPtr.Validate())
  123. {
  124. Matrix mtx;
  125. GetMatrix(mtx);
  126. sound->SetPosition(mtx.pos);
  127. }
  128. return;
  129. }
  130. }
  131. ReleaseSound();
  132. DelUpdate(&MissionLocator::UpdateSound);
  133. }
  134. //Удалить звук
  135. void MissionLocator::ReleaseSound()
  136. {
  137. if(sound)
  138. {
  139. sound->Release();
  140. sound = null;
  141. }
  142. }
  143. //============================================================================================
  144. //Параметры инициализации
  145. //============================================================================================
  146. MOP_BEGINLISTCG(MissionLocator, "Mission locator", '1.00', 0x0fffffff, "Editable locator", "Geometry")
  147. MOP_POSITION("Position", Vector(0.0f))
  148. MOP_ANGLES("Angles", Vector(0.0f))
  149. MOP_STRING("Connect to object", "")
  150. MOP_BOOL("Debug draw", false)
  151. MOP_ENDLIST(MissionLocator)