Bugfix: Race Condition behoben mit GameState (State Machine)
This commit is contained in:
parent
f954d09a29
commit
37060ed06e
62
src/main.cpp
62
src/main.cpp
@ -2,6 +2,16 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
|
|
||||||
|
enum class GameState
|
||||||
|
{
|
||||||
|
Idle, // kein Würfel offen, Eingabe erlaubt
|
||||||
|
OneFlipped, // ein Würfel offen
|
||||||
|
CheckingMatch, // zwei Würfel vollständig aufgeklappt, Vergleich läuft
|
||||||
|
LockInput // Würfel drehen gerade – Eingabe kurz blockiert
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
// 3D Memory Game – Hauptprogramm
|
// 3D Memory Game – Hauptprogramm
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
@ -56,6 +66,8 @@ int main()
|
|||||||
float flipSpeed = 5.0f; // Drehgeschwindigkeit
|
float flipSpeed = 5.0f; // Drehgeschwindigkeit
|
||||||
bool gameWon = false;
|
bool gameWon = false;
|
||||||
|
|
||||||
|
GameState state = GameState::Idle; // Start Zustand
|
||||||
|
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
// Hauptspielschleife
|
// Hauptspielschleife
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
@ -67,7 +79,10 @@ int main()
|
|||||||
timerStarted = true;
|
timerStarted = true;
|
||||||
}
|
}
|
||||||
// Klick-Erkennung
|
// Klick-Erkennung
|
||||||
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
if (!gameWon
|
||||||
|
&& state != GameState::LockInput
|
||||||
|
&& state != GameState::CheckingMatch
|
||||||
|
&& IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||||
{
|
{
|
||||||
Vector2 mouse = GetMousePosition();
|
Vector2 mouse = GetMousePosition();
|
||||||
|
|
||||||
@ -78,31 +93,57 @@ int main()
|
|||||||
Vector2 screenPos = GetWorldToScreen({c.GetPosition().x, c.GetPosition().y, c.GetPosition().z}, camera);
|
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 (fabs(mouse.x - screenPos.x) < 40 && fabs(mouse.y - screenPos.y) < 40)
|
||||||
|
{
|
||||||
c.FlipForward();
|
c.FlipForward();
|
||||||
|
|
||||||
|
// ZUSTANDSUMSCHALTLOGIK
|
||||||
|
if (state == GameState::Idle)
|
||||||
|
{
|
||||||
|
// 1. Click
|
||||||
|
c.FlipForward();
|
||||||
|
first = &c;
|
||||||
|
state = GameState::OneFlipped;
|
||||||
|
break; // wichtig!!! Wir verlassen die Schleife :)
|
||||||
|
}
|
||||||
|
else if (state == GameState::OneFlipped)
|
||||||
|
{
|
||||||
|
// 2. Click
|
||||||
|
// Lass uns überprüfen, ob es NICHT derselbe Würfel ist.
|
||||||
|
if (&c != first)
|
||||||
|
{
|
||||||
|
c.FlipForward();
|
||||||
|
second = &c;
|
||||||
|
state = GameState::LockInput;
|
||||||
|
break; // wichtig!!! Wir verlassen die Schleife :)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Animation aller Würfel
|
// Animation aller Würfel
|
||||||
|
bool animationBusy = false;
|
||||||
|
|
||||||
for (auto &c : cubes)
|
for (auto &c : cubes)
|
||||||
{
|
{
|
||||||
c.Update(flipSpeed);
|
c.Update(flipSpeed);
|
||||||
|
}
|
||||||
|
|
||||||
// Sobald ein Würfel vollständig umgedreht ist → merken
|
if (state == GameState::LockInput)
|
||||||
if (c.IsFlipped() && !c.IsMatched())
|
{
|
||||||
|
if (first && first->IsFlipped() && second && second->IsFlipped())
|
||||||
{
|
{
|
||||||
if (!first) first = &c;
|
state = GameState::CheckingMatch;
|
||||||
else if (!second && &c != first) second = &c;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matching-Logik
|
// Matching-Logik
|
||||||
if (first && second)
|
if (state == GameState::CheckingMatch && first && second)
|
||||||
{
|
{
|
||||||
Color col1 = first->GetColor();
|
if (first->GetColor().r == second->GetColor().r &&
|
||||||
Color col2 = second->GetColor();
|
first->GetColor().g == second->GetColor().g &&
|
||||||
|
first->GetColor().b == second->GetColor().b)
|
||||||
if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b)
|
|
||||||
{
|
{
|
||||||
first->SetMatched(true);
|
first->SetMatched(true);
|
||||||
second->SetMatched(true);
|
second->SetMatched(true);
|
||||||
@ -114,6 +155,7 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
first = second = nullptr;
|
first = second = nullptr;
|
||||||
|
state = GameState::Idle;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gewinnprüfung
|
// Gewinnprüfung
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user