[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: private:
/** Friend to allow access to the destructor */ /** Friend to allow access to the destructor */
friend class Service<AudioService>; // friend class Service<AudioService>;
std::optional<Music> music; std::optional<Music> music;
EMusic currentTrack; EMusic currentTrack;
protected: protected:
~AudioService(); // ~AudioService();
public: public:
void setMusic(EMusic music); void setMusic(EMusic music);
+2 -1
View File
@@ -11,7 +11,8 @@ enum class EScreen {
GAME, GAME,
PAUSE, PAUSE,
GAMEOVER, GAMEOVER,
VICTORY VICTORY,
CLOSE
}; };
#endif #endif
+3
View File
@@ -9,6 +9,9 @@ class MainScreen : public Screen {
private: private:
int hoveredButton = 0; int hoveredButton = 0;
void mouseLogic();
void keyboardLogic();
public: public:
MainScreen(); MainScreen();
~MainScreen(); ~MainScreen();
-2
View File
@@ -7,8 +7,6 @@
class Screen { class Screen {
private: private:
void handleMusic();
protected: protected:
std::vector<Text> text; std::vector<Text> text;
+9 -4
View File
@@ -15,6 +15,15 @@ class StateService : public Service<StateService>
{ {
private: private:
std::unique_ptr<Screen> currentScreen; std::unique_ptr<Screen> currentScreen;
bool gameRunning = true;
/**
* One step of the gameloop
*/
void step();
void handleMusic();
public: public:
@@ -24,10 +33,6 @@ public:
* Start the game loop * Start the game loop
*/ */
void startGame(); void startGame();
/**
* One step of the gameloop
*/
void step();
}; };
#endif #endif
+8 -3
View File
@@ -30,13 +30,18 @@ class Text {
this->color = color; this->color = color;
} }
void setText(std::string text) {
this->text = text;
}
Vector2 getPosition() {
return position;
}
std::string getText() { std::string getText() {
return this->text; return this->text;
} }
void setText(std::string text) {
this->text = text;
}
void draw(); void draw();
}; };
+51 -1
View File
@@ -2,6 +2,7 @@
#include "AssetService.h" #include "AssetService.h"
#include "AudioService.h" #include "AudioService.h"
#include <iostream> #include <iostream>
#include "StateService.h"
MainScreen::MainScreen() MainScreen::MainScreen()
{ {
@@ -37,6 +38,42 @@ MainScreen::~MainScreen()
} }
void MainScreen::logic() 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)) if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_UP))
{ {
@@ -46,8 +83,21 @@ void MainScreen::logic()
text[hoveredButton].setColor(RED); text[hoveredButton].setColor(RED);
} }
if (IsKeyPressed(KEY_ENTER)) if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{ {
std::cout << text[hoveredButton].getText() << std::endl; 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 "AudioService.h"
#include <optional> #include <optional>
void Screen::handleMusic()
{
std::optional<Music> music = AudioService::getInstance().getMusic();
if(music.has_value()) {
UpdateMusicStream(music.value());
}
}
void Screen::draw() void Screen::draw()
{ {
handleMusic();
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
+1 -1
View File
@@ -45,7 +45,7 @@ TitleScreen::~TitleScreen()
void TitleScreen::logic() void TitleScreen::logic()
{ {
if (IsKeyPressed(KEY_ENTER)) if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{ {
StateService::getInstance().setScreen(EScreen::MAIN); StateService::getInstance().setScreen(EScreen::MAIN);
// std::cout << "Next screen!" << std::endl; // std::cout << "Next screen!" << std::endl;
+6 -6
View File
@@ -1,12 +1,12 @@
#include "AudioService.h" #include "AudioService.h"
#include "AssetService.h" #include "AssetService.h"
AudioService::~AudioService() // AudioService::~AudioService()
{ // {
if (!music.has_value()) // if (!music.has_value())
return; // return;
UnloadMusicStream(music.value()); // UnloadMusicStream(music.value());
} // }
void AudioService::setMusic(EMusic music) void AudioService::setMusic(EMusic music)
{ {
+17 -2
View File
@@ -2,6 +2,8 @@
#include "raylib.h" #include "raylib.h"
#include "TitleScreen.h" #include "TitleScreen.h"
#include "MainScreen.h" #include "MainScreen.h"
#include <optional>
#include "AudioService.h"
void StateService::setScreen(EScreen state) void StateService::setScreen(EScreen state)
{ {
@@ -13,6 +15,9 @@ void StateService::setScreen(EScreen state)
case EScreen::MAIN: case EScreen::MAIN:
currentScreen = std::make_unique<MainScreen>(); currentScreen = std::make_unique<MainScreen>();
break; break;
case EScreen::CLOSE:
gameRunning = false;
break;
default: default:
currentScreen = std::make_unique<TitleScreen>(); currentScreen = std::make_unique<TitleScreen>();
} }
@@ -35,7 +40,7 @@ void StateService::startGame()
emscripten_set_main_loop(step, 0, 1); emscripten_set_main_loop(step, 0, 1);
#else #else
while (!WindowShouldClose()) while (!WindowShouldClose() && gameRunning)
{ {
step(); step();
} }
@@ -45,8 +50,18 @@ void StateService::startGame()
CloseAudioDevice(); CloseAudioDevice();
} }
void StateService::handleMusic()
{
std::optional<Music> music = AudioService::getInstance().getMusic();
if(music.has_value()) {
UpdateMusicStream(music.value());
}
}
void StateService::step() void StateService::step()
{ {
handleMusic();
currentScreen->draw(); currentScreen->draw();
currentScreen->logic(); currentScreen->logic();
} }