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
33 changes: 33 additions & 0 deletions Header/Event/EventService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#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 Header/Global/ServiceLocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
#include "../../Header/Graphic/GraphicService.h"
#include "../../Header/Event/EventService.h"
#include "../../Header/Player/PlayerService.h"
#include "../../Header/Time/TimeService.h"

namespace Global
{

class ServiceLocator
{
private:
// Private Attributes:
Graphic::GraphicService* graphic_service;

Event::EventService* event_service;

Player::PlayerService* player_service;

Time::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:
Graphic::GraphicService* getGraphicService();
Event::EventService* getEventService();
Player::PlayerService* getPlayerService();
Time::TimeService* getTimeService();

};
}
42 changes: 42 additions & 0 deletions Header/Graphic/GraphicService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <SFML/Graphics.hpp>

namespace Graphic
{

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::Blue;

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

namespace Main
{
using namespace Global;

class GameService
{
private:

Global::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.
};
}
32 changes: 32 additions & 0 deletions Header/Player/PlayerController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include <SFML/Graphics.hpp>

enum class PlayerSate;
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();


};
46 changes: 46 additions & 0 deletions Header/Player/PlayerModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#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(500.f, 500.f); //new var
sf::Vector2f player_position; //new var

PlayerState player_state;


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);

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

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

namespace Player
{

enum class PlayerState;
//class PlayerController;

class PlayerService
{
private:
PlayerController* player_controller;

public:
PlayerService();
~PlayerService();

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



class PlayerView
{
private:

PlayerController* player_controller; // ptr to player controller

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

//player sprite and texture
sf::Texture player_texture;
sf::Sprite player_sprite;


//player sprite attribute(height & Width)(60, 60)
const int player_sprite_width = 60;
const int player_sprite_height = 60;


//ptr to game window for rendering our player
sf::RenderWindow* game_window;


//initializePlayerSprite()
void initializePlayerSprite();


//scaleSprite()
void scalePlayerSprite();

public:

//constructor Destructor
PlayerView();
~PlayerView();


//initialise, update, render

void initialize(PlayerController* controller); // we pass a pointer of type controller because we need to use this in the view later.
void update();
void render();


};
36 changes: 36 additions & 0 deletions Header/Time/TimeService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include <chrono>

namespace Time
{

/*
// 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;
// No need to worry about the syntax of this variable too much right now
// It'll be explained in detail at a later 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();
};
}

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

namespace Event
{
using namespace Global;

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; }

bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; }

bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; }
}
Loading