PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/nebula/code/src/pdserver/collserver2d_main.cc

https://code.google.com/p/u-nebula/
C++ | 200 lines | 95 code | 22 blank | 83 comment | 21 complexity | 1fc5f25a424712266eb4e018e767032d MD5 | raw file
Possible License(s): AGPL-3.0, 0BSD, BSD-3-Clause
  1. #define N_IMPLEMENTS pdCollideServer2D
  2. //------------------------------------------------------------------------------
  3. // cwcollserver2d_main.cc
  4. // (C) 2002 Clockwise Ltd. -- I.Kliot
  5. //------------------------------------------------------------------------------
  6. #include "pdnode/collidenode2d.h"
  7. #include "pdserver/collideserver2d.h"
  8. nNebulaScriptClass(pdCollideServer2D, "nroot");
  9. //------------------------------------------------------------------------------
  10. /**
  11. pdCollideServer2D()
  12. default constructor
  13. created 16-May-2002 -- Ilya Kliot
  14. */
  15. pdCollideServer2D::pdCollideServer2D() : nRoot(), visualize(false), active(true)
  16. {
  17. this->AddClass("default");
  18. this->SetRelation("default", "default", false);
  19. }
  20. //------------------------------------------------------------------------------
  21. /**
  22. ~pdCollideServer2D()
  23. destructor
  24. created 16-May-2002 -- Ilya Kliot
  25. */
  26. pdCollideServer2D::~pdCollideServer2D()
  27. {
  28. this->curr_context.clear();
  29. this->next_context.clear();
  30. this->collnodes_list.clear();
  31. this->classes.clear();
  32. }
  33. //------------------------------------------------------------------------------
  34. /**
  35. AddCollNode(pdCollideNode2D* node)
  36. add node to list of collision nodes
  37. 1 time per node occurred
  38. created 20-May-2002 -- Ilya Kliot
  39. */
  40. void pdCollideServer2D::AddCollNode(pdCollideNode2D* node)
  41. {
  42. this->collnodes_list.push_back(node);
  43. }
  44. //------------------------------------------------------------------------------
  45. /**
  46. Attach(pdCollideNode2D* node)
  47. add node to render context
  48. 1 time per node occurred
  49. created 20-May-2002 -- Ilya Kliot
  50. */
  51. pdCollContext2D* pdCollideServer2D::Attach(pdCollideNode2D* node)
  52. {
  53. this->next_context.push_back(node);
  54. return &this->curr_context;
  55. }
  56. //------------------------------------------------------------------------------
  57. /**
  58. Remove(pdCollideNode2D* node)
  59. remove node from contexts
  60. created 20-May-2002 -- Ilya Kliot
  61. */
  62. void pdCollideServer2D::Remove(pdCollideNode2D* node)
  63. {
  64. this->curr_context.remove(node);
  65. this->next_context.remove(node);
  66. this->collnodes_list.remove(node);
  67. }
  68. //------------------------------------------------------------------------------
  69. /**
  70. created 16-may-2002 -- Ilya Kliot
  71. clear current context
  72. */
  73. void pdCollideServer2D::ClearContext()
  74. {
  75. for (pdCollContext2D::iterator i = this->curr_context.begin();
  76. i != this->curr_context.end(); i++)
  77. (*i)->SetContext(0);
  78. this->next_context.clear();
  79. }
  80. //------------------------------------------------------------------------------
  81. /**
  82. created 09-Jul-2002 -- Ilya Kliot
  83. get id of class by name
  84. */
  85. long pdCollideServer2D::find_class(const char* name) const
  86. {
  87. size_t num = this->classes.size();
  88. for (size_t i = 0; i < num; i++)
  89. if (!n_stricmp(name, this->classes[i].c_str())) return (long)i;
  90. return -1;
  91. }
  92. //------------------------------------------------------------------------------
  93. /**
  94. created 09-Jul-2002 -- Ilya Kliot
  95. add class of collision objects
  96. */
  97. void pdCollideServer2D::AddClass(const char* name)
  98. {
  99. if (this->find_class(name) < 0)
  100. {
  101. this->classes.push_back(name);
  102. this->rel_mx.inc();
  103. }
  104. }
  105. //------------------------------------------------------------------------------
  106. /**
  107. created 09-Jul-2002 -- Ilya Kliot
  108. get name of class by id
  109. */
  110. const char* pdCollideServer2D::GetCClassName(long id) const
  111. {n_precondition(id >= 0 && id < (long)this->classes.size() && "id of class out of range");
  112. return classes[id].c_str();
  113. }
  114. //------------------------------------------------------------------------------
  115. /**
  116. created 09-Jul-2002 -- Ilya Kliot
  117. enables or disables collision between 2 classes
  118. */
  119. void pdCollideServer2D::SetRelation(const char* name1, const char* name2, bool check)
  120. {
  121. long id1(this->find_class(name1));
  122. long id2(this->find_class(name2));
  123. n_assert(id1 >= 0 && "first class undefined");
  124. n_assert(id2 >= 0 && "second class undefined");
  125. this->rel_mx[id1][id2] = check;
  126. this->rel_mx[id2][id1] = check;
  127. }
  128. //------------------------------------------------------------------------------
  129. /**
  130. created 09-Jul-2002 -- Ilya Kliot
  131. check relation between 2 classes
  132. */
  133. bool pdCollideServer2D::GetRelation(long id1, long id2) const
  134. {n_precondition(id1 >=0 && id1 < (long)this->rel_mx.dim() && "class 1 not declared");
  135. n_assert(id2 >=0 && id2 < (long)this->rel_mx.dim() && "class 2 not declared");
  136. return this->rel_mx[id1][id2];
  137. }
  138. //------------------------------------------------------------------------------
  139. /**
  140. created 09-Jul-2002 -- Ilya Kliot
  141. check relation between 2 classes by names
  142. */
  143. bool pdCollideServer2D::GetRelation(const char* name1, const char* name2) const
  144. {
  145. return this->GetRelation(this->find_class(name1), this->find_class(name2));
  146. }
  147. //------------------------------------------------------------------------------
  148. /**
  149. created 16-may-2002 -- Ilya Kliot
  150. swap contexts and begin new context;
  151. */
  152. void pdCollideServer2D::Trigger()
  153. {
  154. if (!this->GetActive())
  155. return;
  156. this->curr_context.swap(this->next_context);
  157. this->ClearContext();
  158. }
  159. //------------------------------------------------------------------------------
  160. /**
  161. created 16-jul-2002 -- Ilya Kliot
  162. check for collision between node and
  163. all nodes in current_context
  164. */
  165. void pdCollideServer2D::Compute(pdCollideNode2D* node)
  166. {
  167. if (!this->GetActive()) return;
  168. //|| !node->GetActive() || node->GetCRType() == pdCollideNode2D::STATIC) return;
  169. if (this->curr_context.size() < 2) return;
  170. for (pdCollContext2D::iterator i = this->curr_context.begin();
  171. i != this->curr_context.end(); i++){
  172. if (node != *i)
  173. {
  174. if (this->GetRelation((*i)->GetClassId(), node->GetClassId()))
  175. node->CheckCollide(**i);
  176. }
  177. }
  178. }