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;
float GetRotationY() const;
Color GetColor() const { return color; }
bool IsAnimating() const;
//zustände klarer abbikden bzw weniger bools = klarare Logik
private:
Vec3 position;

View File

@ -63,4 +63,8 @@ void gamecube::Draw() const
}
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
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()) {
if (state == MENU) {
if (state == GameState::Menu)
{
MenuResult res = DrawMenu();
switch (res) {
case MENU_SELECT_3: pairCount = 3; state = GAME; break;
case MENU_SELECT_6: pairCount = 6; state = GAME; break;
case MENU_SELECT_9: pairCount = 9; state = GAME; break;
default: break;
switch (res)
{
case MENU_SELECT_3:
case MENU_SELECT_6:
case MENU_SELECT_9:
state = GameState::Idle;
break;
default:
break;
}
continue;
if (state == GameState::Menu)
continue;
}
// === GAME ===
// 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())
if (!c.IsAnimating() && !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)
{
c.FlipForward();
if (!first)
{
first = &c;
state = GameState::OneFlipped;
}
else if (!second)
{
second = &c;
state = GameState::LockInput;
}
break;
}
}
}
}
// Animation aller Würfel
bool anyAnimating = false;
for (auto &c : cubes)
{
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;
else if (!second && &c != first) second = &c;
state = GameState::Idle;
}
else
{
// Two cubes fully opened → proceed to matching
state = GameState::CheckingMatch;
}
}
// Matching-Logik
if (first && second)
if (state == GameState::CheckingMatch && first && second)
{
Color col1 = first->GetColor();
Color col2 = second->GetColor();
bool match =
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);
second->SetMatched(true);
first = nullptr;
second = nullptr;
state = GameState::Idle;
}
else
{
first->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
// -----------------------------------------------------------