[Updated] Codebase to deal with wasm-style keyboard and mouse inputs
This commit is contained in:
@@ -2,11 +2,11 @@
|
|||||||
#define BACKGROUND_H
|
#define BACKGROUND_H
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "EBackground.h"
|
#include "enums/EBackground.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "TextureWrapper.h"
|
#include "entities/TextureWrapper.h"
|
||||||
#include "AssetService.h"
|
#include "services/AssetService.h"
|
||||||
#include "StateService.h"
|
#include "services/StateService.h"
|
||||||
|
|
||||||
class Background {
|
class Background {
|
||||||
public:
|
public:
|
||||||
@@ -14,8 +14,9 @@ class Background {
|
|||||||
|
|
||||||
}
|
}
|
||||||
Background(EBackground background){
|
Background(EBackground background){
|
||||||
Texture2D texture = AssetService::getInstance().getBackground(background);
|
// Texture2D texture = AssetService::getInstance().getBackground(background);
|
||||||
TextureWrapper wrapper = TextureWrapper(texture, StateService::getInstance().getScreenSize(), {0,0});
|
TextureWrapper wrapper = TextureWrapper(AssetService::getInstance().getBackground(background), StateService::getInstance().getScreenSize(), {0,0});
|
||||||
|
wrapper.setSourceRect({0,0,wrapper.getSize().x, wrapper.getSize().y});
|
||||||
textures.push_back(wrapper);
|
textures.push_back(wrapper);
|
||||||
}
|
}
|
||||||
Background(std::vector<TextureWrapper> backgrounds){
|
Background(std::vector<TextureWrapper> backgrounds){
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
#ifndef GAME_H
|
#ifndef GAME_H
|
||||||
#define GAME_H
|
#define GAME_H
|
||||||
|
|
||||||
#include "Map.h"
|
#include "entities/Map.h"
|
||||||
#include "Background.h"
|
#include "entities/Background.h"
|
||||||
#include "StateService.h"
|
#include "services/StateService.h"
|
||||||
|
#include "services/AudioService.h"
|
||||||
|
|
||||||
class Game
|
class Game
|
||||||
{
|
{
|
||||||
@@ -31,6 +32,9 @@ public:
|
|||||||
camera.rotation = 0.0f;
|
camera.rotation = 0.0f;
|
||||||
camera.zoom = 1.0f;
|
camera.zoom = 1.0f;
|
||||||
this->camera = camera;
|
this->camera = camera;
|
||||||
|
|
||||||
|
AudioService::getInstance().setMusic(EMusic::GAME);
|
||||||
|
AudioService::getInstance().playMusic();
|
||||||
}
|
}
|
||||||
~Game()
|
~Game()
|
||||||
{
|
{
|
||||||
@@ -2,10 +2,10 @@
|
|||||||
#define TEXT_H
|
#define TEXT_H
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "EFont.h"
|
#include "enums/EFont.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "AssetService.h"
|
#include "services/AssetService.h"
|
||||||
|
|
||||||
class Text {
|
class Text {
|
||||||
private:
|
private:
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
class TextureWrapper {
|
class TextureWrapper {
|
||||||
public:
|
public:
|
||||||
TextureWrapper(Texture2D texture, Vector2 size = {32,32}, Vector2 position = {0,0}, Rectangle sourceRect = {0,0,100,100}) {
|
TextureWrapper(Texture2D texture, Vector2 size = {32,32}, Vector2 position = {0,0}, Rectangle sourceRect = {0,0,32,32}) {
|
||||||
this->texture = texture;
|
this->texture = texture;
|
||||||
this->size = size;
|
this->size = size;
|
||||||
this->position = position;
|
this->position = position;
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef EMAP_H
|
||||||
|
#define EMAP_H
|
||||||
|
|
||||||
|
enum class EMap {
|
||||||
|
TEXTURE,
|
||||||
|
COLLISION
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#ifndef GAME_SCREEN_H
|
#ifndef GAME_SCREEN_H
|
||||||
#define GAME_SCREEN_H
|
#define GAME_SCREEN_H
|
||||||
|
|
||||||
#include "Screen.h"
|
#include "screens/Screen.h"
|
||||||
#include "Game.h"
|
#include "entities/Game.h"
|
||||||
|
|
||||||
class GameScreen: public Screen {
|
class GameScreen: public Screen {
|
||||||
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#ifndef MAIN_SCREEN_H
|
#ifndef MAIN_SCREEN_H
|
||||||
#define MAIN_SCREEN_H
|
#define MAIN_SCREEN_H
|
||||||
|
|
||||||
#include "Screen.h"
|
#include "screens/Screen.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "Text.h"
|
#include "entities/Text.h"
|
||||||
|
|
||||||
class MainScreen : public Screen {
|
class MainScreen : public Screen {
|
||||||
private:
|
private:
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#ifndef TITLE_SCREEN_H
|
#ifndef TITLE_SCREEN_H
|
||||||
#define TITLE_SCREEN_H
|
#define TITLE_SCREEN_H
|
||||||
|
|
||||||
#include "Screen.h"
|
#include "screens/Screen.h"
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "Text.h"
|
#include "entities/Text.h"
|
||||||
|
|
||||||
class TitleScreen : public Screen {
|
class TitleScreen : public Screen {
|
||||||
private:
|
private:
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#ifndef TUTORIAL_SCREEN_H
|
#ifndef TUTORIAL_SCREEN_H
|
||||||
#define TUTORIAL_SCREEN_H
|
#define TUTORIAL_SCREEN_H
|
||||||
|
|
||||||
#include "Screen.h"
|
#include "screens/Screen.h"
|
||||||
#include "Background.h"
|
#include "entities/Background.h"
|
||||||
#include "Text.h"
|
#include "entities/Text.h"
|
||||||
|
|
||||||
class TutorialScreen : public Screen
|
class TutorialScreen : public Screen
|
||||||
{
|
{
|
||||||
@@ -5,31 +5,14 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include "EFont.h"
|
#include "enums/EFont.h"
|
||||||
#include "EMusic.h"
|
#include "enums/EMusic.h"
|
||||||
#include "EBackground.h"
|
#include "enums/EBackground.h"
|
||||||
#include "Service.h"
|
#include "enums/EMap.h"
|
||||||
|
#include "services/Service.h"
|
||||||
|
|
||||||
class AssetService: public Service<AssetService>
|
class AssetService: public Service<AssetService>
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
/** Friend to allow access to the constructor */
|
|
||||||
friend class Service<AssetService>;
|
|
||||||
|
|
||||||
std::string assetDir;
|
|
||||||
std::unordered_map<EFont, std::string> fonts;
|
|
||||||
std::vector<Sound> sounds;
|
|
||||||
std::unordered_map<EMusic, Music> music;
|
|
||||||
std::unordered_map<EBackground, Texture2D> backgrounds;
|
|
||||||
|
|
||||||
void setAssets();
|
|
||||||
|
|
||||||
Music loadMusic(std::string filename, bool loops);
|
|
||||||
Sound loadSound(std::string filename);
|
|
||||||
std::string getFontUrl(EFont font);
|
|
||||||
protected:
|
|
||||||
AssetService();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void setAssetDir(std::string assetDir){this->assetDir = assetDir;}
|
void setAssetDir(std::string assetDir){this->assetDir = assetDir;}
|
||||||
|
|
||||||
@@ -41,6 +24,29 @@ public:
|
|||||||
return this->backgrounds[background];
|
return this->backgrounds[background];
|
||||||
}
|
}
|
||||||
Texture2D loadTexture(std::string filename);
|
Texture2D loadTexture(std::string filename);
|
||||||
|
std::string getMapUrl(EMap map) {
|
||||||
|
return this->assetDir + this->maps[map];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
AssetService();
|
||||||
|
|
||||||
|
private:
|
||||||
|
/** Friend to allow access to the constructor */
|
||||||
|
friend class Service<AssetService>;
|
||||||
|
|
||||||
|
std::string assetDir;
|
||||||
|
std::unordered_map<EFont, std::string> fonts;
|
||||||
|
std::vector<Sound> sounds;
|
||||||
|
std::unordered_map<EMusic, Music> music;
|
||||||
|
std::unordered_map<EBackground, Texture2D> backgrounds;
|
||||||
|
std::unordered_map<EMap, std::string> maps;
|
||||||
|
|
||||||
|
void setAssets();
|
||||||
|
|
||||||
|
Music loadMusic(std::string filename, bool loops);
|
||||||
|
Sound loadSound(std::string filename);
|
||||||
|
std::string getFontUrl(EFont font);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -2,18 +2,12 @@
|
|||||||
#define AUDIO_SERVICE_H
|
#define AUDIO_SERVICE_H
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "EMusic.h"
|
#include "enums/EMusic.h"
|
||||||
#include "Service.h"
|
#include "services/Service.h"
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
class AudioService : public Service<AudioService>
|
class AudioService : public Service<AudioService>
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
std::optional<Music> music;
|
|
||||||
EMusic currentTrack = EMusic::NONE;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void setMusic(EMusic music);
|
void setMusic(EMusic music);
|
||||||
std::optional<Music> getMusic(){return music;}
|
std::optional<Music> getMusic(){return music;}
|
||||||
@@ -24,6 +18,13 @@ public:
|
|||||||
|
|
||||||
void playSound();
|
void playSound();
|
||||||
void stopSounds();
|
void stopSounds();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::optional<Music> music;
|
||||||
|
EMusic currentTrack = EMusic::NONE;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
#ifndef INPUT_SERVICE_H
|
||||||
|
#define INPUT_SERVICE_H
|
||||||
|
|
||||||
|
#include "Service.h"
|
||||||
|
#include "raylib.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tracks all user input, which is then retrieved by the screens
|
||||||
|
*/
|
||||||
|
class InputService : public Service<InputService>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Setting up the keys to watch, so that only the keys that might get pressed are being watched
|
||||||
|
*/
|
||||||
|
void setKeysToWatch(std::vector<KeyboardKey> keysToWatch, std::vector<MouseButton> mouseButtonsToWatch)
|
||||||
|
{
|
||||||
|
this->watchedKeys.clear();
|
||||||
|
this->keysPressed.clear();
|
||||||
|
|
||||||
|
for (int i = 0; i < keysToWatch.size(); i++)
|
||||||
|
{
|
||||||
|
this->watchedKeys.insert(keysToWatch[i]);
|
||||||
|
this->keysPressed[keysToWatch[i]] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
setMouseButtonsToWatch(mouseButtonsToWatch);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a button has been pressed
|
||||||
|
* The flow:
|
||||||
|
* 1. Screen constructor: Announce which keys to watch
|
||||||
|
* 2. StateService loop: Check keys, store the result
|
||||||
|
* 3. Screen logic: If pressed, execute logic
|
||||||
|
*/
|
||||||
|
void checkKeys()
|
||||||
|
{
|
||||||
|
for (auto key : watchedKeys)
|
||||||
|
{
|
||||||
|
keysPressed[key] = false;
|
||||||
|
if (IsKeyPressed(key))
|
||||||
|
{
|
||||||
|
keysPressed[key] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto button : watchedMouseButtons)
|
||||||
|
{
|
||||||
|
mouseButtonsPressed[button] = false;
|
||||||
|
if (IsMouseButtonPressed(button))
|
||||||
|
{
|
||||||
|
mouseButtonsPressed[button] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isKeyPressed(KeyboardKey key)
|
||||||
|
{
|
||||||
|
return keysPressed[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isMouseButtonPressed(MouseButton button)
|
||||||
|
{
|
||||||
|
return mouseButtonsPressed[button];
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_set<KeyboardKey> watchedKeys;
|
||||||
|
std::unordered_map<KeyboardKey, bool> keysPressed;
|
||||||
|
std::unordered_set<MouseButton> watchedMouseButtons;
|
||||||
|
std::unordered_map<MouseButton, bool> mouseButtonsPressed;
|
||||||
|
|
||||||
|
void setMouseButtonsToWatch(std::vector<MouseButton> mouseButtonsToWatch)
|
||||||
|
{
|
||||||
|
this->watchedMouseButtons.clear();
|
||||||
|
this->mouseButtonsPressed.clear();
|
||||||
|
|
||||||
|
for (int i = 0; i < mouseButtonsToWatch.size(); i++)
|
||||||
|
{
|
||||||
|
this->watchedMouseButtons.insert(mouseButtonsToWatch[i]);
|
||||||
|
this->mouseButtonsPressed[mouseButtonsToWatch[i]] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -4,11 +4,6 @@
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class Service
|
class Service
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
protected:
|
|
||||||
static T* instance;
|
|
||||||
Service() {}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Service(const Service &) = delete;
|
Service(const Service &) = delete;
|
||||||
Service &operator=(const Service &) = delete;
|
Service &operator=(const Service &) = delete;
|
||||||
@@ -18,6 +13,13 @@ public:
|
|||||||
static T instance;
|
static T instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static T* instance;
|
||||||
|
Service() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
#ifndef STATE_SERVICE_H
|
#ifndef STATE_SERVICE_H
|
||||||
#define STATE_SERVICE_H
|
#define STATE_SERVICE_H
|
||||||
|
|
||||||
#include "Screen.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "EScreen.h"
|
#include "services/Service.h"
|
||||||
#include "Service.h"
|
#include "screens/Screen.h"
|
||||||
|
#include "enums/EScreen.h"
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#if defined(PLATFORM_WEB)
|
#if defined(PLATFORM_WEB)
|
||||||
@@ -17,6 +17,32 @@
|
|||||||
*/
|
*/
|
||||||
class StateService : public Service<StateService>
|
class StateService : public Service<StateService>
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
Vector2 getScreenSize()
|
||||||
|
{
|
||||||
|
return screenSize;
|
||||||
|
}
|
||||||
|
void setScreenSize(Vector2 screenSize)
|
||||||
|
{
|
||||||
|
this->screenSize = screenSize;
|
||||||
|
}
|
||||||
|
void setScreen(EScreen screen);
|
||||||
|
Screen *getScreen();
|
||||||
|
/**
|
||||||
|
* Start the game loop
|
||||||
|
*/
|
||||||
|
void startGame();
|
||||||
|
|
||||||
|
static void staticStep()
|
||||||
|
{
|
||||||
|
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||||
|
{
|
||||||
|
std::cout << "Input!" << std::endl;
|
||||||
|
}
|
||||||
|
// Access the singleton instance and call step
|
||||||
|
getInstance().step();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Screen> currentScreen;
|
std::unique_ptr<Screen> currentScreen;
|
||||||
|
|
||||||
@@ -30,20 +56,6 @@ private:
|
|||||||
void step();
|
void step();
|
||||||
|
|
||||||
void handleMusic();
|
void handleMusic();
|
||||||
|
|
||||||
public:
|
|
||||||
Vector2 getScreenSize(){
|
|
||||||
return screenSize;
|
|
||||||
}
|
|
||||||
void setScreenSize(Vector2 screenSize) {
|
|
||||||
this->screenSize = screenSize;
|
|
||||||
}
|
|
||||||
void setScreen(EScreen screen);
|
|
||||||
Screen *getScreen();
|
|
||||||
/**
|
|
||||||
* Start the game loop
|
|
||||||
*/
|
|
||||||
void startGame();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+11
-1
@@ -1,5 +1,15 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
emcc -o dist/game.js src/main.cpp -Os -Wall /app/raylib/src/libraylib.a -I. -I/app/raylib/build/raylib/include -L. -L$LD_LIBRARY_PATH -s USE_GLFW=3 -s --preload-file assets@/assets -DPLATFORM_WEB
|
# emcc src/main.cpp -o dist/game.js -Wno-missing-braces -I/app/libs/raylib/build/raylib/include -L/app/libs/raylib/src /app/libs/raylib/src/libraylib.a -s USE_GLFW=3 -DPLATFORM_WEB
|
||||||
|
|
||||||
|
|
||||||
|
emcc $(find src/ -name "*.cpp") -o dist/game.js -Wno-missing-braces -I./include -I/app/libs/raylib/build/raylib/include -L/app/libs/raylib/src /app/libs/raylib/src/libraylib.a -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=268435456 -s MAXIMUM_MEMORY=4294967296 -s --preload-file assets@/assets -s USE_GLFW=3 -DPLATFORM_WEB
|
||||||
|
# -I./include
|
||||||
|
# -L$LD_LIBRARY_PATH
|
||||||
|
# -I.
|
||||||
|
# -L.
|
||||||
|
# -s --preload-file assets@/assets
|
||||||
|
# /app/libs/raylib/src/libraylib.a
|
||||||
|
# -Os -Wall
|
||||||
# --shell-file /app/raylib/src/shell.html
|
# --shell-file /app/raylib/src/shell.html
|
||||||
cd dist
|
cd dist
|
||||||
python3 -m http.server 8080;
|
python3 -m http.server 8080;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "Background.h"
|
#include "entities/Background.h"
|
||||||
|
|
||||||
#include "AssetService.h"
|
#include "services/AssetService.h"
|
||||||
|
|
||||||
// Background::Background(EBackground background)
|
// Background::Background(EBackground background)
|
||||||
// {
|
// {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#include "Map.h"
|
#include "entities/Map.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "AssetService.h"
|
#include "services/AssetService.h"
|
||||||
|
|
||||||
Map::Map()
|
Map::Map()
|
||||||
{
|
{
|
||||||
@@ -14,7 +14,7 @@ Map::Map()
|
|||||||
void Map::loadMap()
|
void Map::loadMap()
|
||||||
{
|
{
|
||||||
std::vector<Vector2> tempMap;
|
std::vector<Vector2> tempMap;
|
||||||
std::ifstream openfile("assets/Maps/map1.txt");
|
std::ifstream openfile(AssetService::getInstance().getMapUrl(EMap::TEXTURE));
|
||||||
|
|
||||||
map.clear();
|
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)
|
Text::Text(std::string text, EFont font, int fontSize, int spacing, Color color, Vector2 position)
|
||||||
{
|
{
|
||||||
|
|||||||
+55
-5
@@ -1,16 +1,66 @@
|
|||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "Screen.h"
|
// #include "screens/Screen.h"
|
||||||
#include "TitleScreen.h"
|
// #include "screens/TitleScreen.h"
|
||||||
#include "StateService.h"
|
#include "services/StateService.h"
|
||||||
#include <iostream>
|
// #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& stateService = StateService::getInstance();
|
||||||
stateService.setScreenSize({640,544});
|
stateService.setScreenSize({640,544});
|
||||||
stateService.startGame();
|
stateService.startGame();
|
||||||
// std::cout << "StateService instance: " << service << std::endl;
|
// 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;
|
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 "screens/GameScreen.h"
|
||||||
#include "Map.h"
|
#include "entities/Map.h"
|
||||||
#include "StateService.h"
|
#include "services/StateService.h"
|
||||||
|
|
||||||
GameScreen::GameScreen()
|
GameScreen::GameScreen()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#include "MainScreen.h"
|
#include "screens/MainScreen.h"
|
||||||
#include "AudioService.h"
|
#include "services/AudioService.h"
|
||||||
#include "StateService.h"
|
#include "services/StateService.h"
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
#include "services/InputService.h"
|
||||||
|
|
||||||
MainScreen::MainScreen()
|
MainScreen::MainScreen()
|
||||||
{
|
{
|
||||||
@@ -33,6 +34,8 @@ MainScreen::MainScreen()
|
|||||||
AudioService::getInstance().setMusic(EMusic::MAIN);
|
AudioService::getInstance().setMusic(EMusic::MAIN);
|
||||||
AudioService::getInstance().playMusic();
|
AudioService::getInstance().playMusic();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InputService::getInstance().setKeysToWatch({KEY_ENTER, KEY_UP, KEY_DOWN}, {MOUSE_BUTTON_LEFT});
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainScreen::logic()
|
void MainScreen::logic()
|
||||||
@@ -78,18 +81,18 @@ void MainScreen::mouseLogic()
|
|||||||
|
|
||||||
void MainScreen::keyboardLogic()
|
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);
|
buttons[hoveredButton].setColor(WHITE);
|
||||||
/** Move up or down depending on the pressed button. +4 is used so that -1 becomes 3 (the last button) */
|
/** 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);
|
buttons[hoveredButton].setColor(RED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainScreen::clickLogic()
|
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();
|
std::string selectedButton = buttons[hoveredButton].getText();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
#include "Screen.h"
|
#include "screens/Screen.h"
|
||||||
#include "AudioService.h"
|
|
||||||
#include <optional>
|
|
||||||
|
|
||||||
|
|
||||||
// void Screen::draw()
|
// void Screen::draw()
|
||||||
// {
|
// {
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
#include "TitleScreen.h"
|
#include "screens/TitleScreen.h"
|
||||||
#include "AssetService.h"
|
#include "services/AssetService.h"
|
||||||
#include "StateService.h"
|
#include "services/StateService.h"
|
||||||
#include "EFont.h"
|
#include "enums/EFont.h"
|
||||||
#include "AudioService.h"
|
#include "services/AudioService.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "services/InputService.h"
|
||||||
|
|
||||||
TitleScreen::TitleScreen()
|
TitleScreen::TitleScreen()
|
||||||
{
|
{
|
||||||
@@ -27,14 +28,16 @@ TitleScreen::TitleScreen()
|
|||||||
AudioService::getInstance().setMusic(EMusic::MAIN);
|
AudioService::getInstance().setMusic(EMusic::MAIN);
|
||||||
AudioService::getInstance().playMusic();
|
AudioService::getInstance().playMusic();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InputService::getInstance().setKeysToWatch({KEY_ENTER}, {MOUSE_BUTTON_LEFT});
|
||||||
}
|
}
|
||||||
|
|
||||||
void TitleScreen::logic()
|
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);
|
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 "services/StateService.h"
|
||||||
#include "EFont.h"
|
#include "enums/EFont.h"
|
||||||
#include "EScreen.h"
|
#include "enums/EScreen.h"
|
||||||
|
#include "services/InputService.h"
|
||||||
|
|
||||||
TutorialScreen::TutorialScreen() {
|
TutorialScreen::TutorialScreen() {
|
||||||
Text textBack = Text("Press Enter to go back", EFont::SANSATION, 30, 0, RED);
|
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->text.push_back(textBack);
|
||||||
|
|
||||||
this->background = Background(EBackground::TUTORIAL);
|
this->background = Background(EBackground::TUTORIAL);
|
||||||
|
|
||||||
|
InputService::getInstance().setKeysToWatch({KEY_ENTER}, {MOUSE_BUTTON_LEFT});
|
||||||
}
|
}
|
||||||
|
|
||||||
void TutorialScreen::logic()
|
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);
|
StateService::getInstance().setScreen(EScreen::MAIN);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include "AssetService.h"
|
#include "services/AssetService.h"
|
||||||
|
#include <iostream>
|
||||||
AssetService::AssetService()
|
AssetService::AssetService()
|
||||||
{
|
{
|
||||||
setAssetDir("assets/");
|
setAssetDir("assets/");
|
||||||
@@ -25,6 +25,9 @@ void AssetService::setAssets()
|
|||||||
backgrounds[EBackground::GAME2] = loadTexture("Textures/GameBackground_2.jpg");
|
backgrounds[EBackground::GAME2] = loadTexture("Textures/GameBackground_2.jpg");
|
||||||
backgrounds[EBackground::GAME3] = loadTexture("Textures/GameBackground_3.png");
|
backgrounds[EBackground::GAME3] = loadTexture("Textures/GameBackground_3.png");
|
||||||
backgrounds[EBackground::VICTORY] = loadTexture("Textures/VictoryScreenBackground.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)
|
Font AssetService::getFont(EFont font, int fontSize)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include "AudioService.h"
|
#include "services/AudioService.h"
|
||||||
#include "AssetService.h"
|
#include "services/AssetService.h"
|
||||||
|
|
||||||
// AudioService::~AudioService()
|
// AudioService::~AudioService()
|
||||||
// {
|
// {
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
#include "StateService.h"
|
#include "services/StateService.h"
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "TitleScreen.h"
|
#include "screens/TitleScreen.h"
|
||||||
#include "MainScreen.h"
|
#include "screens/MainScreen.h"
|
||||||
#include "TutorialScreen.h"
|
#include "screens/TutorialScreen.h"
|
||||||
#include "GameScreen.h"
|
#include "screens/GameScreen.h"
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include "AudioService.h"
|
#include "services/AudioService.h"
|
||||||
|
#include "services/InputService.h"
|
||||||
|
|
||||||
void StateService::setScreen(EScreen state)
|
void StateService::setScreen(EScreen state)
|
||||||
{
|
{
|
||||||
@@ -38,16 +39,30 @@ Screen *StateService::getScreen()
|
|||||||
|
|
||||||
void StateService::startGame()
|
void StateService::startGame()
|
||||||
{
|
{
|
||||||
|
// if(!instance)
|
||||||
|
// return;
|
||||||
InitAudioDevice();
|
InitAudioDevice();
|
||||||
|
// InitWindow(640,544, "Jelly Rescue");
|
||||||
InitWindow(screenSize.x, screenSize.y, "Jelly Rescue");
|
InitWindow(screenSize.x, screenSize.y, "Jelly Rescue");
|
||||||
SetTargetFPS(60);
|
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);
|
setScreen(EScreen::TITLE);
|
||||||
|
|
||||||
#if defined(PLATFORM_WEB)
|
#if defined(PLATFORM_WEB)
|
||||||
emscripten_set_main_loop(step, 0, 1);
|
emscripten_set_main_loop(StateService::staticStep, 0, 1);
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
// while (!WindowShouldClose() && instance->gameRunning)
|
||||||
while (!WindowShouldClose() && gameRunning)
|
while (!WindowShouldClose() && gameRunning)
|
||||||
{
|
{
|
||||||
step();
|
step();
|
||||||
@@ -69,7 +84,18 @@ void StateService::handleMusic()
|
|||||||
|
|
||||||
void StateService::step()
|
void StateService::step()
|
||||||
{
|
{
|
||||||
|
InputService::getInstance().checkKeys();
|
||||||
|
|
||||||
handleMusic();
|
handleMusic();
|
||||||
currentScreen->draw();
|
currentScreen->draw();
|
||||||
currentScreen->logic();
|
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