#include "gamecube.h" #include "createCube.h" #include "userinterface.h" #include #include #include #include "ScoreManager.h" // ----------------------------------------------------------- // 3D Memory Game – Hauptprogramm // ----------------------------------------------------------- int main() { UserInterface userInterface; ScoreManager score("Highscores.txt"); //CHRIS WAR HIER //AXIOM WAR HIER // Zufall initialisieren srand(time(NULL)); // Fenster und Kamera InitWindow(800, 600, "3D Memory Game with Matrix3D Library"); SetTargetFPS(60); Camera3D camera{}; camera.position = {8.0f, 8.5f, 8.0f}; camera.target = {0.0f, 0.0f, 0.0f}; camera.up = {0.0f, 1.0f, 0.0f}; camera.fovy = 45.0f; camera.projection = CAMERA_PERSPECTIVE; GameState state = GameState::Idle; //TODO: Hier die Zeit abrufen. double currentScoreTime = 0.0; double startTime = 0.0; double highScoreTime = 43.4; //TODO: Hier die Anzahl der Züge abrufen. int currentScoreTurns = 0; int highScoreTurns = score.getHighScore(); //if (currentScoreTime <= highScoreTime) highScoreTime = currentScoreTime; //if (currentScoreTime <= highScoreTurns) highScoreTurns = currentScoreTurns; int cubePairs = userInterface.getCubeCount(); if (cubePairs <= 0) { userInterface.showMenu(currentScoreTime, highScoreTime, currentScoreTurns, highScoreTurns); cubePairs = userInterface.getCubeCount(); // Startzeit speichern nach Enter startTime = currentScoreTime; } std::vector positions =createCubes(cubePairs); // Nur x Farben für x Paare std::vector colors= getColors(cubePairs); // Farben doppelt in einen Pool legen und mischen std::vector colorPool; for (int i = 0; i < cubePairs; i++) { colorPool.push_back(colors[i]); colorPool.push_back(colors[i]); } // Fisher-Yates Shuffle mit rand() for (int i = colorPool.size() - 1; i > 0; --i) { int j = rand() % (i + 1); // Zufallsindex von 0 bis i std::swap(colorPool[i], colorPool[j]); } // Karten/Würfel erstellen std::vector cubes; for (int i = 0; i < cubePairs*2; i++) cubes.emplace_back(positions[i], colorPool[i]); gamecube* first = nullptr; gamecube* second = nullptr; float flipSpeed = 5.0f; // Drehgeschwindigkeit bool gameWon = false; // ----------------------------------------------------------- // Hauptspielschleife // ----------------------------------------------------------- while (!WindowShouldClose()) { // Klick-Erkennung //if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) if (!gameWon && state != GameState::LockInput && state != GameState::CheckingMatch && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { Vector2 mouse = GetMousePosition(); for (auto &c : cubes) { if (!c.IsFlipped() && !c.IsMatched()) { Vector2 screenPos = GetWorldToScreen({c.GetPosition().x, c.GetPosition().y, c.GetPosition().z}, camera); if (fabs(mouse.x - screenPos.x) < 40 && fabs(mouse.y - screenPos.y) < 40) { if (state == GameState::OneFlipped && first == &c) break; c.FlipForward(); if (state == GameState::Idle) { first = &c; state = GameState::OneFlipped; }else if (state == GameState::OneFlipped && first != &c) { second = &c; state = GameState::LockInput; } score.incrementScore(); currentScoreTurns = score.getCurrentScore() / 2; break; } } } } // Animation aller Würfel for (auto &c : cubes) { c.Update(flipSpeed); } if (state == GameState::LockInput && first && second) { if (first->GetRotationY() == 180.0f && second->GetRotationY() == 180.0f) { state = GameState::CheckingMatch; } } // Matching-Logik if (state == GameState::CheckingMatch && first && second) { //currentScoreTurns = score.getCurrentScore(); Color col1 = first->GetColor(); Color col2 = second->GetColor(); if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b) { first->SetMatched(true); second->SetMatched(true); } else { first->FlipBackward(); second->FlipBackward(); } first = second = nullptr; state = GameState::Idle; std::cout <<"unlock"; } // Gewinnprüfung if (!gameWon) gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); }); //Zeit Berechnung if (!gameWon) { currentScoreTime = GetTime() - startTime; DrawText(TextFormat("%.2f", currentScoreTime), 10, 30, 20, DARKGRAY); } // ----------------------------------------------------------- // Zeichnen // ----------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); for (auto &c : cubes) c.Draw(); EndMode3D(); if (gameWon) { DrawText("Congrats! You found all pairs!", 150, 260, 30, DARKBLUE); //TODO: Neue Scores zuweisen, current genügen. //currentScoreTime = 17.3; //currentScoreTurns = 10; if (currentScoreTime <= highScoreTime) highScoreTime = currentScoreTime; if (currentScoreTurns <= highScoreTurns) highScoreTurns = currentScoreTurns; score.saveHighScore(); userInterface.showScore(currentScoreTime, highScoreTime, currentScoreTurns, highScoreTurns); } else DrawText("Flip 2 cubes - find matching pairs!", 10, 10, 20, DARKGRAY); EndDrawing(); } CloseWindow(); return 0; }