[Added] Tutorial Screen, Start of the Game Screen
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
#include "Background.h"
|
||||
|
||||
#include "AssetService.h"
|
||||
|
||||
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};
|
||||
}
|
||||
}
|
||||
|
||||
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::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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#include "Map.h"
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "AssetService.h"
|
||||
|
||||
Map::Map()
|
||||
{
|
||||
loadTileset();
|
||||
loadMap();
|
||||
}
|
||||
|
||||
void Map::loadMap()
|
||||
{
|
||||
std::vector<Vector2> tempMap;
|
||||
std::ifstream openfile("assets/Maps/map1.txt");
|
||||
|
||||
map.clear();
|
||||
|
||||
if (openfile.is_open())
|
||||
{
|
||||
std::string tileLocation;
|
||||
openfile >> tileLocation;
|
||||
// tileTexture.loadFromFile(tileLocation);
|
||||
// tileSprites.setTexture(tileTexture);
|
||||
while (!openfile.eof())
|
||||
{
|
||||
std::string str, value;
|
||||
std::getline(openfile, str);
|
||||
std::stringstream stream(str);
|
||||
|
||||
while (std::getline(stream, value, ' '))
|
||||
{
|
||||
if (value.length() > 0)
|
||||
{
|
||||
std::string xx = value.substr(0, value.find(','));
|
||||
std::string yy = value.substr(value.find(',') + 1);
|
||||
|
||||
int x = 0, y = 0, i, j;
|
||||
// Look at all characters before the ','
|
||||
for (i = 0; i < xx.length(); i++)
|
||||
{
|
||||
// if one of them is not a number, break
|
||||
if (!isdigit(xx[i]))
|
||||
break;
|
||||
}
|
||||
for (j = 0; j < yy.length(); j++)
|
||||
{
|
||||
if (!isdigit(yy[j]))
|
||||
break;
|
||||
}
|
||||
|
||||
// If all characters are numbers, convert string to int (the number that the sprite has in the sheet ( in this case, on the x axis)
|
||||
if (i == xx.length())
|
||||
x = atoi(xx.c_str());
|
||||
else
|
||||
x = -1;
|
||||
// Otherwise, return -1 (aka no sprite)
|
||||
|
||||
if (j == yy.length())
|
||||
y = atoi(yy.c_str());
|
||||
else
|
||||
y = -1;
|
||||
|
||||
// Hold both the x and y positions of the sprite
|
||||
Vector2 vec2 = {(float)x, (float)y};
|
||||
tempMap.push_back(vec2);
|
||||
}
|
||||
}
|
||||
|
||||
if (tempMap.size() > 0)
|
||||
{
|
||||
map.push_back(tempMap);
|
||||
tempMap.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Map::loadTileset()
|
||||
{
|
||||
this->tileset = AssetService::getInstance().loadTexture("Textures/tileset.png");
|
||||
}
|
||||
|
||||
void Map::draw()
|
||||
{
|
||||
for (int i = 0; i < map.size(); i++)
|
||||
{
|
||||
for (int j = 0; j < map[i].size(); j++)
|
||||
{
|
||||
if (map[i][j].x != -1 && map[i][j].y != -1)
|
||||
{
|
||||
// Rectangle source = {0,0,32,32};
|
||||
Rectangle sourceRect = {map[i][j].x * tileSize, map[i][j].y * tileSize, tileSize, tileSize};
|
||||
DrawTextureRec(tileset, sourceRect, {j * tileSize, i * tileSize}, WHITE);
|
||||
// tileSprites.setPosition(j * tileSize, i * tileSize);
|
||||
// tileSprites.setTextureRect(sf::IntRect(map[i][j].x * tileSize, map[i][j].y * tileSize, tileSize, tileSize));
|
||||
// GetWindow().draw(tileSprites);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,6 @@ Text::Text(std::string text, EFont font, int fontSize, int spacing, Color color,
|
||||
this->position = position;
|
||||
}
|
||||
|
||||
Text::~Text()
|
||||
{
|
||||
// Causes double free for some reason
|
||||
// UnloadFont(this->font);
|
||||
}
|
||||
|
||||
Vector2 Text::getDimensions()
|
||||
{
|
||||
return MeasureTextEx(font, text.c_str(), (float)fontSize, (float)spacing);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
int main(void)
|
||||
{
|
||||
StateService& stateService = StateService::getInstance();
|
||||
stateService.setScreenSize({640,544});
|
||||
stateService.startGame();
|
||||
// std::cout << "StateService instance: " << service << std::endl;
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#include "GameScreen.h"
|
||||
#include "Map.h"
|
||||
#include "StateService.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void GameScreen::logic()
|
||||
{
|
||||
// this->background.value().updatePosition({-2.5f,0});
|
||||
this->camera.value().target.x += 1.0f;
|
||||
}
|
||||
+23
-21
@@ -1,8 +1,7 @@
|
||||
#include "MainScreen.h"
|
||||
#include "AssetService.h"
|
||||
#include "AudioService.h"
|
||||
#include <iostream>
|
||||
#include "StateService.h"
|
||||
#include "raylib.h"
|
||||
|
||||
MainScreen::MainScreen()
|
||||
{
|
||||
@@ -17,7 +16,6 @@ MainScreen::MainScreen()
|
||||
Text txt = Text("new game", EFont::GTEK, 30, 0, WHITE);
|
||||
text.push_back(txt);
|
||||
}
|
||||
|
||||
text[0].setColor(RED);
|
||||
text[1].setText("load game");
|
||||
text[2].setText("tutorial");
|
||||
@@ -29,12 +27,12 @@ MainScreen::MainScreen()
|
||||
}
|
||||
|
||||
/** Will be ignored if already active (like when coming from the TitleScreen) */
|
||||
AudioService::getInstance().setMusic(EMusic::MAIN);
|
||||
AudioService::getInstance().playMusic();
|
||||
}
|
||||
|
||||
MainScreen::~MainScreen()
|
||||
{
|
||||
EMusic track = AudioService::getInstance().getCurrentTrack();
|
||||
if (track != EMusic::MAIN)
|
||||
{
|
||||
AudioService::getInstance().setMusic(EMusic::MAIN);
|
||||
AudioService::getInstance().playMusic();
|
||||
}
|
||||
}
|
||||
|
||||
void MainScreen::logic()
|
||||
@@ -43,10 +41,12 @@ void MainScreen::logic()
|
||||
// std::cout << mouseD.y <<std::endl;
|
||||
// if (mouseD.x != 0.0f && mouseD.y != 0.0f)
|
||||
// {
|
||||
// mouseLogic();
|
||||
mouseLogic();
|
||||
// }
|
||||
|
||||
keyboardLogic();
|
||||
|
||||
clickLogic();
|
||||
}
|
||||
|
||||
void MainScreen::mouseLogic()
|
||||
@@ -57,13 +57,15 @@ void MainScreen::mouseLogic()
|
||||
if (hoveredButton == i)
|
||||
continue;
|
||||
|
||||
/** 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();
|
||||
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 >= rect.x && mousePos.x <= (rect.x+rect.width) && mousePos.y >= rect.y && mousePos.y <= (rect.y + rect.height))
|
||||
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);
|
||||
hoveredButton = i;
|
||||
@@ -82,22 +84,22 @@ void MainScreen::keyboardLogic()
|
||||
hoveredButton = ((hoveredButton - (IsKeyPressed(KEY_DOWN) ? -1 : 1)) + 4) % 4;
|
||||
text[hoveredButton].setColor(RED);
|
||||
}
|
||||
}
|
||||
|
||||
void MainScreen::clickLogic()
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
std::cout << text[hoveredButton].getText() << std::endl;
|
||||
std::string selectedButton = text[hoveredButton].getText();
|
||||
|
||||
|
||||
if(selectedButton == "new game") {
|
||||
if (selectedButton == "new game")
|
||||
StateService::getInstance().setScreen(EScreen::GAME);
|
||||
else if (selectedButton == "load game")
|
||||
{
|
||||
}
|
||||
else if(selectedButton == "load game") {
|
||||
}
|
||||
else if(selectedButton == "tutorial") {
|
||||
else if (selectedButton == "tutorial")
|
||||
StateService::getInstance().setScreen(EScreen::TUTORIAL);
|
||||
}
|
||||
else if(selectedButton == "quit") {
|
||||
else if (selectedButton == "quit")
|
||||
StateService::getInstance().setScreen(EScreen::CLOSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
-1
@@ -2,10 +2,24 @@
|
||||
#include "AudioService.h"
|
||||
#include <optional>
|
||||
|
||||
Screen::Screen()
|
||||
{
|
||||
}
|
||||
|
||||
void Screen::draw()
|
||||
{
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
|
||||
if (camera.has_value())
|
||||
BeginMode2D(this->camera.value());
|
||||
|
||||
if (!background.has_value())
|
||||
ClearBackground(BLACK);
|
||||
else
|
||||
this->background.value().draw();
|
||||
|
||||
if (map.has_value())
|
||||
this->map.value().draw();
|
||||
|
||||
if (text.size() > 0)
|
||||
{
|
||||
@@ -14,5 +28,8 @@ void Screen::draw()
|
||||
text[t].draw();
|
||||
}
|
||||
}
|
||||
if (camera.has_value())
|
||||
EndMode2D();
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
@@ -27,20 +27,11 @@ TitleScreen::TitleScreen()
|
||||
text.push_back(textCenter);
|
||||
text.push_back(testSub);
|
||||
|
||||
|
||||
AudioService::getInstance().setMusic(EMusic::MAIN);
|
||||
AudioService::getInstance().playMusic();
|
||||
|
||||
// bg = LoadMusicStream("assets/Audio/Music/Crash_WarpRoomTheme.ogg");
|
||||
// bg.looping = true;
|
||||
// SetMasterVolume(1.0f);
|
||||
// SetMusicVolume(bg, 1.0f);
|
||||
// PlayMusicStream(bg);
|
||||
}
|
||||
|
||||
TitleScreen::~TitleScreen()
|
||||
{
|
||||
// UnloadMusicStream(bg);
|
||||
EMusic track = AudioService::getInstance().getCurrentTrack();
|
||||
if (track != EMusic::MAIN) {
|
||||
AudioService::getInstance().setMusic(EMusic::MAIN);
|
||||
AudioService::getInstance().playMusic();
|
||||
}
|
||||
}
|
||||
|
||||
void TitleScreen::logic()
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "TutorialScreen.h"
|
||||
|
||||
#include "StateService.h"
|
||||
#include "EFont.h"
|
||||
#include "EScreen.h"
|
||||
|
||||
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});
|
||||
this->text.push_back(textBack);
|
||||
|
||||
this->background = Background(EBackground::TUTORIAL);
|
||||
}
|
||||
|
||||
void TutorialScreen::logic()
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
StateService::getInstance().setScreen(EScreen::MAIN);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "AssetService.h"
|
||||
#include <iostream>
|
||||
|
||||
AssetService::AssetService()
|
||||
{
|
||||
@@ -19,6 +18,13 @@ void AssetService::setAssets()
|
||||
music[EMusic::MAIN] = loadMusic("Audio/Music/Crash_WarpRoomTheme.ogg", true);
|
||||
music[EMusic::GAME] = loadMusic("Audio/Music/GameTheme_2.ogg", true);
|
||||
music[EMusic::GAME_OVER] = loadMusic("Audio/Music/gameOverTheme.ogg", false);
|
||||
/** Todo: set volume */
|
||||
|
||||
backgrounds[EBackground::TUTORIAL] = loadTexture("Textures/TutorialBackground2.png");
|
||||
backgrounds[EBackground::GAME1] = loadTexture("Textures/GameBackground_1.png");
|
||||
backgrounds[EBackground::GAME2] = loadTexture("Textures/GameBackground_2.jpg");
|
||||
backgrounds[EBackground::GAME3] = loadTexture("Textures/GameBackground_3.png");
|
||||
backgrounds[EBackground::VICTORY] = loadTexture("Textures/VictoryScreenBackground.png");
|
||||
}
|
||||
|
||||
Font AssetService::getFont(EFont font, int fontSize)
|
||||
@@ -29,6 +35,12 @@ Font AssetService::getFont(EFont font, int fontSize)
|
||||
return fnt;
|
||||
}
|
||||
|
||||
Texture2D AssetService::loadTexture(std::string filename)
|
||||
{
|
||||
Texture2D texture = LoadTexture((assetDir + filename).c_str());
|
||||
return texture;
|
||||
}
|
||||
|
||||
std::string AssetService::getFontUrl(EFont font)
|
||||
{
|
||||
return assetDir + fonts[font];
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#include "raylib.h"
|
||||
#include "TitleScreen.h"
|
||||
#include "MainScreen.h"
|
||||
#include "TutorialScreen.h"
|
||||
#include "GameScreen.h"
|
||||
#include <optional>
|
||||
#include "AudioService.h"
|
||||
|
||||
@@ -15,6 +17,12 @@ void StateService::setScreen(EScreen state)
|
||||
case EScreen::MAIN:
|
||||
currentScreen = std::make_unique<MainScreen>();
|
||||
break;
|
||||
case EScreen::TUTORIAL:
|
||||
currentScreen = std::make_unique<TutorialScreen>();
|
||||
break;
|
||||
case EScreen::GAME:
|
||||
currentScreen = std::make_unique<GameScreen>();
|
||||
break;
|
||||
case EScreen::CLOSE:
|
||||
gameRunning = false;
|
||||
break;
|
||||
@@ -31,7 +39,7 @@ Screen *StateService::getScreen()
|
||||
void StateService::startGame()
|
||||
{
|
||||
InitAudioDevice();
|
||||
InitWindow(640, 544, "Jelly Rescue");
|
||||
InitWindow(screenSize.x, screenSize.y, "Jelly Rescue");
|
||||
SetTargetFPS(60);
|
||||
|
||||
setScreen(EScreen::TITLE);
|
||||
@@ -53,8 +61,9 @@ void StateService::startGame()
|
||||
void StateService::handleMusic()
|
||||
{
|
||||
std::optional<Music> music = AudioService::getInstance().getMusic();
|
||||
if(music.has_value()) {
|
||||
UpdateMusicStream(music.value());
|
||||
if (music.has_value())
|
||||
{
|
||||
UpdateMusicStream(music.value());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,4 +73,3 @@ void StateService::step()
|
||||
currentScreen->draw();
|
||||
currentScreen->logic();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user