WorldMapWalkmesh.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * V-Gears
  3. * Copyright (C) 2022 V-Gears Team
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma once
  19. #include "OGRE/Ogre.h"
  20. #include "common/TypeDefine.h"
  21. /**
  22. * Represents a walkmesh for a world map.
  23. */
  24. class WorldMapWalkmesh{
  25. public:
  26. class Triangle{
  27. public:
  28. /**
  29. * Triangle reached when exiting this triangle from one of its
  30. * borders.
  31. */
  32. struct Target{
  33. /**
  34. * Target triangle ID.
  35. *
  36. * If -1, there is no way out by this side.
  37. */
  38. int id;
  39. /**
  40. * False if the target triangle is truly adjacent, true if
  41. * it warps to other side of the world.
  42. */
  43. bool warp;
  44. };
  45. /**
  46. * Creates a triangle
  47. *
  48. * @param[in] id Triangle ID.
  49. * @param[in] a X, Y, Z coordinates of the first vertex.
  50. * @param[in] b X, Y, Z coordinates of the second vertex.
  51. * @param[in] c X, Y, Z coordinates of the third vertex.
  52. * @param[in] walkability Walkability mode ID.
  53. * @param[in] event Event triggered upon entering the triangle,
  54. * or -1 for no event.
  55. */
  56. Triangle(
  57. const unsigned int id, Ogre::Vector3 a, const Ogre::Vector3 b,
  58. const Ogre::Vector3 c, const unsigned int walkability, const int event
  59. );
  60. /**
  61. * Retrieves the triangle ID in the walkmesh.
  62. *
  63. * @return The triangle ID.
  64. */
  65. unsigned int getId() const;
  66. /**
  67. * Retrieves the coordinates of the first vertex
  68. *
  69. * @return Vertex coordinates (X, Y, Z).
  70. */
  71. Ogre::Vector3 getA() const;
  72. /**
  73. * Retrieves the coordinates of the second vertex
  74. *
  75. * @return Vertex coordinates (X, Y, Z).
  76. */
  77. Ogre::Vector3 getB() const;
  78. /**
  79. * Retrieves the coordinates of the third vertex
  80. *
  81. * @return Vertex coordinates (X, Y, Z).
  82. */
  83. Ogre::Vector3 getC() const;
  84. /**
  85. * Retrieves the triangle reached when exiting the triangle by
  86. * the side between the vertices A and B.
  87. *
  88. * @return Target triangle.
  89. */
  90. Target getAB() const;
  91. /**
  92. * Retrieves the triangle reached when exiting the triangle by
  93. * the side between the vertices B and C.
  94. *
  95. * @return Target triangle.
  96. */
  97. Target getBC() const;
  98. /**
  99. * Retrieves the triangle reached when exiting the triangle by
  100. * the side between the vertices C and A.
  101. *
  102. * @return Target triangle.
  103. */
  104. Target getCA() const;
  105. /**
  106. * Retrieves the triangle walkability mode.
  107. *
  108. * @return The walkability mode number.
  109. */
  110. unsigned int getWalkability() const;
  111. /**
  112. * Retrieves the event triggered upon entering the triangle.
  113. *
  114. * @return The ID of the event triggered on entering, or -1 if
  115. * none.
  116. */
  117. int getEvent() const;
  118. /**
  119. * Sets the triangle reached when exiting the triangle by the
  120. * side between the vertices A and B.
  121. *
  122. * @param[in] id Target triangle ID. -1 to indicate no way out
  123. * by the edge.
  124. * @param[in] warp True to indicate world warping (ie, if
  125. * walking into an edge of the world gets you at the other end).
  126. */
  127. void setAB(const int id, const bool warp);
  128. /**
  129. * Sets the triangle reached when exiting the triangle by the
  130. * side between the vertices B and C.
  131. *
  132. * @param[in] id Target triangle ID. -1 to indicate no way out
  133. * by the edge.
  134. * @param[in] warp True to indicate world warping (ie, if
  135. * walking into an edge of the world gets you at the other end).
  136. */
  137. void setBC(const int id, const bool warp);
  138. /**
  139. * Sets the triangle reached when exiting the triangle by the
  140. * side between the vertices C and A.
  141. *
  142. * @param[in] id Target triangle ID. -1 to indicate no way out
  143. * by the edge.
  144. * @param[in] warp True to indicate world warping (ie, if
  145. * walking into an edge of the world gets you at the other end).
  146. * the world gets you at the other end).
  147. */
  148. void setCA(const int id, const bool warp);
  149. private:
  150. /**
  151. * Triangle ID.
  152. */
  153. unsigned int id_;
  154. /**
  155. * First vertex, X, Y, Z coordinates.
  156. */
  157. Ogre::Vector3 a_;
  158. /**
  159. * Second vertex, X, Y, Z coordinates.
  160. */
  161. Ogre::Vector3 b_;
  162. /**
  163. * Third vertex, X, Y, Z coordinates.
  164. */
  165. Ogre::Vector3 c_;
  166. /**
  167. * Target reached when exiting by the side formed by {@see a_}
  168. * and {@see b_}.
  169. */
  170. Target ab_;
  171. /**
  172. * Target reached when exiting by the side formed by {@see b_}
  173. * and {@see c_}.
  174. */
  175. Target bc_;
  176. /**
  177. * Target reached when exiting by the side formed by {@see c_}
  178. * and {@see a_}.
  179. */
  180. Target ca_;
  181. /**
  182. * Walkability mode.
  183. */
  184. unsigned int walkability_;
  185. /**
  186. * Id of the event triggered upon entering the triangle.
  187. */
  188. int event_;
  189. };
  190. /**
  191. * Constructor.
  192. *
  193. * @param[in] id World map ID.
  194. * @param[in] warp Indicates if the world map is warpable (ie, if
  195. * walking into an edge of the world gets you at the other end).
  196. */
  197. WorldMapWalkmesh(const unsigned int id, const bool warp);
  198. /**
  199. * Adds a triangle.
  200. *
  201. * @param[in] a X, Y, Z coordinates of the first vertex.
  202. * @param[in] b X, Y, Z coordinates of the second vertex.
  203. * @param[in] c X, Y, Z coordinates of the third vertex.
  204. * @param[in] walkability Walkability mode ID.
  205. * @param[in] event Event triggered upon entering the triangle. -1 for
  206. * no event.
  207. */
  208. void addTriangle(
  209. const Ogre::Vector3 a, const Ogre::Vector3 b, const Ogre::Vector3 c,
  210. const unsigned int walkability, const int event
  211. );
  212. /**
  213. * Generates a world map walkmesh file.
  214. *
  215. * @param[in] path Path where the file will be saved (directory must
  216. * exist).
  217. */
  218. void generate(const std::string path);
  219. private:
  220. /**
  221. * Worldmap ID
  222. */
  223. unsigned int id_;
  224. /**
  225. * Indicates if the world is a warpable one.
  226. */
  227. bool warp_;
  228. /**
  229. * The list of triangles.
  230. */
  231. std::vector<Triangle> triangles_;
  232. /**
  233. * Indicates if the relations between triangles has been calculated
  234. * since the last triangle was added.
  235. */
  236. bool calculated_;
  237. /**
  238. * Calculates the relations between triangles.
  239. */
  240. void calculate();
  241. };