[Updated] Codebase to deal with wasm-style keyboard and mouse inputs
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
#define BACKGROUND_H
|
||||
|
||||
#include "raylib.h"
|
||||
#include "EBackground.h"
|
||||
#include "enums/EBackground.h"
|
||||
#include <vector>
|
||||
#include "TextureWrapper.h"
|
||||
#include "AssetService.h"
|
||||
#include "StateService.h"
|
||||
#include "entities/TextureWrapper.h"
|
||||
#include "services/AssetService.h"
|
||||
#include "services/StateService.h"
|
||||
|
||||
class Background {
|
||||
public:
|
||||
@@ -14,8 +14,9 @@ class Background {
|
||||
|
||||
}
|
||||
Background(EBackground background){
|
||||
Texture2D texture = AssetService::getInstance().getBackground(background);
|
||||
TextureWrapper wrapper = TextureWrapper(texture, StateService::getInstance().getScreenSize(), {0,0});
|
||||
// Texture2D texture = AssetService::getInstance().getBackground(background);
|
||||
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);
|
||||
}
|
||||
Background(std::vector<TextureWrapper> backgrounds){
|
||||
@@ -1,9 +1,10 @@
|
||||
#ifndef GAME_H
|
||||
#define GAME_H
|
||||
|
||||
#include "Map.h"
|
||||
#include "Background.h"
|
||||
#include "StateService.h"
|
||||
#include "entities/Map.h"
|
||||
#include "entities/Background.h"
|
||||
#include "services/StateService.h"
|
||||
#include "services/AudioService.h"
|
||||
|
||||
class Game
|
||||
{
|
||||
@@ -16,7 +17,7 @@ public:
|
||||
wrapper2.setSourceRect({0,200,wrapper2.getSize().x, 200+wrapper2.getSize().y});
|
||||
TextureWrapper wrapper3 = TextureWrapper(AssetService::getInstance().getBackground(EBackground::GAME3), {1792.0f,(float)GetScreenHeight()}, {wrapper1.getSize().x+wrapper2.getSize().x,0.0});
|
||||
wrapper3.setSourceRect({64,200,64+wrapper3.getSize().x, 200+wrapper3.getSize().y});
|
||||
|
||||
|
||||
std::vector<TextureWrapper> wrappers;
|
||||
wrappers.push_back(wrapper1);
|
||||
wrappers.push_back(wrapper2);
|
||||
@@ -31,6 +32,9 @@ public:
|
||||
camera.rotation = 0.0f;
|
||||
camera.zoom = 1.0f;
|
||||
this->camera = camera;
|
||||
|
||||
AudioService::getInstance().setMusic(EMusic::GAME);
|
||||
AudioService::getInstance().playMusic();
|
||||
}
|
||||
~Game()
|
||||
{
|
||||
@@ -2,10 +2,10 @@
|
||||
#define TEXT_H
|
||||
|
||||
#include "raylib.h"
|
||||
#include "EFont.h"
|
||||
#include "enums/EFont.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "AssetService.h"
|
||||
#include "services/AssetService.h"
|
||||
|
||||
class Text {
|
||||
private:
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
class TextureWrapper {
|
||||
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->size = size;
|
||||
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
|
||||
#define GAME_SCREEN_H
|
||||
|
||||
#include "Screen.h"
|
||||
#include "Game.h"
|
||||
#include "screens/Screen.h"
|
||||
#include "entities/Game.h"
|
||||
|
||||
class GameScreen: public Screen {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef MAIN_SCREEN_H
|
||||
#define MAIN_SCREEN_H
|
||||
|
||||
#include "Screen.h"
|
||||
#include "screens/Screen.h"
|
||||
#include <vector>
|
||||
#include "Text.h"
|
||||
#include "entities/Text.h"
|
||||
|
||||
class MainScreen : public Screen {
|
||||
private:
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef TITLE_SCREEN_H
|
||||
#define TITLE_SCREEN_H
|
||||
|
||||
#include "Screen.h"
|
||||
#include "screens/Screen.h"
|
||||
#include "raylib.h"
|
||||
#include "Text.h"
|
||||
#include "entities/Text.h"
|
||||
|
||||
class TitleScreen : public Screen {
|
||||
private:
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef TUTORIAL_SCREEN_H
|
||||
#define TUTORIAL_SCREEN_H
|
||||
|
||||
#include "Screen.h"
|
||||
#include "Background.h"
|
||||
#include "Text.h"
|
||||
#include "screens/Screen.h"
|
||||
#include "entities/Background.h"
|
||||
#include "entities/Text.h"
|
||||
|
||||
class TutorialScreen : public Screen
|
||||
{
|
||||
@@ -5,31 +5,14 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include "EFont.h"
|
||||
#include "EMusic.h"
|
||||
#include "EBackground.h"
|
||||
#include "Service.h"
|
||||
#include "enums/EFont.h"
|
||||
#include "enums/EMusic.h"
|
||||
#include "enums/EBackground.h"
|
||||
#include "enums/EMap.h"
|
||||
#include "services/Service.h"
|
||||
|
||||
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:
|
||||
void setAssetDir(std::string assetDir){this->assetDir = assetDir;}
|
||||
|
||||
@@ -41,6 +24,29 @@ public:
|
||||
return this->backgrounds[background];
|
||||
}
|
||||
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
|
||||
@@ -2,18 +2,12 @@
|
||||
#define AUDIO_SERVICE_H
|
||||
|
||||
#include "raylib.h"
|
||||
#include "EMusic.h"
|
||||
#include "Service.h"
|
||||
#include "enums/EMusic.h"
|
||||
#include "services/Service.h"
|
||||
#include <optional>
|
||||
|
||||
class AudioService : public Service<AudioService>
|
||||
{
|
||||
private:
|
||||
std::optional<Music> music;
|
||||
EMusic currentTrack = EMusic::NONE;
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
void setMusic(EMusic music);
|
||||
std::optional<Music> getMusic(){return music;}
|
||||
@@ -24,6 +18,13 @@ public:
|
||||
|
||||
void playSound();
|
||||
void stopSounds();
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
std::optional<Music> music;
|
||||
EMusic currentTrack = EMusic::NONE;
|
||||
|
||||
};
|
||||
|
||||
#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>
|
||||
class Service
|
||||
{
|
||||
private:
|
||||
protected:
|
||||
static T* instance;
|
||||
Service() {}
|
||||
|
||||
public:
|
||||
Service(const Service &) = delete;
|
||||
Service &operator=(const Service &) = delete;
|
||||
@@ -18,6 +13,13 @@ public:
|
||||
static T instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
protected:
|
||||
static T* instance;
|
||||
Service() {}
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,11 +1,11 @@
|
||||
#ifndef STATE_SERVICE_H
|
||||
#define STATE_SERVICE_H
|
||||
|
||||
#include "Screen.h"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include "EScreen.h"
|
||||
#include "Service.h"
|
||||
#include "services/Service.h"
|
||||
#include "screens/Screen.h"
|
||||
#include "enums/EScreen.h"
|
||||
#include "raylib.h"
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
@@ -17,6 +17,32 @@
|
||||
*/
|
||||
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:
|
||||
std::unique_ptr<Screen> currentScreen;
|
||||
|
||||
@@ -30,20 +56,6 @@ private:
|
||||
void step();
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user