Skip to content
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
30 changes: 30 additions & 0 deletions Space-Invaders/Header/Event/EventService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>

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(); // getting inputs for the player
bool pressedRightKey();

};
41 changes: 41 additions & 0 deletions Space-Invaders/Header/Global/ServiceLocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include "../../Header/Graphic/GraphicService.h"
#include "../../Header/Event/EventService.h"
#include "../../Header/Player/PlayerService.h"
#include "../../Header/Time/TimeService.h"

// ServiceLocator Class Summary: This class manages access to various services in the application.
// include relevant headers files

class ServiceLocator
{
private:

// Private Attributes:
GraphicService* graphic_service;
EventService* event_service;
PlayerService* player_service;
TimeService* time_service;

// Public Methods
ServiceLocator();
~ServiceLocator();

// Private Methods:
void createServices();
void clearAllServices();

public:

// Public Methods:
static ServiceLocator* getInstance();
void initialize(); // Initializes the ServiceLocator.
void update(); // Updates all services.
void render(); // Renders using the services.

// Methods to Get Specific Services:
EventService* getEventService(); // Retrieve the EventService instance
GraphicService* getGraphicService(); // Retrieve the GraphicService instance
PlayerService* getPlayerService(); // Retrieve the PlayerService instance
TimeService* getTimeService(); // Retrieve the TimeService instance
};
38 changes: 38 additions & 0 deletions Space-Invaders/Header/Graphic/GraphicService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
#include <SFML/Graphics.hpp>

class GraphicService
{
private:

const std::string game_window_title = "Alien Invader";

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

const int frame_rate = 60;

const sf::Color window_color = sf::Color::Black;

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
};
25 changes: 25 additions & 0 deletions Space-Invaders/Header/Main/GameService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "../../Header/Global/ServiceLocator.h"

class GameService
{
private:

ServiceLocator* service_locator;
sf::RenderWindow* game_window;


void initialize(); // Handles game initialization.
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.
};
28 changes: 28 additions & 0 deletions Space-Invaders/Header/Player/PlayerController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <SFML/Graphics.hpp>

enum class PlayerState;
class PlayerView;
class PlayerModel;

class PlayerController
{
private:

PlayerView* player_view;
PlayerModel* player_model;

void processPlayerInput();
void moveLeft();
void moveRight();

public:
PlayerController();
~PlayerController();

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

sf::Vector2f getPlayerPosition();
};
43 changes: 43 additions & 0 deletions Space-Invaders/Header/Player/PlayerModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once
#include <SFML/Graphics.hpp>

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

class PlayerModel
{
private:
const sf::Vector2f initial_player_position = sf::Vector2f(400.f, 400.f);

sf::Vector2f player_position;
PlayerState player_state; //Declaration
int player_score;

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 = 300.0f;

PlayerModel();
~PlayerModel();

void initialize();
void reset();

sf::Vector2f getPlayerPosition();
void setPlayerPosition(sf::Vector2f position);

int getPlayerScore();
void setPlayerScore(int score);

//new getter and setter
PlayerState getPlayerState();
void setPlayerState(PlayerState state);


};
16 changes: 16 additions & 0 deletions Space-Invaders/Header/Player/PlayerService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "../../Header/Player/PlayerController.h"

class PlayerService
{
private:
PlayerController* player_controller;

public:
PlayerService();
~PlayerService();

void initialize();
void update();
void render();
};
31 changes: 31 additions & 0 deletions Space-Invaders/Header/Player/PlayerView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "../../Header/Player/PlayerController.h"

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; // ptr to player controller

public:
PlayerView();
~PlayerView();

void initialize();
void update();
void render();
void initialize(PlayerController* controller);
};
28 changes: 28 additions & 0 deletions Space-Invaders/Header/Time/TimeService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <chrono>

// The TimeService class helps keep track of time in game and calculate delta time.
// Utilizes the <chrono> library to calculate delta time.
class TimeService
{
private:

// A point in time which indicates the starting time of previous frame.
std::chrono::time_point<std::chrono::steady_clock> previous_time;

float delta_time; //to store the detla time

void updateDeltaTime(); // method to update time
float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time
void updatePreviousTime(); // finally update the current time to be previous time

public:

//lifecycle methods
void initialize();
void update();

//getter
float getDeltaTime();
};

44 changes: 44 additions & 0 deletions Space-Invaders/Source/Event/EventService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "../../Header/Event/EventService.h"
#include "../../Header/Main/GameService.h"
#include "../../Header/Graphic/GraphicService.h"

EventService::EventService() { game_window = nullptr; }

EventService::~EventService() = default; //calls the default destructor

void EventService::initialize()
{
game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow();
}

void EventService::update()
{
//for later
}

void EventService::processEvents()
{
if (isGameWindowOpen()) {
while (game_window->pollEvent(game_event)) {
// Check for window closure
if (gameWindowWasClosed() || hasQuitGame())
{
game_window->close();
}
}
}
}

bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered

//checks for if a keyboard key has been pressed
bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; }

//control click on the SFML functions to see what they do internally
bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; }
bool EventService::isGameWindowOpen() { return game_window != nullptr; }
bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; }

// Player inputs
bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; }
bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; }
Loading