[Updated] Backgrounds to display the desired portion of the image, [Added] Start of the Game class
This commit is contained in:
+30
-14
@@ -14,16 +14,16 @@ MainScreen::MainScreen()
|
||||
for (int i = 0; i < AMNT_OF_BUTTONS; i++)
|
||||
{
|
||||
Text txt = Text("new game", EFont::GTEK, 30, 0, WHITE);
|
||||
text.push_back(txt);
|
||||
buttons.push_back(txt);
|
||||
}
|
||||
text[0].setColor(RED);
|
||||
text[1].setText("load game");
|
||||
text[2].setText("tutorial");
|
||||
text[3].setText("quit");
|
||||
buttons[0].setColor(RED);
|
||||
buttons[1].setText("load game");
|
||||
buttons[2].setText("tutorial");
|
||||
buttons[3].setText("quit");
|
||||
|
||||
for (int i = 0; i < AMNT_OF_BUTTONS; i++)
|
||||
{
|
||||
text[i].setPosition({(float)(GetScreenWidth() - text[i].getDimensions().x) / 2.0f, (float)TOP_GAP + SPACE_PER_ITEM * i});
|
||||
buttons[i].setPosition({(float)(GetScreenWidth() - buttons[i].getDimensions().x) / 2.0f, (float)TOP_GAP + SPACE_PER_ITEM * i});
|
||||
}
|
||||
|
||||
/** Will be ignored if already active (like when coming from the TitleScreen) */
|
||||
@@ -49,9 +49,10 @@ void MainScreen::logic()
|
||||
clickLogic();
|
||||
}
|
||||
|
||||
|
||||
void MainScreen::mouseLogic()
|
||||
{
|
||||
for (int i = 0; i < text.size(); i++)
|
||||
for (int i = 0; i < buttons.size(); i++)
|
||||
{
|
||||
/** No need to check the button that is already being hovered */
|
||||
if (hoveredButton == i)
|
||||
@@ -59,17 +60,17 @@ void MainScreen::mouseLogic()
|
||||
|
||||
/** Pixels around the text that also count towards that button, to make the hover feel better */
|
||||
int textPadding = 20;
|
||||
Vector2 dim = text[i].getDimensions();
|
||||
Vector2 pos = text[i].getPosition();
|
||||
Vector2 dim = buttons[i].getDimensions();
|
||||
Vector2 pos = buttons[i].getPosition();
|
||||
Rectangle rect = {pos.x, pos.y, dim.x, dim.y};
|
||||
|
||||
Vector2 mousePos = GetMousePosition();
|
||||
// mousePos.x >= rect.x && mousePos.x <= (rect.x+rect.width) &&
|
||||
if ((mousePos.x + textPadding) >= rect.x && mousePos.x <= (rect.x + rect.width + textPadding) && (mousePos.y + textPadding) >= rect.y && mousePos.y <= (rect.y + rect.height + textPadding))
|
||||
{
|
||||
text[hoveredButton].setColor(WHITE);
|
||||
buttons[hoveredButton].setColor(WHITE);
|
||||
hoveredButton = i;
|
||||
text[hoveredButton].setColor(RED);
|
||||
buttons[hoveredButton].setColor(RED);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -79,10 +80,10 @@ void MainScreen::keyboardLogic()
|
||||
{
|
||||
if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_UP))
|
||||
{
|
||||
text[hoveredButton].setColor(WHITE);
|
||||
buttons[hoveredButton].setColor(WHITE);
|
||||
/** Move up or down depending on the pressed button. +4 is used so that -1 becomes 3 (the last button) */
|
||||
hoveredButton = ((hoveredButton - (IsKeyPressed(KEY_DOWN) ? -1 : 1)) + 4) % 4;
|
||||
text[hoveredButton].setColor(RED);
|
||||
buttons[hoveredButton].setColor(RED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +91,7 @@ void MainScreen::clickLogic()
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
std::string selectedButton = text[hoveredButton].getText();
|
||||
std::string selectedButton = buttons[hoveredButton].getText();
|
||||
|
||||
if (selectedButton == "new game")
|
||||
StateService::getInstance().setScreen(EScreen::GAME);
|
||||
@@ -103,3 +104,18 @@ void MainScreen::clickLogic()
|
||||
StateService::getInstance().setScreen(EScreen::CLOSE);
|
||||
}
|
||||
}
|
||||
|
||||
void MainScreen::draw()
|
||||
{
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
|
||||
if (buttons.size() > 0)
|
||||
{
|
||||
for (int i = 0; i < buttons.size(); i++)
|
||||
{
|
||||
buttons[i].draw();
|
||||
}
|
||||
}
|
||||
EndDrawing();
|
||||
}
|
||||
Reference in New Issue
Block a user