[Added] Mouselogic, to navigate the menu with the mouse

This commit is contained in:
Ninjaws
2024-12-08 14:46:47 +01:00
parent 22ac2c732e
commit 83d1d622ee
11 changed files with 99 additions and 31 deletions
+2 -2
View File
@@ -10,12 +10,12 @@ class AudioService : public Service<AudioService>
{
private:
/** Friend to allow access to the destructor */
friend class Service<AudioService>;
// friend class Service<AudioService>;
std::optional<Music> music;
EMusic currentTrack;
protected:
~AudioService();
// ~AudioService();
public:
void setMusic(EMusic music);
+2 -1
View File
@@ -11,7 +11,8 @@ enum class EScreen {
GAME,
PAUSE,
GAMEOVER,
VICTORY
VICTORY,
CLOSE
};
#endif
+3
View File
@@ -9,6 +9,9 @@ class MainScreen : public Screen {
private:
int hoveredButton = 0;
void mouseLogic();
void keyboardLogic();
public:
MainScreen();
~MainScreen();
-2
View File
@@ -7,8 +7,6 @@
class Screen {
private:
void handleMusic();
protected:
std::vector<Text> text;
+9 -4
View File
@@ -15,6 +15,15 @@ class StateService : public Service<StateService>
{
private:
std::unique_ptr<Screen> currentScreen;
bool gameRunning = true;
/**
* One step of the gameloop
*/
void step();
void handleMusic();
public:
@@ -24,10 +33,6 @@ public:
* Start the game loop
*/
void startGame();
/**
* One step of the gameloop
*/
void step();
};
#endif
+8 -3
View File
@@ -30,13 +30,18 @@ class Text {
this->color = color;
}
void setText(std::string text) {
this->text = text;
}
Vector2 getPosition() {
return position;
}
std::string getText() {
return this->text;
}
void setText(std::string text) {
this->text = text;
}
void draw();
};
+51 -1
View File
@@ -2,6 +2,7 @@
#include "AssetService.h"
#include "AudioService.h"
#include <iostream>
#include "StateService.h"
MainScreen::MainScreen()
{
@@ -37,6 +38,42 @@ MainScreen::~MainScreen()
}
void MainScreen::logic()
{
// Vector2 mouseD = GetMouseDelta();
// std::cout << mouseD.y <<std::endl;
// if (mouseD.x != 0.0f && mouseD.y != 0.0f)
// {
// mouseLogic();
// }
keyboardLogic();
}
void MainScreen::mouseLogic()
{
for (int i = 0; i < text.size(); i++)
{
/** No need to check the button that is already being hovered */
if (hoveredButton == i)
continue;
Vector2 dim = text[i].getDimensions();
Vector2 pos = text[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 >= rect.x && mousePos.x <= (rect.x+rect.width) && mousePos.y >= rect.y && mousePos.y <= (rect.y + rect.height))
{
text[hoveredButton].setColor(WHITE);
hoveredButton = i;
text[hoveredButton].setColor(RED);
break;
}
}
}
void MainScreen::keyboardLogic()
{
if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_UP))
{
@@ -46,8 +83,21 @@ void MainScreen::logic()
text[hoveredButton].setColor(RED);
}
if (IsKeyPressed(KEY_ENTER))
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
std::cout << text[hoveredButton].getText() << std::endl;
std::string selectedButton = text[hoveredButton].getText();
if(selectedButton == "new game") {
}
else if(selectedButton == "load game") {
}
else if(selectedButton == "tutorial") {
StateService::getInstance().setScreen(EScreen::TUTORIAL);
}
else if(selectedButton == "quit") {
StateService::getInstance().setScreen(EScreen::CLOSE);
}
}
}
-9
View File
@@ -2,17 +2,8 @@
#include "AudioService.h"
#include <optional>
void Screen::handleMusic()
{
std::optional<Music> music = AudioService::getInstance().getMusic();
if(music.has_value()) {
UpdateMusicStream(music.value());
}
}
void Screen::draw()
{
handleMusic();
BeginDrawing();
ClearBackground(BLACK);
+1 -1
View File
@@ -45,7 +45,7 @@ TitleScreen::~TitleScreen()
void TitleScreen::logic()
{
if (IsKeyPressed(KEY_ENTER))
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
StateService::getInstance().setScreen(EScreen::MAIN);
// std::cout << "Next screen!" << std::endl;
+6 -6
View File
@@ -1,12 +1,12 @@
#include "AudioService.h"
#include "AssetService.h"
AudioService::~AudioService()
{
if (!music.has_value())
return;
UnloadMusicStream(music.value());
}
// AudioService::~AudioService()
// {
// if (!music.has_value())
// return;
// UnloadMusicStream(music.value());
// }
void AudioService::setMusic(EMusic music)
{
+17 -2
View File
@@ -2,6 +2,8 @@
#include "raylib.h"
#include "TitleScreen.h"
#include "MainScreen.h"
#include <optional>
#include "AudioService.h"
void StateService::setScreen(EScreen state)
{
@@ -13,6 +15,9 @@ void StateService::setScreen(EScreen state)
case EScreen::MAIN:
currentScreen = std::make_unique<MainScreen>();
break;
case EScreen::CLOSE:
gameRunning = false;
break;
default:
currentScreen = std::make_unique<TitleScreen>();
}
@@ -35,7 +40,7 @@ void StateService::startGame()
emscripten_set_main_loop(step, 0, 1);
#else
while (!WindowShouldClose())
while (!WindowShouldClose() && gameRunning)
{
step();
}
@@ -45,8 +50,18 @@ void StateService::startGame()
CloseAudioDevice();
}
void StateService::handleMusic()
{
std::optional<Music> music = AudioService::getInstance().getMusic();
if(music.has_value()) {
UpdateMusicStream(music.value());
}
}
void StateService::step()
{
handleMusic();
currentScreen->draw();
currentScreen->logic();
}
}