Skip to content

Feature 9 sound system #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Header/Core/GameLoop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "../../Header/Core/GameWindowManager.h"
#include "../../Header/Event/EventManager.h"
#include "../../Header/Gameplay/GameplayManager.h"
#include "../../Header/Sound/SoundManager.h"

using namespace sf;
using namespace Core;
using namespace CoustomEvent;
using namespace std;
using namespace Gameplay;
using namespace Sound;

namespace Core {

class GameLoop
{
private:
GameplayManager* gameplay_manager;
GameWindowManager* game_window_manager;
EventManager* event_manager;
SoundManager* sound_manager;

public:
void initialize();

bool isGameRunning();
void update();
void pollEvent();
void render();
};
}
24 changes: 24 additions & 0 deletions Header/Core/GameWindowManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include <SFML/Graphics.hpp>

using namespace sf;

namespace Core {
class GameWindowManager {
private:
int game_window_width = 1280;
int game_window_height = 720;
std::string game_title = "SFML-Pong";

RenderWindow* game_window;
void createGameWindow();

public:
void initialize();
RenderWindow* getGameWindow();
bool isGameRunning();
//void render();
void clearGameWindow();
void displayGameWindow();
};
}
14 changes: 14 additions & 0 deletions Header/Event/EventManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;

namespace CoustomEvent {
class EventManager {
public:
void pollEvents(RenderWindow* game_window);
bool isKeyPressed(Keyboard::Key key);
bool isLeftMouseButtonClicked();

};
}
76 changes: 76 additions & 0 deletions Header/Gameplay/Ball/Ball.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "../../Header/Gameplay/Paddle/Paddle.h"
#include "../../Header/Utility/TimeService.h"
#include "../../Header/Sound/SoundManager.h"

using namespace sf;
using namespace std;
using namespace Sound;

namespace Gameplay {
enum class BallState {
Idel,
Moving
};
class Ball {
private:
Texture pong_ball_texture;
Sprite pong_ball_sprite;

string texture_path = "Assets/Textures/Ball.png";
CircleShape ball_sprite;

const float scale_x = 0.02f;
const float scale_y = 0.02f;

const float radius = 10.0f;
const float position_x = 615.0f;
const float position_y = 335.0f;

float ball_speed = 100.0f;
Vector2f velocity = Vector2f(ball_speed, ball_speed);

const float top_boundary = 20.0f;
const float bottom_boundary = 700.0f;

const float left_boundary = 0.0f;
const float right_boundary = 1280.0f;

//Center Position
const float center_position_x = 615.0f;
const float center_position_y = 325.0f;

int speed_multiplier = 10;

float delay_duration = .1f;
float elapsed_delay_time = 0.0f;

bool had_left_collision = false;
bool had_right_collision = false;

BallState current_state;

void loadTexture();
void initializeVariables();
void move(TimeService* time_service);
void onCollision(Paddle* player1, Paddle* player2);
void updateDelayTime(float delta_time);

public:
Ball();
bool isLeftCollisionOccurred();
void updateLeftCollisionState(bool value);

bool isRightCollisionOccurred();
void updateRightCollisionState(bool value);

void handlePaddleCollision(Paddle* player1, Paddle* player2);
void handleBoundaryCollision();
void handleOutofBoundCollision();
void reset();
void update(Paddle* player1, Paddle* player2,TimeService* time_server);
void render(RenderWindow* game_window);

};
}
59 changes: 59 additions & 0 deletions Header/Gameplay/Boundary/Boundary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once
#include <SFML/Graphics.hpp>

using namespace sf;
namespace Gameplay {
class Boundary
{
private:
RectangleShape topBoundary;
RectangleShape leftBoundary;
RectangleShape centerLine;
RectangleShape rightBoundary;
RectangleShape bottomBoundary;

//horizontal values for boundarys
const float horizontal_boundary_width = 1280.0f;
const float horizontal_boundary_height = 20.0f;

//vertical values for boundarys
const float vertical_boundary_width = 20.0f;
const float vertical_boundary_height = 720.0f;

const float right_position_x = 1260.0f;
const float right_position_y = 0.0f;

const float top_position_x = 0.0f;
const float top_position_y = 0.0f;

const float left_position_x = 0.0f;
const float left_position_y = 0.0f;

const float bottom_position_x = 0.0f;
const float bottom_position_y = 700.0f;

//Boundary Colors
const Color boundary_color = Color::Blue;
const Color center_line_color = Color::White;

//center lines properties
const float center_line_width = 10.0f;
const float center_line_height = 680.0f;
const float center_line_position_x = 640.0f;
const float center_line_position_y = 20.0f;

//create boundaries and the center line
void createTopBoundary();
void createBottomBoundary();
void createLeftBoundary();
void createRightBoundary();

void createCenterLine();

public:
Boundary();
void update();
void render(RenderWindow* game_window);

};
}
41 changes: 41 additions & 0 deletions Header/Gameplay/GameplayManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include "Paddle/Paddle.h"
#include "Ball/Ball.h"
#include "Boundary/Boundary.h"
#include "../../Header/Event/EventManager.h"
#include "../../Header/Utility/TimeService.h"
#include "../../Header/UI/UIService.h"

using namespace Utility;
using namespace CoustomEvent;

namespace Gameplay {
class GameplayManager {
private:
float player1_position_x = 40.0f;
float player1_position_y = 300.0f;

float player2_position_x = 1210.0f;
float player2_position_y = 300.0f;

Ball* ball;
Paddle* player1;
Paddle* player2;
EventManager* event_manager;
Boundary* boundary;
TimeService* time_service;
UIService* ui_service;
void initialize();

public:
GameplayManager(EventManager* manager);
//GameplayManager();
void UpdateScore();
void resetPlayer();
void update();
void render(RenderWindow* game_window);


};

}
34 changes: 34 additions & 0 deletions Header/Gameplay/Paddle/Paddle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "../../Header/Utility/TimeService.h"

using namespace sf;
using namespace Utility;

namespace Gameplay {
class Paddle {
private:
RectangleShape paddle_sprite;

const float paddle_width = 20.0f;
const float paddle_height = 140.0f;

float paddle_speed = 100.0f;
float topBoundary = 20.0f;
float bottomBoundary = 700.0f;
int speedMultiplier = 10;

void createPaddle(float position_x, float position_y);
void movePaddle(bool move_up_key_pressed, bool move_down_key_pressed, TimeService* time_service);

public:

Paddle(float position_x, float position_y);

RectangleShape getPaddleSprite();
void reset(float position_x, float position_y);

void update(bool move_up_key_pressed, bool move_down_key_pressed, TimeService* time_service);
void render(RenderWindow* game_window);
};
}
30 changes: 30 additions & 0 deletions Header/Sound/SoundManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <SFML/Audio.hpp>
#include <string>

namespace Sound
{
enum class SoundType
{
BALL_BOUNCE
};

class SoundManager {
private:
static sf::Music backgroundMusic;
static sf::Sound soundEffect;
static sf::SoundBuffer ballBounce;

static float backgroundMusicVolume;
static const std::string bgmPath;
static const std::string ballBouncePath;


static void LoadSoundFromFile();
public:
static void Initialize();
static void PlaySoundEffect(SoundType soundType);
static void PlayBackgroundMusic();
};

}
39 changes: 39 additions & 0 deletions Header/UI/UIService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#include <SFML/Graphics.hpp>

using namespace sf;


class UIService {
private:
Font font;
Text left_score_text;
Text right_score_text;

std::string texture_path = "Assets/Fonts/AmberyGardenRegular-PKjGd.ttf";


int font_size = 40;
Color font_color = Color::White;
std::string initial_string = "00";

float left_score_position_x = 570.0f;
float left_score_position_y = 30.0f;

float right_score_position_x = 670.0f;
float right_score_position_y = 30.0f;

int player1_score = 0;
int player2_score = 0;


public:
void loadFontTexture();
void createLeftScoreText();
void createRightScoreText();
std::string formatScore(int score);
void incrementPlayer1Score();
void incrementPlayer2Score();
void update();
void render(RenderWindow* game_window);
};
19 changes: 19 additions & 0 deletions Header/Utility/TimeService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <chrono>
namespace Utility
{
class TimeService {
private:
std::chrono::steady_clock::time_point previous_time;
float delta_time;

void updateDeltaTime();
float calculateDeltaTime();
void updatePreviousTime();

public:
void initialize();
void update();
float getDeltaTime();
};
}
Loading