Skip to content

Commit

Permalink
Merge pull request #65 from 8enrich/NewButtons
Browse files Browse the repository at this point in the history
Melhorias sonoras e outras mudanças
  • Loading branch information
dabzr authored Sep 1, 2024
2 parents 8a5f00a + f9d1500 commit 4a9511e
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 24 deletions.
Binary file added assets/button.wav
Binary file not shown.
Binary file added assets/clear.wav
Binary file not shown.
Binary file added assets/fix.wav
Binary file not shown.
Binary file added assets/gameover.wav
Binary file not shown.
Binary file added assets/gameplay.mp3
Binary file not shown.
Binary file added assets/menu.mp3
Binary file not shown.
Binary file added assets/move.wav
Binary file not shown.
2 changes: 1 addition & 1 deletion assets/user_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
10,
20
],
"CELLSIZE": 22,
"CELLSIZE": 23,
"CONTROL": 0,
"CUSTOM_CONTROLS": [
87,
Expand Down
1 change: 1 addition & 0 deletions include/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Button {
Texture2D *image = nullptr;
virtual void Update();
bool isButtonClickedByMouse();
Sound hoveringSound;
bool isClicked;
int hoveringPadding = 20, padding = 27;
Screens screen = NOTSCREEN;
Expand Down
6 changes: 5 additions & 1 deletion include/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Game : public Screen {
Game(Board *board);
~Game();
void Tick() override;
int GetScore();
int GetScore();
private:
Shape i, o, t, j, l, s, z;
Shape shapes[7];
Expand All @@ -31,6 +31,10 @@ class Game : public Screen {
for (int i = 0; i < 7; ++i) this->shapes[i] = &shapes[i];
}
};
Sound clearLineSound;
Sound moveShapeSound;
Sound fixShapeSound;
Sound gameOverSound;
Texture2D *backgroundTexture = nullptr;
int tickCount;
int hold;
Expand Down
3 changes: 2 additions & 1 deletion include/GameOver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class GameOver : public Screen {
ScreenButton PlayAgain = ScreenButton("Play Again", Vec2<double>{1.0f / 4, 1 / 1.5}, fontSize, GAME);
ScreenButton Options = ScreenButton("Options", Vec2<double>{1.0f / 2, 1 / 1.5}, fontSize, OPTIONS);
ScreenButton MainMenu = ScreenButton("Main Menu", Vec2<double>{3.0f / 4, 1 / 1.5}, fontSize, MENU);
const std::vector<Button*> buttons = {&PlayAgain, &Options, &MainMenu};
ScreenButton Stats = ScreenButton(Vec2<double>{1.0f/1.08, 1.0f/50}, STATS, "stats.png");
const std::vector<Button*> buttons = {&PlayAgain, &Options, &MainMenu, &Stats};
ButtonManager buttonManager = ButtonManager(buttons);
void OptionsHandling();
void Draw() override;
Expand Down
2 changes: 1 addition & 1 deletion include/Menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Menu : public Screen {
ScreenButton Coop = ScreenButton("Coop", Vec2<double>{3.8f / 10, 1.0f /1.5}, 1.5f / 15, COOPOPTIONS, Color{122,207,89,255});
ScreenButton Options = ScreenButton("Options", Vec2<double>{5.9f / 10, 1.0f / 1.5}, 1.5f / 15, OPTIONS, Color{75, 6, 218, 255});
ScreenButton Quit = ScreenButton("Quit", Vec2<double>{8.0f / 10, 1.0f / 1.5}, 1.5f / 15, EXIT, Color{229, 50, 60, 255});
ScreenButton Stats = ScreenButton(Vec2<double>{1.0f/1.08, 1.0f/50}, EXIT, "stats.png");
ScreenButton Stats = ScreenButton(Vec2<double>{1.0f/1.08, 1.0f/50}, STATS, "stats.png");
const std::vector<Button*> buttons = { &Solo, &Coop, &Options, &Quit, &Stats };
ButtonManager buttonManager = ButtonManager(buttons);
};
1 change: 1 addition & 0 deletions include/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum Screens {
MENU,
PAUSE,
GAMEOVER,
STATS,
NOTSCREEN,
};

Expand Down
19 changes: 19 additions & 0 deletions include/Stats.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <raylib.h>
#include "Screen.hpp"
#include "ScreenButton.hpp"
#include "ButtonManager.hpp"

class Stats : public Screen {
public:
Stats();
~Stats();
void Tick() override;
void OpenClose() override;
private:
void OptionsHandling();
void Draw() override;
const float fontSize = 1.0f/30;
const std::vector<Button*> buttons = { &returnButton };
ScreenButton returnButton;
ButtonManager *buttonManager;
};
8 changes: 6 additions & 2 deletions src/Button.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../include/Button.hpp"
#include "../include/raylibFunctions.hpp"
#include <raylib.h>
#include <string>

Button::Button(std::string buttonText, Vec2<double> buttonPosition, float fontSize, ButtonTypes type):
Button(buttonText, buttonPosition, fontSize, type, ButtonStyles::TEXT)
Expand All @@ -15,12 +16,14 @@ Button::Button(std::string buttonText, Vec2<double> buttonPosition, float fontSi
Button::Button(Vec2<double> buttonPosition, ButtonTypes type, std::string fileName):
buttonPosition(buttonPosition), style(ButtonStyles::IMAGE),
image(new Texture2D(LoadTexture((std::string(ASSETS_PATH) + fileName).c_str()))),
isSelected(false), isClicked(false), buttonWidthHeight({0, 0}), realButtonPosition({0, 0})
isSelected(false), isClicked(false), buttonWidthHeight({0, 0}), realButtonPosition({0, 0}),
hoveringSound(LoadSound((std::string(ASSETS_PATH)+"button.wav").c_str()))
{}

Button::Button(std::string buttonText, Vec2<double> buttonPosition, float fontSize, ButtonTypes type, ButtonStyles style):
buttonText(buttonText), buttonPosition(buttonPosition), fontSize(fontSize), type(type), style(style),
isClicked(false), isSelected(false), buttonWidthHeight({0, 0}), realButtonPosition(0, 0)
isClicked(false), isSelected(false), buttonWidthHeight({0, 0}), realButtonPosition(0, 0),
hoveringSound(LoadSound((std::string(ASSETS_PATH)+"button.wav").c_str()))
{}

Button::~Button(){
Expand Down Expand Up @@ -92,6 +95,7 @@ std::string Button::GetText() {
void Button::Select(){
isSelected = true;
SetMouseCursor(MOUSE_CURSOR_POINTING_HAND);
PlaySound(hoveringSound);
}

void Button::Unselect(){
Expand Down
2 changes: 1 addition & 1 deletion src/ButtonManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void ButtonManager::MouseHandling(Button* button){
return;
}
currentSelectedButtonIndex = GetButtonIndex(button);
button->Select();
if(!button->isSelected) button->Select();
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) currentScreen = buttons[currentSelectedButtonIndex]->Click();
if(typeid(*button) == typeid(Button)){
if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)){
Expand Down
18 changes: 14 additions & 4 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
#include "../include/Settings.hpp"
#include <new>
#include <raylib.h>
#include <string>

Game::Game(Board *board) :
board(board), Screen(std::string(ASSETS_PATH)+"tetris.mp3"),
board(board), Screen(std::string(ASSETS_PATH)+"gameplay.mp3"),
i(I_Shape(*board)), o(O_Shape(*board)), t(T_Shape(*board)),
j(J_Shape(*board)), l(L_Shape(*board)), s(S_Shape(*board)), z(Z_Shape(*board)),
shapes{ i, o, t, j, l, s, z },
hold(-1), score(0), level(0), speed(15), cleanedLinesCount(0), maxTickToFix(30),
player(new Player(maxTickToFix, true, shapes)),
backgroundTexture(new Texture2D(LoadTexture((std::string(ASSETS_PATH) + "relaxing-bg.png").c_str())))
backgroundTexture(new Texture2D(LoadTexture((std::string(ASSETS_PATH) + "relaxing-bg.png").c_str()))),
clearLineSound(LoadSound((std::string(ASSETS_PATH)+ "clear.wav").c_str())),
moveShapeSound(LoadSound((std::string(ASSETS_PATH)+ "move.wav").c_str())),
gameOverSound(LoadSound((std::string(ASSETS_PATH)+"gameover.wav").c_str())),
fixShapeSound(LoadSound((std::string(ASSETS_PATH)+"fix.wav").c_str()))
{
if(!backgroundTexture) throw std::bad_alloc();
board->ResetBoardCells();
Expand Down Expand Up @@ -45,7 +50,7 @@ void Game::Tick(){

bool Game::HasLost(){
for(int x = 0, width = board->GetWidth(); x < width; x++){
if(board->CellExists({x, 0})){ return true; }
if(board->CellExists({x, 0})){ PlaySound(gameOverSound); return true;}
}
return false;
}
Expand All @@ -71,6 +76,7 @@ void Game::ClearLines(){
for (int x = 0; x < width; x++){
if(!board->CellExists({x, y})){ break; }
if(x + 1 != width){ continue; }
PlaySound(clearLineSound);
for (int i = 0; i < width; i++){ board->RemoveCell({i, y}); }
cleanedLines[index++] = y;
}
Expand Down Expand Up @@ -104,7 +110,8 @@ void Game::UpdateShape(Player *player){
if ((player->shape)->WillCollideDown()){
(player->tickToFix)--;
if((player->tickToFix) > 0) return;
FixShape(player->shape);
FixShape(player->shape);
PlaySound(fixShapeSound);
player->shape = NextShape(player);
player->canHold = true;
}
Expand Down Expand Up @@ -205,12 +212,14 @@ void Game::MoveIfKeyDown(Player *player, int control){
if (!(player->shape)->WillCollideRight()){
(player->shape)->MoveRight();
(player->tickToFix)++;
PlaySound(moveShapeSound);
}
break;
case LEFT:
if (!(player->shape)->WillCollideLeft()) {
(player->shape)->MoveLeft();
(player->tickToFix)++;
PlaySound(moveShapeSound);
}
break;
case DOWN:
Expand All @@ -224,6 +233,7 @@ void Game::MoveIfKeyDown(Player *player, int control){

void Game::Hold(Player *player){
player->canHold = false;
player->tickToFix = maxTickToFix;
int index = (player->shape)->GetIndex();
if(hold >= 0){
SwapShapeAndHold(index, player);
Expand Down
11 changes: 3 additions & 8 deletions src/GameOver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ void GameOver::Draw(){
}

void GameOver::DrawScores(){
ray_functions::DrawFormatedText("Score:", Vec2<double>{1.0f/4, 1/3.2}, fontSize, RAYWHITE);
ray_functions::DrawFormatedText(TextFormat("%d", score), Vec2<double>{1.0f/4, 1/2.6}, fontSize, RAYWHITE);
ray_functions::DrawFormatedText("HighScores:", Vec2<double>{3.0f/4, 1/3.2}, fontSize, RAYWHITE);
for(int i = 0; i < 5; i++){
ray_functions::DrawFormatedText(TextFormat("%d", settings::highscores[i]), Vec2<double>{3.0f/4, (1 + 0.17 *(i + 1))/3.2},
fontSize, RAYWHITE);
}
if(hasNewHighscore) ray_functions::DrawFormatedText("New HighScore!", Vec2<double>{1.0f/4, 1/1.8}, fontSize, RAYWHITE);
ray_functions::DrawFormatedText("Score:", Vec2<double>{1.0f/2, 1/3.2}, fontSize, RAYWHITE);
ray_functions::DrawFormatedText(TextFormat("%d", score), Vec2<double>{1.0f/2, 1/2.6}, fontSize, RAYWHITE);
if(hasNewHighscore) ray_functions::DrawFormatedText("New HighScore!", Vec2<double>{1.0f/2, 1/1.8}, fontSize, RAYWHITE);
}

void GameOver::OptionsHandling(){
Expand Down
4 changes: 3 additions & 1 deletion src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#include "../include/Settings.hpp"
#include <raylib.h>
#include <string>

Menu::Menu() :
logoTexture(new Texture2D(LoadTexture((std::string(ASSETS_PATH) + "logo.png").c_str())))
logoTexture(new Texture2D(LoadTexture((std::string(ASSETS_PATH) + "logo.png").c_str()))),
Screen(std::string(ASSETS_PATH)+"menu.mp3")
{
if(!logoTexture) throw std::bad_alloc();
}
Expand Down
5 changes: 1 addition & 4 deletions src/ScreenManager.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#include "../include/ScreenManager.hpp"
#include <iostream>
#include <unordered_map>
#include <memory>
#include <raylib.h>
#include <thread> // Necessário para std::this_thread::sleep_for
#include <chrono>

ScreenManager::ScreenManager() : actualScreen(MENU), lastScreen(EXIT), entered(false) {}

Expand All @@ -20,7 +17,7 @@ void ScreenManager::SetScreen(Screens type) {
void ScreenManager::UpdateScreen() {
if (!screens[actualScreen]) return;
if (!entered) {
if (actualScreen == OPTIONS) screens[actualScreen]->SetNextScreen(lastScreen);
if (actualScreen == OPTIONS || actualScreen == STATS) screens[actualScreen]->SetNextScreen(lastScreen);
if (lastScreen == COOP || lastScreen == GAME) screens[actualScreen]->SetNextScreen(lastScreen);
if (actualScreen == GAMEOVER) SetScoreInGameOver();
screens[actualScreen]->OpenClose();
Expand Down
46 changes: 46 additions & 0 deletions src/Stats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "../include/Stats.hpp"
#include "raylib.h"

Stats::Stats():
returnButton(ScreenButton("<", Vec2<double>{1.0f/30, 1.0f/50}, 1.0f/20, MENU)),
buttonManager(new ButtonManager(buttons))
{}

Stats::~Stats(){
for(Button *button : buttons) delete button;
delete buttonManager;
}

void Stats::Tick(){
OptionsHandling();
BeginDrawing();
Draw();
buttonManager->Tick();
EndDrawing();
}

void Stats::Draw(){
ClearBackground(BLACK);
ray_functions::DrawFormatedText("HIGHSCORES", Vec2<double>{1.0f/2, 1.0f/5}, 1.0f/13, RAYWHITE);
for(int i = 0; i < 5; i++){
ray_functions::DrawFormatedText(TextFormat("%d", settings::highscores[i]), Vec2<double>{1.0f/2, (1 + 0.17 *(i + 1))/3.2},
1.0f/20, RAYWHITE);
}
}

void Stats::OptionsHandling(){
Screens screen = buttonManager->GetScreen();
if(screen != NOTSCREEN){
buttonManager->ResetScreen();
nextScreen = screen;
OpenClose();
}
if(GetKeyPressed() == KEY_ESCAPE) OpenClose();
}

void Stats::OpenClose(){
Screen::OpenClose();
if(!shouldClose){
returnButton.SetScreen(nextScreen);
}
}
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../include/Coop.hpp"
#include "../include/ScreenManager.hpp"
#include "../include/CoopOptions.hpp"
#include "../include/Stats.hpp"
#include <raylib.h>
#include <memory>
#include <iostream>
Expand All @@ -34,6 +35,7 @@ int main() {
screenManager.AddScreen(MENU, std::make_unique<Menu>());
screenManager.AddScreen(PAUSE, std::make_unique<Pause>());
screenManager.AddScreen(GAMEOVER, std::make_unique<GameOver>());
screenManager.AddScreen(STATS, std::make_unique<Stats>());
while(!IsWindowReady() && !IsAudioDeviceReady()){}
if (!settings::db["WINDOWED"]) settings::FullScreen();
while (!screenManager.ShouldClose()) {
Expand Down

0 comments on commit 4a9511e

Please sign in to comment.