Compare commits
3 Commits
b13d535c27
...
683917f564
| Author | SHA1 | Date | |
|---|---|---|---|
| 683917f564 | |||
| 4276656917 | |||
| 6a54e6d4a2 |
249
src/main.cpp
249
src/main.cpp
@ -1,40 +1,227 @@
|
||||
#pragma once
|
||||
#include "gamematrix.h"
|
||||
#include "gamecube.h"
|
||||
#include "raylib.h"
|
||||
#include <rlgl.h>
|
||||
#include <algorithm>
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
#include <cstdio>
|
||||
|
||||
struct Vec3
|
||||
// -----------------------------------------------------------
|
||||
// 3D Memory Game – Hauptprogramm
|
||||
// -----------------------------------------------------------
|
||||
|
||||
enum GameScreen { MENU = 0, GAMEPLAY };
|
||||
|
||||
void SetupGame(std::vector<gamecube>& cubes, int pairs)
|
||||
{
|
||||
float x, y, z;
|
||||
};
|
||||
cubes.clear();
|
||||
|
||||
class gamecube
|
||||
Color colors[] = { RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, WHITE, SKYBLUE };
|
||||
|
||||
int count = pairs * 2;
|
||||
int cols = (count > 6) ? 4 : 3;
|
||||
int rows = (count > 6) ? 3 : 2;
|
||||
|
||||
std::vector<Vec3> positions;
|
||||
int index = 0;
|
||||
for (int r = 0; r < rows && index < count; ++r)
|
||||
{
|
||||
for (int c = 0; c < cols && index < count; ++c)
|
||||
{
|
||||
positions.push_back({ (float)c * 2.0f - (cols - 1), 0.0f,
|
||||
(float)r * 2.0f - (rows - 1) });
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Color> colorPool;
|
||||
for (int i = 0; i < pairs; ++i)
|
||||
{
|
||||
colorPool.push_back(colors[i]);
|
||||
colorPool.push_back(colors[i]);
|
||||
}
|
||||
|
||||
// Shuffle
|
||||
for (int i = (int)colorPool.size() - 1; i > 0; --i)
|
||||
{
|
||||
int j = rand() % (i + 1);
|
||||
std::swap(colorPool[i], colorPool[j]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
cubes.emplace_back(positions[i], colorPool[i]);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
public:
|
||||
gamecube(const Vec3 &pos, Color col);
|
||||
void Update(float flipSpeed);
|
||||
void FlipForward();
|
||||
void FlipBackward();
|
||||
srand((unsigned)time(nullptr));
|
||||
|
||||
// Wichtig für die Zustandsmaschine (LockInput-Wartezustand)
|
||||
bool IsFlipping() const { return flippingForward || flippingBackward; }
|
||||
InitWindow(800, 600, "3D Memory Game");
|
||||
SetTargetFPS(60);
|
||||
|
||||
bool IsFlipped() const;
|
||||
bool IsMatched() const;
|
||||
void SetMatched(bool m);
|
||||
void Draw() const;
|
||||
Vec3 GetPosition() const;
|
||||
float GetRotationY() const;
|
||||
Camera3D camera{};
|
||||
camera.position = { 6.0f, 6.0f, 6.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;
|
||||
|
||||
// Bereits vorhanden und inline implementiert!
|
||||
Color GetColor() const { return color; }
|
||||
GameScreen currentScreen = MENU;
|
||||
|
||||
private:
|
||||
Vec3 position;
|
||||
Color color;
|
||||
bool flipped = false;
|
||||
bool matched = false;
|
||||
bool flippingForward = false;
|
||||
bool flippingBackward = false;
|
||||
float rotation = 0.0f;
|
||||
};
|
||||
std::vector<gamecube> cubes;
|
||||
gamecube* first = nullptr;
|
||||
gamecube* second = nullptr;
|
||||
|
||||
int selectedPairs = 3;
|
||||
float flipSpeed = 180.0f; // Grad pro Sekunde
|
||||
|
||||
bool gameWon = false;
|
||||
bool timerStarted = false;
|
||||
double startTime = 0.0;
|
||||
double endTime = 0.0;
|
||||
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// ---------------- UPDATE ----------------
|
||||
switch (currentScreen)
|
||||
{
|
||||
case MENU:
|
||||
{
|
||||
if (IsKeyPressed(KEY_THREE))
|
||||
{
|
||||
selectedPairs = 3;
|
||||
SetupGame(cubes, selectedPairs);
|
||||
currentScreen = GAMEPLAY;
|
||||
gameWon = false;
|
||||
first = second = nullptr;
|
||||
timerStarted = false;
|
||||
}
|
||||
else if (IsKeyPressed(KEY_SIX))
|
||||
{
|
||||
selectedPairs = 6;
|
||||
SetupGame(cubes, selectedPairs);
|
||||
currentScreen = GAMEPLAY;
|
||||
gameWon = false;
|
||||
first = second = nullptr;
|
||||
timerStarted = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case GAMEPLAY:
|
||||
{
|
||||
if (!timerStarted)
|
||||
{
|
||||
startTime = GetTime();
|
||||
timerStarted = true;
|
||||
}
|
||||
|
||||
// Klick-Erkennung mit Ray
|
||||
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && second == nullptr)
|
||||
{
|
||||
Ray ray = GetMouseRay(GetMousePosition(), camera);
|
||||
gamecube* hit = nullptr;
|
||||
float bestDist = 1e9f;
|
||||
|
||||
for (auto& c : cubes)
|
||||
{
|
||||
if (c.IsMatched() || c.IsFlipped()) continue;
|
||||
|
||||
Vec3 p = c.GetPosition();
|
||||
BoundingBox box;
|
||||
box.min = { p.x - 0.75f, p.y - 0.75f, p.z - 0.75f };
|
||||
box.max = { p.x + 0.75f, p.y + 0.75f, p.z + 0.75f };
|
||||
|
||||
RayCollision col = GetRayCollisionBox(ray, box);
|
||||
if (col.hit && col.distance < bestDist)
|
||||
{
|
||||
bestDist = col.distance;
|
||||
hit = &c;
|
||||
}
|
||||
}
|
||||
|
||||
if (hit)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
first = hit;
|
||||
first->FlipForward();
|
||||
}
|
||||
else if (hit != first)
|
||||
{
|
||||
second = hit;
|
||||
second->FlipForward();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Animation
|
||||
for (auto& c : cubes)
|
||||
c.Update(flipSpeed);
|
||||
|
||||
// Matching
|
||||
if (first && second && first->IsFlipped() && second->IsFlipped())
|
||||
{
|
||||
if (first->GetColor().r == second->GetColor().r &&
|
||||
first->GetColor().g == second->GetColor().g &&
|
||||
first->GetColor().b == second->GetColor().b)
|
||||
{
|
||||
first->SetMatched(true);
|
||||
second->SetMatched(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
first->FlipBackward();
|
||||
second->FlipBackward();
|
||||
}
|
||||
|
||||
first = nullptr;
|
||||
second = nullptr;
|
||||
}
|
||||
|
||||
if (!gameWon)
|
||||
{
|
||||
gameWon = std::all_of(cubes.begin(), cubes.end(),
|
||||
[](const gamecube& c){ return c.IsMatched(); });
|
||||
if (gameWon)
|
||||
endTime = GetTime() - startTime;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- DRAW ----------------
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
if (currentScreen == MENU)
|
||||
{
|
||||
DrawText("3D MEMORY SPIEL", 200, 80, 40, DARKGRAY);
|
||||
DrawText("[3] = 3 Paare", 200, 200, 24, BLUE);
|
||||
DrawText("[6] = 6 Paare", 200, 240, 24, BLUE);
|
||||
}
|
||||
else
|
||||
{
|
||||
BeginMode3D(camera);
|
||||
for (auto& c : cubes) c.Draw();
|
||||
EndMode3D();
|
||||
|
||||
if (gameWon)
|
||||
{
|
||||
char buf[64];
|
||||
sprintf(buf, "Gewonnen in %.2f Sekunden", endTime);
|
||||
DrawText(buf, 150, 260, 30, DARKGREEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
char buf[64];
|
||||
sprintf(buf, "Time: %.2f", GetTime() - startTime);
|
||||
DrawText(buf, 10, 10, 20, DARKGRAY);
|
||||
}
|
||||
}
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user