[Updated] Codebase to deal with wasm-style keyboard and mouse inputs
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "Background.h"
|
||||
#include "entities/Background.h"
|
||||
|
||||
#include "AssetService.h"
|
||||
#include "services/AssetService.h"
|
||||
|
||||
// Background::Background(EBackground background)
|
||||
// {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "Map.h"
|
||||
#include "entities/Map.h"
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "AssetService.h"
|
||||
#include "services/AssetService.h"
|
||||
|
||||
Map::Map()
|
||||
{
|
||||
@@ -14,7 +14,7 @@ Map::Map()
|
||||
void Map::loadMap()
|
||||
{
|
||||
std::vector<Vector2> tempMap;
|
||||
std::ifstream openfile("assets/Maps/map1.txt");
|
||||
std::ifstream openfile(AssetService::getInstance().getMapUrl(EMap::TEXTURE));
|
||||
|
||||
map.clear();
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "Text.h"
|
||||
#include "entities/Text.h"
|
||||
|
||||
Text::Text(std::string text, EFont font, int fontSize, int spacing, Color color, Vector2 position)
|
||||
{
|
||||
|
||||
+57
-7
@@ -1,16 +1,66 @@
|
||||
#include "raylib.h"
|
||||
#include "Screen.h"
|
||||
#include "TitleScreen.h"
|
||||
#include "StateService.h"
|
||||
#include <iostream>
|
||||
// #include "screens/Screen.h"
|
||||
// #include "screens/TitleScreen.h"
|
||||
#include "services/StateService.h"
|
||||
// #include <iostream>
|
||||
|
||||
// #if defined(PLATFORM_WEB)
|
||||
// #include <emscripten/emscripten.h>
|
||||
// #endif
|
||||
|
||||
int main(void)
|
||||
// void step();
|
||||
// static void gameloop();
|
||||
|
||||
int main()
|
||||
{
|
||||
StateService& stateService = StateService::getInstance();
|
||||
stateService.setScreenSize({640,544});
|
||||
stateService.startGame();
|
||||
// std::cout << "StateService instance: " << service << std::endl;
|
||||
|
||||
|
||||
// InitWindow(640, 544, "Jelly Rescue");
|
||||
// // SetTargetFPS(60);
|
||||
|
||||
// // setScreen(EScreen::TITLE);
|
||||
|
||||
// #if defined(PLATFORM_WEB)
|
||||
// emscripten_set_main_loop(step, 0, 1);
|
||||
// #else
|
||||
|
||||
// while (!WindowShouldClose())
|
||||
// {
|
||||
// step();
|
||||
// }
|
||||
// #endif
|
||||
|
||||
// CloseWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// static void gameloop()
|
||||
// {
|
||||
// // InitAudioDevice();
|
||||
// InitWindow(640, 544, "Jelly Rescue");
|
||||
// // SetTargetFPS(60);
|
||||
|
||||
// // setScreen(EScreen::TITLE);
|
||||
|
||||
// #if defined(PLATFORM_WEB)
|
||||
// emscripten_set_main_loop(step, 0, 1);
|
||||
// #else
|
||||
|
||||
// while (!WindowShouldClose())
|
||||
// {
|
||||
// step();
|
||||
// }
|
||||
// #endif
|
||||
|
||||
// CloseWindow();
|
||||
// // CloseAudioDevice();
|
||||
// }
|
||||
|
||||
// void step()
|
||||
// {
|
||||
// ClearBackground(BLACK);
|
||||
// }
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "GameScreen.h"
|
||||
#include "Map.h"
|
||||
#include "StateService.h"
|
||||
#include "screens/GameScreen.h"
|
||||
#include "entities/Map.h"
|
||||
#include "services/StateService.h"
|
||||
|
||||
GameScreen::GameScreen()
|
||||
{
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "MainScreen.h"
|
||||
#include "AudioService.h"
|
||||
#include "StateService.h"
|
||||
#include "screens/MainScreen.h"
|
||||
#include "services/AudioService.h"
|
||||
#include "services/StateService.h"
|
||||
#include "raylib.h"
|
||||
#include "services/InputService.h"
|
||||
|
||||
MainScreen::MainScreen()
|
||||
{
|
||||
@@ -33,6 +34,8 @@ MainScreen::MainScreen()
|
||||
AudioService::getInstance().setMusic(EMusic::MAIN);
|
||||
AudioService::getInstance().playMusic();
|
||||
}
|
||||
|
||||
InputService::getInstance().setKeysToWatch({KEY_ENTER, KEY_UP, KEY_DOWN}, {MOUSE_BUTTON_LEFT});
|
||||
}
|
||||
|
||||
void MainScreen::logic()
|
||||
@@ -78,18 +81,18 @@ void MainScreen::mouseLogic()
|
||||
|
||||
void MainScreen::keyboardLogic()
|
||||
{
|
||||
if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_UP))
|
||||
if (InputService::getInstance().isKeyPressed(KEY_DOWN) || InputService::getInstance().isKeyPressed(KEY_UP))
|
||||
{
|
||||
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;
|
||||
hoveredButton = ((hoveredButton - (InputService::getInstance().isKeyPressed(KEY_DOWN) ? -1 : 1)) + 4) % 4;
|
||||
buttons[hoveredButton].setColor(RED);
|
||||
}
|
||||
}
|
||||
|
||||
void MainScreen::clickLogic()
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
if (InputService::getInstance().isKeyPressed(KEY_ENTER) || InputService::getInstance().isMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
std::string selectedButton = buttons[hoveredButton].getText();
|
||||
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
#include "Screen.h"
|
||||
#include "AudioService.h"
|
||||
#include <optional>
|
||||
|
||||
#include "screens/Screen.h"
|
||||
|
||||
// void Screen::draw()
|
||||
// {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#include "TitleScreen.h"
|
||||
#include "AssetService.h"
|
||||
#include "StateService.h"
|
||||
#include "EFont.h"
|
||||
#include "AudioService.h"
|
||||
#include "screens/TitleScreen.h"
|
||||
#include "services/AssetService.h"
|
||||
#include "services/StateService.h"
|
||||
#include "enums/EFont.h"
|
||||
#include "services/AudioService.h"
|
||||
#include <iostream>
|
||||
#include "services/InputService.h"
|
||||
|
||||
TitleScreen::TitleScreen()
|
||||
{
|
||||
@@ -27,14 +28,16 @@ TitleScreen::TitleScreen()
|
||||
AudioService::getInstance().setMusic(EMusic::MAIN);
|
||||
AudioService::getInstance().playMusic();
|
||||
}
|
||||
|
||||
InputService::getInstance().setKeysToWatch({KEY_ENTER}, {MOUSE_BUTTON_LEFT});
|
||||
}
|
||||
|
||||
void TitleScreen::logic()
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
// std::cout << "Next screen!" << std::endl;
|
||||
if (InputService::getInstance().isKeyPressed(KEY_ENTER) || InputService::getInstance().isMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
StateService::getInstance().setScreen(EScreen::MAIN);
|
||||
// std::cout << "Next screen!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "TutorialScreen.h"
|
||||
#include "screens/TutorialScreen.h"
|
||||
|
||||
#include "StateService.h"
|
||||
#include "EFont.h"
|
||||
#include "EScreen.h"
|
||||
#include "services/StateService.h"
|
||||
#include "enums/EFont.h"
|
||||
#include "enums/EScreen.h"
|
||||
#include "services/InputService.h"
|
||||
|
||||
TutorialScreen::TutorialScreen() {
|
||||
Text textBack = Text("Press Enter to go back", EFont::SANSATION, 30, 0, RED);
|
||||
@@ -11,11 +12,13 @@ TutorialScreen::TutorialScreen() {
|
||||
this->text.push_back(textBack);
|
||||
|
||||
this->background = Background(EBackground::TUTORIAL);
|
||||
|
||||
InputService::getInstance().setKeysToWatch({KEY_ENTER}, {MOUSE_BUTTON_LEFT});
|
||||
}
|
||||
|
||||
void TutorialScreen::logic()
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
if ( InputService::getInstance().isKeyPressed(KEY_ENTER) || InputService::getInstance().isMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
StateService::getInstance().setScreen(EScreen::MAIN);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "AssetService.h"
|
||||
|
||||
#include "services/AssetService.h"
|
||||
#include <iostream>
|
||||
AssetService::AssetService()
|
||||
{
|
||||
setAssetDir("assets/");
|
||||
@@ -25,6 +25,9 @@ void AssetService::setAssets()
|
||||
backgrounds[EBackground::GAME2] = loadTexture("Textures/GameBackground_2.jpg");
|
||||
backgrounds[EBackground::GAME3] = loadTexture("Textures/GameBackground_3.png");
|
||||
backgrounds[EBackground::VICTORY] = loadTexture("Textures/VictoryScreenBackground.png");
|
||||
|
||||
maps[EMap::TEXTURE] = "Maps/map1.txt";
|
||||
maps[EMap::COLLISION] = "Maps/colmap1.txt";
|
||||
}
|
||||
|
||||
Font AssetService::getFont(EFont font, int fontSize)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "AudioService.h"
|
||||
#include "AssetService.h"
|
||||
#include "services/AudioService.h"
|
||||
#include "services/AssetService.h"
|
||||
|
||||
// AudioService::~AudioService()
|
||||
// {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#include "StateService.h"
|
||||
#include "services/StateService.h"
|
||||
#include "raylib.h"
|
||||
#include "TitleScreen.h"
|
||||
#include "MainScreen.h"
|
||||
#include "TutorialScreen.h"
|
||||
#include "GameScreen.h"
|
||||
#include "screens/TitleScreen.h"
|
||||
#include "screens/MainScreen.h"
|
||||
#include "screens/TutorialScreen.h"
|
||||
#include "screens/GameScreen.h"
|
||||
#include <optional>
|
||||
#include "AudioService.h"
|
||||
#include "services/AudioService.h"
|
||||
#include "services/InputService.h"
|
||||
|
||||
void StateService::setScreen(EScreen state)
|
||||
{
|
||||
@@ -38,16 +39,30 @@ Screen *StateService::getScreen()
|
||||
|
||||
void StateService::startGame()
|
||||
{
|
||||
// if(!instance)
|
||||
// return;
|
||||
InitAudioDevice();
|
||||
// InitWindow(640,544, "Jelly Rescue");
|
||||
InitWindow(screenSize.x, screenSize.y, "Jelly Rescue");
|
||||
SetTargetFPS(60);
|
||||
|
||||
// #if defined(PLATFORM_WEB)
|
||||
// emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_CANVAS, nullptr, 1, staticStep);
|
||||
// #endif
|
||||
|
||||
// #if defined(PLATFORM_WEB)
|
||||
// emscripten_set_focusin_callback(EMSCRIPTEN_EVENT_TARGET_CANVAS, nullptr, 1, [](const EmscriptenFocusEvent* event) {
|
||||
// std::cout << "Canvas Focused!" << std::endl;
|
||||
// });
|
||||
// #endif
|
||||
|
||||
setScreen(EScreen::TITLE);
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
emscripten_set_main_loop(step, 0, 1);
|
||||
emscripten_set_main_loop(StateService::staticStep, 0, 1);
|
||||
#else
|
||||
|
||||
// while (!WindowShouldClose() && instance->gameRunning)
|
||||
while (!WindowShouldClose() && gameRunning)
|
||||
{
|
||||
step();
|
||||
@@ -69,7 +84,18 @@ void StateService::handleMusic()
|
||||
|
||||
void StateService::step()
|
||||
{
|
||||
InputService::getInstance().checkKeys();
|
||||
|
||||
handleMusic();
|
||||
currentScreen->draw();
|
||||
currentScreen->logic();
|
||||
// if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
// {
|
||||
// setScreen(EScreen::MAIN);
|
||||
// }
|
||||
// if(instance) {
|
||||
// instance->handleMusic();
|
||||
// instance->currentScreen->draw();
|
||||
// instance->currentScreen->logic();
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user