[Updated] Codebase to deal with wasm-style keyboard and mouse inputs
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#ifndef BACKGROUND_H
|
||||
#define BACKGROUND_H
|
||||
|
||||
#include "raylib.h"
|
||||
#include "enums/EBackground.h"
|
||||
#include <vector>
|
||||
#include "entities/TextureWrapper.h"
|
||||
#include "services/AssetService.h"
|
||||
#include "services/StateService.h"
|
||||
|
||||
class Background {
|
||||
public:
|
||||
Background() {
|
||||
|
||||
}
|
||||
Background(EBackground background){
|
||||
// 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){
|
||||
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(){
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
#ifndef GAME_H
|
||||
#define GAME_H
|
||||
|
||||
#include "entities/Map.h"
|
||||
#include "entities/Background.h"
|
||||
#include "services/StateService.h"
|
||||
#include "services/AudioService.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;
|
||||
|
||||
AudioService::getInstance().setMusic(EMusic::GAME);
|
||||
AudioService::getInstance().playMusic();
|
||||
}
|
||||
~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
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef MAP_H
|
||||
#define MAP_H
|
||||
|
||||
#include <vector>
|
||||
#include "raylib.h"
|
||||
|
||||
/**
|
||||
* Holds the collision and texture maps for the game world
|
||||
*/
|
||||
class Map
|
||||
{
|
||||
public:
|
||||
Map();
|
||||
~Map() {}
|
||||
|
||||
void draw();
|
||||
|
||||
private:
|
||||
/** Holds the number for every single tile, which refers to a certain sprite in the tileset */
|
||||
std::vector<std::vector<Vector2>> map;
|
||||
/** Holds 1s (collision) and 0s (no collision) for every tile */
|
||||
std::vector<std::vector<int>> colMap;
|
||||
|
||||
float tileSize = 32;
|
||||
|
||||
Texture2D tileset;
|
||||
|
||||
void loadMap();
|
||||
void loadColMap(){}
|
||||
void loadTileset();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef TEXT_H
|
||||
#define TEXT_H
|
||||
|
||||
#include "raylib.h"
|
||||
#include "enums/EFont.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "services/AssetService.h"
|
||||
|
||||
class Text {
|
||||
private:
|
||||
std::string text;
|
||||
Font font;
|
||||
int fontSize;
|
||||
int spacing;
|
||||
Color color;
|
||||
Vector2 position;
|
||||
|
||||
public:
|
||||
Text(std::string text, EFont font, int fontSize = 30, int spacing = 0, Color color = WHITE, Vector2 position = {100,100});
|
||||
~Text(){}
|
||||
|
||||
Vector2 getDimensions();
|
||||
|
||||
void setPosition(Vector2 position) {
|
||||
this->position = position;
|
||||
}
|
||||
|
||||
void setColor(Color color) {
|
||||
this->color = color;
|
||||
}
|
||||
|
||||
void setText(std::string text) {
|
||||
this->text = text;
|
||||
}
|
||||
|
||||
Vector2 getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
std::string getText() {
|
||||
return this->text;
|
||||
}
|
||||
|
||||
void draw();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -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,32,32}) {
|
||||
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
|
||||
Reference in New Issue
Block a user