gamestate update #11

Open
straetemansbe94095 wants to merge 1 commits from gamestate into master
3 changed files with 78 additions and 29 deletions

View File

@ -22,6 +22,7 @@ public:
Vec3 GetPosition() const; Vec3 GetPosition() const;
float GetRotationY() const; float GetRotationY() const;
Color GetColor() const { return color; } Color GetColor() const { return color; }
bool IsAnimating() const;
//zustände klarer abbikden bzw weniger bools = klarare Logik //zustände klarer abbikden bzw weniger bools = klarare Logik
private: private:
Vec3 position; Vec3 position;

View File

@ -63,4 +63,8 @@ void gamecube::Draw() const
} }
Vec3 gamecube::GetPosition() const { return position; } Vec3 gamecube::GetPosition() const { return position; }
float gamecube::GetRotationY() const { return rotation; } float gamecube::GetRotationY() const { return rotation; }
bool gamecube::IsAnimating() const
{
return flippingForward || flippingBackward;
}

View File

@ -54,81 +54,125 @@ int main()
float flipSpeed = 5.0f; // Drehgeschwindigkeit float flipSpeed = 5.0f; // Drehgeschwindigkeit
bool gameWon = false; bool gameWon = false;
enum GameState { MENU, GAME };
GameState state = MENU;
int pairCount = 3; enum class GameState
{
Menu,
Idle, // kein Würfel offen, Eingabe erlaubt
OneFlipped, // ein Würfel offen
LockInput, // Würfel drehen gerade Eingabe kurz blockiert
CheckingMatch // zwei Würfel vollständig aufgeklappt, Vergleich läuft
};
GameState state = GameState::Menu;
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
if (state == MENU) { if (state == GameState::Menu)
{
MenuResult res = DrawMenu(); MenuResult res = DrawMenu();
switch (res)
switch (res) { {
case MENU_SELECT_3: pairCount = 3; state = GAME; break; case MENU_SELECT_3:
case MENU_SELECT_6: pairCount = 6; state = GAME; break; case MENU_SELECT_6:
case MENU_SELECT_9: pairCount = 9; state = GAME; break; case MENU_SELECT_9:
default: break; state = GameState::Idle;
break;
default:
break;
} }
if (state == GameState::Menu)
continue; continue;
} }
// === GAME === // === GAME ===
// 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();
for (auto &c : cubes) for (auto &c : cubes)
{ {
if (!c.IsFlipped() && !c.IsMatched()) if (!c.IsAnimating() && !c.IsMatched())
{ {
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();
if (!first)
{
first = &c;
state = GameState::OneFlipped;
}
else if (!second)
{
second = &c;
state = GameState::LockInput;
}
break;
}
} }
} }
} }
// Animation aller Würfel // Animation aller Würfel
bool anyAnimating = false;
for (auto &c : cubes) for (auto &c : cubes)
{ {
c.Update(flipSpeed); c.Update(flipSpeed);
if (c.IsAnimating()) anyAnimating = true;
}
// Sobald ein Würfel vollständig umgedreht ist → merken
if (c.IsFlipped() && !c.IsMatched()) if (state == GameState::LockInput && !anyAnimating)
{
// If both pointers are null → this was a failed attempt finishing its backflip
if (!first && !second)
{ {
if (!first) first = &c; state = GameState::Idle;
else if (!second && &c != first) second = &c; }
else
{
// Two cubes fully opened → proceed to matching
state = GameState::CheckingMatch;
} }
} }
// Matching-Logik // Matching-Logik
if (first && second) if (state == GameState::CheckingMatch && first && second)
{ {
Color col1 = first->GetColor(); bool match =
Color col2 = second->GetColor(); first->GetColor().r == second->GetColor().r &&
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) if (match)
{ {
first->SetMatched(true); first->SetMatched(true);
second->SetMatched(true); second->SetMatched(true);
first = nullptr;
second = nullptr;
state = GameState::Idle;
} }
else else
{ {
first->FlipBackward(); first->FlipBackward();
second->FlipBackward(); second->FlipBackward();
first = nullptr;
second = nullptr;
// WAIT FOR BACKFLIP ANIMATION
state = GameState::LockInput;
} }
first = second = nullptr;
} }
// Gewinnprüfung
if (!gameWon)
gameWon = std::all_of(cubes.begin(), cubes.end(), [](const gamecube &c){ return c.IsMatched(); });
// ----------------------------------------------------------- // -----------------------------------------------------------
// Zeichnen // Zeichnen
// ----------------------------------------------------------- // -----------------------------------------------------------