Skip to content
Open
31 changes: 31 additions & 0 deletions Space-Invaders/Headers/EVENT/EventService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
namespace event {
class EventService
{
private:
sf::Event game_event; //event var
sf::RenderWindow* game_window; //ptr to our game window

bool isGameWindowOpen();
bool gameWindowWasClosed(); //for the condition we already had - the title bar cross.
bool hasQuitGame(); //for our new 'ESC' condition




public:
EventService();
~EventService();

void initialize();
void update();
void processEvents(); // while window is open we will check for events
bool pressedEscapeKey();
bool isKeyboardEvent();
bool pressedLeftKey();
bool pressedRightKey();

};
}
45 changes: 45 additions & 0 deletions Space-Invaders/Headers/Global/ServiceLocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include"../Graphic/GraphicService.h"
#include"../EVENT/EventService.h"
#include"../player/PlayerService.h"
#include"../TIME/TimeService .h"


namespace Global {
using namespace player;
using namespace Graphic;
using namespace event;

class ServiceLocator
{
private:
// Private Attributes:
GraphicService* graphic_service;
EventService* event_service;
PlayerService* player_service;
TimeService* time_Service;

// Private Constructor and Destructor:
ServiceLocator();
// Constructor for initializing the ServiceLocator.
~ServiceLocator(); // Destructor for cleaning up resources upon object deletion.

// Private Methods:
void createServices(); // Creates instances of all services.
void clearAllServices(); // Deletes and deallocates memory for all services.

public:
// Public Methods:
static ServiceLocator* getInstance(); // Provides a method to access the unique ServiceLocator instance (object).
void initialize(); // Initializes the ServiceLocator.
void update(); // Updates all services.
void render(); // Renders using the services.

// Methods to Get Specific Services:
GraphicService* getGraphicService();
EventService* getEventService();
PlayerService* getplayerservice();
TimeService* gettimeservice();
};
}
44 changes: 44 additions & 0 deletions Space-Invaders/Headers/Graphic/GraphicService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#pragma once

#include <SFML/Graphics.hpp>
namespace Graphic {

class GraphicService
{
private:


const std::string game_window_title = "Outscal Presents - Alien Invader";

const int game_window_width = 800;
const int game_window_height = 600;

const sf::Color window_color = sf::Color::Blue;

const int framerate = 60;

sf::VideoMode* video_mode; // ptr to video mode
sf::RenderWindow* game_window; // ptr to a RenderWindow

void setVideoMode(); // Method for setting our video mode
void onDestroy(); // method to run when window is deleted

public:
GraphicService();
~GraphicService(); //cleanup

//method to create the game window. returns a pointer to an instance of the game window
sf::RenderWindow* createGameWindow();


void initialize(); //lifecycle functions
void update(); //..
void render(); //..
bool isGameWindowOpen(); //check if the window is open

sf::RenderWindow* getGameWindow(); //getter for the game window instance
sf::Color getWindowColor();//get the color
};

}
23 changes: 23 additions & 0 deletions Space-Invaders/Headers/TIME/TimeService .h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
//Capture the current time at the beginning of a frame
//Capture the current time again at the beginning of the next frame
//Calculate the delta time by subtracting the previous frame's start time from the current frame's start time.
//Update the current frame's start time to become the previous frame's start time so as to prepare for the next frame.
#include<chrono>

class TimeService {
private:
std::chrono::time_point<std::chrono::steady_clock> previous_time;

float delta_time;//to store the value of delta time
float calculate_delta_time();//calculate time by subtracting the previous time from the current time
void updatePreviousTime(); // finally update the current time to be previous time
void updateDeltaTime();
public:
void update();
void intialize();
float getdeltatime();



};
32 changes: 32 additions & 0 deletions Space-Invaders/Headers/main/GameService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <SFML/Graphics.hpp>
#include"../Global/ServiceLocator.h"

namespace Main {

using namespace Global;

class GameService
{
private:


ServiceLocator* service_locator;
sf::RenderWindow* game_window;

void initialize();
void initializeVariables();// Handles game initialization.
void destroy(); // Handles cleanup tasks.

public:

GameService(); // Constructor for initializing the GameService object.
~GameService(); // Destructor for cleaning up resources upon object deletion.

void ignite(); // Initiates the game.
void update(); // Updates the game logic and game state.
void render(); // Renders each frame of the game.
bool isRunning(); // Checks if the game is currently running.
};
}
35 changes: 35 additions & 0 deletions Space-Invaders/Headers/player/PlayerController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include<SFML/Graphics.hpp>
namespace player {

enum class PlayerState;
class PlayerView;
class PlayerModel;

class PlayerController {

private:



void processinput();
void processmoveleft();
void processmoveright();
public:

PlayerModel* model;
PlayerView* view;
PlayerController();
~PlayerController();
void intialize();
void update();
void render();




sf::Vector2f getPlayerPosition();

};

}
58 changes: 58 additions & 0 deletions Space-Invaders/Headers/player/PlayerModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once
#include <SFML/Graphics.hpp>
class PlayerView;
class PlayerModel;

namespace player {

enum class PlayerState //Our Enum
{
ALIVE,
DEAD,
// we will add more states later
};



class PlayerModel

{
private:

const sf::Vector2f initial_player_position = sf::Vector2f(500.f, 500.f); //new var
sf::Vector2f player_position; //new var
PlayerState player_state;
PlayerModel* model;
PlayerView* view;
int player_score = 0;


public:
const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f);
const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f);

const float player_movement_speed = 200.0f;

PlayerModel();
~PlayerModel();

void initialize();

void reset(); //new method




//getters and setters
sf::Vector2f getPlayerPosition();
void setPlayerPosition(sf::Vector2f position);

PlayerState getplayerstate();


void setplayerstate(PlayerState state);



};
}
48 changes: 48 additions & 0 deletions Space-Invaders/Headers/player/PlayerService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once
#include<SFML/Graphics.hpp>"
#include"../TIME/TimeService .h"
#include"../player/PlayerController.h"


namespace player {
class PlayerService
{

private:



int health = 3;
sf::Vector2f position = sf::Vector2f(200.0f, 100.0f);
float movement_speed = 5.0f;
int player_score = 0;

const sf::String player_texture_path = "assets/textures/player_ship.png";

sf::Texture player_texture;
sf::Sprite player_sprite;

sf::RenderWindow* game_window; //as always

void initializePlayerSprite();
void processPlayerInput();

PlayerController* player_controller;


public:

PlayerService();
~PlayerService();

void initialize();
void update();
void render();

void moveLef();
void moveRight();
int getMoveSpeed();
sf::Vector2f getPlayerPosition();

};
}
33 changes: 33 additions & 0 deletions Space-Invaders/Headers/player/PlayerView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include <SFML/Graphics.hpp>
#include"../player/PlayerController.h"

class PlayerController;
namespace player {
class PlayerView
{
private:

const sf::String player_texture_path = "assets/textures/player_ship.png";
const float player_sprite_width = 60.f;
const float player_sprite_height = 60.f;

sf::RenderWindow* game_window;

sf::Texture player_texture;
sf::Sprite player_sprite;

void initializePlayerSprite();
void scalePlayerSprite();

PlayerController* player_controller;

public:
PlayerView();
~PlayerView();

void initialize(PlayerController* player_controller);
void update();
void render();
};
}
Loading