Data.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include "Data.h"
  2. #include <M2Datei.h>
  3. #include <Textur2D.h>
  4. // Konstructor
  5. GameData::GameData(const char* shipM2, const char* asteroidsM2)
  6. : ReferenceCounter()
  7. {
  8. world = new Welt2D();
  9. //world->setAirResistance( 0.01f );
  10. schuss = new RCArray< Schuss >();
  11. ship = 0;
  12. asteroid = new RCArray< Asteroid >();
  13. aData = new Model2DData * [7]();
  14. sData = new Model2DData * [2]();
  15. M2Datei m2d(asteroidsM2);
  16. m2d.leseDaten();
  17. for (int i = 0; i < 7; i++)
  18. {
  19. Text name = "";
  20. name.append((char)('a' + i));
  21. aData[i] = m2d.ladeModel(name);
  22. }
  23. m2d.setPfad(shipM2);
  24. m2d.leseDaten();
  25. for (int i = 0; i < 2; i++)
  26. {
  27. Text name = "";
  28. name.append((char)('a' + i));
  29. sData[i] = m2d.ladeModel(name);
  30. }
  31. shipN = 0;
  32. score = 0;
  33. scoreCheck = score * 11197;
  34. breite = 0;
  35. höhe = 0;
  36. aGröße = 0;
  37. maxTimer = 0;
  38. mTimer = 0;
  39. timer = 0;
  40. beendet = 1;
  41. rend = 0;
  42. gameTime = 0;
  43. tastenStände = 0;
  44. rGen = 0;
  45. }
  46. // Destructor
  47. GameData::~GameData()
  48. {
  49. schuss->release();
  50. if (ship)
  51. ship->release();
  52. asteroid->release();
  53. for (int i = 0; i < 7; i++)
  54. aData[i]->release();
  55. for (int i = 0; i < 2; i++)
  56. sData[i]->release();
  57. delete[] aData;
  58. delete[] sData;
  59. if (rGen)
  60. rGen->release();
  61. world->release();
  62. }
  63. // private
  64. Asteroid* GameData::createNewAsteroid()
  65. {
  66. int num = (int)(rGen->rand() * 7);
  67. double sw = (rGen->rand() * 360) / 180.0 * PI;
  68. Vec2< float > speed((float)cos(sw), (float)sin(sw));
  69. speed *= (float)(rGen->rand() * 40);
  70. Vec2< float > pos((float)rGen->rand() * breite, (float)rGen->rand() * höhe);
  71. if ((ship->getPosition() - Vertex((float)world->getWorldInfo().size.x, 0) - pos).getLength() > world->getWorldInfo().size.getLength() / 3 &&
  72. (ship->getPosition() + Vertex((float)world->getWorldInfo().size.x, 0) - pos).getLength() > world->getWorldInfo().size.getLength() / 3 &&
  73. (ship->getPosition() - Vertex(0, (float)world->getWorldInfo().size.y) - pos).getLength() > world->getWorldInfo().size.getLength() / 3 &&
  74. (ship->getPosition() + Vertex(0, (float)world->getWorldInfo().size.y) - pos).getLength() > world->getWorldInfo().size.getLength() / 3 &&
  75. (ship->getPosition() - Vertex((float)world->getWorldInfo().size.x, (float)world->getWorldInfo().size.y) - pos).getLength() > world->getWorldInfo().size.getLength() / 3 &&
  76. (ship->getPosition() + Vertex((float)world->getWorldInfo().size.x, (float)world->getWorldInfo().size.y) - pos).getLength() > world->getWorldInfo().size.getLength() / 3 &&
  77. (ship->getPosition() + Vertex((float)world->getWorldInfo().size.x, -(float)world->getWorldInfo().size.y) - pos).getLength() > world->getWorldInfo().size.getLength() / 3 &&
  78. (ship->getPosition() + Vertex(-(float)world->getWorldInfo().size.x, (float)world->getWorldInfo().size.y) - pos).getLength() > world->getWorldInfo().size.getLength() / 3 &&
  79. (ship->getPosition() - pos).getLength() > world->getWorldInfo().size.getLength() / 3)
  80. {
  81. if (speed.x < 0)
  82. pos.x = (float)(breite + 200);
  83. Asteroid* ast = new Asteroid(dynamic_cast<Model2DData*>(aData[num]->getThis()), 0, pos, speed, (float)(rGen->rand() * 100) / 75.f, (float)sw, aGröße / 1000.f, num);
  84. int aAnz = asteroid->getEintragAnzahl();
  85. for (int i = 0; i < aAnz; i++)
  86. {
  87. if (ast->istModelInnen(asteroid->z(i)))
  88. {
  89. ast->release();
  90. return 0;
  91. }
  92. }
  93. return ast;
  94. }
  95. return 0;
  96. }
  97. // nicht constant
  98. void GameData::reset(Text* zOptionen)
  99. {
  100. gameTime = 0;
  101. timer = 0;
  102. beendet = 0;
  103. score = 0;
  104. scoreCheck = score * 11197;
  105. tastenStände = 0;
  106. if (ship)
  107. ship = (Ship*)ship->release();
  108. world->removeAll();
  109. schuss->leeren();
  110. asteroid->leeren();
  111. Text* tmp = zOptionen->getTeilText(zOptionen->positionVon('=') + 1, zOptionen->positionVon(','));
  112. breite = (int)*tmp;
  113. tmp->release();
  114. tmp = zOptionen->getTeilText(zOptionen->positionVon('=', 1) + 1, zOptionen->positionVon(',', 1));
  115. höhe = (int)*tmp;
  116. tmp->release();
  117. tmp = zOptionen->getTeilText(zOptionen->positionVon('=', 2) + 1, zOptionen->positionVon(',', 2));
  118. aGröße = (int)*tmp;
  119. tmp->release();
  120. tmp = zOptionen->getTeilText(zOptionen->positionVon('=', 3) + 1, zOptionen->positionVon(',', 3));
  121. maxTimer = (int)*tmp;
  122. tmp->release();
  123. mTimer = maxTimer;
  124. tmp = zOptionen->getTeilText(zOptionen->positionVon('=', 4) + 1, zOptionen->positionVon(',', 4));
  125. shipN = (int)*tmp;
  126. tmp->release();
  127. Vec2< float > shipPos((float)(breite / 2), (float)(höhe / 2));
  128. Vec2< float > shipSpeed(0, 0);
  129. float shipR = (float)-PI / 2;
  130. if (rGen)
  131. rGen = (RandomGenerator*)rGen->release();
  132. rGen = new RandomGenerator();
  133. if (zOptionen->positionVon('=', 7) >= 0)
  134. {
  135. tmp = zOptionen->getTeilText(zOptionen->positionVon('=', 7) + 1);
  136. rGen->setSeed((__int64)*tmp);
  137. tmp->release();
  138. }
  139. ship = new Ship(dynamic_cast<Model2DData*>(sData[shipN]->getThis()), shipPos, shipSpeed, shipR);
  140. world->addObject(ship);
  141. }
  142. bool GameData::tick(double tickVal)
  143. {
  144. if (beendet)
  145. {
  146. bool ret = rend;
  147. rend = 0;
  148. return ret;
  149. }
  150. cs.lock();
  151. gameTime += tickVal;
  152. // Timer
  153. timer -= tickVal;
  154. if (timer <= 0)
  155. {
  156. mTimer -= 25;
  157. if (mTimer < 500)
  158. mTimer = 500;
  159. timer = mTimer / 1000.0;
  160. for (int i = 0; i < rGen->rand() * 1 + 1; i++)
  161. {
  162. Asteroid* newA = createNewAsteroid();
  163. if (newA)
  164. {
  165. asteroid->add(newA);
  166. world->addObject(dynamic_cast<Asteroid*>(newA->getThis()));
  167. }
  168. }
  169. }
  170. // Update
  171. ship->setTastenstände(tastenStände);
  172. int aAnz = asteroid->getEintragAnzahl();
  173. int sAnz = schuss->getEintragAnzahl();
  174. for (int i = aAnz - 1; i >= 0; i--)
  175. {
  176. if (asteroid->z(i)->getSize() == 0)
  177. {
  178. score += asteroid->z(i)->getScore();
  179. scoreCheck = score * 11197;
  180. world->removeObject(asteroid->z(i));
  181. asteroid->remove(i);
  182. aAnz--;
  183. }
  184. }
  185. for (int i = sAnz - 1; i >= 0; i--)
  186. {
  187. Vec2< float > pos = schuss->z(i)->getPosition();
  188. if (pos.x < 0 || pos.y < 0 || pos.x > breite || pos.y > höhe)
  189. {
  190. sAnz--;
  191. world->removeObject(schuss->z(i));
  192. schuss->remove(i);
  193. }
  194. }
  195. // Collision
  196. for (int i = sAnz - 1; i >= 0; i--)
  197. {
  198. Schuss* zs = schuss->z(i);
  199. bool b = 0;
  200. for (int j = 0; j < aAnz; j++)
  201. {
  202. Polygon2D ap;
  203. Polygon2D bp;
  204. Punkt pa;
  205. Punkt pb;
  206. if (asteroid->z(j)->istGetroffen(zs, ap, bp, pa, pb, rGen))
  207. {
  208. Array< Polygon2D >* npaA = new Array< Polygon2D >();
  209. npaA->add(ap);
  210. Model2DData* npdA = new Model2DData();
  211. npdA->erstelleModell(npaA);
  212. float rot = asteroid->z(j)->getDrehung();
  213. char id = asteroid->z(j)->getId();
  214. if (id >= 0)
  215. id = -id - 1;
  216. Asteroid* newA = new Asteroid(npdA, dynamic_cast<Bild*>(asteroid->z(j)->zTextur()->zTextur()->getThis()), pa.rotation(rot) + asteroid->z(j)->getPosition(), asteroid->z(j)->getSpeed(), asteroid->z(j)->getDrehungSpeed(), rot, asteroid->z(j)->getSize(), id);
  217. asteroid->add(newA);
  218. world->addObject(dynamic_cast<Asteroid*>(newA->getThis()));
  219. Array< Polygon2D >* npaB = new Array< Polygon2D >();
  220. npaB->add(bp);
  221. Model2DData* npdB = new Model2DData();
  222. npdB->erstelleModell(npaB);
  223. newA = new Asteroid(npdB, dynamic_cast<Bild*>(asteroid->z(j)->zTextur()->zTextur()->getThis()), pb.rotation(rot) + asteroid->z(j)->getPosition(), asteroid->z(j)->getSpeed(), asteroid->z(j)->getDrehungSpeed(), rot, asteroid->z(j)->getSize(), id);
  224. asteroid->add(newA);
  225. world->addObject(dynamic_cast<Asteroid*>(newA->getThis()));
  226. b = 1;
  227. asteroid->z(j)->setSize(0);
  228. }
  229. }
  230. if (!b)
  231. b |= ship->istGetroffen(zs);
  232. if (b)
  233. {
  234. world->removeObject(schuss->z(i));
  235. schuss->remove(i);
  236. sAnz--;
  237. }
  238. }
  239. for (int i = 0; i < aAnz && !beendet; i++)
  240. beendet |= ship->istModelInnen(asteroid->z(i));
  241. beendet |= (score * 11197 != scoreCheck);
  242. if (score * 11197 != scoreCheck)
  243. {
  244. score = 0;
  245. scoreCheck = 0;
  246. }
  247. cs.unlock();
  248. return 1;
  249. }
  250. // constant
  251. int GameData::getScore() const
  252. {
  253. return score;
  254. }
  255. bool GameData::istBeendet() const
  256. {
  257. return beendet;
  258. }