[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