diff --git a/include/Background.h b/include/Background.h index 26a5326..573ebe3 100644 --- a/include/Background.h +++ b/include/Background.h @@ -4,20 +4,40 @@ #include "raylib.h" #include "EBackground.h" #include +#include "TextureWrapper.h" +#include "AssetService.h" +#include "StateService.h" class Background { - private: - Vector2 position; - std::vector textures; - protected: public: - Background(EBackground background); - Background(std::vector backgrounds); + Background() { + + } + Background(EBackground background){ + Texture2D texture = AssetService::getInstance().getBackground(background); + TextureWrapper wrapper = TextureWrapper(texture, StateService::getInstance().getScreenSize(), {0,0}); + textures.push_back(wrapper); + } + Background(std::vector backgrounds){ + for(int i = 0; i < backgrounds.size(); i++) { + this->textures.push_back(backgrounds[i]); + } + } ~Background(){} - Vector2 getPosition(); - void updatePosition(Vector2 deltaPos); - void setPosition(Vector2 position); - void draw(); + // Vector2 getPosition(); + // void updatePosition(Vector2 deltaPos); + // void setPosition(Vector2 position); + void draw(){ + for(int i = 0; i < textures.size(); i++) { + TextureWrapper texture = textures[i]; + // Rectangle sourceRect = {0, 0, texture.getSize().x, texture.getSize().y}; + DrawTextureRec(texture.getTexture(), texture.getSourceRect(), texture.getPosition(),WHITE); + } + } + private: + std::vector textures; + // Vector2 position; + // Vector2 size; }; diff --git a/include/Game.h b/include/Game.h new file mode 100644 index 0000000..c8f6759 --- /dev/null +++ b/include/Game.h @@ -0,0 +1,71 @@ +#ifndef GAME_H +#define GAME_H + +#include "Map.h" +#include "Background.h" +#include "StateService.h" + +class Game +{ +public: + Game() + { + TextureWrapper wrapper1 = TextureWrapper(AssetService::getInstance().getBackground(EBackground::GAME1), {GetScreenWidth()*2.0f,(float)GetScreenHeight()}, {0,0}); + wrapper1.setSourceRect({0,0,wrapper1.getSize().x, wrapper1.getSize().y}); + TextureWrapper wrapper2 = TextureWrapper(AssetService::getInstance().getBackground(EBackground::GAME2), {864.0f,(float)GetScreenHeight()}, {wrapper1.getSize().x,0.0}); + 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 wrappers; + wrappers.push_back(wrapper1); + wrappers.push_back(wrapper2); + wrappers.push_back(wrapper3); + this->background = Background(wrappers); + this->map = Map(); + + Vector2 screenSize = StateService::getInstance().getScreenSize(); + Camera2D camera = {0}; + camera.offset = (Vector2){screenSize.x / 2.0f, screenSize.y / 2.0f}; // Set offset to center + camera.target = (Vector2){screenSize.x / 2.0f, screenSize.y / 2.0f}; // Initially at the center of the world + camera.rotation = 0.0f; + camera.zoom = 1.0f; + this->camera = camera; + } + ~Game() + { + } + + void logic() + { + this->camera.offset.x -= 1.5; + } + + void draw() + { + BeginDrawing(); + BeginMode2D(this->camera); + + this->background.draw(); + this->map.draw(); + + EndMode2D(); + EndDrawing(); + } + +private: + Camera2D camera; + Background background; + Map map; + // Player player + // std::vector enemies + // std::vector crates + // std::vector playerBullets + // std::vector enemyBullets + // std::vector jellies + // std::vector ammo + // std::vector healthpacks + // Powerup powerup +}; + +#endif \ No newline at end of file diff --git a/include/GameScreen.h b/include/GameScreen.h index fcd75f8..9fb2645 100644 --- a/include/GameScreen.h +++ b/include/GameScreen.h @@ -2,15 +2,17 @@ #define GAME_SCREEN_H #include "Screen.h" +#include "Game.h" class GameScreen: public Screen { private: + Game game; protected: public: GameScreen(); void logic() override; - + void draw() override; }; #endif \ No newline at end of file diff --git a/include/MainScreen.h b/include/MainScreen.h index 97ccd28..9e8f624 100644 --- a/include/MainScreen.h +++ b/include/MainScreen.h @@ -2,10 +2,13 @@ #define MAIN_SCREEN_H #include "Screen.h" +#include +#include "Text.h" class MainScreen : public Screen { private: int hoveredButton = 0; + std::vector buttons; void mouseLogic(); void keyboardLogic(); @@ -15,6 +18,7 @@ public: MainScreen(); void logic() override; + void draw() override; }; #endif \ No newline at end of file diff --git a/include/Screen.h b/include/Screen.h index 532b724..955bfe9 100644 --- a/include/Screen.h +++ b/include/Screen.h @@ -1,36 +1,28 @@ #ifndef SCREEN_H #define SCREEN_H -#include -#include "Text.h" -#include "raylib.h" -#include -#include "Background.h" -#include "Map.h" +class Screen +{ +private: +protected: + // std::optional map; + // std::optional camera; + // std::optional map; + // std::optional background; + // std::vector text; -class Screen { - private: - - protected: - // std::optional map; - std::optional camera; - std::optional map; - std::optional background; - std::vector text; - - public: - Screen(); - ~Screen(){} - - /** - * Execute all logic for this frame - */ - virtual void logic() = 0; - /** - * Draw frame to the screen - */ - void draw(); +public: + Screen(){} + ~Screen() {} + /** + * Execute all logic for this frame + */ + virtual void logic() = 0; + /** + * Draw frame to the screen + */ + virtual void draw() = 0; }; #endif diff --git a/include/StateService.h b/include/StateService.h index 3d66009..121c492 100644 --- a/include/StateService.h +++ b/include/StateService.h @@ -6,6 +6,7 @@ #include #include "EScreen.h" #include "Service.h" +#include "raylib.h" #if defined(PLATFORM_WEB) #include diff --git a/include/TextureWrapper.h b/include/TextureWrapper.h new file mode 100644 index 0000000..26c381b --- /dev/null +++ b/include/TextureWrapper.h @@ -0,0 +1,44 @@ +#ifndef TEXTURE_WRAPPER_H +#define TEXTURE_WRAPPER_H + +#include "raylib.h" + +class TextureWrapper { + public: + TextureWrapper(Texture2D texture, Vector2 size = {32,32}, Vector2 position = {0,0}, Rectangle sourceRect = {0,0,100,100}) { + this->texture = texture; + this->size = size; + this->position = position; + } + ~TextureWrapper() { + + } + + Texture2D getTexture() { + return this->texture; + } + + Rectangle getSourceRect(){ + return this->sourceRect; + } + + Vector2 getSize() { + return this->size; + } + + Vector2 getPosition() { + return this->position; + } + + void setSourceRect(Rectangle rect) { + this->sourceRect = rect; + } + + private: + Texture2D texture; + Rectangle sourceRect; + Vector2 size; + Vector2 position; +}; + +#endif \ No newline at end of file diff --git a/include/TitleScreen.h b/include/TitleScreen.h index 45b33c4..31d5896 100644 --- a/include/TitleScreen.h +++ b/include/TitleScreen.h @@ -3,15 +3,18 @@ #include "Screen.h" #include "raylib.h" +#include "Text.h" class TitleScreen : public Screen { private: Music bg; + std::vector text; public: TitleScreen(); void logic() override; + void draw() override; }; #endif \ No newline at end of file diff --git a/include/TutorialScreen.h b/include/TutorialScreen.h index 853ab30..b974ef0 100644 --- a/include/TutorialScreen.h +++ b/include/TutorialScreen.h @@ -2,16 +2,22 @@ #define TUTORIAL_SCREEN_H #include "Screen.h" +#include "Background.h" +#include "Text.h" -class TutorialScreen : public Screen { +class TutorialScreen : public Screen +{ private: + std::vector text; + Background background; + protected: public: TutorialScreen(); + ~TutorialScreen() {} void logic() override; - + void draw() override; }; - #endif \ No newline at end of file diff --git a/src/entities/Background.cpp b/src/entities/Background.cpp index 11c9cc5..a3fc549 100644 --- a/src/entities/Background.cpp +++ b/src/entities/Background.cpp @@ -2,38 +2,38 @@ #include "AssetService.h" -Background::Background(EBackground background) -{ - this->textures.push_back(AssetService::getInstance().getBackground(background)); - this->position = {0,0}; -} +// Background::Background(EBackground background) +// { +// this->textures.push_back(AssetService::getInstance().getBackground(background)); +// this->position = {0,0}; +// } -Background::Background(std::vector backgrounds) -{ - for(int i = 0; i < backgrounds.size(); i++) { - this->textures.push_back(AssetService::getInstance().getBackground(backgrounds[i])); - this->position = {0,0}; - } -} +// Background::Background(std::vector backgrounds) +// { +// for(int i = 0; i < backgrounds.size(); i++) { +// this->textures.push_back(AssetService::getInstance().getBackground(backgrounds[i])); +// this->position = {0,0}; +// } +// } -Vector2 Background::getPosition() -{ - return this->position; -} +// Vector2 Background::getPosition() +// { +// return this->position; +// } -void Background::updatePosition(Vector2 deltaPos) -{ - this->position = {this->position.x+deltaPos.x, this->position.y+deltaPos.y}; -} +// void Background::updatePosition(Vector2 deltaPos) +// { +// this->position = {this->position.x+deltaPos.x, this->position.y+deltaPos.y}; +// } -void Background::setPosition(Vector2 position) -{ - this->position = position; -} +// void Background::setPosition(Vector2 position) +// { +// this->position = position; +// } -void Background::draw() -{ - for(int i = 0; i < textures.size(); i++) { - DrawTexture(textures[i], position.x+ (i != 0 ? i*textures[i-1].width : 0), position.y, WHITE); - } -} +// void Background::draw() +// { +// for(int i = 0; i < textures.size(); i++) { +// DrawTexture(textures[i], position.x+ (i != 0 ? i*textures[i-1].width : 0), position.y, WHITE); +// } +// } diff --git a/src/screens/GameScreen.cpp b/src/screens/GameScreen.cpp index b06934a..aa64886 100644 --- a/src/screens/GameScreen.cpp +++ b/src/screens/GameScreen.cpp @@ -4,24 +4,24 @@ GameScreen::GameScreen() { - std::vector bgs; - bgs.push_back(EBackground::GAME1); - bgs.push_back(EBackground::GAME2); - bgs.push_back(EBackground::GAME3); - this->background = Background(bgs); - this->map = Map(); - - Vector2 screenSize = StateService::getInstance().getScreenSize(); - Camera2D camera = { 0 }; - camera.offset = (Vector2){ screenSize.x / 2.0f, screenSize.y / 2.0f }; // Set offset to center - camera.target = (Vector2){ screenSize.x /2.0f, screenSize.y / 2.0f }; // Initially at the center of the world - camera.rotation = 0.0f; - camera.zoom = 1.0f; - this->camera = camera; + // std::vector bgs; + // bgs.push_back(EBackground::GAME1); + // bgs.push_back(EBackground::GAME2); + // bgs.push_back(EBackground::GAME3); + // this->background = Background(bgs); + // this->map = Map(); + + this->game = Game(); + // this->camera = camera; } void GameScreen::logic() { + this->game.logic(); // this->background.value().updatePosition({-2.5f,0}); - this->camera.value().target.x += 1.0f; + // this->camera.value().target.x += 1.0f; +} + +void GameScreen::draw() { + this->game.draw(); } diff --git a/src/screens/MainScreen.cpp b/src/screens/MainScreen.cpp index 0be1ee5..a8e7523 100644 --- a/src/screens/MainScreen.cpp +++ b/src/screens/MainScreen.cpp @@ -14,16 +14,16 @@ MainScreen::MainScreen() for (int i = 0; i < AMNT_OF_BUTTONS; i++) { Text txt = Text("new game", EFont::GTEK, 30, 0, WHITE); - text.push_back(txt); + buttons.push_back(txt); } - text[0].setColor(RED); - text[1].setText("load game"); - text[2].setText("tutorial"); - text[3].setText("quit"); + buttons[0].setColor(RED); + buttons[1].setText("load game"); + buttons[2].setText("tutorial"); + buttons[3].setText("quit"); for (int i = 0; i < AMNT_OF_BUTTONS; i++) { - text[i].setPosition({(float)(GetScreenWidth() - text[i].getDimensions().x) / 2.0f, (float)TOP_GAP + SPACE_PER_ITEM * i}); + buttons[i].setPosition({(float)(GetScreenWidth() - buttons[i].getDimensions().x) / 2.0f, (float)TOP_GAP + SPACE_PER_ITEM * i}); } /** Will be ignored if already active (like when coming from the TitleScreen) */ @@ -49,9 +49,10 @@ void MainScreen::logic() clickLogic(); } + void MainScreen::mouseLogic() { - for (int i = 0; i < text.size(); i++) + for (int i = 0; i < buttons.size(); i++) { /** No need to check the button that is already being hovered */ if (hoveredButton == i) @@ -59,17 +60,17 @@ void MainScreen::mouseLogic() /** Pixels around the text that also count towards that button, to make the hover feel better */ int textPadding = 20; - Vector2 dim = text[i].getDimensions(); - Vector2 pos = text[i].getPosition(); + Vector2 dim = buttons[i].getDimensions(); + Vector2 pos = buttons[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 + textPadding) >= rect.x && mousePos.x <= (rect.x + rect.width + textPadding) && (mousePos.y + textPadding) >= rect.y && mousePos.y <= (rect.y + rect.height + textPadding)) { - text[hoveredButton].setColor(WHITE); + buttons[hoveredButton].setColor(WHITE); hoveredButton = i; - text[hoveredButton].setColor(RED); + buttons[hoveredButton].setColor(RED); break; } } @@ -79,10 +80,10 @@ void MainScreen::keyboardLogic() { if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_UP)) { - text[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) */ hoveredButton = ((hoveredButton - (IsKeyPressed(KEY_DOWN) ? -1 : 1)) + 4) % 4; - text[hoveredButton].setColor(RED); + buttons[hoveredButton].setColor(RED); } } @@ -90,7 +91,7 @@ void MainScreen::clickLogic() { if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - std::string selectedButton = text[hoveredButton].getText(); + std::string selectedButton = buttons[hoveredButton].getText(); if (selectedButton == "new game") StateService::getInstance().setScreen(EScreen::GAME); @@ -103,3 +104,18 @@ void MainScreen::clickLogic() StateService::getInstance().setScreen(EScreen::CLOSE); } } + +void MainScreen::draw() +{ + BeginDrawing(); + ClearBackground(BLACK); + + if (buttons.size() > 0) + { + for (int i = 0; i < buttons.size(); i++) + { + buttons[i].draw(); + } + } + EndDrawing(); +} \ No newline at end of file diff --git a/src/screens/Screen.cpp b/src/screens/Screen.cpp index 473d121..2793dff 100644 --- a/src/screens/Screen.cpp +++ b/src/screens/Screen.cpp @@ -2,34 +2,31 @@ #include "AudioService.h" #include -Screen::Screen() -{ -} -void Screen::draw() -{ - BeginDrawing(); +// void Screen::draw() +// { +// BeginDrawing(); - if (camera.has_value()) - BeginMode2D(this->camera.value()); +// if (camera.has_value()) +// BeginMode2D(this->camera.value()); - if (!background.has_value()) - ClearBackground(BLACK); - else - this->background.value().draw(); +// if (!background.has_value()) +// ClearBackground(BLACK); +// else +// this->background.value().draw(); - if (map.has_value()) - this->map.value().draw(); +// if (map.has_value()) +// this->map.value().draw(); - if (text.size() > 0) - { - for (int t = 0; t < text.size(); t++) - { - text[t].draw(); - } - } - if (camera.has_value()) - EndMode2D(); +// if (text.size() > 0) +// { +// for (int t = 0; t < text.size(); t++) +// { +// text[t].draw(); +// } +// } +// if (camera.has_value()) +// EndMode2D(); - EndDrawing(); -} \ No newline at end of file +// EndDrawing(); +// } \ No newline at end of file diff --git a/src/screens/TitleScreen.cpp b/src/screens/TitleScreen.cpp index 579948c..dd517a6 100644 --- a/src/screens/TitleScreen.cpp +++ b/src/screens/TitleScreen.cpp @@ -7,28 +7,23 @@ TitleScreen::TitleScreen() { - Text textAbout = Text("Made by Ian Vink", EFont::SANSATION, 20,0, WHITE); - textAbout.setPosition({ - (float)(GetScreenWidth()-textAbout.getDimensions().x)/1.2f, - (float)(GetScreenHeight()-textAbout.getDimensions().y)/6.0f - }); - Text textCenter = Text("Jelly rescue", EFont::RECKONER_BOLD, 100, 0, {255,25,25,255}); - textCenter.setPosition({ - (float)(GetScreenWidth()-textCenter.getDimensions().x)/2.0f, - (float)(GetScreenHeight()-textCenter.getDimensions().y)/2.0f - }); + Text textAbout = Text("Made by Ian Vink", EFont::SANSATION, 20, 0, WHITE); + textAbout.setPosition({(float)(GetScreenWidth() - textAbout.getDimensions().x) / 1.2f, + (float)(GetScreenHeight() - textAbout.getDimensions().y) / 6.0f}); + Text textCenter = Text("Jelly rescue", EFont::RECKONER_BOLD, 100, 0, {255, 25, 25, 255}); + textCenter.setPosition({(float)(GetScreenWidth() - textCenter.getDimensions().x) / 2.0f, + (float)(GetScreenHeight() - textCenter.getDimensions().y) / 2.0f}); Text testSub = Text("Press Enter", EFont::SANSATION, 30, 0, WHITE); - testSub.setPosition({ - (float)(GetScreenWidth()-testSub.getDimensions().x)/2.0f, - (float)(GetScreenHeight()-testSub.getDimensions().y)/1.5f - }); + testSub.setPosition({(float)(GetScreenWidth() - testSub.getDimensions().x) / 2.0f, + (float)(GetScreenHeight() - testSub.getDimensions().y) / 1.5f}); text.push_back(textAbout); text.push_back(textCenter); text.push_back(testSub); EMusic track = AudioService::getInstance().getCurrentTrack(); - if (track != EMusic::MAIN) { + if (track != EMusic::MAIN) + { AudioService::getInstance().setMusic(EMusic::MAIN); AudioService::getInstance().playMusic(); } @@ -41,4 +36,19 @@ void TitleScreen::logic() StateService::getInstance().setScreen(EScreen::MAIN); // std::cout << "Next screen!" << std::endl; } +} + +void TitleScreen::draw() +{ + BeginDrawing(); + ClearBackground(BLACK); + + if (text.size() > 0) + { + for (int t = 0; t < text.size(); t++) + { + text[t].draw(); + } + } + EndDrawing(); } \ No newline at end of file diff --git a/src/screens/TutorialScreen.cpp b/src/screens/TutorialScreen.cpp index 2135016..2ddf6d0 100644 --- a/src/screens/TutorialScreen.cpp +++ b/src/screens/TutorialScreen.cpp @@ -4,8 +4,7 @@ #include "EFont.h" #include "EScreen.h" -TutorialScreen::TutorialScreen() -{ +TutorialScreen::TutorialScreen() { Text textBack = Text("Press Enter to go back", EFont::SANSATION, 30, 0, RED); textBack.setPosition({(float)(GetScreenWidth() - textBack.getDimensions().x) / 1.3f, (float)(GetScreenHeight() - textBack.getDimensions().y) / 1.15f}); @@ -21,3 +20,18 @@ void TutorialScreen::logic() StateService::getInstance().setScreen(EScreen::MAIN); } } + +void TutorialScreen::draw() +{ + BeginDrawing(); + this->background.draw(); + + if (text.size() > 0) + { + for (int t = 0; t < text.size(); t++) + { + text[t].draw(); + } + } + EndDrawing(); +}