Map.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include "Map.h"
  2. #include <InitDatei.h>
  3. #include <Datei.h>
  4. #include <Text.h>
  5. #include <M2Datei.h>
  6. #include <DateiSystem.h>
  7. #include <TastaturEreignis.h>
  8. #include <KSGTDatei.h>
  9. #include <Zeit.h>
  10. #include <Rahmen.h>
  11. #include <Globals.h>
  12. #include <AsynchronCall.h>
  13. #include <Animation.h>
  14. // Inhalt der Map Klasse aus Map.h
  15. // Konstruktor
  16. Map::Map(KSGClient::MinigameServerClient* klient)
  17. : GameData("data/Minigames/Asteroids/models/ship.m2", "data/Minigames/Asteroids/models/asteroids.m2")
  18. {
  19. kamera = new Kamera2D();
  20. kamera->setWelt(dynamic_cast<Welt2D*>(world->getThis()), 1);
  21. kamera->setSize(800, 500);
  22. mapKamera = new Kamera2D();
  23. mapKamera->setWelt(dynamic_cast<Welt2D*>(world->getThis()), 0);
  24. this->klient = klient;
  25. aTextur = new Bild * [7]();
  26. sTextur = new Bild * [2]();
  27. // lade Flammen animation
  28. LTDBDatei flammenStartLTDB;
  29. flammenStartLTDB.setDatei(new Text("data/Minigames/Asteroids/bilder/f_start.ltdb"));
  30. flammenStartLTDB.leseDaten(0);
  31. Animation2DData* flammenStart = new Animation2DData();
  32. flammenStart->ladeAnimation(dynamic_cast<LTDBDatei*>(flammenStartLTDB.getThis()));
  33. flammenStart->setFPS(60);
  34. flammenStart->setWiederhohlend(0);
  35. LTDBDatei flammenLTDB;
  36. flammenLTDB.setDatei(new Text("data/Minigames/Asteroids/bilder/f_burn.ltdb"));
  37. Animation2DData* flammenBurn = new Animation2DData();
  38. flammenBurn->ladeAnimation(dynamic_cast<LTDBDatei*>(flammenLTDB.getThis()));
  39. flammenBurn->setFPS(60);
  40. flammenBurn->setWiederhohlend(1);
  41. flammenM = new Textur2D();
  42. flammenM->addAnimationZ(dynamic_cast<Animation2DData*>(flammenStart->getThis()));
  43. flammenM->addAnimationZ(dynamic_cast<Animation2DData*>(flammenBurn->getThis()));
  44. flammenL = new Textur2D();
  45. flammenL->addAnimationZ(dynamic_cast<Animation2DData*>(flammenStart->getThis()));
  46. flammenL->addAnimationZ(dynamic_cast<Animation2DData*>(flammenBurn->getThis()));
  47. flammenR = new Textur2D();
  48. flammenR->addAnimationZ(dynamic_cast<Animation2DData*>(flammenStart));
  49. flammenR->addAnimationZ(dynamic_cast<Animation2DData*>(flammenBurn));
  50. LTDBDatei td;
  51. td.setDatei(new Text("data/Minigames/Asteroids/bilder/asteroids.ltdb"));
  52. td.leseDaten(0);
  53. for (int i = 0; i < 7; i++)
  54. {
  55. Text name = "";
  56. name.append((char)('a' + i));
  57. aTextur[i] = td.laden(0, new Text(name + ".png"));
  58. }
  59. td.setDatei(new Text("data/Minigames/Asteroids/bilder/ship.ltdb"));
  60. td.leseDaten(0);
  61. for (int i = 0; i < 2; i++)
  62. {
  63. Text name = "";
  64. name.append((char)('a' + i));
  65. sTextur[i] = td.laden(0, new Text(name + ".png"));
  66. }
  67. map = new LRahmen();
  68. map->setFarbe(0xFFFFFFFF);
  69. map->setPosition(10, 10);
  70. map->setSize(200, 200);
  71. mapKamera->setPosition(10, 10);
  72. mapKamera->setSize(200, 200);
  73. autoKam = 0;
  74. }
  75. // Destruktor
  76. Map::~Map()
  77. {
  78. for (int i = 0; i < 7; i++)
  79. aTextur[i]->release();
  80. for (int i = 0; i < 2; i++)
  81. sTextur[i]->release();
  82. delete[] aTextur;
  83. delete[] sTextur;
  84. map->release();
  85. if (klient)
  86. klient->release();
  87. flammenM->release();
  88. flammenL->release();
  89. flammenR->release();
  90. if (kamera)
  91. kamera->release();
  92. if (mapKamera)
  93. mapKamera->release();
  94. }
  95. Asteroid* Map::createNewAsteroid()
  96. {
  97. Asteroid* a = __super::createNewAsteroid();
  98. if (a)
  99. {
  100. Textur2D* text = new Textur2D();
  101. text->setTexturZ(dynamic_cast<Bild*>(aTextur[a->getId()]->getThis()));
  102. a->setTextur(text);
  103. }
  104. return a;
  105. }
  106. // nicht constant
  107. void Map::reset(Text* zOptionen)
  108. {
  109. flammenL->setAnimation(-1);
  110. flammenR->setAnimation(-1);
  111. flammenM->setAnimation(-1);
  112. __super::reset(zOptionen);
  113. world->setSize(breite, höhe);
  114. world->setSize(1);
  115. world->setCircular(1);
  116. Text* tmp = zOptionen->getTeilText(zOptionen->positionVon('=', 5) + 1, zOptionen->positionVon(',', 5));
  117. bool fortsetzen = (int)*tmp != 0;
  118. tmp->release();
  119. tmp = zOptionen->getTeilText(zOptionen->positionVon('=', 6) + 1, zOptionen->positionVon(',', 6) > 0 ? zOptionen->positionVon(',', 6) : zOptionen->getLength());
  120. autoKam = (int)*tmp != 0;
  121. if (!autoKam)
  122. kamera->setDrehung(0);
  123. tmp->release();
  124. if (fortsetzen && DateiExistiert("data/Minigames/Asteroids/data/game.save") && klient)
  125. {
  126. M2Datei sad;
  127. int index = 0;
  128. int sadI = 0;
  129. Text pfad = "data/Minigames/Asteroids/data/sasave_";
  130. pfad += sadI;
  131. pfad += ".m2";
  132. sad.setPfad(pfad);
  133. sad.leseDaten();
  134. if (capture.istOffen())
  135. capture.close();
  136. capture.setDatei("data/Minigames/Asteroids/data/game.mgc");
  137. capture.open(Datei::Style::schreiben | Datei::Style::ende | Datei::Style::lesen);
  138. Datei* save = new Datei();
  139. save->setDatei("data/Minigames/Asteroids/data/game.save");
  140. save->open(Datei::Style::lesen);
  141. int br = 0;
  142. int hö = 0;
  143. int gr = 0;
  144. int shn = 0;
  145. __int64 seed;
  146. if (rGen)
  147. rGen = (RandomGenerator*)rGen->release();
  148. save->lese((char*)&seed, 8);
  149. rGen = new RandomGenerator();
  150. rGen->setSeed(seed);
  151. save->lese((char*)&gameTime, 8);
  152. save->lese((char*)&br, 4);
  153. save->lese((char*)&hö, 4);
  154. save->lese((char*)&gr, 4);
  155. save->lese((char*)&shn, 4);
  156. if (br == breite && hö == höhe && gr == aGröße && shn == shipN)
  157. {
  158. save->lese((char*)&score, 4);
  159. scoreCheck = score * 11197;
  160. save->lese((char*)&mTimer, 4);
  161. // Schiff laden
  162. Vec2< float > shipPos((float)(breite / 2), (float)(höhe / 2));
  163. Vec2< float > shipSpeed(0, 0);
  164. float shipR = (float)-PI / 2;
  165. float shipRSpeed = 0;
  166. save->lese((char*)&shipPos.x, 4);
  167. save->lese((char*)&shipPos.y, 4);
  168. save->lese((char*)&shipSpeed.x, 4);
  169. save->lese((char*)&shipSpeed.y, 4);
  170. save->lese((char*)&shipR, 4);
  171. save->lese((char*)&shipRSpeed, 4);
  172. ship->setPRS(shipPos, shipSpeed, shipR, shipRSpeed);
  173. int anz = 0;
  174. save->lese((char*)&anz, 4);
  175. for (int i = 0; i < anz; i++)
  176. { // Asteroiden Laden
  177. char n = 0;
  178. float x = 0;
  179. float y = 0;
  180. float xs = 0;
  181. float ys = 0;
  182. float rs = 0;
  183. float r = 0;
  184. float gr = 0;
  185. save->lese(&n, 1);
  186. save->lese((char*)&x, 4);
  187. save->lese((char*)&y, 4);
  188. save->lese((char*)&xs, 4);
  189. save->lese((char*)&ys, 4);
  190. save->lese((char*)&rs, 4);
  191. save->lese((char*)&r, 4);
  192. save->lese((char*)&gr, 4);
  193. Bild* at = 0;
  194. Model2DData* ad = 0;
  195. if (n < 0)
  196. {
  197. at = dynamic_cast<Bild*>(aTextur[-n - 1]->getThis());
  198. ad = sad.ladeModel(sad.zModelName(index++)->getText());
  199. if (index == 120)
  200. {
  201. index = 0;
  202. sadI++;
  203. Text pfad = "data/Minigames/Asteroids/data/sasave_";
  204. pfad += sadI;
  205. pfad += ".m2";
  206. sad = M2Datei();
  207. sad.setPfad(pfad);
  208. sad.leseDaten();
  209. }
  210. }
  211. else
  212. {
  213. at = dynamic_cast<Bild*>(aTextur[n]->getThis());
  214. ad = dynamic_cast<Model2DData*>(aData[n]->getThis());
  215. }
  216. asteroid->add(new Asteroid(ad, at,
  217. Vec2< float >(x, y), Vec2< float >(xs, ys), rs, r, gr, n));
  218. }
  219. save->lese((char*)&anz, 4);
  220. for (int i = 0; i < anz; i++)
  221. { // Schüsse Laden
  222. float x = 0;
  223. float y = 0;
  224. float xs = 0;
  225. float ys = 0;
  226. save->lese((char*)&x, 4);
  227. save->lese((char*)&y, 4);
  228. save->lese((char*)&xs, 4);
  229. save->lese((char*)&ys, 4);
  230. if (schuss->getEintragAnzahl() > 100)
  231. {
  232. Schuss* newS = new Schuss(Vec2< float >(x, y), Vec2< float >(xs, ys));
  233. schuss->add(newS);
  234. world->addObject(dynamic_cast<Object2D*>(newS->getThis()));
  235. }
  236. }
  237. }
  238. save->close();
  239. save->release();
  240. }
  241. else
  242. {
  243. if (klient)
  244. {
  245. if (capture.istOffen())
  246. capture.close();
  247. DateiRemove("data/Minigames/Asteroids/data/game.mgc");
  248. capture.setDatei("data/Minigames/Asteroids/data/game.mgc");
  249. capture.erstellen();
  250. capture.open(Datei::Style::schreiben);
  251. __int64 seed = rGen->getSeed();
  252. capture.schreibe((char*)&seed, 8);
  253. }
  254. }
  255. ship->setTexture(flammenM, flammenL, flammenR, dynamic_cast<Bild*>(sTextur[shipN]->getThis()));
  256. }
  257. void Map::doPublicMausEreignis(MausEreignis& me)
  258. {}
  259. void Map::doTastaturEreignis(TastaturEreignis& te)
  260. {
  261. cs.lock();
  262. bool ok = 1;
  263. if (!beendet)
  264. {
  265. char tmp = tastenStände;
  266. if (te.taste == 'w' || te.taste == 'W' || te.taste == T_Oben)
  267. {
  268. if (te.id == TE_Press)
  269. {
  270. if ((tastenStände | 1) != tastenStände)
  271. flammenM->setAnimation(0);
  272. tastenStände |= 1;
  273. }
  274. else
  275. {
  276. tastenStände &= ~1;
  277. flammenM->setAnimation(-1);
  278. }
  279. }
  280. if (te.taste == 'd' || te.taste == 'D' || te.taste == T_Rechts)
  281. {
  282. if (te.id == TE_Press)
  283. {
  284. if ((tastenStände | 2) != tastenStände)
  285. flammenL->setAnimation(0);
  286. tastenStände |= 2;
  287. }
  288. else
  289. {
  290. tastenStände &= ~2;
  291. flammenL->setAnimation(-1);
  292. }
  293. }
  294. if (te.taste == 'a' || te.taste == 'A' || te.taste == T_Links)
  295. {
  296. if (te.id == TE_Press)
  297. {
  298. if ((tastenStände | 4) != tastenStände)
  299. flammenR->setAnimation(0);
  300. tastenStände |= 4;
  301. }
  302. else
  303. {
  304. tastenStände &= ~4;
  305. flammenR->setAnimation(-1);
  306. }
  307. }
  308. if (te.taste == T_Space)
  309. {
  310. if (te.id == TE_Press)
  311. {
  312. if ((tastenStände | 8) != tastenStände)
  313. ok = 0;
  314. tastenStände |= 8;
  315. }
  316. else
  317. tastenStände &= ~8;
  318. }
  319. if (tmp != tastenStände && klient)
  320. {
  321. if (klient)
  322. {
  323. capture.schreibe((char*)&gameTime, 8);
  324. capture.schreibe(&tastenStände, 1);
  325. }
  326. }
  327. }
  328. if (te.taste == T_Space && te.id == TE_Press)
  329. {
  330. if (klient && ok)
  331. {
  332. capture.schreibe((char*)&gameTime, 8);
  333. capture.schreibe("\x10", 1);
  334. }
  335. if (schuss->getEintragAnzahl() < 100)
  336. {
  337. Schuss* newS = ship->getSchuss();
  338. schuss->add(newS);
  339. world->addObject(dynamic_cast<Object2D*>(newS->getThis()));
  340. }
  341. }
  342. cs.unlock();
  343. }
  344. bool Map::tick(double t)
  345. {
  346. if (beendet)
  347. return 0;
  348. bool ret = __super::tick(t);
  349. ret |= kamera->tick(t);
  350. if (beendet && klient)
  351. {
  352. capture.close();
  353. DateiRemove("data/Minigames/Asteroids/data/upload.mgc");
  354. DateiUmbenennen("data/Minigames/Asteroids/data/game.mgc", "data/Minigames/Asteroids/data/upload.mgc");
  355. int tmpScore = score;
  356. KSGClient::MinigameServerClient* tmpKlient = dynamic_cast<KSGClient::MinigameServerClient*>(klient->getThis());
  357. new AsynchronCall([tmpScore, tmpKlient]()
  358. {
  359. InitDatei* opd = new InitDatei("data/Minigames/Asteroids/data/optionen.ini");
  360. opd->laden();
  361. Text optionen = "Width=";
  362. optionen += opd->zWert("Breite")->getText();
  363. optionen += ",Height=";
  364. optionen += opd->zWert("Höhe")->getText();
  365. optionen += ",Size=";
  366. optionen += opd->zWert("Größe")->getText();
  367. optionen += ",Timer=";
  368. optionen += opd->zWert("Timer")->getText();
  369. optionen += ",Ship=";
  370. optionen += opd->zWert("Ship")->getText();
  371. opd->release();
  372. Datei d;
  373. d.setDatei("data/Minigames/Asteroids/data/upload.mgc");
  374. tmpKlient->reportEndOfGame("Asteroids", optionen, tmpScore, &d);
  375. DateiRemove("data/Minigames/Asteroids/data/upload.mgc");
  376. tmpKlient->release();
  377. });
  378. KSGTDatei* stb = new KSGTDatei("data/Minigames/Asteroids/data/score.ksgt");
  379. if (!stb->laden())
  380. DateiPfadErstellen("data/Minigames/Asteroids/data/score.ksgt");
  381. RCArray< Text >* zeile = new RCArray< Text >();
  382. Zeit* zeit = getZeit();
  383. zeile->add(zeit->getZeit("y-m-d h:i:s"));
  384. zeit->release();
  385. Text* scoreT = new Text();
  386. scoreT->append(score);
  387. zeile->add(scoreT);
  388. Text* breiteT = new Text();
  389. breiteT->append(breite);
  390. zeile->add(breiteT);
  391. Text* höheT = new Text();
  392. höheT->append(höhe);
  393. zeile->add(höheT);
  394. Text* timerT = new Text();
  395. timerT->append(maxTimer);
  396. zeile->add(timerT);
  397. Text* aGrößeT = new Text();
  398. aGrößeT->append(aGröße);
  399. zeile->add(aGrößeT);
  400. Text* shipT = new Text();
  401. shipT->append(shipN);
  402. zeile->add(shipT);
  403. stb->addZeile(7, zeile);
  404. zeile->release();
  405. stb->speichern();
  406. stb->release();
  407. DateiRemove("data/Minigames/Asteroids/data/game.save");
  408. }
  409. ret |= flammenM->tick(t);
  410. ret |= flammenL->tick(t);
  411. ret |= flammenR->tick(t);
  412. return ret;
  413. }
  414. void Map::render(Bild& zRObj)
  415. {
  416. cs.lock();
  417. kamera->lookAtWorldPos(ship->getPosition().x, ship->getPosition().y);
  418. if (autoKam)
  419. kamera->setDrehung(-ship->getDrehung());
  420. kamera->render(zRObj);
  421. if (breite >= 800 || höhe >= 500)
  422. { // Minimap
  423. zRObj.setAlpha(50);
  424. if (breite > höhe)
  425. mapKamera->setSize(200, (int)((200.0 / world->getWorldInfo().size.x) * world->getWorldInfo().size.y));
  426. else
  427. mapKamera->setSize((int)((200.0 / world->getWorldInfo().size.y) * world->getWorldInfo().size.x), 200);
  428. mapKamera->lookAtWorldPos(world->getWorldInfo().size.x / 2.f, world->getWorldInfo().size.y / 2.f);
  429. mapKamera->lookAtWorldArea((float)world->getWorldInfo().size.x, (float)world->getWorldInfo().size.y);
  430. mapKamera->render(zRObj);
  431. map->setSize(mapKamera->getBreite(), mapKamera->getHeight());
  432. map->render(zRObj);
  433. zRObj.releaseAlpha();
  434. }
  435. cs.unlock();
  436. }
  437. void Map::speichern()
  438. {
  439. if (!beendet && klient)
  440. {
  441. if (capture.istOffen())
  442. capture.close();
  443. cs.lock();
  444. int anz = asteroid->getEintragAnzahl();
  445. int index = 0;
  446. for (int i = 0; index < anz; i++)
  447. {
  448. M2Datei sad;
  449. Text pfad = "data/Minigames/Asteroids/data/sasave_";
  450. pfad += i;
  451. pfad += ".m2";
  452. DateiRemove(pfad);
  453. sad.setPfad(pfad);
  454. sad.leseDaten();
  455. char name[2];
  456. name[1] = 0;
  457. for (char n = 1; n <= 120 && index < anz; n++)
  458. {
  459. name[0] = n;
  460. if (asteroid->z(index++)->getId() < 0)
  461. sad.saveModel(asteroid->z(index - 1)->zModel(), name);
  462. else
  463. n--;
  464. }
  465. }
  466. Datei* d = new Datei();
  467. d->setDatei("data/Minigames/Asteroids/data/game.save");
  468. d->erstellen();
  469. d->open(Datei::Style::schreiben);
  470. __int64 seed = rGen->getSeed();
  471. d->schreibe((char*)&seed, 8);
  472. d->schreibe((char*)&gameTime, 8);
  473. d->schreibe((char*)&breite, 4);
  474. d->schreibe((char*)&höhe, 4);
  475. d->schreibe((char*)&aGröße, 4);
  476. d->schreibe((char*)&shipN, 4);
  477. d->schreibe((char*)&score, 4);
  478. d->schreibe((char*)&mTimer, 4);
  479. ship->save(d);
  480. d->schreibe((char*)&anz, 4);
  481. for (int i = 0; i < anz; i++)
  482. asteroid->z(i)->save(d);
  483. anz = schuss->getEintragAnzahl();
  484. d->schreibe((char*)&anz, 4);
  485. for (int i = 0; i < anz; i++)
  486. schuss->z(i)->save(d);
  487. d->close();
  488. d->release();
  489. cs.unlock();
  490. }
  491. else if (klient)
  492. DateiRemove("data/Minigames/Asteroids/data/game.save");
  493. }