[Updated] Backgrounds to display the desired portion of the image, [Added] Start of the Game class

This commit is contained in:
Ninjaws
2024-12-09 22:29:08 +01:00
parent 76dd80d3a3
commit cbf6e658ea
15 changed files with 323 additions and 143 deletions
+30 -10
View File
@@ -4,20 +4,40 @@
#include "raylib.h"
#include "EBackground.h"
#include <vector>
#include "TextureWrapper.h"
#include "AssetService.h"
#include "StateService.h"
class Background {
private:
Vector2 position;
std::vector<Texture2D> textures;
protected:
public:
Background(EBackground background);
Background(std::vector<EBackground> 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<TextureWrapper> 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<TextureWrapper> textures;
// Vector2 position;
// Vector2 size;
};
+71
View File
@@ -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<TextureWrapper> 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<Enemy> enemies
// std::vector<Create> crates
// std::vector<PBullet> playerBullets
// std::vector<EBullet> enemyBullets
// std::vector<Jelly> jellies
// std::vector<Ammo> ammo
// std::vector<Healthpack> healthpacks
// Powerup powerup
};
#endif
+3 -1
View File
@@ -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
+4
View File
@@ -2,10 +2,13 @@
#define MAIN_SCREEN_H
#include "Screen.h"
#include <vector>
#include "Text.h"
class MainScreen : public Screen {
private:
int hoveredButton = 0;
std::vector<Text> buttons;
void mouseLogic();
void keyboardLogic();
@@ -15,6 +18,7 @@ public:
MainScreen();
void logic() override;
void draw() override;
};
#endif
+20 -28
View File
@@ -1,36 +1,28 @@
#ifndef SCREEN_H
#define SCREEN_H
#include <vector>
#include "Text.h"
#include "raylib.h"
#include <optional>
#include "Background.h"
#include "Map.h"
class Screen
{
private:
protected:
// std::optional<Map> map;
// std::optional<Camera2D> camera;
// std::optional<Map> map;
// std::optional<Background> background;
// std::vector<Text> text;
class Screen {
private:
protected:
// std::optional<Map> map;
std::optional<Camera2D> camera;
std::optional<Map> map;
std::optional<Background> background;
std::vector<Text> 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
+1
View File
@@ -6,6 +6,7 @@
#include <memory>
#include "EScreen.h"
#include "Service.h"
#include "raylib.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
+44
View File
@@ -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
+3
View File
@@ -3,15 +3,18 @@
#include "Screen.h"
#include "raylib.h"
#include "Text.h"
class TitleScreen : public Screen {
private:
Music bg;
std::vector<Text> text;
public:
TitleScreen();
void logic() override;
void draw() override;
};
#endif
+9 -3
View File
@@ -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> text;
Background background;
protected:
public:
TutorialScreen();
~TutorialScreen() {}
void logic() override;
void draw() override;
};
#endif
+30 -30
View File
@@ -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<EBackground> 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<EBackground> 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);
// }
// }
+15 -15
View File
@@ -4,24 +4,24 @@
GameScreen::GameScreen()
{
std::vector<EBackground> 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<EBackground> 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();
}
+30 -14
View File
@@ -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();
}
+22 -25
View File
@@ -2,34 +2,31 @@
#include "AudioService.h"
#include <optional>
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();
}
// EndDrawing();
// }
+25 -15
View File
@@ -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();
}
+16 -2
View File
@@ -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();
}