From 46d3eb914ebec96c25f0997df65c4dece32700c2 Mon Sep 17 00:00:00 2001 From: Tarun Date: Fri, 26 Jul 2024 19:48:55 +0530 Subject: [PATCH 01/27] The whole file gets corrupted thats why i have to write all programs till player ship again --- Space-Invaders/Headers/GameService.cpp | 59 +++++++++++++++++++ Space-Invaders/Headers/GameService.h | 26 ++++++++ Space-Invaders/Headers/GraphicService.cpp | 56 ++++++++++++++++++ Space-Invaders/Headers/GraphicService.h | 38 ++++++++++++ Space-Invaders/Headers/ServiceLocator.cpp | 49 +++++++++++++++ Space-Invaders/Headers/ServiceLocator.h | 29 +++++++++ Space-Invaders/Space-Invaders.vcxproj | 8 +++ Space-Invaders/Space-Invaders.vcxproj.filters | 20 +++++++ Space-Invaders/main.cpp | 19 +++++- 9 files changed, 302 insertions(+), 2 deletions(-) create mode 100644 Space-Invaders/Headers/GameService.cpp create mode 100644 Space-Invaders/Headers/GameService.h create mode 100644 Space-Invaders/Headers/GraphicService.cpp create mode 100644 Space-Invaders/Headers/GraphicService.h create mode 100644 Space-Invaders/Headers/ServiceLocator.cpp create mode 100644 Space-Invaders/Headers/ServiceLocator.h diff --git a/Space-Invaders/Headers/GameService.cpp b/Space-Invaders/Headers/GameService.cpp new file mode 100644 index 000000000..4607039c2 --- /dev/null +++ b/Space-Invaders/Headers/GameService.cpp @@ -0,0 +1,59 @@ +#include "GameService.h" +#include"GraphicService.h" + +GameService::GameService() { + service_locator = nullptr; // Set service locator to null + game_window = nullptr; // Set game window to null +} + +// Destructor: Calls the destroy function to clean up resources. +GameService::~GameService() { + destroy(); // Clean up and release resources +} + +// Prepares the game service for use by obtaining the service locator instance and initializing services. +void GameService::ignite() { + service_locator = ServiceLocator::getInstance(); // Get ServiceLocator + initialize(); // Initialize services. +} + +//initialize service locator and other variables +void GameService::initialize() +{ + service_locator->initialize(); + initializeVariables(); +} + +void GameService::initializeVariables() +{ + game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) +} + +void GameService::destroy() +{ + // don't need to do anything here for now. +} + +// Updates the game logic by delegating to the service locator's update method. +void GameService::update() { + + service_locator->update(); // Call update on the service locator which then updates all its managed services +} + +// Clears the window then display it. +void GameService::render() { + // Clears the game window with the background color provided by the graphic service + game_window->clear(service_locator->getGraphicService()->getWindowColor()); + service_locator->render(); // Render the current frame using the service locator + game_window->display(); // Display the rendered frame on the game window +} + +// Checks if the game is still running by querying the graphic service's window open status. +bool GameService::isRunning() { + // Returns true if the game window is open, indicating the game is still running + return service_locator->getGraphicService()->isGameWindowOpen(); +} + + + + diff --git a/Space-Invaders/Headers/GameService.h b/Space-Invaders/Headers/GameService.h new file mode 100644 index 000000000..c09fa800f --- /dev/null +++ b/Space-Invaders/Headers/GameService.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include "ServiceLocator.h" + +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. +}; \ No newline at end of file diff --git a/Space-Invaders/Headers/GraphicService.cpp b/Space-Invaders/Headers/GraphicService.cpp new file mode 100644 index 000000000..1303689b7 --- /dev/null +++ b/Space-Invaders/Headers/GraphicService.cpp @@ -0,0 +1,56 @@ +#include"ServiceLocator.h" +#include"GraphicService.h" + +// Constructor: Initializes game window and video mode pointers to null. +GraphicService::GraphicService() { + game_window = nullptr; // Initializes game window pointer to null + video_mode = nullptr; // Initializes video mode pointer to null +} + +// Destructor: Cleans up resources by calling onDestroy. +GraphicService::~GraphicService() { + onDestroy(); // Calls onDestroy method to clean up resources +} + +// Initializes the graphic service by creating a new game window. +void GraphicService::initialize() { + game_window = createGameWindow(); // Assigns a new game window to the game_window pointer +} + +// Creates a new SFML RenderWindow object with specified video mode and title. +sf::RenderWindow* GraphicService::createGameWindow() { + setVideoMode(); // Sets up the video mode for the window + return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object +} + +// Sets up the video mode for the game window using specified dimensions and system's color depth. +void GraphicService::setVideoMode() { + video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode +} + +// Cleans up allocated memory for video mode and game window to avoid memory leaks. +void GraphicService::onDestroy() { + delete(video_mode); // Deletes the video mode object + delete(game_window); // Deletes the game window object +} + +// Placeholder function for game update logic. +void GraphicService::update() { } + +// Placeholder function for game rendering logic. +void GraphicService::render() { } + +// Checks if the game window is currently open. +bool GraphicService::isGameWindowOpen() { + return game_window->isOpen(); // Returns the open status of the game window +} + +// Returns a pointer to the game window object. +sf::RenderWindow* GraphicService::getGameWindow() { + return game_window; +} + +// Returns the configured window background color. +sf::Color GraphicService::getWindowColor() { + return window_color; +} diff --git a/Space-Invaders/Headers/GraphicService.h b/Space-Invaders/Headers/GraphicService.h new file mode 100644 index 000000000..3a6cac6df --- /dev/null +++ b/Space-Invaders/Headers/GraphicService.h @@ -0,0 +1,38 @@ + +#pragma once + +#include + +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; + + 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 +}; \ No newline at end of file diff --git a/Space-Invaders/Headers/ServiceLocator.cpp b/Space-Invaders/Headers/ServiceLocator.cpp new file mode 100644 index 000000000..fd1922572 --- /dev/null +++ b/Space-Invaders/Headers/ServiceLocator.cpp @@ -0,0 +1,49 @@ +#include "ServiceLocator.h" + + + +// Constructor: Initializes the graphic_service pointer to null and creates services. +ServiceLocator::ServiceLocator() { + graphic_service = nullptr; // Initialize graphic_service to null + createServices(); // Call createServices to instantiate services +} + +// Destructor: Cleans up resources by clearing all services. +ServiceLocator::~ServiceLocator() { + clearAllServices(); // Call clearAllServices to delete any allocated services +} + +// Creates service instances, specifically the graphic service in this case. +void ServiceLocator::createServices() { + graphic_service = new GraphicService(); // Dynamically create a GraphicService instance +} + +// Deletes allocated services to prevent memory leaks, specifically the graphic service. +void ServiceLocator::clearAllServices() { + delete(graphic_service); // Delete the graphic_service instance + graphic_service = nullptr; // Reset pointer to null to avoid dangling pointer +} + +// Returns a pointer to ServiceLocator. +ServiceLocator* ServiceLocator::getInstance() { + static ServiceLocator instance; // we will discuss what 'static' means at a later time. + return &instance; // Return address of the instance +} + +// Calls initialize on the graphic service, readying it for use. +void ServiceLocator::initialize() { + graphic_service->initialize(); // Initialize graphic service +} + +// Updates the state of the graphic service. +void ServiceLocator::update() { + graphic_service->update(); // Update graphic service +} + +// Renders using the graphic service. +void ServiceLocator::render() { + graphic_service->render(); // Render graphic service +} + +// Returns a pointer to the currently set graphic service. +GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } \ No newline at end of file diff --git a/Space-Invaders/Headers/ServiceLocator.h b/Space-Invaders/Headers/ServiceLocator.h new file mode 100644 index 000000000..84028c811 --- /dev/null +++ b/Space-Invaders/Headers/ServiceLocator.h @@ -0,0 +1,29 @@ +#pragma once +#include "GraphicService.h" + +class ServiceLocator +{ +private: + // Private Attributes: + GraphicService* graphic_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(); + +}; diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 6f7fa388d..fa705159d 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,8 +132,16 @@ + + + + + + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index ce0c35ccf..c1a3d6972 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -18,5 +18,25 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 7d5f90dff..1a2cf9819 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,20 @@ - +#include "Headers/GameService.h" int main() { + + GameService* game_service = new GameService(); + + game_service->ignite(); + + while (game_service->isRunning()) + { + game_service->update(); + game_service->render(); + + + + + } return 0; -} \ No newline at end of file +} + From ef66188d0746f79e04d8e20d57328872c815dd6b Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 28 Jul 2024 15:12:44 +0530 Subject: [PATCH 02/27] IMPLEMENTED POLLING EVENT --- Space-Invaders/Headers/EventService.cpp | 0 Space-Invaders/Headers/EventService.h | 5 +++++ Space-Invaders/Space-Invaders.vcxproj | 1 + Space-Invaders/Space-Invaders.vcxproj.filters | 3 +++ 4 files changed, 9 insertions(+) create mode 100644 Space-Invaders/Headers/EventService.cpp create mode 100644 Space-Invaders/Headers/EventService.h diff --git a/Space-Invaders/Headers/EventService.cpp b/Space-Invaders/Headers/EventService.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Headers/EventService.h b/Space-Invaders/Headers/EventService.h new file mode 100644 index 000000000..09dfcb4c7 --- /dev/null +++ b/Space-Invaders/Headers/EventService.h @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +class E \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index fa705159d..0ee183c2a 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -138,6 +138,7 @@ + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index c1a3d6972..5f6eba855 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -38,5 +38,8 @@ Header Files + + Header Files + \ No newline at end of file From 5673590201775fa44acc60f8fd80cc34cfa611d2 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 28 Jul 2024 16:04:40 +0530 Subject: [PATCH 03/27] added the player spirite and also added the movement --- Space-Invaders/Headers/EventService.cpp | 49 ++++++++++++++ Space-Invaders/Headers/EventService.h | 31 ++++++++- Space-Invaders/Headers/GameService.cpp | 2 + Space-Invaders/Headers/GraphicService.cpp | 12 +++- Space-Invaders/Headers/GraphicService.h | 1 + Space-Invaders/Headers/PlayerService.cpp | 66 +++++++++++++++++++ Space-Invaders/Headers/PlayerService.h | 38 +++++++++++ Space-Invaders/Headers/ServiceLocator.cpp | 31 +++++++-- Space-Invaders/Headers/ServiceLocator.h | 6 ++ Space-Invaders/Space-Invaders.vcxproj | 3 + Space-Invaders/Space-Invaders.vcxproj.filters | 9 +++ 11 files changed, 239 insertions(+), 9 deletions(-) create mode 100644 Space-Invaders/Headers/PlayerService.cpp create mode 100644 Space-Invaders/Headers/PlayerService.h diff --git a/Space-Invaders/Headers/EventService.cpp b/Space-Invaders/Headers/EventService.cpp index e69de29bb..a5c4965e3 100644 --- a/Space-Invaders/Headers/EventService.cpp +++ b/Space-Invaders/Headers/EventService.cpp @@ -0,0 +1,49 @@ +#include"ServiceLocator.h" +#include"GraphicService.h" +#include"EventService.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; } + +bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } +bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } + diff --git a/Space-Invaders/Headers/EventService.h b/Space-Invaders/Headers/EventService.h index 09dfcb4c7..467eff7ca 100644 --- a/Space-Invaders/Headers/EventService.h +++ b/Space-Invaders/Headers/EventService.h @@ -1,5 +1,30 @@ #pragma once -#include -#include +#include +#include -class E \ No newline at end of file +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(); + +}; \ No newline at end of file diff --git a/Space-Invaders/Headers/GameService.cpp b/Space-Invaders/Headers/GameService.cpp index 4607039c2..960d7acb4 100644 --- a/Space-Invaders/Headers/GameService.cpp +++ b/Space-Invaders/Headers/GameService.cpp @@ -37,6 +37,8 @@ void GameService::destroy() // Updates the game logic by delegating to the service locator's update method. void GameService::update() { + service_locator->getEventService()->processEvents(); + service_locator->update(); // Call update on the service locator which then updates all its managed services } diff --git a/Space-Invaders/Headers/GraphicService.cpp b/Space-Invaders/Headers/GraphicService.cpp index 1303689b7..f6987679e 100644 --- a/Space-Invaders/Headers/GraphicService.cpp +++ b/Space-Invaders/Headers/GraphicService.cpp @@ -1,10 +1,12 @@ #include"ServiceLocator.h" #include"GraphicService.h" + // Constructor: Initializes game window and video mode pointers to null. GraphicService::GraphicService() { game_window = nullptr; // Initializes game window pointer to null video_mode = nullptr; // Initializes video mode pointer to null + } // Destructor: Cleans up resources by calling onDestroy. @@ -35,10 +37,16 @@ void GraphicService::onDestroy() { } // Placeholder function for game update logic. -void GraphicService::update() { } +void GraphicService::update() { + + +} + // Placeholder function for game rendering logic. -void GraphicService::render() { } +void GraphicService::render() { + + } // Checks if the game window is currently open. bool GraphicService::isGameWindowOpen() { diff --git a/Space-Invaders/Headers/GraphicService.h b/Space-Invaders/Headers/GraphicService.h index 3a6cac6df..68ca09cef 100644 --- a/Space-Invaders/Headers/GraphicService.h +++ b/Space-Invaders/Headers/GraphicService.h @@ -6,6 +6,7 @@ class GraphicService { private: + const std::string game_window_title = "Outscal Presents - Alien Invader"; diff --git a/Space-Invaders/Headers/PlayerService.cpp b/Space-Invaders/Headers/PlayerService.cpp new file mode 100644 index 000000000..49be2d11a --- /dev/null +++ b/Space-Invaders/Headers/PlayerService.cpp @@ -0,0 +1,66 @@ +#include "PlayerService.h" +#include"ServiceLocator.h" + + +void PlayerService::initializePlayerSprite() +{ + if (player_texture.loadFromFile(player_texture_path)) { + player_sprite.setTexture(player_texture); + } +} + +void PlayerService::processPlayerInput() +{ + + EventService* checkkey = ServiceLocator::getInstance()->getEventService(); + if (checkkey->isKeyboardEvent()) { + if (checkkey->pressedLeftKey()) { + move(-1.0 * getMoveSpeed()); + } + + if (checkkey->pressedRightKey()) { + move(1.0 * getMoveSpeed()); + } + } +} + +PlayerService::PlayerService() +{ + game_window = nullptr; +} + +PlayerService::~PlayerService() +{ +} + +void PlayerService::initialize() +{ + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +void PlayerService::update() +{ + processPlayerInput(); + player_sprite.setPosition(getPlayerPosition()); +} + +void PlayerService::render() +{ + game_window->draw(player_sprite); +} + +void PlayerService::move(float offsetX) +{ + position.x += offsetX; +} + +int PlayerService::getMoveSpeed() +{ + return movement_speed; +} + +sf::Vector2f PlayerService::getPlayerPosition() +{ + return position; +} diff --git a/Space-Invaders/Headers/PlayerService.h b/Space-Invaders/Headers/PlayerService.h new file mode 100644 index 000000000..a9440127c --- /dev/null +++ b/Space-Invaders/Headers/PlayerService.h @@ -0,0 +1,38 @@ +#pragma once +#include +class PlayerService +{ + +private: + + + + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); + int movement_speed = 5; + 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(); + +public: + + PlayerService(); + ~PlayerService(); + + void initialize(); + void update(); + void render(); + + void move(float offsetX); + int getMoveSpeed(); + sf::Vector2f getPlayerPosition(); + +}; diff --git a/Space-Invaders/Headers/ServiceLocator.cpp b/Space-Invaders/Headers/ServiceLocator.cpp index fd1922572..0a81bc1aa 100644 --- a/Space-Invaders/Headers/ServiceLocator.cpp +++ b/Space-Invaders/Headers/ServiceLocator.cpp @@ -1,10 +1,12 @@ #include "ServiceLocator.h" - +#include"EventService.h" // Constructor: Initializes the graphic_service pointer to null and creates services. ServiceLocator::ServiceLocator() { - graphic_service = nullptr; // Initialize graphic_service to null + graphic_service = nullptr;// Initialize graphic_service to null + event_service = nullptr; + player_service = nullptr; createServices(); // Call createServices to instantiate services } @@ -16,12 +18,17 @@ ServiceLocator::~ServiceLocator() { // Creates service instances, specifically the graphic service in this case. void ServiceLocator::createServices() { graphic_service = new GraphicService(); // Dynamically create a GraphicService instance + event_service = new EventService(); + player_service = new PlayerService(); + } // Deletes allocated services to prevent memory leaks, specifically the graphic service. void ServiceLocator::clearAllServices() { delete(graphic_service); // Delete the graphic_service instance - graphic_service = nullptr; // Reset pointer to null to avoid dangling pointer + delete(event_service); + delete(player_service); + } // Returns a pointer to ServiceLocator. @@ -33,17 +40,33 @@ ServiceLocator* ServiceLocator::getInstance() { // Calls initialize on the graphic service, readying it for use. void ServiceLocator::initialize() { graphic_service->initialize(); // Initialize graphic service + event_service->initialize(); + player_service->initialize(); } // Updates the state of the graphic service. void ServiceLocator::update() { graphic_service->update(); // Update graphic service + event_service->update(); + player_service->update(); } // Renders using the graphic service. void ServiceLocator::render() { graphic_service->render(); // Render graphic service + player_service->render(); } // Returns a pointer to the currently set graphic service. -GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } \ No newline at end of file +GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } + +EventService* ServiceLocator::getEventService() { + return event_service; +} + +PlayerService* ServiceLocator::getplayerservice() { + return player_service; +} + + + diff --git a/Space-Invaders/Headers/ServiceLocator.h b/Space-Invaders/Headers/ServiceLocator.h index 84028c811..c8a41b91e 100644 --- a/Space-Invaders/Headers/ServiceLocator.h +++ b/Space-Invaders/Headers/ServiceLocator.h @@ -1,11 +1,15 @@ #pragma once #include "GraphicService.h" +#include"EventService.h" +#include"PlayerService.h" class ServiceLocator { private: // Private Attributes: GraphicService* graphic_service; + EventService* event_service; + PlayerService* player_service; // Private Constructor and Destructor: ServiceLocator(); @@ -25,5 +29,7 @@ class ServiceLocator // Methods to Get Specific Services: GraphicService* getGraphicService(); + EventService* getEventService(); + PlayerService* getplayerservice(); }; diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 0ee183c2a..efb75e465 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,8 +132,10 @@ + + @@ -141,6 +143,7 @@ + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 5f6eba855..73c3a15d7 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -27,6 +27,12 @@ Source Files + + Source Files + + + Source Files + @@ -41,5 +47,8 @@ Header Files + + Header Files + \ No newline at end of file From cd4e9cd77a6191b9f0e2cd1ee701371a64afac12 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 28 Jul 2024 18:12:06 +0530 Subject: [PATCH 04/27] in this commit we are adding the time dependency in the sip for the better and movement of the ship --- Space-Invaders/Headers/ServiceLocator.cpp | 14 ++++++- Space-Invaders/Headers/ServiceLocator.h | 4 +- Space-Invaders/Headers/TimeService .cpp | 42 +++++++++++++++++++ Space-Invaders/Headers/TimeService .h | 23 ++++++++++ Space-Invaders/Space-Invaders.vcxproj | 2 + Space-Invaders/Space-Invaders.vcxproj.filters | 6 +++ 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 Space-Invaders/Headers/TimeService .cpp create mode 100644 Space-Invaders/Headers/TimeService .h diff --git a/Space-Invaders/Headers/ServiceLocator.cpp b/Space-Invaders/Headers/ServiceLocator.cpp index 0a81bc1aa..21ba91f85 100644 --- a/Space-Invaders/Headers/ServiceLocator.cpp +++ b/Space-Invaders/Headers/ServiceLocator.cpp @@ -7,6 +7,7 @@ ServiceLocator::ServiceLocator() { graphic_service = nullptr;// Initialize graphic_service to null event_service = nullptr; player_service = nullptr; + time_Service = nullptr; createServices(); // Call createServices to instantiate services } @@ -20,7 +21,7 @@ void ServiceLocator::createServices() { graphic_service = new GraphicService(); // Dynamically create a GraphicService instance event_service = new EventService(); player_service = new PlayerService(); - + time_Service = new TimeService(); } // Deletes allocated services to prevent memory leaks, specifically the graphic service. @@ -28,6 +29,7 @@ void ServiceLocator::clearAllServices() { delete(graphic_service); // Delete the graphic_service instance delete(event_service); delete(player_service); + delete(time_Service); } @@ -42,6 +44,7 @@ void ServiceLocator::initialize() { graphic_service->initialize(); // Initialize graphic service event_service->initialize(); player_service->initialize(); + time_Service->intialize(); } // Updates the state of the graphic service. @@ -49,8 +52,10 @@ void ServiceLocator::update() { graphic_service->update(); // Update graphic service event_service->update(); player_service->update(); + time_Service->update(); } + // Renders using the graphic service. void ServiceLocator::render() { graphic_service->render(); // Render graphic service @@ -68,5 +73,10 @@ PlayerService* ServiceLocator::getplayerservice() { return player_service; } - +TimeService* ServiceLocator::gettimeservice() +{ + return time_Service; +} + + diff --git a/Space-Invaders/Headers/ServiceLocator.h b/Space-Invaders/Headers/ServiceLocator.h index c8a41b91e..86a93eed9 100644 --- a/Space-Invaders/Headers/ServiceLocator.h +++ b/Space-Invaders/Headers/ServiceLocator.h @@ -2,6 +2,7 @@ #include "GraphicService.h" #include"EventService.h" #include"PlayerService.h" +#include"TimeService .h" class ServiceLocator { @@ -10,6 +11,7 @@ class ServiceLocator GraphicService* graphic_service; EventService* event_service; PlayerService* player_service; + TimeService* time_Service; // Private Constructor and Destructor: ServiceLocator(); @@ -31,5 +33,5 @@ class ServiceLocator GraphicService* getGraphicService(); EventService* getEventService(); PlayerService* getplayerservice(); - + TimeService* gettimeservice(); }; diff --git a/Space-Invaders/Headers/TimeService .cpp b/Space-Invaders/Headers/TimeService .cpp new file mode 100644 index 000000000..0c719b841 --- /dev/null +++ b/Space-Invaders/Headers/TimeService .cpp @@ -0,0 +1,42 @@ +#include "TimeService .h" + +float TimeService::calculate_delta_time() +{ + // Calculate time difference in microseconds between the current and previous frame. + int delta = std::chrono::duration_cast( + std::chrono::steady_clock::now() - previous_time).count(); + + // The cast is used to convert delta time from microseconds into seconds. + // We will learn aboit how this works in detail later. + return static_cast(delta) / static_cast(1000000); +} + +void TimeService::updatePreviousTime() +{ + + previous_time = std::chrono::steady_clock::now(); +} + +void TimeService::updateDeltaTime() +{ + delta_time = calculate_delta_time(); + updatePreviousTime(); + +} + +void TimeService::update() +{ + updateDeltaTime(); +} + +void TimeService::intialize() +{ + previous_time = std::chrono::steady_clock::now(); + delta_time = 0; +} + +float TimeService::getdeltatime() +{ + return delta_time; +} + diff --git a/Space-Invaders/Headers/TimeService .h b/Space-Invaders/Headers/TimeService .h new file mode 100644 index 000000000..2a4183d17 --- /dev/null +++ b/Space-Invaders/Headers/TimeService .h @@ -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 + +class TimeService { +private: + std::chrono::time_point 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(); + + + +}; diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index efb75e465..f85d9a4d0 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -137,6 +137,7 @@ + @@ -145,6 +146,7 @@ + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 73c3a15d7..219bacc87 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -33,6 +33,9 @@ Source Files + + Source Files + @@ -50,5 +53,8 @@ Header Files + + Header Files + \ No newline at end of file From b02ba2867db71a6320aba115f41d9efc20953ff5 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 28 Jul 2024 19:50:32 +0530 Subject: [PATCH 05/27] in this commit we are implementing the mvc model for the player service --- Space-Invaders/Headers/GraphicService.cpp | 3 ++ Space-Invaders/Headers/GraphicService.h | 2 + Space-Invaders/Headers/PlayerService.cpp | 17 +++++-- Space-Invaders/Headers/PlayerService.h | 6 ++- .../Headers/player/PlayerController.h | 1 + Space-Invaders/Headers/player/PlayerModel.cpp | 50 ++++++++++++++++++ Space-Invaders/Headers/player/PlayerModel.h | 51 +++++++++++++++++++ Space-Invaders/Headers/player/PlayerView.h | 1 + Space-Invaders/Space-Invaders.vcxproj | 4 ++ Space-Invaders/Space-Invaders.vcxproj.filters | 12 +++++ 10 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 Space-Invaders/Headers/player/PlayerController.h create mode 100644 Space-Invaders/Headers/player/PlayerModel.cpp create mode 100644 Space-Invaders/Headers/player/PlayerModel.h create mode 100644 Space-Invaders/Headers/player/PlayerView.h diff --git a/Space-Invaders/Headers/GraphicService.cpp b/Space-Invaders/Headers/GraphicService.cpp index f6987679e..e4a35584d 100644 --- a/Space-Invaders/Headers/GraphicService.cpp +++ b/Space-Invaders/Headers/GraphicService.cpp @@ -17,8 +17,11 @@ GraphicService::~GraphicService() { // Initializes the graphic service by creating a new game window. void GraphicService::initialize() { game_window = createGameWindow(); // Assigns a new game window to the game_window pointer + + game_window->setFramerateLimit(framerate); } + // Creates a new SFML RenderWindow object with specified video mode and title. sf::RenderWindow* GraphicService::createGameWindow() { setVideoMode(); // Sets up the video mode for the window diff --git a/Space-Invaders/Headers/GraphicService.h b/Space-Invaders/Headers/GraphicService.h index 68ca09cef..570c7f584 100644 --- a/Space-Invaders/Headers/GraphicService.h +++ b/Space-Invaders/Headers/GraphicService.h @@ -15,6 +15,8 @@ class GraphicService 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 diff --git a/Space-Invaders/Headers/PlayerService.cpp b/Space-Invaders/Headers/PlayerService.cpp index 49be2d11a..e8932f90d 100644 --- a/Space-Invaders/Headers/PlayerService.cpp +++ b/Space-Invaders/Headers/PlayerService.cpp @@ -15,11 +15,11 @@ void PlayerService::processPlayerInput() EventService* checkkey = ServiceLocator::getInstance()->getEventService(); if (checkkey->isKeyboardEvent()) { if (checkkey->pressedLeftKey()) { - move(-1.0 * getMoveSpeed()); + moveLef(); } if (checkkey->pressedRightKey()) { - move(1.0 * getMoveSpeed()); + moveRight(); } } } @@ -50,11 +50,20 @@ void PlayerService::render() game_window->draw(player_sprite); } -void PlayerService::move(float offsetX) +void PlayerService::moveLef() { - position.x += offsetX; + + position.x -= ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); } +void PlayerService::moveRight() +{ + + position.x += ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); +} + + + int PlayerService::getMoveSpeed() { return movement_speed; diff --git a/Space-Invaders/Headers/PlayerService.h b/Space-Invaders/Headers/PlayerService.h index a9440127c..997402155 100644 --- a/Space-Invaders/Headers/PlayerService.h +++ b/Space-Invaders/Headers/PlayerService.h @@ -1,5 +1,6 @@ #pragma once #include +#include"TimeService .h" class PlayerService { @@ -9,7 +10,7 @@ class PlayerService int health = 3; sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - int movement_speed = 5; + float movement_speed = 5.0f; int player_score = 0; const sf::String player_texture_path = "assets/textures/player_ship.png"; @@ -31,7 +32,8 @@ class PlayerService void update(); void render(); - void move(float offsetX); + void moveLef(); + void moveRight(); int getMoveSpeed(); sf::Vector2f getPlayerPosition(); diff --git a/Space-Invaders/Headers/player/PlayerController.h b/Space-Invaders/Headers/player/PlayerController.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerController.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Headers/player/PlayerModel.cpp b/Space-Invaders/Headers/player/PlayerModel.cpp new file mode 100644 index 000000000..bc266d1d0 --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerModel.cpp @@ -0,0 +1,50 @@ +#include"PlayerModel.h" + +PlayerModel::PlayerModel() { } + +PlayerModel::~PlayerModel() { } + +void PlayerModel::initialize() { reset(); } + +void PlayerModel::reset() +{ + player_position = initial_player_position; + player_score = 0; +} + +sf::Vector2f PlayerModel::getPlayerPosition() +{ + return player_position; +} + +void PlayerModel::setPlayerPosition(sf::Vector2f position) +{ + player_position = position; +} + +PlayerState PlayerModel::getplayerstate() +{ + return player_state; +} + +void PlayerModel::setplayerstate(PlayerState state) + +{ + player_state = state; + +} + + + + + + + + + + + + + + + diff --git a/Space-Invaders/Headers/player/PlayerModel.h b/Space-Invaders/Headers/player/PlayerModel.h new file mode 100644 index 000000000..5d0ba8e79 --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerModel.h @@ -0,0 +1,51 @@ +#pragma once +#include + + +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; + + 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); + + + +}; \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerView.h b/Space-Invaders/Headers/player/PlayerView.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerView.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index f85d9a4d0..87d1530fc 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -136,6 +136,7 @@ + @@ -145,6 +146,9 @@ + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 219bacc87..a5307cb2f 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -36,6 +36,9 @@ Source Files + + Source Files + @@ -56,5 +59,14 @@ Header Files + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file From b1564b096b29492f74d2ddd3e771b29d05589be0 Mon Sep 17 00:00:00 2001 From: Tarun Date: Mon, 29 Jul 2024 18:29:50 +0530 Subject: [PATCH 06/27] IN THSI COMMIT WE HAVE IMPLEMENTED THE MVC MODEL AND WE ENCOUNTER SOME OF THE ERRORS WHICH WE ARE GOING TO FIX IN NEXT MODULE --- Space-Invaders/Headers/PlayerService.cpp | 4 ++ Space-Invaders/Headers/PlayerService.h | 3 + .../Headers/player/PlayerController.cpp | 67 +++++++++++++++++++ .../Headers/player/PlayerController.h | 30 ++++++++- Space-Invaders/Headers/player/PlayerView.cpp | 42 ++++++++++++ Space-Invaders/Headers/player/PlayerView.h | 31 +++++++++ Space-Invaders/Space-Invaders.vcxproj | 2 + Space-Invaders/Space-Invaders.vcxproj.filters | 6 ++ 8 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 Space-Invaders/Headers/player/PlayerController.cpp create mode 100644 Space-Invaders/Headers/player/PlayerView.cpp diff --git a/Space-Invaders/Headers/PlayerService.cpp b/Space-Invaders/Headers/PlayerService.cpp index e8932f90d..d06939a78 100644 --- a/Space-Invaders/Headers/PlayerService.cpp +++ b/Space-Invaders/Headers/PlayerService.cpp @@ -27,6 +27,7 @@ void PlayerService::processPlayerInput() PlayerService::PlayerService() { game_window = nullptr; + pcontroller = nullptr; } PlayerService::~PlayerService() @@ -36,17 +37,20 @@ PlayerService::~PlayerService() void PlayerService::initialize() { game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + pcontroller->intialize(); initializePlayerSprite(); } void PlayerService::update() { processPlayerInput(); + pcontroller->update(); player_sprite.setPosition(getPlayerPosition()); } void PlayerService::render() { + pcontroller->render(); game_window->draw(player_sprite); } diff --git a/Space-Invaders/Headers/PlayerService.h b/Space-Invaders/Headers/PlayerService.h index 997402155..a1cdb65d1 100644 --- a/Space-Invaders/Headers/PlayerService.h +++ b/Space-Invaders/Headers/PlayerService.h @@ -1,6 +1,7 @@ #pragma once #include #include"TimeService .h" +#include"../Headers/player/PlayerController.h" class PlayerService { @@ -23,6 +24,8 @@ class PlayerService void initializePlayerSprite(); void processPlayerInput(); + PlayerController* pcontroller; + public: PlayerService(); diff --git a/Space-Invaders/Headers/player/PlayerController.cpp b/Space-Invaders/Headers/player/PlayerController.cpp new file mode 100644 index 000000000..4be7a2a9c --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerController.cpp @@ -0,0 +1,67 @@ +#include "PlayerController.h" +#include"../EventService.h" +#include"../ServiceLocator.h" +#include +void PlayerController::processinput() +{ + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + processmoveright(); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + processmoveleft(); + } +} + +void PlayerController::processmoveleft() +{ + + sf::Vector2f current_position = model->getPlayerPosition(); + current_position.x -= model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + current_position.x = std::max(current_position.x, model->left_most_position.x); + model->setPlayerPosition(current_position); +} + + +void PlayerController::processmoveright() +{ + sf::Vector2f current_position = model->getPlayerPosition(); + current_position.x += model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + current_position.x = std::min(current_position.x, model->right_most_position.x); + model->setPlayerPosition(current_position); + +} + +PlayerController::PlayerController() +{ + + model = new PlayerModel(); + view = new PlayerView(); +} + +PlayerController::~PlayerController() +{ + delete (view); + delete (model); +} + +void PlayerController::intialize() +{ + model->initialize(); + view->initialize(); +} + +void PlayerController::update() +{ + view->update(); +} + +void PlayerController::render() +{ + view->render(); +} + +sf::Vector2f PlayerController::getPlayerPosition() +{ + return model->getPlayerPosition(); +} diff --git a/Space-Invaders/Headers/player/PlayerController.h b/Space-Invaders/Headers/player/PlayerController.h index 6f70f09be..924e5b845 100644 --- a/Space-Invaders/Headers/player/PlayerController.h +++ b/Space-Invaders/Headers/player/PlayerController.h @@ -1 +1,29 @@ -#pragma once +#pragma one +#include +#include"PlayerModel.h" +#include"PlayerView.h" + +class PlayerController { + +private: + + PlayerModel* model; + PlayerView* view; + + + void processinput(); + void processmoveleft(); + void processmoveright(); +public: + PlayerController(); + ~PlayerController(); + void intialize(); + void update(); + void render(); + + + + + sf::Vector2f getPlayerPosition(); + +}; diff --git a/Space-Invaders/Headers/player/PlayerView.cpp b/Space-Invaders/Headers/player/PlayerView.cpp new file mode 100644 index 000000000..ceb9af683 --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerView.cpp @@ -0,0 +1,42 @@ +#include"PlayerView.h" +#include"../ServiceLocator.h" +PlayerView::PlayerView() { } + +PlayerView::~PlayerView() { } + +void PlayerView::initialize(PlayerController* controller) +{ + + player_controller = controller; + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +void PlayerView::initializePlayerSprite() +{ + if (player_texture.loadFromFile(player_texture_path)) + { + player_sprite.setTexture(player_texture); + scalePlayerSprite(); + } +} + +void PlayerView::scalePlayerSprite() +{ + // setScale is an inbuilt method of the sprite class that takes two floats to scale the sprite. it scales the sprite to our desired height + player_sprite.setScale( + //Here we find the factor to scale our sprites with. Ignore the static_cast for now, we will discuss it later. + static_cast(player_sprite_width) / player_sprite.getTexture()->getSize().x, + static_cast(player_sprite_height) / player_sprite.getTexture()->getSize().y + ); +} + +void PlayerView::update() +{ + //empty for now +} + +void PlayerView::render() +{ + game_window->draw(player_sprite); +} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerView.h b/Space-Invaders/Headers/player/PlayerView.h index 6f70f09be..23dd3dad1 100644 --- a/Space-Invaders/Headers/player/PlayerView.h +++ b/Space-Invaders/Headers/player/PlayerView.h @@ -1 +1,32 @@ #pragma once +#include +#include"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; + + + +public: + PlayerView(); + ~PlayerView(); + + void initialize(PlayerController* player_controller); + void update(); + void render(); +}; \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 87d1530fc..9c1f9c7a3 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -136,7 +136,9 @@ + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index a5307cb2f..e01c392bf 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -39,6 +39,12 @@ Source Files + + Source Files + + + Source Files + From 571656de35b456208007afd36f41fa0f53cbe772 Mon Sep 17 00:00:00 2001 From: Tarun Date: Mon, 29 Jul 2024 19:52:30 +0530 Subject: [PATCH 07/27] did nothing much just add forward declrations --- Space-Invaders/Headers/{ => EVENT}/EventService.h | 0 Space-Invaders/Headers/{ => Global}/ServiceLocator.h | 0 Space-Invaders/Headers/{ => Graphic}/GraphicService.h | 0 Space-Invaders/Headers/{ => TIME}/TimeService .h | 0 Space-Invaders/Headers/{ => main}/GameService.h | 0 Space-Invaders/Headers/{ => player}/PlayerService.h | 0 Space-Invaders/{Headers => source}/EventService.cpp | 0 Space-Invaders/{Headers => source}/GameService.cpp | 0 Space-Invaders/{Headers => source}/GraphicService.cpp | 0 Space-Invaders/{Headers/player => source}/PlayerController.cpp | 0 Space-Invaders/{Headers/player => source}/PlayerModel.cpp | 0 Space-Invaders/{Headers => source}/PlayerService.cpp | 0 Space-Invaders/{Headers/player => source}/PlayerView.cpp | 0 Space-Invaders/{Headers => source}/ServiceLocator.cpp | 0 Space-Invaders/{Headers => source}/TimeService .cpp | 0 15 files changed, 0 insertions(+), 0 deletions(-) rename Space-Invaders/Headers/{ => EVENT}/EventService.h (100%) rename Space-Invaders/Headers/{ => Global}/ServiceLocator.h (100%) rename Space-Invaders/Headers/{ => Graphic}/GraphicService.h (100%) rename Space-Invaders/Headers/{ => TIME}/TimeService .h (100%) rename Space-Invaders/Headers/{ => main}/GameService.h (100%) rename Space-Invaders/Headers/{ => player}/PlayerService.h (100%) rename Space-Invaders/{Headers => source}/EventService.cpp (100%) rename Space-Invaders/{Headers => source}/GameService.cpp (100%) rename Space-Invaders/{Headers => source}/GraphicService.cpp (100%) rename Space-Invaders/{Headers/player => source}/PlayerController.cpp (100%) rename Space-Invaders/{Headers/player => source}/PlayerModel.cpp (100%) rename Space-Invaders/{Headers => source}/PlayerService.cpp (100%) rename Space-Invaders/{Headers/player => source}/PlayerView.cpp (100%) rename Space-Invaders/{Headers => source}/ServiceLocator.cpp (100%) rename Space-Invaders/{Headers => source}/TimeService .cpp (100%) diff --git a/Space-Invaders/Headers/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h similarity index 100% rename from Space-Invaders/Headers/EventService.h rename to Space-Invaders/Headers/EVENT/EventService.h diff --git a/Space-Invaders/Headers/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h similarity index 100% rename from Space-Invaders/Headers/ServiceLocator.h rename to Space-Invaders/Headers/Global/ServiceLocator.h diff --git a/Space-Invaders/Headers/GraphicService.h b/Space-Invaders/Headers/Graphic/GraphicService.h similarity index 100% rename from Space-Invaders/Headers/GraphicService.h rename to Space-Invaders/Headers/Graphic/GraphicService.h diff --git a/Space-Invaders/Headers/TimeService .h b/Space-Invaders/Headers/TIME/TimeService .h similarity index 100% rename from Space-Invaders/Headers/TimeService .h rename to Space-Invaders/Headers/TIME/TimeService .h diff --git a/Space-Invaders/Headers/GameService.h b/Space-Invaders/Headers/main/GameService.h similarity index 100% rename from Space-Invaders/Headers/GameService.h rename to Space-Invaders/Headers/main/GameService.h diff --git a/Space-Invaders/Headers/PlayerService.h b/Space-Invaders/Headers/player/PlayerService.h similarity index 100% rename from Space-Invaders/Headers/PlayerService.h rename to Space-Invaders/Headers/player/PlayerService.h diff --git a/Space-Invaders/Headers/EventService.cpp b/Space-Invaders/source/EventService.cpp similarity index 100% rename from Space-Invaders/Headers/EventService.cpp rename to Space-Invaders/source/EventService.cpp diff --git a/Space-Invaders/Headers/GameService.cpp b/Space-Invaders/source/GameService.cpp similarity index 100% rename from Space-Invaders/Headers/GameService.cpp rename to Space-Invaders/source/GameService.cpp diff --git a/Space-Invaders/Headers/GraphicService.cpp b/Space-Invaders/source/GraphicService.cpp similarity index 100% rename from Space-Invaders/Headers/GraphicService.cpp rename to Space-Invaders/source/GraphicService.cpp diff --git a/Space-Invaders/Headers/player/PlayerController.cpp b/Space-Invaders/source/PlayerController.cpp similarity index 100% rename from Space-Invaders/Headers/player/PlayerController.cpp rename to Space-Invaders/source/PlayerController.cpp diff --git a/Space-Invaders/Headers/player/PlayerModel.cpp b/Space-Invaders/source/PlayerModel.cpp similarity index 100% rename from Space-Invaders/Headers/player/PlayerModel.cpp rename to Space-Invaders/source/PlayerModel.cpp diff --git a/Space-Invaders/Headers/PlayerService.cpp b/Space-Invaders/source/PlayerService.cpp similarity index 100% rename from Space-Invaders/Headers/PlayerService.cpp rename to Space-Invaders/source/PlayerService.cpp diff --git a/Space-Invaders/Headers/player/PlayerView.cpp b/Space-Invaders/source/PlayerView.cpp similarity index 100% rename from Space-Invaders/Headers/player/PlayerView.cpp rename to Space-Invaders/source/PlayerView.cpp diff --git a/Space-Invaders/Headers/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp similarity index 100% rename from Space-Invaders/Headers/ServiceLocator.cpp rename to Space-Invaders/source/ServiceLocator.cpp diff --git a/Space-Invaders/Headers/TimeService .cpp b/Space-Invaders/source/TimeService .cpp similarity index 100% rename from Space-Invaders/Headers/TimeService .cpp rename to Space-Invaders/source/TimeService .cpp From c033f7310ced58b1bf2bf1267033ac2ec8385701 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sat, 3 Aug 2024 10:40:56 +0530 Subject: [PATCH 08/27] here i have put the namespace in the code and l also debug the code for the error after the namespace --- Space-Invaders/Headers/EVENT/EventService.h | 41 ++--- .../Headers/Global/ServiceLocator.h | 80 +++++---- .../Headers/Graphic/GraphicService.h | 53 +++--- Space-Invaders/Headers/main/GameService.h | 40 +++-- .../Headers/player/PlayerController.h | 44 +++-- Space-Invaders/Headers/player/PlayerModel.h | 69 ++++---- Space-Invaders/Headers/player/PlayerService.h | 57 +++--- Space-Invaders/Headers/player/PlayerView.h | 45 ++--- Space-Invaders/Space-Invaders.vcxproj | 32 ++-- Space-Invaders/Space-Invaders.vcxproj.filters | 36 ++-- Space-Invaders/main.cpp | 3 +- Space-Invaders/source/EventService.cpp | 84 +++++---- Space-Invaders/source/GameService.cpp | 104 ++++++----- Space-Invaders/source/GraphicService.cpp | 100 +++++------ Space-Invaders/source/PlayerController.cpp | 110 ++++++------ Space-Invaders/source/PlayerModel.cpp | 54 +++--- Space-Invaders/source/PlayerService.cpp | 155 +++++++++-------- Space-Invaders/source/PlayerView.cpp | 77 +++++---- Space-Invaders/source/ServiceLocator.cpp | 162 +++++++++--------- Space-Invaders/source/TimeService .cpp | 2 +- 20 files changed, 723 insertions(+), 625 deletions(-) diff --git a/Space-Invaders/Headers/EVENT/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h index 467eff7ca..93f993fd5 100644 --- a/Space-Invaders/Headers/EVENT/EventService.h +++ b/Space-Invaders/Headers/EVENT/EventService.h @@ -1,30 +1,31 @@ #pragma once #include #include +namespace event { + class EventService + { + private: + sf::Event game_event; //event var + sf::RenderWindow* game_window; //ptr to our game window -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 - bool isGameWindowOpen(); - bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. - bool hasQuitGame(); //for our new 'ESC' condition - -public: - EventService(); - ~EventService(); + 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(); + void initialize(); + void update(); + void processEvents(); // while window is open we will check for events + bool pressedEscapeKey(); + bool isKeyboardEvent(); + bool pressedLeftKey(); + bool pressedRightKey(); -}; \ No newline at end of file + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/Global/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h index 86a93eed9..bf7b53343 100644 --- a/Space-Invaders/Headers/Global/ServiceLocator.h +++ b/Space-Invaders/Headers/Global/ServiceLocator.h @@ -1,37 +1,45 @@ #pragma once -#include "GraphicService.h" -#include"EventService.h" -#include"PlayerService.h" -#include"TimeService .h" - -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(); -}; + +#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(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/Graphic/GraphicService.h b/Space-Invaders/Headers/Graphic/GraphicService.h index 570c7f584..e013140cf 100644 --- a/Space-Invaders/Headers/Graphic/GraphicService.h +++ b/Space-Invaders/Headers/Graphic/GraphicService.h @@ -2,40 +2,43 @@ #pragma once #include +namespace Graphic { -class GraphicService -{ -private: - + 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 std::string game_window_title = "Outscal Presents - Alien Invader"; - const sf::Color window_color = sf::Color::Blue; + const int game_window_width = 800; + const int game_window_height = 600; - const int framerate = 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 + const int framerate = 60; - void setVideoMode(); // Method for setting our video mode - void onDestroy(); // method to run when window is deleted + sf::VideoMode* video_mode; // ptr to video mode + sf::RenderWindow* game_window; // ptr to a RenderWindow -public: - GraphicService(); - ~GraphicService(); //cleanup + void setVideoMode(); // Method for setting our video mode + void onDestroy(); // method to run when window is deleted - //method to create the game window. returns a pointer to an instance of the game window - sf::RenderWindow* createGameWindow(); + 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 -}; \ No newline at end of file + 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 + }; + +} \ No newline at end of file diff --git a/Space-Invaders/Headers/main/GameService.h b/Space-Invaders/Headers/main/GameService.h index c09fa800f..96ffa1177 100644 --- a/Space-Invaders/Headers/main/GameService.h +++ b/Space-Invaders/Headers/main/GameService.h @@ -1,26 +1,32 @@ #pragma once #include -#include "ServiceLocator.h" +#include"../Global/ServiceLocator.h" -class GameService -{ -private: +namespace Main { - ServiceLocator* service_locator; - sf::RenderWindow* game_window; + using namespace Global; - void initialize(); - void initializeVariables();// Handles game initialization. - void destroy(); // Handles cleanup tasks. + class GameService + { + private: -public: - GameService(); // Constructor for initializing the GameService object. - ~GameService(); // Destructor for cleaning up resources upon object deletion. + ServiceLocator* service_locator; + sf::RenderWindow* game_window; - 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. -}; \ No newline at end of file + 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. + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerController.h b/Space-Invaders/Headers/player/PlayerController.h index 924e5b845..9406c7c23 100644 --- a/Space-Invaders/Headers/player/PlayerController.h +++ b/Space-Invaders/Headers/player/PlayerController.h @@ -1,29 +1,35 @@ -#pragma one +#pragma once #include -#include"PlayerModel.h" -#include"PlayerView.h" +namespace player { -class PlayerController { + enum class PlayerState; + class PlayerView; + class PlayerModel; -private: + class PlayerController { - PlayerModel* model; - PlayerView* view; + private: - void processinput(); - void processmoveleft(); - void processmoveright(); -public: - PlayerController(); - ~PlayerController(); - void intialize(); - void update(); - void render(); - + void processinput(); + void processmoveleft(); + void processmoveright(); + public: + PlayerModel* model; + PlayerView* view; + PlayerController(); + ~PlayerController(); + void intialize(); + void update(); + void render(); - sf::Vector2f getPlayerPosition(); -}; + + + sf::Vector2f getPlayerPosition(); + + }; + +} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerModel.h b/Space-Invaders/Headers/player/PlayerModel.h index 5d0ba8e79..90d116e84 100644 --- a/Space-Invaders/Headers/player/PlayerModel.h +++ b/Space-Invaders/Headers/player/PlayerModel.h @@ -1,51 +1,58 @@ #pragma once #include +class PlayerView; +class PlayerModel; +namespace player { -enum class PlayerState //Our Enum -{ - ALIVE, - DEAD, - // we will add more states later -}; + 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; - - int player_score = 0; + class PlayerModel + { + private: -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 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; - const float player_movement_speed = 200.0f; - PlayerModel(); - ~PlayerModel(); + 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); - void initialize(); + const float player_movement_speed = 200.0f; - void reset(); //new method + 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); - + //getters and setters + sf::Vector2f getPlayerPosition(); + void setPlayerPosition(sf::Vector2f position); -}; \ No newline at end of file + PlayerState getplayerstate(); + + + void setplayerstate(PlayerState state); + + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerService.h b/Space-Invaders/Headers/player/PlayerService.h index a1cdb65d1..25d03fecf 100644 --- a/Space-Invaders/Headers/player/PlayerService.h +++ b/Space-Invaders/Headers/player/PlayerService.h @@ -1,43 +1,48 @@ #pragma once -#include -#include"TimeService .h" -#include"../Headers/player/PlayerController.h" -class PlayerService -{ +#include" +#include"../TIME/TimeService .h" +#include"../player/PlayerController.h" -private: - +namespace player { + class PlayerService + { - int health = 3; - sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - float movement_speed = 5.0f; - int player_score = 0; + private: - 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 + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); + float movement_speed = 5.0f; + int player_score = 0; - void initializePlayerSprite(); - void processPlayerInput(); + const sf::String player_texture_path = "assets/textures/player_ship.png"; - PlayerController* pcontroller; + sf::Texture player_texture; + sf::Sprite player_sprite; -public: + sf::RenderWindow* game_window; //as always + + void initializePlayerSprite(); + void processPlayerInput(); + + PlayerController* player_controller; + + + public: PlayerService(); - ~PlayerService(); + ~PlayerService(); - void initialize(); - void update(); - void render(); + void initialize(); + void update(); + void render(); void moveLef(); void moveRight(); - int getMoveSpeed(); - sf::Vector2f getPlayerPosition(); + int getMoveSpeed(); + sf::Vector2f getPlayerPosition(); -}; + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerView.h b/Space-Invaders/Headers/player/PlayerView.h index 23dd3dad1..cba5f3eba 100644 --- a/Space-Invaders/Headers/player/PlayerView.h +++ b/Space-Invaders/Headers/player/PlayerView.h @@ -1,32 +1,33 @@ #pragma once #include -#include"PlayerController.h" +#include"../player/PlayerController.h" -class PlayerView -{ -private: + 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; + 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::RenderWindow* game_window; - sf::Texture player_texture; - sf::Sprite player_sprite; + sf::Texture player_texture; + sf::Sprite player_sprite; - void initializePlayerSprite(); - void scalePlayerSprite(); + void initializePlayerSprite(); + void scalePlayerSprite(); - PlayerController* player_controller; + PlayerController* player_controller; + public: + PlayerView(); + ~PlayerView(); - -public: - PlayerView(); - ~PlayerView(); - - void initialize(PlayerController* player_controller); - void update(); - void render(); -}; \ No newline at end of file + void initialize(PlayerController* player_controller); + void update(); + void render(); + }; + } \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 9c1f9c7a3..69138384f 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -1,4 +1,4 @@ - + @@ -132,27 +132,27 @@ - - - - - - - - - + + + + + + + + + - - - - + + + + - - + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index e01c392bf..31167764f 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -18,60 +18,60 @@ Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 1a2cf9819..a7a0b06f7 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,4 +1,5 @@ -#include "Headers/GameService.h" +#include"../Space-Invaders/Headers/main/GameService.h" +using namespace Main; int main() { diff --git a/Space-Invaders/source/EventService.cpp b/Space-Invaders/source/EventService.cpp index a5c4965e3..729ac8cd0 100644 --- a/Space-Invaders/source/EventService.cpp +++ b/Space-Invaders/source/EventService.cpp @@ -1,49 +1,61 @@ -#include"ServiceLocator.h" -#include"GraphicService.h" -#include"EventService.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(); +//#include"ServiceLocator.h" +//#include"GraphicService.h" +//#include"EventService.h" + +#include"../Headers/main/GameService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/EVENT/EventService.h" + + +namespace event { + + using namespace Global; + using namespace Graphic; + using namespace event; + + 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 + 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; } + //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; } + //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::isGameWindowOpen() { return game_window != nullptr; } -bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } + 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; } + bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } + bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } +} \ No newline at end of file diff --git a/Space-Invaders/source/GameService.cpp b/Space-Invaders/source/GameService.cpp index 960d7acb4..398a7bb0b 100644 --- a/Space-Invaders/source/GameService.cpp +++ b/Space-Invaders/source/GameService.cpp @@ -1,61 +1,73 @@ -#include "GameService.h" -#include"GraphicService.h" +//#include "GameService.h" +//#include"GraphicService.h" +#include"../Headers/Graphic/GraphicService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/main/GameService.h" -GameService::GameService() { - service_locator = nullptr; // Set service locator to null - game_window = nullptr; // Set game window to null -} -// Destructor: Calls the destroy function to clean up resources. -GameService::~GameService() { - destroy(); // Clean up and release resources -} -// Prepares the game service for use by obtaining the service locator instance and initializing services. -void GameService::ignite() { - service_locator = ServiceLocator::getInstance(); // Get ServiceLocator - initialize(); // Initialize services. -} +namespace Main { + using namespace Global; + using namespace event; + using namespace Graphic; + GameService::GameService() { -//initialize service locator and other variables -void GameService::initialize() -{ - service_locator->initialize(); - initializeVariables(); -} -void GameService::initializeVariables() -{ - game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) -} -void GameService::destroy() -{ - // don't need to do anything here for now. -} + service_locator = nullptr; // Set service locator to null + game_window = nullptr; // Set game window to null + } -// Updates the game logic by delegating to the service locator's update method. -void GameService::update() { + // Destructor: Calls the destroy function to clean up resources. + GameService::~GameService() { + destroy(); // Clean up and release resources + } - service_locator->getEventService()->processEvents(); + // Prepares the game service for use by obtaining the service locator instance and initializing services. + void GameService::ignite() { + service_locator = ServiceLocator::getInstance(); // Get ServiceLocator + initialize(); // Initialize services. + } - service_locator->update(); // Call update on the service locator which then updates all its managed services -} + //initialize service locator and other variables + void GameService::initialize() + { + service_locator->initialize(); + initializeVariables(); + } -// Clears the window then display it. -void GameService::render() { - // Clears the game window with the background color provided by the graphic service - game_window->clear(service_locator->getGraphicService()->getWindowColor()); - service_locator->render(); // Render the current frame using the service locator - game_window->display(); // Display the rendered frame on the game window -} + void GameService::initializeVariables() + { + game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) + } -// Checks if the game is still running by querying the graphic service's window open status. -bool GameService::isRunning() { - // Returns true if the game window is open, indicating the game is still running - return service_locator->getGraphicService()->isGameWindowOpen(); -} + void GameService::destroy() + { + // don't need to do anything here for now. + } + + // Updates the game logic by delegating to the service locator's update method. + void GameService::update() { + + service_locator->getEventService()->processEvents(); + service_locator->update(); // Call update on the service locator which then updates all its managed services + } + // Clears the window then display it. + void GameService::render() { + // Clears the game window with the background color provided by the graphic service + game_window->clear(service_locator->getGraphicService()->getWindowColor()); + service_locator->render(); // Render the current frame using the service locator + game_window->display(); // Display the rendered frame on the game window + } + + // Checks if the game is still running by querying the graphic service's window open status. + bool GameService::isRunning() { + // Returns true if the game window is open, indicating the game is still running + return service_locator->getGraphicService()->isGameWindowOpen(); + } + +} diff --git a/Space-Invaders/source/GraphicService.cpp b/Space-Invaders/source/GraphicService.cpp index e4a35584d..5b690f231 100644 --- a/Space-Invaders/source/GraphicService.cpp +++ b/Space-Invaders/source/GraphicService.cpp @@ -1,67 +1,71 @@ -#include"ServiceLocator.h" -#include"GraphicService.h" +//#include"ServiceLocator.h" +//#include"GraphicService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/Graphic/GraphicService.h" +namespace Graphic { -// Constructor: Initializes game window and video mode pointers to null. -GraphicService::GraphicService() { - game_window = nullptr; // Initializes game window pointer to null - video_mode = nullptr; // Initializes video mode pointer to null - -} + // Constructor: Initializes game window and video mode pointers to null. + GraphicService::GraphicService() { + game_window = nullptr; // Initializes game window pointer to null + video_mode = nullptr; // Initializes video mode pointer to null -// Destructor: Cleans up resources by calling onDestroy. -GraphicService::~GraphicService() { - onDestroy(); // Calls onDestroy method to clean up resources -} + } -// Initializes the graphic service by creating a new game window. -void GraphicService::initialize() { - game_window = createGameWindow(); // Assigns a new game window to the game_window pointer + // Destructor: Cleans up resources by calling onDestroy. + GraphicService::~GraphicService() { + onDestroy(); // Calls onDestroy method to clean up resources + } - game_window->setFramerateLimit(framerate); -} + // Initializes the graphic service by creating a new game window. + void GraphicService::initialize() { + game_window = createGameWindow(); // Assigns a new game window to the game_window pointer + game_window->setFramerateLimit(framerate); + } -// Creates a new SFML RenderWindow object with specified video mode and title. -sf::RenderWindow* GraphicService::createGameWindow() { - setVideoMode(); // Sets up the video mode for the window - return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object -} -// Sets up the video mode for the game window using specified dimensions and system's color depth. -void GraphicService::setVideoMode() { - video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode -} + // Creates a new SFML RenderWindow object with specified video mode and title. + sf::RenderWindow* GraphicService::createGameWindow() { + setVideoMode(); // Sets up the video mode for the window + return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object + } -// Cleans up allocated memory for video mode and game window to avoid memory leaks. -void GraphicService::onDestroy() { - delete(video_mode); // Deletes the video mode object - delete(game_window); // Deletes the game window object -} + // Sets up the video mode for the game window using specified dimensions and system's color depth. + void GraphicService::setVideoMode() { + video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode + } -// Placeholder function for game update logic. -void GraphicService::update() { + // Cleans up allocated memory for video mode and game window to avoid memory leaks. + void GraphicService::onDestroy() { + delete(video_mode); // Deletes the video mode object + delete(game_window); // Deletes the game window object + } + // Placeholder function for game update logic. + void GraphicService::update() { -} + + } -// Placeholder function for game rendering logic. -void GraphicService::render() { + // Placeholder function for game rendering logic. + void GraphicService::render() { } -// Checks if the game window is currently open. -bool GraphicService::isGameWindowOpen() { - return game_window->isOpen(); // Returns the open status of the game window -} + // Checks if the game window is currently open. + bool GraphicService::isGameWindowOpen() { + return game_window->isOpen(); // Returns the open status of the game window + } -// Returns a pointer to the game window object. -sf::RenderWindow* GraphicService::getGameWindow() { - return game_window; -} + // Returns a pointer to the game window object. + sf::RenderWindow* GraphicService::getGameWindow() { + return game_window; + } -// Returns the configured window background color. -sf::Color GraphicService::getWindowColor() { - return window_color; -} + // Returns the configured window background color. + sf::Color GraphicService::getWindowColor() { + return window_color; + } +} \ No newline at end of file diff --git a/Space-Invaders/source/PlayerController.cpp b/Space-Invaders/source/PlayerController.cpp index 4be7a2a9c..05d122b62 100644 --- a/Space-Invaders/source/PlayerController.cpp +++ b/Space-Invaders/source/PlayerController.cpp @@ -1,67 +1,73 @@ -#include "PlayerController.h" -#include"../EventService.h" -#include"../ServiceLocator.h" +#include"../Headers/player/PlayerController.h" +#include"../Headers//EVENT/EventService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/player/PlayerModel.h" +#include"../Headers//player/PlayerView.h" #include -void PlayerController::processinput() -{ +namespace player{ + using namespace Global; + void PlayerController::processinput() + { - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - processmoveright(); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + processmoveright(); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + processmoveleft(); + } } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - processmoveleft(); - } -} -void PlayerController::processmoveleft() -{ + void PlayerController::processmoveleft() + { - sf::Vector2f current_position = model->getPlayerPosition(); - current_position.x -= model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); - current_position.x = std::max(current_position.x, model->left_most_position.x); - model->setPlayerPosition(current_position); -} + sf::Vector2f current_position = model->getPlayerPosition(); + current_position.x -= model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + current_position.x = std::max(current_position.x, model->left_most_position.x); + model->setPlayerPosition(current_position); + } -void PlayerController::processmoveright() -{ - sf::Vector2f current_position = model->getPlayerPosition(); - current_position.x += model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); - current_position.x = std::min(current_position.x, model->right_most_position.x); - model->setPlayerPosition(current_position); + void PlayerController::processmoveright() + { + sf::Vector2f current_position = model->getPlayerPosition(); + current_position.x += model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + current_position.x = std::min(current_position.x, model->right_most_position.x); + model->setPlayerPosition(current_position); -} + } -PlayerController::PlayerController() -{ - - model = new PlayerModel(); - view = new PlayerView(); -} + PlayerController::PlayerController() + { -PlayerController::~PlayerController() -{ - delete (view); - delete (model); -} + model = new PlayerModel(); + view = new PlayerView(); + } -void PlayerController::intialize() -{ - model->initialize(); - view->initialize(); -} + PlayerController::~PlayerController() + { + delete (view); + delete (model); + } -void PlayerController::update() -{ - view->update(); -} + void PlayerController::intialize() + { + model->initialize(); + view->initialize(this); + } -void PlayerController::render() -{ - view->render(); -} + void PlayerController::update() + { + processinput(); + view->update(); + } + + void PlayerController::render() + { + view->render(); + } -sf::Vector2f PlayerController::getPlayerPosition() -{ - return model->getPlayerPosition(); + sf::Vector2f PlayerController::getPlayerPosition() + { + return model->getPlayerPosition(); + } } diff --git a/Space-Invaders/source/PlayerModel.cpp b/Space-Invaders/source/PlayerModel.cpp index bc266d1d0..0218d5e85 100644 --- a/Space-Invaders/source/PlayerModel.cpp +++ b/Space-Invaders/source/PlayerModel.cpp @@ -1,40 +1,44 @@ -#include"PlayerModel.h" +//#include"PlayerModel.h" +#include"../Headers/player/PlayerModel.h" +namespace player { -PlayerModel::PlayerModel() { } + PlayerModel::PlayerModel() { } -PlayerModel::~PlayerModel() { } + PlayerModel::~PlayerModel() { } -void PlayerModel::initialize() { reset(); } + void PlayerModel::initialize() { reset(); } -void PlayerModel::reset() -{ - player_position = initial_player_position; - player_score = 0; -} + void PlayerModel::reset() + { -sf::Vector2f PlayerModel::getPlayerPosition() -{ - return player_position; -} + player_state = PlayerState::ALIVE; + player_position = initial_player_position; + player_score = 0; + } -void PlayerModel::setPlayerPosition(sf::Vector2f position) -{ - player_position = position; -} + sf::Vector2f PlayerModel::getPlayerPosition() + { + return player_position; + } -PlayerState PlayerModel::getplayerstate() -{ - return player_state; -} + void PlayerModel::setPlayerPosition(sf::Vector2f position) + { + player_position = position; + } -void PlayerModel::setplayerstate(PlayerState state) + PlayerState PlayerModel::getplayerstate() + { + return player_state; + } -{ - player_state = state; + void PlayerModel::setplayerstate(PlayerState state) -} + { + player_state = state; + } +} diff --git a/Space-Invaders/source/PlayerService.cpp b/Space-Invaders/source/PlayerService.cpp index d06939a78..8729355e4 100644 --- a/Space-Invaders/source/PlayerService.cpp +++ b/Space-Invaders/source/PlayerService.cpp @@ -1,79 +1,88 @@ -#include "PlayerService.h" -#include"ServiceLocator.h" +//#include "../Headers/PlayerService.h" +//#include"../Headers/ServiceLocator.h" +//#include"../Headers/player/PlayerController.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/player/PlayerService.h" +#include"../Headers/player/PlayerController.h" +#include"../Headers/EVENT/EventService.h" +namespace player { + using namespace event; + using namespace Global; + + void PlayerService::initializePlayerSprite() + { + if (player_texture.loadFromFile(player_texture_path)) { + player_sprite.setTexture(player_texture); + } + } + void PlayerService::processPlayerInput() + { -void PlayerService::initializePlayerSprite() -{ - if (player_texture.loadFromFile(player_texture_path)) { - player_sprite.setTexture(player_texture); + EventService* checkkey = ServiceLocator::getInstance()->getEventService(); + if (checkkey->isKeyboardEvent()) { + if (checkkey->pressedLeftKey()) { + moveLef(); + } + + if (checkkey->pressedRightKey()) { + moveRight(); + } + } } -} -void PlayerService::processPlayerInput() -{ + PlayerService::PlayerService() + { + game_window = nullptr; + player_controller = new PlayerController; + } - EventService* checkkey = ServiceLocator::getInstance()->getEventService(); - if (checkkey->isKeyboardEvent()) { - if (checkkey->pressedLeftKey()) { - moveLef(); - } + PlayerService::~PlayerService() + { + delete player_controller; + } - if (checkkey->pressedRightKey()) { - moveRight(); - } - } -} - -PlayerService::PlayerService() -{ - game_window = nullptr; - pcontroller = nullptr; -} - -PlayerService::~PlayerService() -{ -} - -void PlayerService::initialize() -{ - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - pcontroller->intialize(); - initializePlayerSprite(); -} - -void PlayerService::update() -{ - processPlayerInput(); - pcontroller->update(); - player_sprite.setPosition(getPlayerPosition()); -} - -void PlayerService::render() -{ - pcontroller->render(); - game_window->draw(player_sprite); -} - -void PlayerService::moveLef() -{ - - position.x -= ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); -} - -void PlayerService::moveRight() -{ - - position.x += ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); -} - - - -int PlayerService::getMoveSpeed() -{ - return movement_speed; -} - -sf::Vector2f PlayerService::getPlayerPosition() -{ - return position; -} + void PlayerService::initialize() + { + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + player_controller->intialize(); + initializePlayerSprite(); + } + + void PlayerService::update() + { + processPlayerInput(); + player_controller->update(); + player_sprite.setPosition(getPlayerPosition()); + } + + void PlayerService::render() + { + player_controller->render(); + game_window->draw(player_sprite); + } + + void PlayerService::moveLef() + { + + position.x -= ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + } + + void PlayerService::moveRight() + { + + position.x += ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + } + + + + int PlayerService::getMoveSpeed() + { + return movement_speed; + } + + sf::Vector2f PlayerService::getPlayerPosition() + { + return position; + } +} \ No newline at end of file diff --git a/Space-Invaders/source/PlayerView.cpp b/Space-Invaders/source/PlayerView.cpp index ceb9af683..c4068235f 100644 --- a/Space-Invaders/source/PlayerView.cpp +++ b/Space-Invaders/source/PlayerView.cpp @@ -1,42 +1,49 @@ -#include"PlayerView.h" -#include"../ServiceLocator.h" -PlayerView::PlayerView() { } +//#include"PlayerView.h" +//#include"../ServiceLocator.h" +#include"../Headers/player/PlayerView.h" +#include"../Headers/Global/ServiceLocator.h" -PlayerView::~PlayerView() { } +namespace player { + using namespace Global; -void PlayerView::initialize(PlayerController* controller) -{ + PlayerView::PlayerView() { } - player_controller = controller; - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); -} + PlayerView::~PlayerView() { } -void PlayerView::initializePlayerSprite() -{ - if (player_texture.loadFromFile(player_texture_path)) + void PlayerView::initialize(PlayerController* controller) { - player_sprite.setTexture(player_texture); - scalePlayerSprite(); + + player_controller = controller; + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); + } + + void PlayerView::initializePlayerSprite() + { + if (player_texture.loadFromFile(player_texture_path)) + { + player_sprite.setTexture(player_texture); + scalePlayerSprite(); + } + } + + void PlayerView::scalePlayerSprite() + { + // setScale is an inbuilt method of the sprite class that takes two floats to scale the sprite. it scales the sprite to our desired height + player_sprite.setScale( + //Here we find the factor to scale our sprites with. Ignore the static_cast for now, we will discuss it later. + static_cast(player_sprite_width) / player_sprite.getTexture()->getSize().x, + static_cast(player_sprite_height) / player_sprite.getTexture()->getSize().y + ); + } + + void PlayerView::update() + { + player_sprite.setPosition(player_controller->getPlayerPosition()); + } + + void PlayerView::render() + { + game_window->draw(player_sprite); } -} - -void PlayerView::scalePlayerSprite() -{ - // setScale is an inbuilt method of the sprite class that takes two floats to scale the sprite. it scales the sprite to our desired height - player_sprite.setScale( - //Here we find the factor to scale our sprites with. Ignore the static_cast for now, we will discuss it later. - static_cast(player_sprite_width) / player_sprite.getTexture()->getSize().x, - static_cast(player_sprite_height) / player_sprite.getTexture()->getSize().y - ); -} - -void PlayerView::update() -{ - //empty for now -} - -void PlayerView::render() -{ - game_window->draw(player_sprite); } \ No newline at end of file diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index 21ba91f85..a21653f1d 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -1,82 +1,88 @@ -#include "ServiceLocator.h" -#include"EventService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/EVENT/EventService.h" +#include"../Headers/player/PlayerService.h" +#include"../Headers/TIME/TimeService .h" +#include"../Headers//Graphic/GraphicService.h" + +namespace Global { + + // Constructor: Initializes the graphic_service pointer to null and creates services. + ServiceLocator::ServiceLocator() { + graphic_service = nullptr;// Initialize graphic_service to null + event_service = nullptr; + player_service = nullptr; + time_Service = nullptr; + createServices(); // Call createServices to instantiate services + } + + // Destructor: Cleans up resources by clearing all services. + ServiceLocator::~ServiceLocator() { + clearAllServices(); // Call clearAllServices to delete any allocated services + } + + // Creates service instances, specifically the graphic service in this case. + void ServiceLocator::createServices() { + graphic_service = new GraphicService(); // Dynamically create a GraphicService instance + event_service = new EventService(); + + player_service = new PlayerService(); + time_Service = new TimeService(); + } + + // Deletes allocated services to prevent memory leaks, specifically the graphic service. + void ServiceLocator::clearAllServices() { + delete(graphic_service); // Delete the graphic_service instance + delete(event_service); + delete(player_service); + delete(time_Service); + + } + + // Returns a pointer to ServiceLocator. + ServiceLocator* ServiceLocator::getInstance() { + static ServiceLocator instance; // we will discuss what 'static' means at a later time. + return &instance; // Return address of the instance + } + + // Calls initialize on the graphic service, readying it for use. + void ServiceLocator::initialize() { + graphic_service->initialize(); // Initialize graphic service + event_service->initialize(); + player_service->initialize(); + time_Service->intialize(); + } + + // Updates the state of the graphic service. + void ServiceLocator::update() { + graphic_service->update(); // Update graphic service + event_service->update(); + player_service->update(); + time_Service->update(); + } + + + // Renders using the graphic service. + void ServiceLocator::render() { + graphic_service->render(); // Render graphic service + player_service->render(); + } + + // Returns a pointer to the currently set graphic service. + GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } + + EventService* ServiceLocator::getEventService() { + return event_service; + } + + PlayerService* ServiceLocator::getplayerservice() { + return player_service; + } + + TimeService* ServiceLocator::gettimeservice() + { + return time_Service; + } -// Constructor: Initializes the graphic_service pointer to null and creates services. -ServiceLocator::ServiceLocator() { - graphic_service = nullptr;// Initialize graphic_service to null - event_service = nullptr; - player_service = nullptr; - time_Service = nullptr; - createServices(); // Call createServices to instantiate services } -// Destructor: Cleans up resources by clearing all services. -ServiceLocator::~ServiceLocator() { - clearAllServices(); // Call clearAllServices to delete any allocated services -} - -// Creates service instances, specifically the graphic service in this case. -void ServiceLocator::createServices() { - graphic_service = new GraphicService(); // Dynamically create a GraphicService instance - event_service = new EventService(); - player_service = new PlayerService(); - time_Service = new TimeService(); -} - -// Deletes allocated services to prevent memory leaks, specifically the graphic service. -void ServiceLocator::clearAllServices() { - delete(graphic_service); // Delete the graphic_service instance - delete(event_service); - delete(player_service); - delete(time_Service); - -} - -// Returns a pointer to ServiceLocator. -ServiceLocator* ServiceLocator::getInstance() { - static ServiceLocator instance; // we will discuss what 'static' means at a later time. - return &instance; // Return address of the instance -} - -// Calls initialize on the graphic service, readying it for use. -void ServiceLocator::initialize() { - graphic_service->initialize(); // Initialize graphic service - event_service->initialize(); - player_service->initialize(); - time_Service->intialize(); -} - -// Updates the state of the graphic service. -void ServiceLocator::update() { - graphic_service->update(); // Update graphic service - event_service->update(); - player_service->update(); - time_Service->update(); -} - - -// Renders using the graphic service. -void ServiceLocator::render() { - graphic_service->render(); // Render graphic service - player_service->render(); -} - -// Returns a pointer to the currently set graphic service. -GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } - -EventService* ServiceLocator::getEventService() { - return event_service; -} - -PlayerService* ServiceLocator::getplayerservice() { - return player_service; -} - -TimeService* ServiceLocator::gettimeservice() -{ - return time_Service; -} - - - diff --git a/Space-Invaders/source/TimeService .cpp b/Space-Invaders/source/TimeService .cpp index 0c719b841..7667ed227 100644 --- a/Space-Invaders/source/TimeService .cpp +++ b/Space-Invaders/source/TimeService .cpp @@ -1,4 +1,4 @@ -#include "TimeService .h" +#include"../Headers/TIME/TimeService .h" float TimeService::calculate_delta_time() { From abb00d35af5d0c3dd2b783379843837125317524 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 10:53:48 +0530 Subject: [PATCH 09/27] JUST ADDED THE GAME STATE --- Space-Invaders/Headers/main/GameService.h | 14 +++++++++++++- Space-Invaders/source/GameService.cpp | 10 +++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Space-Invaders/Headers/main/GameService.h b/Space-Invaders/Headers/main/GameService.h index 96ffa1177..a6864e698 100644 --- a/Space-Invaders/Headers/main/GameService.h +++ b/Space-Invaders/Headers/main/GameService.h @@ -2,6 +2,12 @@ #include #include"../Global/ServiceLocator.h" +enum class GameState //create the enum +{ + BOOT, + MAIN_MENU, + GAMEPLAY, +}; namespace Main { @@ -12,6 +18,8 @@ namespace Main { private: + static GameState current_state; + ServiceLocator* service_locator; sf::RenderWindow* game_window; @@ -27,6 +35,10 @@ namespace Main { 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. + bool isRunning();// Checks if the game is currently running. + static void setGameState(GameState new_state); + static GameState getGameState(); + + }; } \ No newline at end of file diff --git a/Space-Invaders/source/GameService.cpp b/Space-Invaders/source/GameService.cpp index 398a7bb0b..76641bac5 100644 --- a/Space-Invaders/source/GameService.cpp +++ b/Space-Invaders/source/GameService.cpp @@ -10,10 +10,11 @@ namespace Main { using namespace Global; using namespace event; using namespace Graphic; - GameService::GameService() { + GameState GameService::current_state = GameState::BOOT; + GameService::GameService() { service_locator = nullptr; // Set service locator to null game_window = nullptr; // Set game window to null } @@ -68,6 +69,13 @@ namespace Main { return service_locator->getGraphicService()->isGameWindowOpen(); } + + void GameService::setGameState(GameState new_state) { current_state = new_state; } + + GameState GameService::getGameState() { return current_state; } } + + + From 82351a8d65e05d30fb07660923dc68c2e5b459e2 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 11:47:09 +0530 Subject: [PATCH 10/27] added the ui sevice also added the mainmenucobteoller --- .../Headers/Global/ServiceLocator.h | 6 ++- .../MainMenu/MainMenuUIController.cpp | 32 +++++++++++++ .../UIService/MainMenu/MainMenuUIController.h | 23 +++++++++ .../Headers/UIService/UIService.cpp | 47 +++++++++++++++++++ Space-Invaders/Headers/UIService/UIService.h | 20 ++++++++ Space-Invaders/Space-Invaders.vcxproj | 4 ++ Space-Invaders/source/ServiceLocator.cpp | 12 ++++- 7 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp create mode 100644 Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h create mode 100644 Space-Invaders/Headers/UIService/UIService.cpp create mode 100644 Space-Invaders/Headers/UIService/UIService.h diff --git a/Space-Invaders/Headers/Global/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h index bf7b53343..f3c9ec9be 100644 --- a/Space-Invaders/Headers/Global/ServiceLocator.h +++ b/Space-Invaders/Headers/Global/ServiceLocator.h @@ -4,12 +4,13 @@ #include"../EVENT/EventService.h" #include"../player/PlayerService.h" #include"../TIME/TimeService .h" - +#include"../../Headers/UIService/UIService.h" namespace Global { using namespace player; using namespace Graphic; using namespace event; + using namespace UI; class ServiceLocator { @@ -19,7 +20,7 @@ namespace Global { EventService* event_service; PlayerService* player_service; TimeService* time_Service; - + UiService* uiservice; // Private Constructor and Destructor: ServiceLocator(); // Constructor for initializing the ServiceLocator. @@ -41,5 +42,6 @@ namespace Global { EventService* getEventService(); PlayerService* getplayerservice(); TimeService* gettimeservice(); + UiService* getuiservice(); }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp new file mode 100644 index 000000000..7435399f7 --- /dev/null +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp @@ -0,0 +1,32 @@ +#include "MainMenuUIController.h" +#include"../../Headers/main/GameService.h" +#include"../../Headers/Global/ServiceLocator.h" +#include"../../Headers/Graphic/GraphicService.h" + +namespace UI { + namespace MainMenu { + + using namespace Global; + using namespace Main; + using namespace Graphic; + using namespace event; + + UI::MainMenu::MainMenuUIController::MainMenuUIController() + { + game_window = nullptr; + } + + void UI::MainMenu::MainMenuUIController::initialize() + { + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + } + + void UI::MainMenu::MainMenuUIController::update() + { + } + + void UI::MainMenu::MainMenuUIController::render() + { + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h new file mode 100644 index 000000000..91e7bff33 --- /dev/null +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h @@ -0,0 +1,23 @@ +#pragma once +#include + +namespace UI +{ + namespace MainMenu + { + class MainMenuUIController + { + private: + + sf::RenderWindow* game_window; + + public: + MainMenuUIController(); + + void initialize(); + void update(); + void render(); + + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/UIService.cpp b/Space-Invaders/Headers/UIService/UIService.cpp new file mode 100644 index 000000000..ed09f6af0 --- /dev/null +++ b/Space-Invaders/Headers/UIService/UIService.cpp @@ -0,0 +1,47 @@ +#include "UIService.h" +#include"../main/GameService.h" + + + +using namespace Main; +using namespace MainMenu; + + +void UI::UiService::createControllers() +{ + main_menu_controller = new MainMenuUIController(); +} + +void UI::UiService::initializeControllers() +{ + main_menu_controller->initialize(); +} + +void UI::UiService::destroy() +{ + delete(main_menu_controller); +} + +UI::UiService::UiService() +{ + main_menu_controller = nullptr; + createControllers(); +} + +UI::UiService::~UiService() +{ + destroy(); +} + +void UI::UiService::initialize() +{ + initializeControllers(); +} + +void UI::UiService::update() +{ +} + +void UI::UiService::render() +{ +} diff --git a/Space-Invaders/Headers/UIService/UIService.h b/Space-Invaders/Headers/UIService/UIService.h new file mode 100644 index 000000000..65de033b8 --- /dev/null +++ b/Space-Invaders/Headers/UIService/UIService.h @@ -0,0 +1,20 @@ +#pragma once +#include"../../Headers/UIService/MainMenu/MainMenuUIController.h" +namespace UI{ + class UiService { + private: + MainMenu::MainMenuUIController* main_menu_controller; + void createControllers(); + void initializeControllers(); + void destroy(); + + public: + UiService(); + ~UiService(); + + void initialize(); + void update(); + void render(); + + }; +} \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 69138384f..67c83d458 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,6 +132,8 @@ + + @@ -153,6 +155,8 @@ + + diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index a21653f1d..abe815cf3 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -13,6 +13,7 @@ namespace Global { event_service = nullptr; player_service = nullptr; time_Service = nullptr; + uiservice = nullptr; createServices(); // Call createServices to instantiate services } @@ -28,6 +29,7 @@ namespace Global { player_service = new PlayerService(); time_Service = new TimeService(); + uiservice = new UiService(); } // Deletes allocated services to prevent memory leaks, specifically the graphic service. @@ -36,7 +38,7 @@ namespace Global { delete(event_service); delete(player_service); delete(time_Service); - + delete(uiservice); } // Returns a pointer to ServiceLocator. @@ -51,6 +53,7 @@ namespace Global { event_service->initialize(); player_service->initialize(); time_Service->intialize(); + uiservice->initialize(); } // Updates the state of the graphic service. @@ -59,6 +62,7 @@ namespace Global { event_service->update(); player_service->update(); time_Service->update(); + uiservice->update(); } @@ -66,6 +70,7 @@ namespace Global { void ServiceLocator::render() { graphic_service->render(); // Render graphic service player_service->render(); + uiservice->render(); } // Returns a pointer to the currently set graphic service. @@ -84,5 +89,10 @@ namespace Global { return time_Service; } + UiService* ServiceLocator::getuiservice() { + + return uiservice; + } + } From 02ac2f66b5601cd440002940475ad86e68ce8b00 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 12:12:00 +0530 Subject: [PATCH 11/27] implemented the spirites and textures --- .../Headers/Graphic/GraphicService.h | 4 +- .../MainMenu/MainMenuUIController.cpp | 86 +++++++++++++++++++ .../UIService/MainMenu/MainMenuUIController.h | 48 +++++++++++ Space-Invaders/Headers/player/PlayerService.h | 8 ++ Space-Invaders/source/GraphicService.cpp | 2 +- Space-Invaders/source/ServiceLocator.cpp | 6 +- 6 files changed, 150 insertions(+), 4 deletions(-) diff --git a/Space-Invaders/Headers/Graphic/GraphicService.h b/Space-Invaders/Headers/Graphic/GraphicService.h index e013140cf..fc8491480 100644 --- a/Space-Invaders/Headers/Graphic/GraphicService.h +++ b/Space-Invaders/Headers/Graphic/GraphicService.h @@ -11,8 +11,8 @@ namespace Graphic { const std::string game_window_title = "Outscal Presents - Alien Invader"; - const int game_window_width = 800; - const int game_window_height = 600; + const int game_window_width = 1920; + const int game_window_height = 1080; const sf::Color window_color = sf::Color::Blue; diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp index 7435399f7..703cba2bc 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp @@ -19,6 +19,10 @@ namespace UI { void UI::MainMenu::MainMenuUIController::initialize() { game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializeBackgroundImage(); + initializeButtons(); + + } void UI::MainMenu::MainMenuUIController::update() @@ -27,6 +31,88 @@ namespace UI { void UI::MainMenu::MainMenuUIController::render() { + + + game_window->draw(background_sprite); + game_window->draw(play_button_sprite); + game_window->draw(instructions_button_sprite); + game_window->draw(quit_button_sprite); + + + + + } + void MainMenuUIController::initializeBackgroundImage() + { + if (background_texture.loadFromFile(background_texture_path)) + { //if it did then set the bg image and scale it + background_sprite.setTexture(background_texture); + scaleBackgroundImage(); + } + + } + void MainMenuUIController::scaleBackgroundImage() + { + + background_sprite.setScale( + static_cast(game_window->getSize().x) / background_sprite.getTexture()->getSize().x, + static_cast(game_window->getSize().y) / background_sprite.getTexture()->getSize().y + ); + + } + void MainMenuUIController::initializeButtons() + { + + // check if the tectures loaded + if (loadButtonTexturesFromFile()) + { + // order of function calls matter + setButtonSprites(); + scaleAllButttons(); + positionButtons(); + } + + } + bool MainMenuUIController::loadButtonTexturesFromFile() + { + return play_button_texture.loadFromFile(play_button_texture_path) && + instructions_button_texture.loadFromFile(instructions_button_texture_path) && + quit_button_texture.loadFromFile(quit_button_texture_path); + } + void MainMenuUIController::setButtonSprites() + { + + play_button_sprite.setTexture(play_button_texture); + instructions_button_sprite.setTexture(instructions_button_texture); + quit_button_sprite.setTexture(quit_button_texture); + + } + void MainMenuUIController::scaleAllButttons() + { + scaleButton(&play_button_sprite); + scaleButton(&instructions_button_sprite); + scaleButton(&quit_button_sprite); + + } + void MainMenuUIController::scaleButton(sf::Sprite* button_to_scale) + { + + button_to_scale->setScale( + button_width / button_to_scale->getTexture()->getSize().x, + button_height / button_to_scale->getTexture()->getSize().y + ); + + + + } + void MainMenuUIController::positionButtons() + { + + float x_position = (static_cast(game_window->getSize().x) / 2) - button_width / 2; + + play_button_sprite.setPosition({ x_position, 500.f }); + instructions_button_sprite.setPosition({ x_position, 700.f }); + quit_button_sprite.setPosition({ x_position, 900.f }); } } } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h index 91e7bff33..3b9a0ec50 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h @@ -4,16 +4,64 @@ namespace UI { namespace MainMenu + { + class MainMenuUIController { private: + const sf::String background_texture_path = "assets/textures/space_invaders_bg.png"; + const sf::String play_button_texture_path = "assets/textures/play_button.png"; + const sf::String instructions_button_texture_path = "assets/textures/instructions_button.png"; + const sf::String quit_button_texture_path = "assets/textures/quit_button.png"; + + + sf::Texture background_texture; + sf::Sprite background_sprite; + + sf::Texture play_button_texture; + sf::Sprite play_button_sprite; + + sf::Texture instructions_button_texture; + sf::Sprite instructions_button_sprite; + + sf::Texture quit_button_texture; + sf::Sprite quit_button_sprite; + + + + + + + + const float button_height = 400.f; + const float button_width = 140.f; + + + void initializeBackgroundImage(); + void scaleBackgroundImage(); + + // however, we have 3 buttons so it's better to create a seperate function to check if they are loaded + void initializeButtons(); + bool loadButtonTexturesFromFile(); + void setButtonSprites(); + + void scaleAllButttons(); + void scaleButton(sf::Sprite* button_to_scale); + void positionButtons(); + + + + + sf::RenderWindow* game_window; public: MainMenuUIController(); + + void initialize(); void update(); void render(); diff --git a/Space-Invaders/Headers/player/PlayerService.h b/Space-Invaders/Headers/player/PlayerService.h index 25d03fecf..3819a008c 100644 --- a/Space-Invaders/Headers/player/PlayerService.h +++ b/Space-Invaders/Headers/player/PlayerService.h @@ -16,6 +16,7 @@ namespace player { sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); float movement_speed = 5.0f; int player_score = 0; + const sf::Vector2f initial_player_position = sf::Vector2f(950.f, 950.f); const sf::String player_texture_path = "assets/textures/player_ship.png"; @@ -32,6 +33,13 @@ namespace player { public: + + const sf::Vector2f left_most_position = sf::Vector2f(50.f, 950.f); + const sf::Vector2f right_most_position = sf::Vector2f(1800.f, 950.f); + + const float player_movement_speed = 350.0f; + + PlayerService(); ~PlayerService(); diff --git a/Space-Invaders/source/GraphicService.cpp b/Space-Invaders/source/GraphicService.cpp index 5b690f231..f435a8a0b 100644 --- a/Space-Invaders/source/GraphicService.cpp +++ b/Space-Invaders/source/GraphicService.cpp @@ -28,7 +28,7 @@ namespace Graphic { // Creates a new SFML RenderWindow object with specified video mode and title. sf::RenderWindow* GraphicService::createGameWindow() { setVideoMode(); // Sets up the video mode for the window - return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object + return new sf::RenderWindow(*video_mode, game_window_title, sf::Style::Fullscreen); // Creates and returns a new RenderWindow object } // Sets up the video mode for the game window using specified dimensions and system's color depth. diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index abe815cf3..c1f78bb6f 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -68,9 +68,13 @@ namespace Global { // Renders using the graphic service. void ServiceLocator::render() { + uiservice->render(); + player_service->render(); graphic_service->render(); // Render graphic service player_service->render(); - uiservice->render(); + + + } // Returns a pointer to the currently set graphic service. From 542c41539120014990a753f9603e8701298e2174 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 13:59:34 +0530 Subject: [PATCH 12/27] added the mouse inputs --- Space-Invaders/Headers/EVENT/EventService.h | 3 ++- .../MainMenu/MainMenuUIController.cpp | 25 +++++++++++++++++++ .../UIService/MainMenu/MainMenuUIController.h | 3 +++ .../Headers/UIService/UIService.cpp | 13 ++++++++++ Space-Invaders/Headers/main/GameService.h | 2 ++ Space-Invaders/source/EventService.cpp | 10 ++++++++ Space-Invaders/source/GameService.cpp | 7 ++++++ 7 files changed, 62 insertions(+), 1 deletion(-) diff --git a/Space-Invaders/Headers/EVENT/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h index 93f993fd5..1a82c69da 100644 --- a/Space-Invaders/Headers/EVENT/EventService.h +++ b/Space-Invaders/Headers/EVENT/EventService.h @@ -26,6 +26,7 @@ namespace event { bool isKeyboardEvent(); bool pressedLeftKey(); bool pressedRightKey(); - + bool leftmousebutton(); + bool rightmousebutton(); }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp index 703cba2bc..e759716c4 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp @@ -27,6 +27,7 @@ namespace UI { void UI::MainMenu::MainMenuUIController::update() { + processButtonInteractions(); } void UI::MainMenu::MainMenuUIController::render() @@ -114,5 +115,29 @@ namespace UI { instructions_button_sprite.setPosition({ x_position, 700.f }); quit_button_sprite.setPosition({ x_position, 900.f }); } + void MainMenuUIController::processButtonInteractions() + { + + sf::Vector2f mouse_position = sf::Vector2f(sf::Mouse::getPosition(*game_window)); + + if (clickedButton(&play_button_sprite, mouse_position)) + { + GameService::setGameState(GameState::GAMEPLAY); + } + + if (clickedButton(&instructions_button_sprite, mouse_position)) + { + printf("Clicked Instruction Button \\n"); + } + + if (clickedButton(&quit_button_sprite, mouse_position)) + game_window->close(); + + } + bool MainMenuUIController::clickedButton(sf::Sprite* button_sprite, sf::Vector2f mouse_position) + { + EventService* event_service = ServiceLocator::getInstance()->getEventService(); + return event_service->leftmousebutton() && button_sprite->getGlobalBounds().contains(mouse_position); + } } } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h index 3b9a0ec50..19369eda7 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h @@ -51,6 +51,9 @@ namespace UI void scaleButton(sf::Sprite* button_to_scale); void positionButtons(); + void processButtonInteractions(); + bool clickedButton(sf::Sprite*, sf::Vector2f); + diff --git a/Space-Invaders/Headers/UIService/UIService.cpp b/Space-Invaders/Headers/UIService/UIService.cpp index ed09f6af0..33781fe3d 100644 --- a/Space-Invaders/Headers/UIService/UIService.cpp +++ b/Space-Invaders/Headers/UIService/UIService.cpp @@ -40,8 +40,21 @@ void UI::UiService::initialize() void UI::UiService::update() { + + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->update(); + break; + } } void UI::UiService::render() { + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->render(); + break; + } } diff --git a/Space-Invaders/Headers/main/GameService.h b/Space-Invaders/Headers/main/GameService.h index a6864e698..58eb6ce2c 100644 --- a/Space-Invaders/Headers/main/GameService.h +++ b/Space-Invaders/Headers/main/GameService.h @@ -38,6 +38,8 @@ namespace Main { bool isRunning();// Checks if the game is currently running. static void setGameState(GameState new_state); static GameState getGameState(); + + void showmainmenu(); }; diff --git a/Space-Invaders/source/EventService.cpp b/Space-Invaders/source/EventService.cpp index 729ac8cd0..b1e9156fc 100644 --- a/Space-Invaders/source/EventService.cpp +++ b/Space-Invaders/source/EventService.cpp @@ -58,4 +58,14 @@ namespace event { bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } + bool EventService::leftmousebutton() + { + return game_event.type == sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Left; + } + + bool EventService::rightmousebutton() + { + return game_event.type==sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Right; + } + } \ No newline at end of file diff --git a/Space-Invaders/source/GameService.cpp b/Space-Invaders/source/GameService.cpp index 76641bac5..390341aec 100644 --- a/Space-Invaders/source/GameService.cpp +++ b/Space-Invaders/source/GameService.cpp @@ -35,6 +35,7 @@ namespace Main { { service_locator->initialize(); initializeVariables(); + showmainmenu(); } void GameService::initializeVariables() @@ -73,6 +74,12 @@ namespace Main { void GameService::setGameState(GameState new_state) { current_state = new_state; } GameState GameService::getGameState() { return current_state; } + void GameService::showmainmenu() + { + setGameState(GameState::MAIN_MENU); + + + } } From 845c5053a5e60ed8b8cd99ac2e1db881b36cc7b9 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 14:09:24 +0530 Subject: [PATCH 13/27] added enum class for the checking of the press events --- Space-Invaders/Headers/EVENT/EventService.h | 24 ++++++++ Space-Invaders/source/EventService.cpp | 67 ++++++++++++++++++++- Space-Invaders/source/PlayerController.cpp | 16 +++-- 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/Space-Invaders/Headers/EVENT/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h index 1a82c69da..3591e0e4c 100644 --- a/Space-Invaders/Headers/EVENT/EventService.h +++ b/Space-Invaders/Headers/EVENT/EventService.h @@ -2,6 +2,14 @@ #include #include namespace event { + + enum class ButtonState + { + PRESSED, + HELD, + RELEASED, + }; + class EventService { private: @@ -12,7 +20,19 @@ namespace event { bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. bool hasQuitGame(); //for our new 'ESC' condition + + ButtonState left_mouse_button_state; + ButtonState right_mouse_button_state; + ButtonState left_arrow_button_state; + ButtonState right_arrow_button_state; + ButtonState A_button_state; + ButtonState D_button_state; + //....some other code + void updateMouseButtonsState(ButtonState& current_button_state, sf::Mouse::Button mouse_button); + void updateKeyboardButtonsState(ButtonState& current_button_state, sf::Keyboard::Key keyboard_button); + + public: @@ -28,5 +48,9 @@ namespace event { bool pressedRightKey(); bool leftmousebutton(); bool rightmousebutton(); + + + bool pressedAKey(); + bool pressedDKey(); }; } \ No newline at end of file diff --git a/Space-Invaders/source/EventService.cpp b/Space-Invaders/source/EventService.cpp index b1e9156fc..91ac93fae 100644 --- a/Space-Invaders/source/EventService.cpp +++ b/Space-Invaders/source/EventService.cpp @@ -13,6 +13,52 @@ namespace event { using namespace Graphic; using namespace event; + void EventService::updateMouseButtonsState(ButtonState& current_button_state, sf::Mouse::Button mouse_button) + { + + if (sf::Mouse::isButtonPressed(mouse_button)) + { + switch (current_button_state) + { + case ButtonState::RELEASED: + current_button_state = ButtonState::PRESSED; + break; + case ButtonState::PRESSED: + current_button_state = ButtonState::HELD; + break; + } + } + else + { + current_button_state = ButtonState::RELEASED; + } + + + } + + void EventService::updateKeyboardButtonsState(ButtonState& current_button_state, sf::Keyboard::Key keyboard_button) + { + + + if (sf::Keyboard::isKeyPressed(keyboard_button)) + { + switch (current_button_state) + { + case ButtonState::RELEASED: + current_button_state = ButtonState::PRESSED; + break; + case ButtonState::PRESSED: + current_button_state = ButtonState::HELD; + break; + } + } + else + { + current_button_state = ButtonState::RELEASED; + } + + } + EventService::EventService() { game_window = nullptr; } EventService::~EventService() = default; //calls the default destructor @@ -24,7 +70,16 @@ namespace event { void EventService::update() { - //for later + + + + updateMouseButtonsState(left_mouse_button_state, sf::Mouse::Left); + updateMouseButtonsState(right_mouse_button_state, sf::Mouse::Right); + updateKeyboardButtonsState(left_arrow_button_state, sf::Keyboard::Left); + updateKeyboardButtonsState(right_arrow_button_state, sf::Keyboard::Right); + updateKeyboardButtonsState(A_button_state, sf::Keyboard::A); + updateKeyboardButtonsState(D_button_state, sf::Keyboard::D); + } void EventService::processEvents() @@ -68,4 +123,14 @@ namespace event { return game_event.type==sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Right; } + bool EventService::pressedAKey() + { + return false; + } + + bool EventService::pressedDKey() + { + return false; + } + } \ No newline at end of file diff --git a/Space-Invaders/source/PlayerController.cpp b/Space-Invaders/source/PlayerController.cpp index 05d122b62..e27a1ee91 100644 --- a/Space-Invaders/source/PlayerController.cpp +++ b/Space-Invaders/source/PlayerController.cpp @@ -8,13 +8,21 @@ namespace player{ using namespace Global; void PlayerController::processinput() { + EventService* event_service = ServiceLocator::getInstance()->getEventService(); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - processmoveright(); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + if (event_service->pressedLeftKey() || event_service->pressedAKey()) + { processmoveleft(); } + + if (event_service->pressedRightKey() || event_service->pressedDKey()) + { + processmoveright(); + } + + + + } void PlayerController::processmoveleft() From ec92637619add050f45770e80c7c61d90e2c08e8 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 19:49:21 +0530 Subject: [PATCH 14/27] again created the ui service and ui managnemnt kit --- .../Headers/Global/ServiceLocator.h | 8 +- .../MainMenu/MainMenuUIController.cpp | 143 ------------------ .../Headers/UIService/UIService.cpp | 60 -------- Space-Invaders/Headers/UIService/UIService.h | 20 --- Space-Invaders/Headers/player/PlayerService.h | 2 + Space-Invaders/Space-Invaders.vcxproj | 8 +- .../UI/MainMenu/MainMenuUIController.cpp | 109 +++++++++++++ .../MainMenu/MainMenuUIController.h | 49 +++--- Space-Invaders/UI/UISERVICE.cpp | 44 ++++++ Space-Invaders/UI/UISERVICE.h | 19 +++ Space-Invaders/source/PlayerService.cpp | 5 +- Space-Invaders/source/ServiceLocator.cpp | 29 ++-- 12 files changed, 222 insertions(+), 274 deletions(-) delete mode 100644 Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp delete mode 100644 Space-Invaders/Headers/UIService/UIService.cpp delete mode 100644 Space-Invaders/Headers/UIService/UIService.h create mode 100644 Space-Invaders/UI/MainMenu/MainMenuUIController.cpp rename Space-Invaders/{Headers/UIService => UI}/MainMenu/MainMenuUIController.h (63%) create mode 100644 Space-Invaders/UI/UISERVICE.cpp create mode 100644 Space-Invaders/UI/UISERVICE.h diff --git a/Space-Invaders/Headers/Global/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h index f3c9ec9be..a1f61be32 100644 --- a/Space-Invaders/Headers/Global/ServiceLocator.h +++ b/Space-Invaders/Headers/Global/ServiceLocator.h @@ -4,8 +4,7 @@ #include"../EVENT/EventService.h" #include"../player/PlayerService.h" #include"../TIME/TimeService .h" -#include"../../Headers/UIService/UIService.h" - +#include"../../UI/UISERVICE.h" namespace Global { using namespace player; using namespace Graphic; @@ -20,7 +19,8 @@ namespace Global { EventService* event_service; PlayerService* player_service; TimeService* time_Service; - UiService* uiservice; + UiService* ui_service; + // Private Constructor and Destructor: ServiceLocator(); // Constructor for initializing the ServiceLocator. @@ -42,6 +42,6 @@ namespace Global { EventService* getEventService(); PlayerService* getplayerservice(); TimeService* gettimeservice(); - UiService* getuiservice(); + UiService* getUiservice(); }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp deleted file mode 100644 index e759716c4..000000000 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp +++ /dev/null @@ -1,143 +0,0 @@ -#include "MainMenuUIController.h" -#include"../../Headers/main/GameService.h" -#include"../../Headers/Global/ServiceLocator.h" -#include"../../Headers/Graphic/GraphicService.h" - -namespace UI { - namespace MainMenu { - - using namespace Global; - using namespace Main; - using namespace Graphic; - using namespace event; - - UI::MainMenu::MainMenuUIController::MainMenuUIController() - { - game_window = nullptr; - } - - void UI::MainMenu::MainMenuUIController::initialize() - { - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializeBackgroundImage(); - initializeButtons(); - - - } - - void UI::MainMenu::MainMenuUIController::update() - { - processButtonInteractions(); - } - - void UI::MainMenu::MainMenuUIController::render() - { - - - game_window->draw(background_sprite); - game_window->draw(play_button_sprite); - game_window->draw(instructions_button_sprite); - game_window->draw(quit_button_sprite); - - - - - } - void MainMenuUIController::initializeBackgroundImage() - { - if (background_texture.loadFromFile(background_texture_path)) - { //if it did then set the bg image and scale it - background_sprite.setTexture(background_texture); - scaleBackgroundImage(); - } - - } - void MainMenuUIController::scaleBackgroundImage() - { - - background_sprite.setScale( - static_cast(game_window->getSize().x) / background_sprite.getTexture()->getSize().x, - static_cast(game_window->getSize().y) / background_sprite.getTexture()->getSize().y - ); - - } - void MainMenuUIController::initializeButtons() - { - - // check if the tectures loaded - if (loadButtonTexturesFromFile()) - { - // order of function calls matter - setButtonSprites(); - scaleAllButttons(); - positionButtons(); - } - - } - bool MainMenuUIController::loadButtonTexturesFromFile() - { - return play_button_texture.loadFromFile(play_button_texture_path) && - instructions_button_texture.loadFromFile(instructions_button_texture_path) && - quit_button_texture.loadFromFile(quit_button_texture_path); - } - void MainMenuUIController::setButtonSprites() - { - - play_button_sprite.setTexture(play_button_texture); - instructions_button_sprite.setTexture(instructions_button_texture); - quit_button_sprite.setTexture(quit_button_texture); - - } - void MainMenuUIController::scaleAllButttons() - { - scaleButton(&play_button_sprite); - scaleButton(&instructions_button_sprite); - scaleButton(&quit_button_sprite); - - } - void MainMenuUIController::scaleButton(sf::Sprite* button_to_scale) - { - - button_to_scale->setScale( - button_width / button_to_scale->getTexture()->getSize().x, - button_height / button_to_scale->getTexture()->getSize().y - ); - - - - } - void MainMenuUIController::positionButtons() - { - - float x_position = (static_cast(game_window->getSize().x) / 2) - button_width / 2; - - play_button_sprite.setPosition({ x_position, 500.f }); - instructions_button_sprite.setPosition({ x_position, 700.f }); - quit_button_sprite.setPosition({ x_position, 900.f }); - } - void MainMenuUIController::processButtonInteractions() - { - - sf::Vector2f mouse_position = sf::Vector2f(sf::Mouse::getPosition(*game_window)); - - if (clickedButton(&play_button_sprite, mouse_position)) - { - GameService::setGameState(GameState::GAMEPLAY); - } - - if (clickedButton(&instructions_button_sprite, mouse_position)) - { - printf("Clicked Instruction Button \\n"); - } - - if (clickedButton(&quit_button_sprite, mouse_position)) - game_window->close(); - - } - bool MainMenuUIController::clickedButton(sf::Sprite* button_sprite, sf::Vector2f mouse_position) - { - EventService* event_service = ServiceLocator::getInstance()->getEventService(); - return event_service->leftmousebutton() && button_sprite->getGlobalBounds().contains(mouse_position); - } - } -} \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/UIService.cpp b/Space-Invaders/Headers/UIService/UIService.cpp deleted file mode 100644 index 33781fe3d..000000000 --- a/Space-Invaders/Headers/UIService/UIService.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "UIService.h" -#include"../main/GameService.h" - - - -using namespace Main; -using namespace MainMenu; - - -void UI::UiService::createControllers() -{ - main_menu_controller = new MainMenuUIController(); -} - -void UI::UiService::initializeControllers() -{ - main_menu_controller->initialize(); -} - -void UI::UiService::destroy() -{ - delete(main_menu_controller); -} - -UI::UiService::UiService() -{ - main_menu_controller = nullptr; - createControllers(); -} - -UI::UiService::~UiService() -{ - destroy(); -} - -void UI::UiService::initialize() -{ - initializeControllers(); -} - -void UI::UiService::update() -{ - - switch (GameService::getGameState()) - { - case GameState::MAIN_MENU: - return main_menu_controller->update(); - break; - } -} - -void UI::UiService::render() -{ - switch (GameService::getGameState()) - { - case GameState::MAIN_MENU: - return main_menu_controller->render(); - break; - } -} diff --git a/Space-Invaders/Headers/UIService/UIService.h b/Space-Invaders/Headers/UIService/UIService.h deleted file mode 100644 index 65de033b8..000000000 --- a/Space-Invaders/Headers/UIService/UIService.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include"../../Headers/UIService/MainMenu/MainMenuUIController.h" -namespace UI{ - class UiService { - private: - MainMenu::MainMenuUIController* main_menu_controller; - void createControllers(); - void initializeControllers(); - void destroy(); - - public: - UiService(); - ~UiService(); - - void initialize(); - void update(); - void render(); - - }; -} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerService.h b/Space-Invaders/Headers/player/PlayerService.h index 3819a008c..e8620c5f4 100644 --- a/Space-Invaders/Headers/player/PlayerService.h +++ b/Space-Invaders/Headers/player/PlayerService.h @@ -8,6 +8,8 @@ namespace player { class PlayerService { + + private: diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 67c83d458..6d436d8bf 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,8 +132,6 @@ - - @@ -144,6 +142,8 @@ + + @@ -155,8 +155,8 @@ - - + + diff --git a/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp new file mode 100644 index 000000000..432ca1171 --- /dev/null +++ b/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp @@ -0,0 +1,109 @@ +#include "MainMenuUIController.h" +#include"../../Headers/main/GameService.h" +#include"../../Headers/Global/ServiceLocator.h" +#include"../../Headers/Graphic/GraphicService.h" +namespace UI { + + namespace MainMenu { + + using namespace Global; + using namespace Main; + using namespace Graphic; + using namespace event; + + void MainMenuUI::initializeBackgroundImage() + { + if (background_texture.loadFromFile(background_texture_path)) + { + background_sprite.setTexture(background_texture); + scaleBackgroundImage(); + } + } + + void MainMenuUI::scaleBackgroundImage() + { + background_sprite.setScale( + + static_cast(game_window->getSize().x)/ + background_sprite.getTexture()->getSize().x, + static_cast(game_window->getSize().y)/ + background_sprite.getTexture()->getSize().y + + ); + } + + void MainMenuUI::initializeButtons() + { + } + + bool MainMenuUI::loadButtonTexturesFromFile() + { + return play_button_texture.loadFromFile(play_button_texture_path) + && instructions_button_texture.loadFromFile(instructions_button_texture_path) + && quit_button_texture.loadFromFile(quit_button_texture_path); + } + + void MainMenuUI::setButtonSprites() + { + play_button_sprite.setTexture(play_button_texture); + instructions_button_sprite.setTexture(instructions_button_texture); + quit_button_sprite.setTexture(quit_button_texture); + } + + void MainMenuUI::scaleAllButttons() + { + scaleButton(&play_button_sprite); + scaleButton(&instructions_button_sprite); + scaleButton(&quit_button_sprite); + } + + void MainMenuUI::scaleButton(sf::Sprite* button_to_scale) + { + button_to_scale->setScale( + button_width / button_to_scale->getTexture()->getSize().x, + button_height / button_to_scale->getTexture()->getSize().y + + ); + } + + void MainMenuUI::positionButtons() + { + float xposition = (static_cast(game_window->getSize().x) / 2) - button_width / 2; + play_button_sprite.setPosition(xposition, 540); + instructions_button_sprite.setPosition(xposition, 560); + quit_button_sprite.setPosition(xposition, 580); + } + + UI::MainMenu::MainMenuUI::MainMenuUI() + { + game_window = nullptr; + } + + UI::MainMenu::MainMenuUI::~MainMenuUI() + { + + } + + void UI::MainMenu::MainMenuUI::intialize() + { + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializeButtons(); + initializeBackgroundImage(); + + } + + void UI::MainMenu::MainMenuUI::update() + { + + } + + void UI::MainMenu::MainMenuUI::render() + { + + game_window->draw(background_sprite); + game_window->draw(play_button_sprite); + game_window->draw(instructions_button_sprite); + game_window->draw(quit_button_sprite); + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h b/Space-Invaders/UI/MainMenu/MainMenuUIController.h similarity index 63% rename from Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h rename to Space-Invaders/UI/MainMenu/MainMenuUIController.h index 19369eda7..c842e1d12 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/UI/MainMenu/MainMenuUIController.h @@ -1,22 +1,19 @@ #pragma once -#include +#include +namespace UI { + namespace MainMenu { + class MainMenuUI { -namespace UI -{ - namespace MainMenu - - { - - class MainMenuUIController - { private: + sf::RenderWindow* game_window; + const float button_height = 100.f; + const float button_width = 100.f; const sf::String background_texture_path = "assets/textures/space_invaders_bg.png"; const sf::String play_button_texture_path = "assets/textures/play_button.png"; - const sf::String instructions_button_texture_path = "assets/textures/instructions_button.png"; + const sf::String instructions_button_texture_path = "assets/textures/instructions_button.png"; const sf::String quit_button_texture_path = "assets/textures/quit_button.png"; - sf::Texture background_texture; sf::Sprite background_sprite; @@ -29,20 +26,10 @@ namespace UI sf::Texture quit_button_texture; sf::Sprite quit_button_sprite; - - - - - - - const float button_height = 400.f; - const float button_width = 140.f; - - void initializeBackgroundImage(); void scaleBackgroundImage(); - // however, we have 3 buttons so it's better to create a seperate function to check if they are loaded + void initializeButtons(); bool loadButtonTexturesFromFile(); void setButtonSprites(); @@ -51,24 +38,24 @@ namespace UI void scaleButton(sf::Sprite* button_to_scale); void positionButtons(); - void processButtonInteractions(); - bool clickedButton(sf::Sprite*, sf::Vector2f); - + public: + MainMenuUI(); + ~MainMenuUI(); - sf::RenderWindow* game_window; - public: - MainMenuUIController(); - void initialize(); + void intialize(); void update(); void render(); - }; - } + + + +}; +} } \ No newline at end of file diff --git a/Space-Invaders/UI/UISERVICE.cpp b/Space-Invaders/UI/UISERVICE.cpp new file mode 100644 index 000000000..49b0e2a69 --- /dev/null +++ b/Space-Invaders/UI/UISERVICE.cpp @@ -0,0 +1,44 @@ +#include "UISERVICE.h" +#include"../../Headers/main/GameService.h" +namespace UI { + using namespace Main; + using namespace MainMenu; + void UI::UiService::createControllers() + { + main_menu_controller = new MainMenuUI(); + } + + void UI::UiService::intializecontrollers() + { + main_menu_controller->intialize(); + } + + void UI::UiService::destroycontrollers() + { + delete(main_menu_controller); + } + + UI::UiService::UiService() + { + main_menu_controller = nullptr; + createControllers(); + } + + UI::UiService::~UiService() + { + destroycontrollers(); + } + + void UI::UiService::intialize() + { + intializecontrollers(); + } + + void UI::UiService::update() + { + } + + void UI::UiService::render() + { + } +} \ No newline at end of file diff --git a/Space-Invaders/UI/UISERVICE.h b/Space-Invaders/UI/UISERVICE.h new file mode 100644 index 000000000..aefc88036 --- /dev/null +++ b/Space-Invaders/UI/UISERVICE.h @@ -0,0 +1,19 @@ +#pragma once +#include"../../UI/MainMenu/MainMenuUIController.h" +namespace UI { + class UiService { + + private: + void createControllers(); + void intializecontrollers(); + void destroycontrollers(); + MainMenu::MainMenuUI* main_menu_controller; + public: + UiService(); + ~UiService(); + void intialize(); + void update(); + void render(); + }; + +} \ No newline at end of file diff --git a/Space-Invaders/source/PlayerService.cpp b/Space-Invaders/source/PlayerService.cpp index 8729355e4..6ccda2e3a 100644 --- a/Space-Invaders/source/PlayerService.cpp +++ b/Space-Invaders/source/PlayerService.cpp @@ -9,6 +9,7 @@ namespace player { using namespace event; using namespace Global; + void PlayerService::initializePlayerSprite() { if (player_texture.loadFromFile(player_texture_path)) { @@ -57,9 +58,10 @@ namespace player { } void PlayerService::render() + { player_controller->render(); - game_window->draw(player_sprite); + } void PlayerService::moveLef() @@ -85,4 +87,5 @@ namespace player { { return position; } + } \ No newline at end of file diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index c1f78bb6f..3ad706fe7 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -13,7 +13,7 @@ namespace Global { event_service = nullptr; player_service = nullptr; time_Service = nullptr; - uiservice = nullptr; + ui_service = nullptr; createServices(); // Call createServices to instantiate services } @@ -29,7 +29,7 @@ namespace Global { player_service = new PlayerService(); time_Service = new TimeService(); - uiservice = new UiService(); + ui_service = new UiService(); } // Deletes allocated services to prevent memory leaks, specifically the graphic service. @@ -38,12 +38,13 @@ namespace Global { delete(event_service); delete(player_service); delete(time_Service); - delete(uiservice); + delete(ui_service); + } // Returns a pointer to ServiceLocator. ServiceLocator* ServiceLocator::getInstance() { - static ServiceLocator instance; // we will discuss what 'static' means at a later time. + static ServiceLocator instance; return &instance; // Return address of the instance } @@ -53,7 +54,7 @@ namespace Global { event_service->initialize(); player_service->initialize(); time_Service->intialize(); - uiservice->initialize(); + ui_service->intialize(); } // Updates the state of the graphic service. @@ -62,17 +63,21 @@ namespace Global { event_service->update(); player_service->update(); time_Service->update(); - uiservice->update(); + ui_service->update(); } // Renders using the graphic service. void ServiceLocator::render() { - uiservice->render(); + + player_service->render(); graphic_service->render(); // Render graphic service + + // Render graphic service + player_service->render(); - + ui_service->render(); } @@ -93,10 +98,12 @@ namespace Global { return time_Service; } - UiService* ServiceLocator::getuiservice() { - - return uiservice; + UiService* ServiceLocator::getUiservice() + { + return ui_service; } + + } From 55dbc087c6e478aa9939eda4b8db20fd5ced0b47 Mon Sep 17 00:00:00 2001 From: Tarun Date: Mon, 5 Aug 2024 11:00:13 +0530 Subject: [PATCH 15/27] Just corrected the corrupt files in the game --- Space-Invaders/Headers/EVENT/EventService.h | 4 ++ Space-Invaders/Headers/main/GameService.h | 3 ++ Space-Invaders/Headers/player/PlayerModel.h | 8 ++-- .../UI/MainMenu/MainMenuUIController.cpp | 42 +++++++++++++++---- .../UI/MainMenu/MainMenuUIController.h | 7 ++-- Space-Invaders/UI/UISERVICE.cpp | 21 ++++++++++ Space-Invaders/source/EventService.cpp | 18 ++++++-- Space-Invaders/source/ServiceLocator.cpp | 5 ++- 8 files changed, 87 insertions(+), 21 deletions(-) diff --git a/Space-Invaders/Headers/EVENT/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h index 3591e0e4c..7da3d125e 100644 --- a/Space-Invaders/Headers/EVENT/EventService.h +++ b/Space-Invaders/Headers/EVENT/EventService.h @@ -10,6 +10,7 @@ namespace event { RELEASED, }; + class EventService { private: @@ -49,8 +50,11 @@ namespace event { bool leftmousebutton(); bool rightmousebutton(); + bool pressedLeftMouseButton(); + bool pressedRightMouseButton(); bool pressedAKey(); bool pressedDKey(); + }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/main/GameService.h b/Space-Invaders/Headers/main/GameService.h index 58eb6ce2c..8850e9ade 100644 --- a/Space-Invaders/Headers/main/GameService.h +++ b/Space-Invaders/Headers/main/GameService.h @@ -9,6 +9,9 @@ enum class GameState //create the enum GAMEPLAY, }; + + + namespace Main { using namespace Global; diff --git a/Space-Invaders/Headers/player/PlayerModel.h b/Space-Invaders/Headers/player/PlayerModel.h index 90d116e84..eb54839b4 100644 --- a/Space-Invaders/Headers/player/PlayerModel.h +++ b/Space-Invaders/Headers/player/PlayerModel.h @@ -19,7 +19,7 @@ namespace player { { private: - const sf::Vector2f initial_player_position = sf::Vector2f(500.f, 500.f); //new var + const sf::Vector2f initial_player_position = sf::Vector2f(950.f, 950.f); //new var sf::Vector2f player_position; //new var PlayerState player_state; PlayerModel* model; @@ -28,10 +28,10 @@ namespace player { 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 sf::Vector2f left_most_position = sf::Vector2f(50.f, 950.f); + const sf::Vector2f right_most_position = sf::Vector2f(1800.f, 950.f); - const float player_movement_speed = 200.0f; + const float player_movement_speed = 350.0f; PlayerModel(); ~PlayerModel(); diff --git a/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp index 432ca1171..e7fbf934c 100644 --- a/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp @@ -24,16 +24,20 @@ namespace UI { { background_sprite.setScale( - static_cast(game_window->getSize().x)/ - background_sprite.getTexture()->getSize().x, - static_cast(game_window->getSize().y)/ - background_sprite.getTexture()->getSize().y - + static_cast(game_window->getSize().x) / background_sprite.getTexture()->getSize().x, + static_cast(game_window->getSize().y) / background_sprite.getTexture()->getSize().y ); } void MainMenuUI::initializeButtons() { + if (loadButtonTexturesFromFile()) + { + // order of function calls matter + setButtonSprites(); + scaleAllButttons(); + positionButtons(); + } } bool MainMenuUI::loadButtonTexturesFromFile() @@ -69,9 +73,9 @@ namespace UI { void MainMenuUI::positionButtons() { float xposition = (static_cast(game_window->getSize().x) / 2) - button_width / 2; - play_button_sprite.setPosition(xposition, 540); - instructions_button_sprite.setPosition(xposition, 560); - quit_button_sprite.setPosition(xposition, 580); + play_button_sprite.setPosition(xposition, 500); + instructions_button_sprite.setPosition(xposition,700); + quit_button_sprite.setPosition(xposition, 900); } UI::MainMenu::MainMenuUI::MainMenuUI() @@ -94,6 +98,7 @@ namespace UI { void UI::MainMenu::MainMenuUI::update() { + processButtonInteractions(); } @@ -105,5 +110,26 @@ namespace UI { game_window->draw(instructions_button_sprite); game_window->draw(quit_button_sprite); } + void MainMenuUI::processButtonInteractions() + { + sf::Vector2f mouse_position = sf::Vector2f(sf::Mouse::getPosition(*game_window)); + if (clickedButton(&play_button_sprite, mouse_position)) { + GameService::setGameState(GameState::GAMEPLAY); + } + + if (clickedButton(&instructions_button_sprite, mouse_position)) + { + printf("Clicked Instruction Button \\n"); + } + if (clickedButton(&quit_button_sprite, mouse_position)) + game_window->close(); + + } + bool MainMenuUI::clickedButton(sf::Sprite* button_sprite, sf::Vector2f mouse_position) + { + + EventService* event_service = ServiceLocator::getInstance()->getEventService(); + return event_service->pressedLeftMouseButton() && button_sprite->getGlobalBounds().contains(mouse_position); + } } } \ No newline at end of file diff --git a/Space-Invaders/UI/MainMenu/MainMenuUIController.h b/Space-Invaders/UI/MainMenu/MainMenuUIController.h index c842e1d12..3ed4df406 100644 --- a/Space-Invaders/UI/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/UI/MainMenu/MainMenuUIController.h @@ -6,8 +6,8 @@ namespace UI { private: sf::RenderWindow* game_window; - const float button_height = 100.f; - const float button_width = 100.f; + const float button_height = 200.f; + const float button_width = 200.f; const sf::String background_texture_path = "assets/textures/space_invaders_bg.png"; const sf::String play_button_texture_path = "assets/textures/play_button.png"; @@ -54,7 +54,8 @@ namespace UI { void render(); - + void processButtonInteractions(); + bool clickedButton(sf::Sprite* button_sprite, sf::Vector2f mouse_position); }; } diff --git a/Space-Invaders/UI/UISERVICE.cpp b/Space-Invaders/UI/UISERVICE.cpp index 49b0e2a69..158fc78c2 100644 --- a/Space-Invaders/UI/UISERVICE.cpp +++ b/Space-Invaders/UI/UISERVICE.cpp @@ -31,14 +31,35 @@ namespace UI { void UI::UiService::intialize() { + + + + main_menu_controller->intialize(); intializecontrollers(); + + + } void UI::UiService::update() { + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->update(); + break; + } } void UI::UiService::render() { + + + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->render(); + break; + } } } \ No newline at end of file diff --git a/Space-Invaders/source/EventService.cpp b/Space-Invaders/source/EventService.cpp index 91ac93fae..70f1de3f7 100644 --- a/Space-Invaders/source/EventService.cpp +++ b/Space-Invaders/source/EventService.cpp @@ -123,14 +123,24 @@ namespace event { return game_event.type==sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Right; } - bool EventService::pressedAKey() + bool EventService::pressedLeftMouseButton() { - return false; + return game_event.type == sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Left; } + bool EventService::pressedRightMouseButton() + { + return game_event.type == sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Right; + } + bool EventService::pressedAKey() + { + return A_button_state == ButtonState::HELD; + } bool EventService::pressedDKey() { - return false; + return D_button_state == ButtonState::HELD; } + } + + -} \ No newline at end of file diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index 3ad706fe7..9e21dc4c3 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -71,13 +71,14 @@ namespace Global { void ServiceLocator::render() { - player_service->render(); + graphic_service->render(); // Render graphic service // Render graphic service - player_service->render(); ui_service->render(); + + } From c21e01deaba1e059b8430adb596ce69b96c3f1c1 Mon Sep 17 00:00:00 2001 From: Tarun Date: Mon, 5 Aug 2024 20:44:14 +0530 Subject: [PATCH 16/27] added the texture and spirite --- Space-Invaders/Enemy/EnemyController.cpp | 33 +++++++++++ Space-Invaders/Enemy/EnemyController.h | 23 ++++++++ Space-Invaders/Enemy/EnemyModel.cpp | 39 +++++++++++++ Space-Invaders/Enemy/EnemyModel.h | 23 ++++++++ Space-Invaders/Enemy/EnemyService.cpp | 55 +++++++++++++++++++ Space-Invaders/Enemy/EnemyView.cpp | 42 ++++++++++++++ Space-Invaders/Enemy/EnemyView.h | 36 ++++++++++++ Space-Invaders/Enemy/Enemyservice.h | 19 +++++++ .../Headers/Global/ServiceLocator.h | 6 +- Space-Invaders/Headers/main/GameService.h | 1 + Space-Invaders/Space-Invaders.vcxproj | 8 +++ Space-Invaders/Space-Invaders.vcxproj.filters | 36 ++++++++++++ Space-Invaders/source/PlayerService.cpp | 3 +- Space-Invaders/source/PlayerView.cpp | 5 ++ Space-Invaders/source/ServiceLocator.cpp | 42 +++++++++++--- 15 files changed, 360 insertions(+), 11 deletions(-) create mode 100644 Space-Invaders/Enemy/EnemyController.cpp create mode 100644 Space-Invaders/Enemy/EnemyController.h create mode 100644 Space-Invaders/Enemy/EnemyModel.cpp create mode 100644 Space-Invaders/Enemy/EnemyModel.h create mode 100644 Space-Invaders/Enemy/EnemyService.cpp create mode 100644 Space-Invaders/Enemy/EnemyView.cpp create mode 100644 Space-Invaders/Enemy/EnemyView.h create mode 100644 Space-Invaders/Enemy/Enemyservice.h diff --git a/Space-Invaders/Enemy/EnemyController.cpp b/Space-Invaders/Enemy/EnemyController.cpp new file mode 100644 index 000000000..12cf9e836 --- /dev/null +++ b/Space-Invaders/Enemy/EnemyController.cpp @@ -0,0 +1,33 @@ +#include"../../Enemy/EnemyController.h" +#include"../../Enemy/EnemyModel.h" +#include"../../Enemy/EnemyView.h" +namespace enemy { + EnemyController::EnemyController() + { + enemy_view = new EnemyView(); + enemy_model = new EnemyMode(); + + } + EnemyController::~EnemyController() + { + delete (enemy_view); + delete (enemy_model); + } + void EnemyController::initialize() + { + enemy_model->intialize(); + enemy_view->intialize(this); + } + void EnemyController::update() + { + enemy_view->update(); + } + void EnemyController::render() + { + enemy_view->render(); + } + sf::Vector2f EnemyController::getEnemyPosition() + { + return enemy_model->getenemytposition(); + } +} \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyController.h b/Space-Invaders/Enemy/EnemyController.h new file mode 100644 index 000000000..62a34cbaf --- /dev/null +++ b/Space-Invaders/Enemy/EnemyController.h @@ -0,0 +1,23 @@ +#pragma once +#include +namespace enemy +{ + +class EnemyView; +class EnemyMode; + class EnemyController { + private: + EnemyView* enemy_view; + EnemyMode* enemy_model; + + public: + + EnemyController(); + ~EnemyController(); + void initialize(); + void update(); + void render(); + + sf::Vector2f getEnemyPosition(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyModel.cpp b/Space-Invaders/Enemy/EnemyModel.cpp new file mode 100644 index 000000000..c6704fca1 --- /dev/null +++ b/Space-Invaders/Enemy/EnemyModel.cpp @@ -0,0 +1,39 @@ +#include"../../Enemy/EnemyModel.h" +namespace enemy { + EnemyMode::EnemyMode() + { + } + EnemyMode::~EnemyMode() + { + } + void EnemyMode::setenemyposition(sf::Vector2f pos) + { + enemypostion = pos; + } + sf::Vector2f EnemyMode::getenemytposition() + { + return enemypostion; + } + void EnemyMode::setrefgpos(sf::Vector2f spos) + { + reference_position = spos; + } + sf::Vector2f EnemyMode::getrefpos() + { + return reference_position; + } + void EnemyMode::intialize() + { + enemypostion = reference_position; + } + + void EnemyMode::update() + { + } + + void EnemyMode::render() + { + } + + } + diff --git a/Space-Invaders/Enemy/EnemyModel.h b/Space-Invaders/Enemy/EnemyModel.h new file mode 100644 index 000000000..e3eaf073c --- /dev/null +++ b/Space-Invaders/Enemy/EnemyModel.h @@ -0,0 +1,23 @@ +#pragma once +#include +namespace enemy { + class EnemyMode { + + private: + sf::Vector2f enemypostion; + sf::Vector2f reference_position = sf::Vector2f(0.f, 0.f); + + + public: + EnemyMode(); + ~EnemyMode(); + void setenemyposition(sf::Vector2f pos); + sf::Vector2f getenemytposition(); + void setrefgpos(sf::Vector2f spos); + sf::Vector2f getrefpos(); + + void intialize(); + void update(); + void render(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyService.cpp b/Space-Invaders/Enemy/EnemyService.cpp new file mode 100644 index 000000000..c1ec356a0 --- /dev/null +++ b/Space-Invaders/Enemy/EnemyService.cpp @@ -0,0 +1,55 @@ +#include"../../Enemy/Enemyservice.h" +#include"../../Enemy/EnemyController.h" +#include"../../Enemy/EnemyModel.h" +#include"../../Enemy/EnemyView.h" +#include"../../Headers/Global/ServiceLocator.h" +namespace enemy { + using namespace Global; + + + + void EnemyService::destroy() + { + delete(Enemy); + } + + enemy::EnemyService::EnemyService() + { + Enemy = nullptr; + + } + + + enemy::EnemyService::~EnemyService() + { + Enemy = new EnemyController(); + destroy(); + } + + void enemy::EnemyService::intialize() + { + spawnenemy(); + } + + void enemy::EnemyService::update() + { + + } + + void enemy::EnemyService::render() + { + Enemy->render(); + } + + EnemyController* EnemyService::spawnenemy() + { + + Enemy->initialize(); + return Enemy; + } + + + + + +} \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyView.cpp b/Space-Invaders/Enemy/EnemyView.cpp new file mode 100644 index 000000000..2767331f5 --- /dev/null +++ b/Space-Invaders/Enemy/EnemyView.cpp @@ -0,0 +1,42 @@ +#include"../../Enemy/EnemyView.h" +#include"../../Headers/Graphic/GraphicService.h" +#include"../../Headers/main/GameService.h" +#include"../../Enemy/EnemyController.h" +namespace enemy { + using namespace Global; + using namespace Graphic; + + EnemyView::EnemyView() + { + } + EnemyView::~EnemyView() + { + } + void EnemyView::initializeEnemySprite() + { + if (enemy_texture.loadFromFile("enemy_texture_path)")) { + enemy_sprite.setTexture(enemy_texture); + scaleEnemySprite(); + } + } + void EnemyView::scaleEnemySprite() + { + enemy_sprite.setScale(static_cast(enemy_sprite_width) / enemy_sprite.getTexture()->getSize().x, + static_cast(enemy_sprite_height) / enemy_sprite.getTexture()->getSize().y); + } + + void EnemyView::intialize(EnemyController* controller) + { + enemy_controller = controller; + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializeEnemySprite(); + } + void EnemyView::update() + { + enemy_sprite.setPosition(enemy_controller->getEnemyPosition()); + } + void EnemyView::render() + { + game_window->draw(enemy_sprite); + } +} \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyView.h b/Space-Invaders/Enemy/EnemyView.h new file mode 100644 index 000000000..4af9ab574 --- /dev/null +++ b/Space-Invaders/Enemy/EnemyView.h @@ -0,0 +1,36 @@ +#pragma once +#include +namespace enemy { + + class EnemyController; + class EnemyView{ + + + + + private: + + + + const sf::String enemy_texture_path = "assets/textures/enemy_ship.png"; + + const float enemy_sprite_width = 100.f; + const float enemy_sprite_height = 100.f; + + EnemyController* enemy_controller; + + sf::RenderWindow* game_window; + sf::Texture enemy_texture; + sf::Sprite enemy_sprite; + + void initializeEnemySprite(); + void scaleEnemySprite(); + + public: + EnemyView(); + ~EnemyView(); + void intialize(EnemyController* controller); + void update(); + void render(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Enemy/Enemyservice.h b/Space-Invaders/Enemy/Enemyservice.h new file mode 100644 index 000000000..402c753ed --- /dev/null +++ b/Space-Invaders/Enemy/Enemyservice.h @@ -0,0 +1,19 @@ +#pragma once +#include +namespace enemy { + class EnemyController; + class EnemyService { + private: + EnemyController* Enemy; + void destroy(); + public: + EnemyService(); + virtual ~EnemyService(); + + void intialize(); + void update(); + void render(); + EnemyController* spawnenemy(); + + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/Global/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h index a1f61be32..58fdf959e 100644 --- a/Space-Invaders/Headers/Global/ServiceLocator.h +++ b/Space-Invaders/Headers/Global/ServiceLocator.h @@ -5,12 +5,13 @@ #include"../player/PlayerService.h" #include"../TIME/TimeService .h" #include"../../UI/UISERVICE.h" +#include"../../Enemy/Enemyservice.h" namespace Global { using namespace player; using namespace Graphic; using namespace event; using namespace UI; - + using namespace enemy; class ServiceLocator { private: @@ -20,7 +21,7 @@ namespace Global { PlayerService* player_service; TimeService* time_Service; UiService* ui_service; - + EnemyService* enemy_service; // Private Constructor and Destructor: ServiceLocator(); // Constructor for initializing the ServiceLocator. @@ -43,5 +44,6 @@ namespace Global { PlayerService* getplayerservice(); TimeService* gettimeservice(); UiService* getUiservice(); + EnemyService* getenemyservice(); }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/main/GameService.h b/Space-Invaders/Headers/main/GameService.h index 8850e9ade..14ac7a916 100644 --- a/Space-Invaders/Headers/main/GameService.h +++ b/Space-Invaders/Headers/main/GameService.h @@ -15,6 +15,7 @@ enum class GameState //create the enum namespace Main { using namespace Global; + using namespace Main; class GameService { diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 6d436d8bf..2c2a19341 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,6 +132,10 @@ + + + + @@ -146,6 +150,10 @@ + + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 31167764f..91383d962 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -45,6 +45,24 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + @@ -74,5 +92,23 @@ Header Files + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Space-Invaders/source/PlayerService.cpp b/Space-Invaders/source/PlayerService.cpp index 6ccda2e3a..47433b47a 100644 --- a/Space-Invaders/source/PlayerService.cpp +++ b/Space-Invaders/source/PlayerService.cpp @@ -5,10 +5,11 @@ #include"../Headers/player/PlayerService.h" #include"../Headers/player/PlayerController.h" #include"../Headers/EVENT/EventService.h" + namespace player { using namespace event; using namespace Global; - + void PlayerService::initializePlayerSprite() { diff --git a/Space-Invaders/source/PlayerView.cpp b/Space-Invaders/source/PlayerView.cpp index c4068235f..3ce88b382 100644 --- a/Space-Invaders/source/PlayerView.cpp +++ b/Space-Invaders/source/PlayerView.cpp @@ -2,6 +2,7 @@ //#include"../ServiceLocator.h" #include"../Headers/player/PlayerView.h" #include"../Headers/Global/ServiceLocator.h" +#include "../../Enemy/EnemyView.h" namespace player { using namespace Global; @@ -37,6 +38,10 @@ namespace player { ); } + + + + void PlayerView::update() { player_sprite.setPosition(player_controller->getPlayerPosition()); diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index 9e21dc4c3..96e6fa530 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -4,8 +4,13 @@ #include"../Headers/player/PlayerService.h" #include"../Headers/TIME/TimeService .h" #include"../Headers//Graphic/GraphicService.h" +#include"../Headers/main/GameService.h" + namespace Global { + using namespace Main; + using namespace Graphic; + using namespace event; // Constructor: Initializes the graphic_service pointer to null and creates services. ServiceLocator::ServiceLocator() { @@ -14,6 +19,7 @@ namespace Global { player_service = nullptr; time_Service = nullptr; ui_service = nullptr; + enemy_service =nullptr; createServices(); // Call createServices to instantiate services } @@ -30,6 +36,7 @@ namespace Global { player_service = new PlayerService(); time_Service = new TimeService(); ui_service = new UiService(); + enemy_service = new EnemyService(); } // Deletes allocated services to prevent memory leaks, specifically the graphic service. @@ -39,6 +46,7 @@ namespace Global { delete(player_service); delete(time_Service); delete(ui_service); + delete(enemy_service); } @@ -61,24 +69,37 @@ namespace Global { void ServiceLocator::update() { graphic_service->update(); // Update graphic service event_service->update(); - player_service->update(); time_Service->update(); - ui_service->update(); + + if (GameService::getGameState() == GameState::GAMEPLAY) + { + player_service->update(); + enemy_service->update(); + } + + ui_service->update(); + + + } // Renders using the graphic service. void ServiceLocator::render() { - - + + graphic_service->render(); // Render graphic service - // Render graphic service - player_service->render(); - ui_service->render(); + // Render graphic service - + if (GameService::getGameState() == GameState::GAMEPLAY) + { + player_service->render(); + enemy_service->render(); + } + + ui_service->render(); } @@ -104,6 +125,11 @@ namespace Global { return ui_service; } + EnemyService* ServiceLocator::getenemyservice() + { + return enemy_service; + } + } From 27b220fd50327e7c26717f646e723e960155cb78 Mon Sep 17 00:00:00 2001 From: Tarun Date: Mon, 5 Aug 2024 21:43:41 +0530 Subject: [PATCH 17/27] changes --- Space-Invaders/Enemy/EnemyService.cpp | 4 ++-- Space-Invaders/Enemy/EnemyView.cpp | 4 ++-- Space-Invaders/Space-Invaders.vcxproj.filters | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Space-Invaders/Enemy/EnemyService.cpp b/Space-Invaders/Enemy/EnemyService.cpp index c1ec356a0..d24bde815 100644 --- a/Space-Invaders/Enemy/EnemyService.cpp +++ b/Space-Invaders/Enemy/EnemyService.cpp @@ -22,7 +22,7 @@ namespace enemy { enemy::EnemyService::~EnemyService() { - Enemy = new EnemyController(); + destroy(); } @@ -43,7 +43,7 @@ namespace enemy { EnemyController* EnemyService::spawnenemy() { - + Enemy = new EnemyController(); Enemy->initialize(); return Enemy; } diff --git a/Space-Invaders/Enemy/EnemyView.cpp b/Space-Invaders/Enemy/EnemyView.cpp index 2767331f5..0aeae473f 100644 --- a/Space-Invaders/Enemy/EnemyView.cpp +++ b/Space-Invaders/Enemy/EnemyView.cpp @@ -5,14 +5,14 @@ namespace enemy { using namespace Global; using namespace Graphic; - + EnemyView::EnemyView() { } EnemyView::~EnemyView() { } - void EnemyView::initializeEnemySprite() + void EnemyView::initializeEnemySprite() { if (enemy_texture.loadFromFile("enemy_texture_path)")) { enemy_sprite.setTexture(enemy_texture); diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 91383d962..a47d1d473 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -51,15 +51,15 @@ Source Files - - Source Files - Source Files Source Files + + Source Files + Source Files @@ -98,16 +98,16 @@ Header Files - + Header Files Header Files - + Header Files - + Header Files From 3e0848025dbb92e5eeacbcc16fc5ae74372f8e23 Mon Sep 17 00:00:00 2001 From: Tarun Date: Tue, 6 Aug 2024 10:41:54 +0530 Subject: [PATCH 18/27] added the enemy --- Space-Invaders/Enemy/EnemyController.cpp | 30 ++++++---- Space-Invaders/Enemy/EnemyController.h | 14 ++--- Space-Invaders/Enemy/EnemyModel.cpp | 43 +++++++-------- Space-Invaders/Enemy/EnemyModel.h | 29 +++++----- Space-Invaders/Enemy/EnemyService.cpp | 55 +++++++------------ Space-Invaders/Enemy/EnemyView.cpp | 43 +++++++++------ Space-Invaders/Enemy/EnemyView.h | 46 +++++++--------- Space-Invaders/Enemy/Enemyservice.h | 19 ++++--- .../Headers/Global/ServiceLocator.h | 2 +- Space-Invaders/Space-Invaders.vcxproj | 16 +++--- Space-Invaders/Space-Invaders.vcxproj.filters | 16 +++--- Space-Invaders/source/ServiceLocator.cpp | 1 + 12 files changed, 154 insertions(+), 160 deletions(-) diff --git a/Space-Invaders/Enemy/EnemyController.cpp b/Space-Invaders/Enemy/EnemyController.cpp index 12cf9e836..92823fe6e 100644 --- a/Space-Invaders/Enemy/EnemyController.cpp +++ b/Space-Invaders/Enemy/EnemyController.cpp @@ -1,33 +1,39 @@ -#include"../../Enemy/EnemyController.h" -#include"../../Enemy/EnemyModel.h" -#include"../../Enemy/EnemyView.h" -namespace enemy { +#include "EnemyController.h" +#include"../../ENEMY/EnemyModel.h" +#include"../../ENEMY/EnemyView.h" +#include"../../Headers/Global/ServiceLocator.h" +namespace Enemy { + + using namespace Global; EnemyController::EnemyController() { enemy_view = new EnemyView(); - enemy_model = new EnemyMode(); - + enemy_model = new EnemyModel(); + } EnemyController::~EnemyController() { delete (enemy_view); delete (enemy_model); } - void EnemyController::initialize() + void Enemy::EnemyController::initialize() { - enemy_model->intialize(); - enemy_view->intialize(this); + enemy_model->initialize(); + enemy_view->initialize(this); // we will discuss this soo } - void EnemyController::update() + + void Enemy::EnemyController::update() { enemy_view->update(); } - void EnemyController::render() + + void Enemy::EnemyController::render() { enemy_view->render(); } sf::Vector2f EnemyController::getEnemyPosition() { - return enemy_model->getenemytposition(); + return enemy_model->getEnemyPosition(); + } } \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyController.h b/Space-Invaders/Enemy/EnemyController.h index 62a34cbaf..245a47a05 100644 --- a/Space-Invaders/Enemy/EnemyController.h +++ b/Space-Invaders/Enemy/EnemyController.h @@ -1,23 +1,21 @@ #pragma once -#include -namespace enemy -{ - -class EnemyView; -class EnemyMode; +#include + namespace Enemy { + class EnemyView; + class EnemyModel; class EnemyController { private: EnemyView* enemy_view; - EnemyMode* enemy_model; + EnemyModel* enemy_model; public: EnemyController(); ~EnemyController(); + void initialize(); void update(); void render(); - sf::Vector2f getEnemyPosition(); }; } \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyModel.cpp b/Space-Invaders/Enemy/EnemyModel.cpp index c6704fca1..302f7699c 100644 --- a/Space-Invaders/Enemy/EnemyModel.cpp +++ b/Space-Invaders/Enemy/EnemyModel.cpp @@ -1,39 +1,36 @@ -#include"../../Enemy/EnemyModel.h" -namespace enemy { - EnemyMode::EnemyMode() +#include "EnemyModel.h" +namespace Enemy { + Enemy::EnemyModel::EnemyModel() { } - EnemyMode::~EnemyMode() - { - } - void EnemyMode::setenemyposition(sf::Vector2f pos) - { - enemypostion = pos; - } - sf::Vector2f EnemyMode::getenemytposition() + + Enemy::EnemyModel::~EnemyModel() { - return enemypostion; + enemy_position = reference_position; } - void EnemyMode::setrefgpos(sf::Vector2f spos) + + void Enemy::EnemyModel::initialize() { - reference_position = spos; + enemy_position = reference_position; } - sf::Vector2f EnemyMode::getrefpos() + + sf::Vector2f Enemy::EnemyModel::getEnemyPosition() { - return reference_position; + return enemy_position; } - void EnemyMode::intialize() + + void Enemy::EnemyModel::setEnemyPosition(sf::Vector2f position) { - enemypostion = reference_position; + enemy_position = position; } - void EnemyMode::update() + sf::Vector2f Enemy::EnemyModel::getReferencePosition() { + return reference_position; } - void EnemyMode::render() + void Enemy::EnemyModel::setReferencePosition(sf::Vector2f position) { + reference_position = position; } - - } - +} \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyModel.h b/Space-Invaders/Enemy/EnemyModel.h index e3eaf073c..675c8ce36 100644 --- a/Space-Invaders/Enemy/EnemyModel.h +++ b/Space-Invaders/Enemy/EnemyModel.h @@ -1,23 +1,22 @@ #pragma once #include -namespace enemy { - class EnemyMode { +namespace Enemy { + class EnemyModel { - private: - sf::Vector2f enemypostion; - sf::Vector2f reference_position = sf::Vector2f(0.f, 0.f); + private: + sf::Vector2f reference_position = sf::Vector2f(50.f, 50.f); + sf::Vector2f enemy_position; + public: + EnemyModel(); + ~EnemyModel(); - public: - EnemyMode(); - ~EnemyMode(); - void setenemyposition(sf::Vector2f pos); - sf::Vector2f getenemytposition(); - void setrefgpos(sf::Vector2f spos); - sf::Vector2f getrefpos(); + void initialize(); - void intialize(); - void update(); - void render(); + sf::Vector2f getEnemyPosition(); + void setEnemyPosition(sf::Vector2f position); + + sf::Vector2f getReferencePosition(); + void setReferencePosition(sf::Vector2f position); }; } \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyService.cpp b/Space-Invaders/Enemy/EnemyService.cpp index d24bde815..8888d4370 100644 --- a/Space-Invaders/Enemy/EnemyService.cpp +++ b/Space-Invaders/Enemy/EnemyService.cpp @@ -1,55 +1,42 @@ -#include"../../Enemy/Enemyservice.h" -#include"../../Enemy/EnemyController.h" -#include"../../Enemy/EnemyModel.h" -#include"../../Enemy/EnemyView.h" -#include"../../Headers/Global/ServiceLocator.h" -namespace enemy { - using namespace Global; - - - - void EnemyService::destroy() +#include "Enemyservice.h" +#include"../ENEMY/EnemyController.h" +namespace Enemy { + void Enemy::EnemyService::Destroy() { - delete(Enemy); + delete(enemy); } - enemy::EnemyService::EnemyService() + Enemy::EnemyService::EnemyService() { - Enemy = nullptr; - + enemy = nullptr; } - - enemy::EnemyService::~EnemyService() + Enemy::EnemyService::~EnemyService() { - - destroy(); - } + Destroy(); - void enemy::EnemyService::intialize() - { - spawnenemy(); } - void enemy::EnemyService::update() + void Enemy::EnemyService::initialize() { - + spawnEnemy(); } - void enemy::EnemyService::render() + void Enemy::EnemyService::update() { - Enemy->render(); + } - EnemyController* EnemyService::spawnenemy() + void Enemy::EnemyService::render() { - Enemy = new EnemyController(); - Enemy->initialize(); - return Enemy; + enemy->render(); } + EnemyController* Enemy::EnemyService::spawnEnemy() + { + enemy = new EnemyController(); + enemy->initialize(); - - - + return enemy; + } } \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyView.cpp b/Space-Invaders/Enemy/EnemyView.cpp index 0aeae473f..28d9d5783 100644 --- a/Space-Invaders/Enemy/EnemyView.cpp +++ b/Space-Invaders/Enemy/EnemyView.cpp @@ -1,41 +1,48 @@ -#include"../../Enemy/EnemyView.h" +#include "EnemyView.h" +#include"../../Headers/Global/ServiceLocator.h" #include"../../Headers/Graphic/GraphicService.h" -#include"../../Headers/main/GameService.h" -#include"../../Enemy/EnemyController.h" -namespace enemy { +#include"../../ENEMY/EnemyController.h" +namespace Enemy { using namespace Global; using namespace Graphic; - - EnemyView::EnemyView() + void Enemy::EnemyView::initializeEnemySprite() { + if (enemy_texture.loadFromFile(enemy_texture_path)) //check if the texture loaded + { + enemy_sprite.setTexture(enemy_texture); //set the sprite + scaleEnemySprite(); // call the method to scale the sprite + } } - EnemyView::~EnemyView() + + void Enemy::EnemyView::scaleEnemySprite() { + enemy_sprite.setScale( + static_cast(enemy_sprite_width) / enemy_sprite.getTexture()->getSize().x, + static_cast(enemy_sprite_height) / enemy_sprite.getTexture()->getSize().y + ); } - void EnemyView::initializeEnemySprite() + + Enemy::EnemyView::EnemyView() { - if (enemy_texture.loadFromFile("enemy_texture_path)")) { - enemy_sprite.setTexture(enemy_texture); - scaleEnemySprite(); - } } - void EnemyView::scaleEnemySprite() + + Enemy::EnemyView::~EnemyView() { - enemy_sprite.setScale(static_cast(enemy_sprite_width) / enemy_sprite.getTexture()->getSize().x, - static_cast(enemy_sprite_height) / enemy_sprite.getTexture()->getSize().y); } - void EnemyView::intialize(EnemyController* controller) + void Enemy::EnemyView::initialize(EnemyController* controller) { enemy_controller = controller; game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); initializeEnemySprite(); } - void EnemyView::update() + + void Enemy::EnemyView::update() { enemy_sprite.setPosition(enemy_controller->getEnemyPosition()); } - void EnemyView::render() + + void Enemy::EnemyView::render() { game_window->draw(enemy_sprite); } diff --git a/Space-Invaders/Enemy/EnemyView.h b/Space-Invaders/Enemy/EnemyView.h index 4af9ab574..6422e4ae9 100644 --- a/Space-Invaders/Enemy/EnemyView.h +++ b/Space-Invaders/Enemy/EnemyView.h @@ -1,36 +1,30 @@ #pragma once -#include -namespace enemy { - +#include +namespace Enemy { class EnemyController; - class EnemyView{ - - - - - private: - + class EnemyView { + private: + const sf::String enemy_texture_path = "assets/textures/enemy_ship.png"; - const sf::String enemy_texture_path = "assets/textures/enemy_ship.png"; + const float enemy_sprite_width = 60.f; + const float enemy_sprite_height = 60.f; - const float enemy_sprite_width = 100.f; - const float enemy_sprite_height = 100.f; + EnemyController* enemy_controller; - EnemyController* enemy_controller; + sf::RenderWindow* game_window; + sf::Texture enemy_texture; + sf::Sprite enemy_sprite; - sf::RenderWindow* game_window; - sf::Texture enemy_texture; - sf::Sprite enemy_sprite; + void initializeEnemySprite(); + void scaleEnemySprite(); - void initializeEnemySprite(); - void scaleEnemySprite(); + public: + EnemyView(); + ~EnemyView(); - public: - EnemyView(); - ~EnemyView(); - void intialize(EnemyController* controller); - void update(); - void render(); - }; + void initialize(EnemyController* controller); + void update(); + void render(); +}; } \ No newline at end of file diff --git a/Space-Invaders/Enemy/Enemyservice.h b/Space-Invaders/Enemy/Enemyservice.h index 402c753ed..385a45248 100644 --- a/Space-Invaders/Enemy/Enemyservice.h +++ b/Space-Invaders/Enemy/Enemyservice.h @@ -1,19 +1,24 @@ #pragma once -#include -namespace enemy { +namespace Enemy { class EnemyController; + class EnemyService { private: - EnemyController* Enemy; - void destroy(); + + void Destroy(); + + EnemyController* enemy; + + + public: EnemyService(); virtual ~EnemyService(); - void intialize(); + void initialize(); void update(); void render(); - EnemyController* spawnenemy(); - + + EnemyController* spawnEnemy(); }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/Global/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h index 58fdf959e..102fd58b8 100644 --- a/Space-Invaders/Headers/Global/ServiceLocator.h +++ b/Space-Invaders/Headers/Global/ServiceLocator.h @@ -11,7 +11,7 @@ namespace Global { using namespace Graphic; using namespace event; using namespace UI; - using namespace enemy; + using namespace Enemy; class ServiceLocator { private: diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 2c2a19341..895856d37 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,10 +132,10 @@ - - - - + + + + @@ -150,10 +150,10 @@ - - - - + + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index a47d1d473..88d5dd419 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -51,16 +51,16 @@ Source Files - + Source Files - + Source Files - + Source Files - + Source Files @@ -98,16 +98,16 @@ Header Files - + Header Files - + Header Files - + Header Files - + Header Files diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index 96e6fa530..4dafe72cd 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -63,6 +63,7 @@ namespace Global { player_service->initialize(); time_Service->intialize(); ui_service->intialize(); + enemy_service->initialize(); } // Updates the state of the graphic service. From e2b151987b1557beb28ff339db761abbf0fae651 Mon Sep 17 00:00:00 2001 From: Tarun Date: Tue, 6 Aug 2024 10:49:16 +0530 Subject: [PATCH 19/27] changed the enemy texture to enemy to zapper --- Space-Invaders/Enemy/EnemyView.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Space-Invaders/Enemy/EnemyView.h b/Space-Invaders/Enemy/EnemyView.h index 6422e4ae9..eda546db8 100644 --- a/Space-Invaders/Enemy/EnemyView.h +++ b/Space-Invaders/Enemy/EnemyView.h @@ -5,7 +5,7 @@ namespace Enemy { class EnemyView { private: - const sf::String enemy_texture_path = "assets/textures/enemy_ship.png"; + const sf::String enemy_texture_path = "assets/textures/zapper.png"; const float enemy_sprite_width = 60.f; const float enemy_sprite_height = 60.f; From 22424f93726beead14521f148324b165d52635d0 Mon Sep 17 00:00:00 2001 From: Tarun Date: Tue, 6 Aug 2024 18:44:47 +0530 Subject: [PATCH 20/27] added the enemy movement which is not moving --- Space-Invaders/Enemy/EnemyController.cpp | 61 +++++++++++++++++++++++- Space-Invaders/Enemy/EnemyController.h | 5 ++ Space-Invaders/Enemy/EnemyModel.cpp | 10 ++++ Space-Invaders/Enemy/EnemyModel.h | 26 +++++++++- Space-Invaders/Enemy/EnemyService.cpp | 2 +- 5 files changed, 100 insertions(+), 4 deletions(-) diff --git a/Space-Invaders/Enemy/EnemyController.cpp b/Space-Invaders/Enemy/EnemyController.cpp index 92823fe6e..88fbcd7d2 100644 --- a/Space-Invaders/Enemy/EnemyController.cpp +++ b/Space-Invaders/Enemy/EnemyController.cpp @@ -9,7 +9,7 @@ namespace Enemy { { enemy_view = new EnemyView(); enemy_model = new EnemyModel(); - + } EnemyController::~EnemyController() { @@ -24,6 +24,7 @@ namespace Enemy { void Enemy::EnemyController::update() { + move(); enemy_view->update(); } @@ -36,4 +37,62 @@ namespace Enemy { return enemy_model->getEnemyPosition(); } + void EnemyController::move() + { + switch (enemy_model->getmovementdirection()) + { + case MovementDirection::LEFT: + moveLeft(); + break; + case MovementDirection::RIGHT: + moveRight(); + break; + case MovementDirection::DOWN: + moveDown(); + break; + } + + } + void EnemyController::moveLeft() + { + sf::Vector2f currentposition = enemy_model->getEnemyPosition(); + currentposition.x += enemy_model->movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + if (currentposition.x <= enemy_model->left_most.x) { + enemy_model->setmovementdirection(MovementDirection::DOWN); + enemy_model->setReferencePosition(currentposition); + } + else { + enemy_model->setEnemyPosition(currentposition); + } + } + void EnemyController::moveRight() + { + sf::Vector2f currentposition = enemy_model->getEnemyPosition(); + currentposition.x += enemy_model->movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + if (currentposition.x >= enemy_model->right_most.x) { + enemy_model->setmovementdirection(MovementDirection::DOWN); + enemy_model->setEnemyPosition(currentposition); + } + } + void EnemyController::moveDown() { + + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); + currentPosition.y += enemy_model->movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + if (currentPosition.y >= enemy_model->getReferencePosition().y + enemy_model->downward_distance) { + + + if (enemy_model->getReferencePosition().x <= enemy_model->left_most.x) { + + enemy_model->setmovementdirection(MovementDirection::RIGHT); + } + else { + + enemy_model->setmovementdirection(MovementDirection::LEFT); + } + + } + else { + enemy_model->setEnemyPosition(currentPosition); + } + } } \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyController.h b/Space-Invaders/Enemy/EnemyController.h index 245a47a05..8862bae8f 100644 --- a/Space-Invaders/Enemy/EnemyController.h +++ b/Space-Invaders/Enemy/EnemyController.h @@ -17,5 +17,10 @@ void update(); void render(); sf::Vector2f getEnemyPosition(); + + void move(); + void moveLeft(); + void moveRight(); + void moveDown(); }; } \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyModel.cpp b/Space-Invaders/Enemy/EnemyModel.cpp index 302f7699c..f301052ed 100644 --- a/Space-Invaders/Enemy/EnemyModel.cpp +++ b/Space-Invaders/Enemy/EnemyModel.cpp @@ -2,6 +2,7 @@ namespace Enemy { Enemy::EnemyModel::EnemyModel() { + } Enemy::EnemyModel::~EnemyModel() @@ -11,6 +12,7 @@ namespace Enemy { void Enemy::EnemyModel::initialize() { + movement_direction = MovementDirection::RIGHT; enemy_position = reference_position; } @@ -33,4 +35,12 @@ namespace Enemy { { reference_position = position; } + MovementDirection EnemyModel::getmovementdirection() + { + return movement_direction; + } + void EnemyModel::setmovementdirection(MovementDirection direction) + { + movement_direction = direction; + } } \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyModel.h b/Space-Invaders/Enemy/EnemyModel.h index 675c8ce36..036392c0a 100644 --- a/Space-Invaders/Enemy/EnemyModel.h +++ b/Space-Invaders/Enemy/EnemyModel.h @@ -1,16 +1,33 @@ #pragma once #include namespace Enemy { + enum class MovementDirection { + + RIGHT, + LEFT, + DOWN, + }; + class EnemyModel { private: - sf::Vector2f reference_position = sf::Vector2f(50.f, 50.f); - sf::Vector2f enemy_position; + + MovementDirection movement_direction; + public: EnemyModel(); ~EnemyModel(); + + sf::Vector2f reference_position = sf::Vector2f(50.f, 50.f); + sf::Vector2f enemy_position; + + const float movement_speed = 250.f; + const sf::Vector2f right_most = sf::Vector2f(1800.f, 950.f); + const sf::Vector2f left_most = sf::Vector2f(50.f, 950.f); + float downward_distance = 100.f; + void initialize(); sf::Vector2f getEnemyPosition(); @@ -18,5 +35,10 @@ namespace Enemy { sf::Vector2f getReferencePosition(); void setReferencePosition(sf::Vector2f position); + + MovementDirection getmovementdirection(); + void setmovementdirection(MovementDirection direction); + + }; } \ No newline at end of file diff --git a/Space-Invaders/Enemy/EnemyService.cpp b/Space-Invaders/Enemy/EnemyService.cpp index 8888d4370..30a0102ee 100644 --- a/Space-Invaders/Enemy/EnemyService.cpp +++ b/Space-Invaders/Enemy/EnemyService.cpp @@ -24,7 +24,7 @@ namespace Enemy { void Enemy::EnemyService::update() { - + enemy->update(); } void Enemy::EnemyService::render() From cd2fe9f1025609d2c2f2cd2adf644639b11a20b0 Mon Sep 17 00:00:00 2001 From: Tarun Date: Tue, 6 Aug 2024 18:59:12 +0530 Subject: [PATCH 21/27] Update EnemyController.cpp --- Space-Invaders/Enemy/EnemyController.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Space-Invaders/Enemy/EnemyController.cpp b/Space-Invaders/Enemy/EnemyController.cpp index 88fbcd7d2..9396a4daa 100644 --- a/Space-Invaders/Enemy/EnemyController.cpp +++ b/Space-Invaders/Enemy/EnemyController.cpp @@ -56,7 +56,7 @@ namespace Enemy { void EnemyController::moveLeft() { sf::Vector2f currentposition = enemy_model->getEnemyPosition(); - currentposition.x += enemy_model->movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + currentposition.x -= enemy_model->movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); if (currentposition.x <= enemy_model->left_most.x) { enemy_model->setmovementdirection(MovementDirection::DOWN); enemy_model->setReferencePosition(currentposition); From 581f8acb326c51df1dfac4fb315eff0a27c7baf0 Mon Sep 17 00:00:00 2001 From: Tarun Date: Tue, 6 Aug 2024 19:23:44 +0530 Subject: [PATCH 22/27] error in movement of enemy --- Space-Invaders/Enemy/EnemyController.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Space-Invaders/Enemy/EnemyController.cpp b/Space-Invaders/Enemy/EnemyController.cpp index 9396a4daa..f404c07c0 100644 --- a/Space-Invaders/Enemy/EnemyController.cpp +++ b/Space-Invaders/Enemy/EnemyController.cpp @@ -73,6 +73,10 @@ namespace Enemy { enemy_model->setmovementdirection(MovementDirection::DOWN); enemy_model->setEnemyPosition(currentposition); } + else { + enemy_model->setEnemyPosition(currentposition); + + } } void EnemyController::moveDown() { From f55f6b8b7c0a095d773e392f28b4cb823a6fd9d2 Mon Sep 17 00:00:00 2001 From: Tarun Date: Tue, 6 Aug 2024 19:51:55 +0530 Subject: [PATCH 23/27] error not debugged --- Space-Invaders/Enemy/EnemyController.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Space-Invaders/Enemy/EnemyController.cpp b/Space-Invaders/Enemy/EnemyController.cpp index f404c07c0..04a32b3be 100644 --- a/Space-Invaders/Enemy/EnemyController.cpp +++ b/Space-Invaders/Enemy/EnemyController.cpp @@ -78,25 +78,31 @@ namespace Enemy { } } - void EnemyController::moveDown() { - + void EnemyController::moveDown() + { sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); currentPosition.y += enemy_model->movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); - if (currentPosition.y >= enemy_model->getReferencePosition().y + enemy_model->downward_distance) { + //check if enemy has moved the specified distance downwards + if (currentPosition.y >= enemy_model->getReferencePosition().y + enemy_model->downward_distance) + { + + if (enemy_model->getReferencePosition().x >= enemy_model->right_most.x) { - if (enemy_model->getReferencePosition().x <= enemy_model->left_most.x) { + enemy_model->setmovementdirection(MovementDirection::LEFT); - enemy_model->setmovementdirection(MovementDirection::RIGHT); } else { - enemy_model->setmovementdirection(MovementDirection::LEFT); + enemy_model->setmovementdirection(MovementDirection::RIGHT); } + } else { enemy_model->setEnemyPosition(currentPosition); } + + } } \ No newline at end of file From 50f53085fa97e51ca3399a956229a24679524574 Mon Sep 17 00:00:00 2001 From: Tarun Date: Tue, 6 Aug 2024 20:01:34 +0530 Subject: [PATCH 24/27] so the error is debug i am not setting the reference pos in the right function --- Space-Invaders/Enemy/EnemyController.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Space-Invaders/Enemy/EnemyController.cpp b/Space-Invaders/Enemy/EnemyController.cpp index 04a32b3be..4d25c90ca 100644 --- a/Space-Invaders/Enemy/EnemyController.cpp +++ b/Space-Invaders/Enemy/EnemyController.cpp @@ -2,8 +2,9 @@ #include"../../ENEMY/EnemyModel.h" #include"../../ENEMY/EnemyView.h" #include"../../Headers/Global/ServiceLocator.h" +#include namespace Enemy { - + using namespace std; using namespace Global; EnemyController::EnemyController() { @@ -71,7 +72,7 @@ namespace Enemy { currentposition.x += enemy_model->movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); if (currentposition.x >= enemy_model->right_most.x) { enemy_model->setmovementdirection(MovementDirection::DOWN); - enemy_model->setEnemyPosition(currentposition); + enemy_model->setReferencePosition(currentposition); } else { enemy_model->setEnemyPosition(currentposition); @@ -90,7 +91,7 @@ namespace Enemy { if (enemy_model->getReferencePosition().x >= enemy_model->right_most.x) { enemy_model->setmovementdirection(MovementDirection::LEFT); - + cout << "call left"; } else { From ef5186b7d7caba184220b8d0e63cdff1887fb180 Mon Sep 17 00:00:00 2001 From: Tarun Date: Wed, 7 Aug 2024 20:40:19 +0530 Subject: [PATCH 25/27] added the movement in and spawn in the enemy --- Space-Invaders/Enemy/EnemyService.cpp | 62 ++++++++++++++++++--------- Space-Invaders/Enemy/Enemyservice.h | 19 +++++--- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/Space-Invaders/Enemy/EnemyService.cpp b/Space-Invaders/Enemy/EnemyService.cpp index 30a0102ee..e0bb17f6c 100644 --- a/Space-Invaders/Enemy/EnemyService.cpp +++ b/Space-Invaders/Enemy/EnemyService.cpp @@ -1,42 +1,62 @@ -#include "Enemyservice.h" -#include"../ENEMY/EnemyController.h" -namespace Enemy { - void Enemy::EnemyService::Destroy() - { - delete(enemy); - } +//#include "../../Header/Enemy/EnemyService.h" +//#include "../../Header/Enemy/EnemyController.h" +//#include "../../Header/Global/ServiceLocator.h" +//#include "../../Header/Time/TimeService.h" +#include"../../ENEMY/Enemyservice.h" +#include"../../ENEMY/EnemyController.h" +#include"../../Headers/Global/ServiceLocator.h" +#include"../../Headers/TIME/TimeService .h" + +namespace Enemy +{ + using namespace Global; + + EnemyService::EnemyService() { } - Enemy::EnemyService::EnemyService() + EnemyService::~EnemyService() { destroy(); } + + void EnemyService::initialize() { - enemy = nullptr; + spawn_timer = spawn_interval; // for the first spawn } - Enemy::EnemyService::~EnemyService() + void EnemyService::update() { - Destroy(); + updateSpawnTimer(); + processEnemySpawn(); + for (int i = 0; i < enemy_list.size(); i++) enemy_list[i]->update(); } - void Enemy::EnemyService::initialize() + void EnemyService::render() { - spawnEnemy(); + for (int i = 0; i < enemy_list.size(); i++) enemy_list[i]->render(); } - void Enemy::EnemyService::update() + void EnemyService::updateSpawnTimer() { - enemy->update(); + spawn_timer += ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); // increase timer } - void Enemy::EnemyService::render() + void EnemyService::processEnemySpawn() { - enemy->render(); + if (spawn_timer >= spawn_interval) + { + spawnEnemy(); //spawn + spawn_timer = 0.0f; // reset + } } - EnemyController* Enemy::EnemyService::spawnEnemy() + void EnemyService::spawnEnemy() { - enemy = new EnemyController(); - enemy->initialize(); + EnemyController* enemy_controller = new EnemyController(); // create + enemy_controller->initialize(); // init as soon as created + + enemy_list.push_back(enemy_controller); //add to list + } - return enemy; + void EnemyService::destroy() + { + for (int i = 0; i < enemy_list.size(); i++) delete (enemy_list[i]); //delete all enemies } } \ No newline at end of file diff --git a/Space-Invaders/Enemy/Enemyservice.h b/Space-Invaders/Enemy/Enemyservice.h index 385a45248..22b048585 100644 --- a/Space-Invaders/Enemy/Enemyservice.h +++ b/Space-Invaders/Enemy/Enemyservice.h @@ -1,15 +1,22 @@ #pragma once -namespace Enemy { +#include + +namespace Enemy +{ class EnemyController; - class EnemyService { + class EnemyService + { private: - void Destroy(); - - EnemyController* enemy; + const float spawn_interval = 3.f; + std::vector enemy_list; + float spawn_timer; + void updateSpawnTimer(); + void processEnemySpawn(); + void destroy(); public: EnemyService(); @@ -19,6 +26,6 @@ namespace Enemy { void update(); void render(); - EnemyController* spawnEnemy(); + void spawnEnemy(); }; } \ No newline at end of file From 0d0038cae8c5d513dc586beaef16731d860511cf Mon Sep 17 00:00:00 2001 From: Tarun Date: Thu, 8 Aug 2024 10:54:51 +0530 Subject: [PATCH 26/27] added the gameplay service --- .../Gameplay/GameplayController.cpp | 29 +++++++++++ Space-Invaders/Gameplay/GameplayController.h | 17 +++++++ Space-Invaders/Gameplay/GameplayService.H | 23 +++++++++ Space-Invaders/Gameplay/GameplayService.cpp | 28 +++++++++++ Space-Invaders/Gameplay/GameplayView.H | 26 ++++++++++ Space-Invaders/Gameplay/GameplayView.cpp | 50 +++++++++++++++++++ .../Headers/Global/ServiceLocator.h | 4 ++ Space-Invaders/Space-Invaders.vcxproj | 6 +++ Space-Invaders/Space-Invaders.vcxproj.filters | 18 +++++++ Space-Invaders/source/ServiceLocator.cpp | 22 +++++++- 10 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 Space-Invaders/Gameplay/GameplayController.cpp create mode 100644 Space-Invaders/Gameplay/GameplayController.h create mode 100644 Space-Invaders/Gameplay/GameplayService.H create mode 100644 Space-Invaders/Gameplay/GameplayService.cpp create mode 100644 Space-Invaders/Gameplay/GameplayView.H create mode 100644 Space-Invaders/Gameplay/GameplayView.cpp diff --git a/Space-Invaders/Gameplay/GameplayController.cpp b/Space-Invaders/Gameplay/GameplayController.cpp new file mode 100644 index 000000000..738f07fcf --- /dev/null +++ b/Space-Invaders/Gameplay/GameplayController.cpp @@ -0,0 +1,29 @@ +#include "GameplayController.h" +#include"../../Gameplay/GameplayView.H" +namespace Gameplay { + Gameplay::GameplayController::GameplayController() + { + gameplay_view = new GameplayView(); + } + + Gameplay::GameplayController::~GameplayController() + { + delete(gameplay_view); + + } + + void Gameplay::GameplayController::initialize() + { + gameplay_view->initialize(); + } + + void Gameplay::GameplayController::update() + { + gameplay_view->update(); + } + + void Gameplay::GameplayController::render() + { + gameplay_view->render(); + } +} \ No newline at end of file diff --git a/Space-Invaders/Gameplay/GameplayController.h b/Space-Invaders/Gameplay/GameplayController.h new file mode 100644 index 000000000..61a6513d4 --- /dev/null +++ b/Space-Invaders/Gameplay/GameplayController.h @@ -0,0 +1,17 @@ +#pragma once +namespace Gameplay { + class GameplayView; + class GameplayController { + + private: + GameplayView* gameplay_view; + public: + GameplayController(); + ~GameplayController(); + void initialize(); + void update(); + void render(); + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/Gameplay/GameplayService.H b/Space-Invaders/Gameplay/GameplayService.H new file mode 100644 index 000000000..c39ad0ef0 --- /dev/null +++ b/Space-Invaders/Gameplay/GameplayService.H @@ -0,0 +1,23 @@ +#pragma once +#include +namespace Gameplay { + class GameplayController; + + class GameplayService { + public: + + GameplayController* gameplay_controller; + + public: + + + GameplayService(); + ~GameplayService(); + + void initialize(); + void update(); + void render(); + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/Gameplay/GameplayService.cpp b/Space-Invaders/Gameplay/GameplayService.cpp new file mode 100644 index 000000000..31e5fa27e --- /dev/null +++ b/Space-Invaders/Gameplay/GameplayService.cpp @@ -0,0 +1,28 @@ +#include "GameplayService.H" +#include"../../Gameplay/GameplayController.h" +namespace Gameplay { + Gameplay::GameplayService::GameplayService() + { + gameplay_controller = new GameplayController(); + } + + Gameplay::GameplayService::~GameplayService() + { + delete(gameplay_controller); + } + + void Gameplay::GameplayService::initialize() + { + gameplay_controller->initialize(); + } + + void Gameplay::GameplayService::update() + { + gameplay_controller->update(); + } + + void Gameplay::GameplayService::render() + { + gameplay_controller->render(); + } +} \ No newline at end of file diff --git a/Space-Invaders/Gameplay/GameplayView.H b/Space-Invaders/Gameplay/GameplayView.H new file mode 100644 index 000000000..b416a4d35 --- /dev/null +++ b/Space-Invaders/Gameplay/GameplayView.H @@ -0,0 +1,26 @@ +#pragma once +#include"SFML/Graphics.hpp" +namespace Gameplay { + + class GameplayView { + private: + const sf::String background_texture_path = "assets/textures/space_invaders_bg.png"; + + sf::RenderWindow* game_window; + + sf::Texture background_texture; + + sf::Sprite background_sprite; + + public: + GameplayView(); + ~GameplayView(); + + void initalizebackgroundsprite(); + void scaleBackgroundSprite(); + + void initialize(); + void update(); + void render(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Gameplay/GameplayView.cpp b/Space-Invaders/Gameplay/GameplayView.cpp new file mode 100644 index 000000000..70d71f424 --- /dev/null +++ b/Space-Invaders/Gameplay/GameplayView.cpp @@ -0,0 +1,50 @@ +#include "GameplayView.H" +#include"../../Headers/Global/ServiceLocator.h" +#include"../../Headers/Graphic/GraphicService.h"; + +namespace Gameplay { + + using namespace Global; + using namespace Graphic; + Gameplay::GameplayView::GameplayView() + { + } + + Gameplay::GameplayView::~GameplayView() + { + } + + void GameplayView::initalizebackgroundsprite() + { + if (background_texture.loadFromFile(background_texture_path)) { + background_sprite.setTexture(background_texture); + scaleBackgroundSprite(); + } + } + + void GameplayView::scaleBackgroundSprite() + { + + background_sprite.setScale(static_cast (game_window->getSize().x) / background_sprite.getTexture()->getSize().x, + static_cast (game_window->getSize().y) / background_sprite.getTexture()->getSize().y); + + + + } + + void Gameplay::GameplayView::initialize() + { + + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initalizebackgroundsprite(); + } + + void Gameplay::GameplayView::update() + { + } + + void Gameplay::GameplayView::render() + { + game_window->draw(background_sprite); + } +} \ No newline at end of file diff --git a/Space-Invaders/Headers/Global/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h index 102fd58b8..1983abc3b 100644 --- a/Space-Invaders/Headers/Global/ServiceLocator.h +++ b/Space-Invaders/Headers/Global/ServiceLocator.h @@ -6,12 +6,14 @@ #include"../TIME/TimeService .h" #include"../../UI/UISERVICE.h" #include"../../Enemy/Enemyservice.h" +#include"../../Gameplay/GameplayService.H" namespace Global { using namespace player; using namespace Graphic; using namespace event; using namespace UI; using namespace Enemy; + using namespace Gameplay; class ServiceLocator { private: @@ -22,6 +24,7 @@ namespace Global { TimeService* time_Service; UiService* ui_service; EnemyService* enemy_service; + GameplayService* game_play; // Private Constructor and Destructor: ServiceLocator(); // Constructor for initializing the ServiceLocator. @@ -45,5 +48,6 @@ namespace Global { TimeService* gettimeservice(); UiService* getUiservice(); EnemyService* getenemyservice(); + GameplayService* getgameplayservice(); }; } \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 895856d37..69e067865 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -136,6 +136,9 @@ + + + @@ -154,6 +157,9 @@ + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 88d5dd419..9fa9c872a 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -63,6 +63,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files + @@ -110,5 +119,14 @@ Header Files + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index 4dafe72cd..c5a5c0746 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -5,12 +5,14 @@ #include"../Headers/TIME/TimeService .h" #include"../Headers//Graphic/GraphicService.h" #include"../Headers/main/GameService.h" +#include"../../Gameplay/GameplayService.H" namespace Global { using namespace Main; using namespace Graphic; using namespace event; + using namespace Gameplay; // Constructor: Initializes the graphic_service pointer to null and creates services. ServiceLocator::ServiceLocator() { @@ -20,6 +22,8 @@ namespace Global { time_Service = nullptr; ui_service = nullptr; enemy_service =nullptr; + game_play = nullptr; + createServices(); // Call createServices to instantiate services } @@ -37,6 +41,7 @@ namespace Global { time_Service = new TimeService(); ui_service = new UiService(); enemy_service = new EnemyService(); + game_play = new GameplayService(); } // Deletes allocated services to prevent memory leaks, specifically the graphic service. @@ -47,6 +52,7 @@ namespace Global { delete(time_Service); delete(ui_service); delete(enemy_service); + delete(game_play); } @@ -64,6 +70,7 @@ namespace Global { time_Service->intialize(); ui_service->intialize(); enemy_service->initialize(); + game_play->initialize(); } // Updates the state of the graphic service. @@ -74,10 +81,12 @@ namespace Global { if (GameService::getGameState() == GameState::GAMEPLAY) { + game_play->update(); player_service->update(); enemy_service->update(); + } - + ui_service->update(); @@ -96,9 +105,13 @@ namespace Global { if (GameService::getGameState() == GameState::GAMEPLAY) { + game_play->render(); player_service->render(); enemy_service->render(); - } + + } + + ui_service->render(); @@ -131,6 +144,11 @@ namespace Global { return enemy_service; } + GameplayService* ServiceLocator::getgameplayservice() + { + return game_play; + } + } From bc5358c942378e29282aba2d962817568f785dcb Mon Sep 17 00:00:00 2001 From: Tarun Date: Mon, 12 Aug 2024 18:34:44 +0530 Subject: [PATCH 27/27] here we add Common Properties & Functionalites in the inherited classes --- .../ENEMY/Controllers/.ZapperControllercpp | 0 Space-Invaders/ENEMY/Controllers/SubZeroController.H | 12 ++++++++++++ .../ENEMY/Controllers/SubZeroController.cpp | 6 ++++++ .../ENEMY/Controllers/ZapperController.cpp | 7 +++++++ Space-Invaders/ENEMY/Controllers/ZapperController.h | 10 ++++++++++ Space-Invaders/ENEMY/EnemyConfig.h | 1 + Space-Invaders/ENEMY/EnemyConfig.h i | 0 Space-Invaders/Space-Invaders.vcxproj | 4 ++++ 8 files changed, 40 insertions(+) create mode 100644 Space-Invaders/ENEMY/Controllers/.ZapperControllercpp create mode 100644 Space-Invaders/ENEMY/Controllers/SubZeroController.H create mode 100644 Space-Invaders/ENEMY/Controllers/SubZeroController.cpp create mode 100644 Space-Invaders/ENEMY/Controllers/ZapperController.cpp create mode 100644 Space-Invaders/ENEMY/Controllers/ZapperController.h create mode 100644 Space-Invaders/ENEMY/EnemyConfig.h create mode 100644 Space-Invaders/ENEMY/EnemyConfig.h i diff --git a/Space-Invaders/ENEMY/Controllers/.ZapperControllercpp b/Space-Invaders/ENEMY/Controllers/.ZapperControllercpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/ENEMY/Controllers/SubZeroController.H b/Space-Invaders/ENEMY/Controllers/SubZeroController.H new file mode 100644 index 000000000..66992b73c --- /dev/null +++ b/Space-Invaders/ENEMY/Controllers/SubZeroController.H @@ -0,0 +1,12 @@ +#pragma once +#include"../../ENEMY/EnemyController.h" +namespace Enemy{ + namespace controller { + + class SubzeroController : public EnemyController { + + private: + }; + } + +} \ No newline at end of file diff --git a/Space-Invaders/ENEMY/Controllers/SubZeroController.cpp b/Space-Invaders/ENEMY/Controllers/SubZeroController.cpp new file mode 100644 index 000000000..3e2992d68 --- /dev/null +++ b/Space-Invaders/ENEMY/Controllers/SubZeroController.cpp @@ -0,0 +1,6 @@ +#include"../Controllers/SubZeroController.H" +namespace Enmey { + namespace controller { + + } +} \ No newline at end of file diff --git a/Space-Invaders/ENEMY/Controllers/ZapperController.cpp b/Space-Invaders/ENEMY/Controllers/ZapperController.cpp new file mode 100644 index 000000000..f913da29d --- /dev/null +++ b/Space-Invaders/ENEMY/Controllers/ZapperController.cpp @@ -0,0 +1,7 @@ +#include"../Controllers/ZapperController.h" + +namespace Enemy { + namespace controller { + + } +} \ No newline at end of file diff --git a/Space-Invaders/ENEMY/Controllers/ZapperController.h b/Space-Invaders/ENEMY/Controllers/ZapperController.h new file mode 100644 index 000000000..582d3b8e8 --- /dev/null +++ b/Space-Invaders/ENEMY/Controllers/ZapperController.h @@ -0,0 +1,10 @@ +#pragma once +#include"../../ENEMY/EnemyController.h" +namespace Enemy { + namespace controller { + class ZapperController : public EnemyController { + + private: + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/ENEMY/EnemyConfig.h b/Space-Invaders/ENEMY/EnemyConfig.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/Space-Invaders/ENEMY/EnemyConfig.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/ENEMY/EnemyConfig.h i b/Space-Invaders/ENEMY/EnemyConfig.h i new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 69e067865..f62b774fa 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,6 +132,8 @@ + + @@ -153,6 +155,8 @@ + +