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 <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
|
||||
// -----------------------------------------------------------
|
||||
@ -56,6 +66,8 @@ int main()
|
||||
float flipSpeed = 5.0f; // Drehgeschwindigkeit
|
||||
bool gameWon = false;
|
||||
|
||||
GameState state = GameState::Idle; // Start Zustand
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Hauptspielschleife
|
||||
// -----------------------------------------------------------
|
||||
@ -67,7 +79,10 @@ int main()
|
||||
timerStarted = true;
|
||||
}
|
||||
// Klick-Erkennung
|
||||
if (!gameWon && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||
if (!gameWon
|
||||
&& state != GameState::LockInput
|
||||
&& state != GameState::CheckingMatch
|
||||
&& IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||
{
|
||||
Vector2 mouse = GetMousePosition();
|
||||
|
||||
@ -78,31 +93,57 @@ int main()
|
||||
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)
|
||||
{
|
||||
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
|
||||
bool animationBusy = false;
|
||||
|
||||
for (auto &c : cubes)
|
||||
{
|
||||
c.Update(flipSpeed);
|
||||
}
|
||||
|
||||
// Sobald ein Würfel vollständig umgedreht ist → merken
|
||||
if (c.IsFlipped() && !c.IsMatched())
|
||||
if (state == GameState::LockInput)
|
||||
{
|
||||
if (first && first->IsFlipped() && second && second->IsFlipped())
|
||||
{
|
||||
if (!first) first = &c;
|
||||
else if (!second && &c != first) second = &c;
|
||||
state = GameState::CheckingMatch;
|
||||
}
|
||||
}
|
||||
|
||||
// Matching-Logik
|
||||
if (first && second)
|
||||
if (state == GameState::CheckingMatch && first && second)
|
||||
{
|
||||
Color col1 = first->GetColor();
|
||||
Color col2 = second->GetColor();
|
||||
|
||||
if (col1.r == col2.r && col1.g == col2.g && col1.b == col2.b)
|
||||
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);
|
||||
@ -114,6 +155,7 @@ int main()
|
||||
}
|
||||
|
||||
first = second = nullptr;
|
||||
state = GameState::Idle;
|
||||
}
|
||||
|
||||
// Gewinnprüfung
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user