#include "Map.h" #include #include #include #include #include #include #include #include #include #include #include #include // Inhalt der Map Klasse aus Map.h // Konstruktor Map::Map(KSGClient::MinigameServerClient* klient) : GameData("data/Minigames/Asteroids/models/ship.m2", "data/Minigames/Asteroids/models/asteroids.m2") { kamera = new Kamera2D(); kamera->setWelt(dynamic_cast(world->getThis()), 1); kamera->setSize(800, 500); mapKamera = new Kamera2D(); mapKamera->setWelt(dynamic_cast(world->getThis()), 0); this->klient = klient; aTextur = new Bild * [7](); sTextur = new Bild * [2](); // lade Flammen animation LTDBDatei flammenStartLTDB; flammenStartLTDB.setDatei(new Text("data/Minigames/Asteroids/bilder/f_start.ltdb")); flammenStartLTDB.leseDaten(0); Animation2DData* flammenStart = new Animation2DData(); flammenStart->ladeAnimation(dynamic_cast(flammenStartLTDB.getThis())); flammenStart->setFPS(60); flammenStart->setWiederhohlend(0); LTDBDatei flammenLTDB; flammenLTDB.setDatei(new Text("data/Minigames/Asteroids/bilder/f_burn.ltdb")); Animation2DData* flammenBurn = new Animation2DData(); flammenBurn->ladeAnimation(dynamic_cast(flammenLTDB.getThis())); flammenBurn->setFPS(60); flammenBurn->setWiederhohlend(1); flammenM = new Textur2D(); flammenM->addAnimationZ(dynamic_cast(flammenStart->getThis())); flammenM->addAnimationZ(dynamic_cast(flammenBurn->getThis())); flammenL = new Textur2D(); flammenL->addAnimationZ(dynamic_cast(flammenStart->getThis())); flammenL->addAnimationZ(dynamic_cast(flammenBurn->getThis())); flammenR = new Textur2D(); flammenR->addAnimationZ(dynamic_cast(flammenStart)); flammenR->addAnimationZ(dynamic_cast(flammenBurn)); LTDBDatei td; td.setDatei(new Text("data/Minigames/Asteroids/bilder/asteroids.ltdb")); td.leseDaten(0); for (int i = 0; i < 7; i++) { Text name = ""; name.append((char)('a' + i)); aTextur[i] = td.laden(0, new Text(name + ".png")); } td.setDatei(new Text("data/Minigames/Asteroids/bilder/ship.ltdb")); td.leseDaten(0); for (int i = 0; i < 2; i++) { Text name = ""; name.append((char)('a' + i)); sTextur[i] = td.laden(0, new Text(name + ".png")); } map = new LRahmen(); map->setFarbe(0xFFFFFFFF); map->setPosition(10, 10); map->setSize(200, 200); mapKamera->setPosition(10, 10); mapKamera->setSize(200, 200); autoKam = 0; } // Destruktor Map::~Map() { for (int i = 0; i < 7; i++) aTextur[i]->release(); for (int i = 0; i < 2; i++) sTextur[i]->release(); delete[] aTextur; delete[] sTextur; map->release(); if (klient) klient->release(); flammenM->release(); flammenL->release(); flammenR->release(); if (kamera) kamera->release(); if (mapKamera) mapKamera->release(); } Asteroid* Map::createNewAsteroid() { Asteroid* a = __super::createNewAsteroid(); if (a) { Textur2D* text = new Textur2D(); text->setTexturZ(dynamic_cast(aTextur[a->getId()]->getThis())); a->setTextur(text); } return a; } // nicht constant void Map::reset(Text* zOptionen) { flammenL->setAnimation(-1); flammenR->setAnimation(-1); flammenM->setAnimation(-1); __super::reset(zOptionen); world->setSize(breite, höhe); world->setSize(1); world->setCircular(1); Text* tmp = zOptionen->getTeilText(zOptionen->positionVon('=', 5) + 1, zOptionen->positionVon(',', 5)); bool fortsetzen = (int)*tmp != 0; tmp->release(); tmp = zOptionen->getTeilText(zOptionen->positionVon('=', 6) + 1, zOptionen->positionVon(',', 6) > 0 ? zOptionen->positionVon(',', 6) : zOptionen->getLength()); autoKam = (int)*tmp != 0; if (!autoKam) kamera->setDrehung(0); tmp->release(); if (fortsetzen && DateiExistiert("data/Minigames/Asteroids/data/game.save") && klient) { M2Datei sad; int index = 0; int sadI = 0; Text pfad = "data/Minigames/Asteroids/data/sasave_"; pfad += sadI; pfad += ".m2"; sad.setPfad(pfad); sad.leseDaten(); if (capture.istOffen()) capture.close(); capture.setDatei("data/Minigames/Asteroids/data/game.mgc"); capture.open(Datei::Style::schreiben | Datei::Style::ende | Datei::Style::lesen); Datei* save = new Datei(); save->setDatei("data/Minigames/Asteroids/data/game.save"); save->open(Datei::Style::lesen); int br = 0; int hö = 0; int gr = 0; int shn = 0; __int64 seed; if (rGen) rGen = (RandomGenerator*)rGen->release(); save->lese((char*)&seed, 8); rGen = new RandomGenerator(); rGen->setSeed(seed); save->lese((char*)&gameTime, 8); save->lese((char*)&br, 4); save->lese((char*)&hö, 4); save->lese((char*)&gr, 4); save->lese((char*)&shn, 4); if (br == breite && hö == höhe && gr == aGröße && shn == shipN) { save->lese((char*)&score, 4); scoreCheck = score * 11197; save->lese((char*)&mTimer, 4); // Schiff laden Vec2< float > shipPos((float)(breite / 2), (float)(höhe / 2)); Vec2< float > shipSpeed(0, 0); float shipR = (float)-PI / 2; float shipRSpeed = 0; save->lese((char*)&shipPos.x, 4); save->lese((char*)&shipPos.y, 4); save->lese((char*)&shipSpeed.x, 4); save->lese((char*)&shipSpeed.y, 4); save->lese((char*)&shipR, 4); save->lese((char*)&shipRSpeed, 4); ship->setPRS(shipPos, shipSpeed, shipR, shipRSpeed); int anz = 0; save->lese((char*)&anz, 4); for (int i = 0; i < anz; i++) { // Asteroiden Laden char n = 0; float x = 0; float y = 0; float xs = 0; float ys = 0; float rs = 0; float r = 0; float gr = 0; save->lese(&n, 1); save->lese((char*)&x, 4); save->lese((char*)&y, 4); save->lese((char*)&xs, 4); save->lese((char*)&ys, 4); save->lese((char*)&rs, 4); save->lese((char*)&r, 4); save->lese((char*)&gr, 4); Bild* at = 0; Model2DData* ad = 0; if (n < 0) { at = dynamic_cast(aTextur[-n - 1]->getThis()); ad = sad.ladeModel(sad.zModelName(index++)->getText()); if (index == 120) { index = 0; sadI++; Text pfad = "data/Minigames/Asteroids/data/sasave_"; pfad += sadI; pfad += ".m2"; sad = M2Datei(); sad.setPfad(pfad); sad.leseDaten(); } } else { at = dynamic_cast(aTextur[n]->getThis()); ad = dynamic_cast(aData[n]->getThis()); } asteroid->add(new Asteroid(ad, at, Vec2< float >(x, y), Vec2< float >(xs, ys), rs, r, gr, n)); } save->lese((char*)&anz, 4); for (int i = 0; i < anz; i++) { // Schüsse Laden float x = 0; float y = 0; float xs = 0; float ys = 0; save->lese((char*)&x, 4); save->lese((char*)&y, 4); save->lese((char*)&xs, 4); save->lese((char*)&ys, 4); if (schuss->getEintragAnzahl() > 100) { Schuss* newS = new Schuss(Vec2< float >(x, y), Vec2< float >(xs, ys)); schuss->add(newS); world->addObject(dynamic_cast(newS->getThis())); } } } save->close(); save->release(); } else { if (klient) { if (capture.istOffen()) capture.close(); DateiRemove("data/Minigames/Asteroids/data/game.mgc"); capture.setDatei("data/Minigames/Asteroids/data/game.mgc"); capture.erstellen(); capture.open(Datei::Style::schreiben); __int64 seed = rGen->getSeed(); capture.schreibe((char*)&seed, 8); } } ship->setTexture(flammenM, flammenL, flammenR, dynamic_cast(sTextur[shipN]->getThis())); } void Map::doPublicMausEreignis(MausEreignis& me) {} void Map::doTastaturEreignis(TastaturEreignis& te) { cs.lock(); bool ok = 1; if (!beendet) { char tmp = tastenStände; if (te.taste == 'w' || te.taste == 'W' || te.taste == T_Oben) { if (te.id == TE_Press) { if ((tastenStände | 1) != tastenStände) flammenM->setAnimation(0); tastenStände |= 1; } else { tastenStände &= ~1; flammenM->setAnimation(-1); } } if (te.taste == 'd' || te.taste == 'D' || te.taste == T_Rechts) { if (te.id == TE_Press) { if ((tastenStände | 2) != tastenStände) flammenL->setAnimation(0); tastenStände |= 2; } else { tastenStände &= ~2; flammenL->setAnimation(-1); } } if (te.taste == 'a' || te.taste == 'A' || te.taste == T_Links) { if (te.id == TE_Press) { if ((tastenStände | 4) != tastenStände) flammenR->setAnimation(0); tastenStände |= 4; } else { tastenStände &= ~4; flammenR->setAnimation(-1); } } if (te.taste == T_Space) { if (te.id == TE_Press) { if ((tastenStände | 8) != tastenStände) ok = 0; tastenStände |= 8; } else tastenStände &= ~8; } if (tmp != tastenStände && klient) { if (klient) { capture.schreibe((char*)&gameTime, 8); capture.schreibe(&tastenStände, 1); } } } if (te.taste == T_Space && te.id == TE_Press) { if (klient && ok) { capture.schreibe((char*)&gameTime, 8); capture.schreibe("\x10", 1); } if (schuss->getEintragAnzahl() < 100) { Schuss* newS = ship->getSchuss(); schuss->add(newS); world->addObject(dynamic_cast(newS->getThis())); } } cs.unlock(); } bool Map::tick(double t) { if (beendet) return 0; bool ret = __super::tick(t); ret |= kamera->tick(t); if (beendet && klient) { capture.close(); DateiRemove("data/Minigames/Asteroids/data/upload.mgc"); DateiUmbenennen("data/Minigames/Asteroids/data/game.mgc", "data/Minigames/Asteroids/data/upload.mgc"); int tmpScore = score; KSGClient::MinigameServerClient* tmpKlient = dynamic_cast(klient->getThis()); new AsynchronCall([tmpScore, tmpKlient]() { InitDatei* opd = new InitDatei("data/Minigames/Asteroids/data/optionen.ini"); opd->laden(); Text optionen = "Width="; optionen += opd->zWert("Breite")->getText(); optionen += ",Height="; optionen += opd->zWert("Höhe")->getText(); optionen += ",Size="; optionen += opd->zWert("Größe")->getText(); optionen += ",Timer="; optionen += opd->zWert("Timer")->getText(); optionen += ",Ship="; optionen += opd->zWert("Ship")->getText(); opd->release(); Datei d; d.setDatei("data/Minigames/Asteroids/data/upload.mgc"); tmpKlient->reportEndOfGame("Asteroids", optionen, tmpScore, &d); DateiRemove("data/Minigames/Asteroids/data/upload.mgc"); tmpKlient->release(); }); KSGTDatei* stb = new KSGTDatei("data/Minigames/Asteroids/data/score.ksgt"); if (!stb->laden()) DateiPfadErstellen("data/Minigames/Asteroids/data/score.ksgt"); RCArray< Text >* zeile = new RCArray< Text >(); Zeit* zeit = getZeit(); zeile->add(zeit->getZeit("y-m-d h:i:s")); zeit->release(); Text* scoreT = new Text(); scoreT->append(score); zeile->add(scoreT); Text* breiteT = new Text(); breiteT->append(breite); zeile->add(breiteT); Text* höheT = new Text(); höheT->append(höhe); zeile->add(höheT); Text* timerT = new Text(); timerT->append(maxTimer); zeile->add(timerT); Text* aGrößeT = new Text(); aGrößeT->append(aGröße); zeile->add(aGrößeT); Text* shipT = new Text(); shipT->append(shipN); zeile->add(shipT); stb->addZeile(7, zeile); zeile->release(); stb->speichern(); stb->release(); DateiRemove("data/Minigames/Asteroids/data/game.save"); } ret |= flammenM->tick(t); ret |= flammenL->tick(t); ret |= flammenR->tick(t); return ret; } void Map::render(Bild& zRObj) { cs.lock(); kamera->lookAtWorldPos(ship->getPosition().x, ship->getPosition().y); if (autoKam) kamera->setDrehung(-ship->getDrehung()); kamera->render(zRObj); if (breite >= 800 || höhe >= 500) { // Minimap zRObj.setAlpha(50); if (breite > höhe) mapKamera->setSize(200, (int)((200.0 / world->getWorldInfo().size.x) * world->getWorldInfo().size.y)); else mapKamera->setSize((int)((200.0 / world->getWorldInfo().size.y) * world->getWorldInfo().size.x), 200); mapKamera->lookAtWorldPos(world->getWorldInfo().size.x / 2.f, world->getWorldInfo().size.y / 2.f); mapKamera->lookAtWorldArea((float)world->getWorldInfo().size.x, (float)world->getWorldInfo().size.y); mapKamera->render(zRObj); map->setSize(mapKamera->getBreite(), mapKamera->getHeight()); map->render(zRObj); zRObj.releaseAlpha(); } cs.unlock(); } void Map::speichern() { if (!beendet && klient) { if (capture.istOffen()) capture.close(); cs.lock(); int anz = asteroid->getEintragAnzahl(); int index = 0; for (int i = 0; index < anz; i++) { M2Datei sad; Text pfad = "data/Minigames/Asteroids/data/sasave_"; pfad += i; pfad += ".m2"; DateiRemove(pfad); sad.setPfad(pfad); sad.leseDaten(); char name[2]; name[1] = 0; for (char n = 1; n <= 120 && index < anz; n++) { name[0] = n; if (asteroid->z(index++)->getId() < 0) sad.saveModel(asteroid->z(index - 1)->zModel(), name); else n--; } } Datei* d = new Datei(); d->setDatei("data/Minigames/Asteroids/data/game.save"); d->erstellen(); d->open(Datei::Style::schreiben); __int64 seed = rGen->getSeed(); d->schreibe((char*)&seed, 8); d->schreibe((char*)&gameTime, 8); d->schreibe((char*)&breite, 4); d->schreibe((char*)&höhe, 4); d->schreibe((char*)&aGröße, 4); d->schreibe((char*)&shipN, 4); d->schreibe((char*)&score, 4); d->schreibe((char*)&mTimer, 4); ship->save(d); d->schreibe((char*)&anz, 4); for (int i = 0; i < anz; i++) asteroid->z(i)->save(d); anz = schuss->getEintragAnzahl(); d->schreibe((char*)&anz, 4); for (int i = 0; i < anz; i++) schuss->z(i)->save(d); d->close(); d->release(); cs.unlock(); } else if (klient) DateiRemove("data/Minigames/Asteroids/data/game.save"); }