From 93f3e4476bc2889a7b9892f0eeca8ad2cca58369 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Sun, 28 Jul 2024 14:25:36 +0530 Subject: [PATCH 01/41] Added my own window file --- Space-Invaders/main.cpp | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 7d5f90dff..194234723 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,30 @@ - -int main() -{ - return 0; -} \ No newline at end of file +#include + +int main() +{ + // Define the video mode (dimensions) + sf::VideoMode videoMode = sf::VideoMode(800, 600); + + // Create a window object with specific dimensions and a title + sf::RenderWindow window(videoMode, "My SFML Window"); + + + + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + // Check for window closure + if (event.type == sf::Event::Closed) + window.close(); + } + + + // Clear the window--- + window.clear(sf::Color::Blue); + + // Display whatever you draw + window.display(); + } + + return 0; +} From 676acb27083c928d83d919a1cc580247443014e5 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 13:17:15 +0530 Subject: [PATCH 02/41] Added new Main.cpp --- Space-Invaders/main.cpp | 55 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 7d5f90dff..2963054ca 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,50 @@ - -int main() -{ - return 0; -} \ No newline at end of file +#include + +int main() +{ + // Define the video mode (dimensions) + sf::VideoMode videoMode = sf::VideoMode(800, 600); + + // Create a window object with specific dimensions and a title + sf::RenderWindow window(videoMode, "My SFML Window"); + + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + // Check for window closure + if (event.type == sf::Event::Closed) + window.close(); + } + + + // Clear the window--- + window.clear(sf::Color::Magenta); + + //Start + + // Draw a circle + sf::CircleShape circle(50); // Radius 50 + circle.setFillColor(sf::Color::Green); + circle.setPosition(100, 100); // Set position + window.draw(circle); + + //Draw a Red Square + sf::RectangleShape square(sf::Vector2f(50, 50)); + square.setFillColor(sf::Color::Red); + square.setPosition(200, 200); // Set position + window.draw(square); + + //Draw a Blue Triangle + sf::CircleShape Triangle(50, 3); + Triangle.setFillColor(sf::Color::Blue); + Triangle.setPosition(300, 300); // Set position + window.draw(Triangle); + + //End + + // Display whatever you draw + window.display(); + } + + return 0; +} From fe06d7c4cc2e706476aceb5d6dd8a83d9ad15db2 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 13:27:28 +0530 Subject: [PATCH 03/41] Shapes added --- Space-Invaders/main.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 194234723..2963054ca 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -8,8 +8,6 @@ int main() // Create a window object with specific dimensions and a title sf::RenderWindow window(videoMode, "My SFML Window"); - - while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { @@ -20,7 +18,29 @@ int main() // Clear the window--- - window.clear(sf::Color::Blue); + window.clear(sf::Color::Magenta); + + //Start + + // Draw a circle + sf::CircleShape circle(50); // Radius 50 + circle.setFillColor(sf::Color::Green); + circle.setPosition(100, 100); // Set position + window.draw(circle); + + //Draw a Red Square + sf::RectangleShape square(sf::Vector2f(50, 50)); + square.setFillColor(sf::Color::Red); + square.setPosition(200, 200); // Set position + window.draw(square); + + //Draw a Blue Triangle + sf::CircleShape Triangle(50, 3); + Triangle.setFillColor(sf::Color::Blue); + Triangle.setPosition(300, 300); // Set position + window.draw(Triangle); + + //End // Display whatever you draw window.display(); From a096bda7a4ac996f110d5cde921f1b003a79d54f Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 13:46:20 +0530 Subject: [PATCH 04/41] Logo and Sprites --- Space-Invaders/main.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 2963054ca..c7eaf2cf6 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -18,7 +18,7 @@ int main() // Clear the window--- - window.clear(sf::Color::Magenta); + window.clear(sf::Color::Black); //Start @@ -40,6 +40,26 @@ int main() Triangle.setPosition(300, 300); // Set position window.draw(Triangle); + //Drawing a sprite + sf::Texture outscal_texture; + outscal_texture.loadFromFile("assets/textures/outscal_logo.png"); + + sf::Sprite outscal_sprite; + outscal_sprite.setTexture(outscal_texture); + + outscal_sprite.setPosition(500, 200); // Position + outscal_sprite.setRotation(45); // Rotation in degrees + outscal_sprite.setScale(0.5, 0.5); // Scale factor + + window.draw(outscal_sprite); + + //Drawing a text + sf::Font font; + font.loadFromFile("assets/fonts/OpenSans.ttf"); + sf::Text text("SFML is Awesome", font, 50); + text.setFillColor(sf::Color::White); + window.draw(text); + //End // Display whatever you draw From fcc675e3a76c97449b5e3a7f2cd29e189606ddff Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 13:49:23 +0530 Subject: [PATCH 05/41] added text and sprites --- Space-Invaders/main.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 2963054ca..c7eaf2cf6 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -18,7 +18,7 @@ int main() // Clear the window--- - window.clear(sf::Color::Magenta); + window.clear(sf::Color::Black); //Start @@ -40,6 +40,26 @@ int main() Triangle.setPosition(300, 300); // Set position window.draw(Triangle); + //Drawing a sprite + sf::Texture outscal_texture; + outscal_texture.loadFromFile("assets/textures/outscal_logo.png"); + + sf::Sprite outscal_sprite; + outscal_sprite.setTexture(outscal_texture); + + outscal_sprite.setPosition(500, 200); // Position + outscal_sprite.setRotation(45); // Rotation in degrees + outscal_sprite.setScale(0.5, 0.5); // Scale factor + + window.draw(outscal_sprite); + + //Drawing a text + sf::Font font; + font.loadFromFile("assets/fonts/OpenSans.ttf"); + sf::Text text("SFML is Awesome", font, 50); + text.setFillColor(sf::Color::White); + window.draw(text); + //End // Display whatever you draw From 9b096bbaab273403773ebc492aa7d97b6def8962 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 15:11:02 +0530 Subject: [PATCH 06/41] OOP --- Space-Invaders/main.cpp | 75 +++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index c7eaf2cf6..a92576c58 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,38 @@ +#include #include + +class Player +{ +private: + + // Private Properties + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); + int movement_speed = 5; + int player_score = 0; + +public: + + // Public Properties + sf::Texture player_texture; + sf::Sprite player_sprite; + + //Public Getter & Setter methods + int getScore() { + return player_score; + }; + + void setScore(int newScore) { + player_score = newScore; + }; + + //New methods + void takeDamage() {}; + void move() {}; + void shootBullets() {}; +}; + int main() { // Define the video mode (dimensions) @@ -20,47 +53,7 @@ int main() // Clear the window--- window.clear(sf::Color::Black); - //Start - - // Draw a circle - sf::CircleShape circle(50); // Radius 50 - circle.setFillColor(sf::Color::Green); - circle.setPosition(100, 100); // Set position - window.draw(circle); - - //Draw a Red Square - sf::RectangleShape square(sf::Vector2f(50, 50)); - square.setFillColor(sf::Color::Red); - square.setPosition(200, 200); // Set position - window.draw(square); - - //Draw a Blue Triangle - sf::CircleShape Triangle(50, 3); - Triangle.setFillColor(sf::Color::Blue); - Triangle.setPosition(300, 300); // Set position - window.draw(Triangle); - - //Drawing a sprite - sf::Texture outscal_texture; - outscal_texture.loadFromFile("assets/textures/outscal_logo.png"); - - sf::Sprite outscal_sprite; - outscal_sprite.setTexture(outscal_texture); - - outscal_sprite.setPosition(500, 200); // Position - outscal_sprite.setRotation(45); // Rotation in degrees - outscal_sprite.setScale(0.5, 0.5); // Scale factor - - window.draw(outscal_sprite); - - //Drawing a text - sf::Font font; - font.loadFromFile("assets/fonts/OpenSans.ttf"); - sf::Text text("SFML is Awesome", font, 50); - text.setFillColor(sf::Color::White); - window.draw(text); - - //End + Player player; // Display whatever you draw window.display(); From 1116b79e83abc5d24616221af8faec5fe5cb694a Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 15:31:37 +0530 Subject: [PATCH 07/41] Added keyboard inputs and getters and setters --- Space-Invaders/main.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index a92576c58..1c0f08fff 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -27,6 +27,14 @@ class Player player_score = newScore; }; + sf::Vector2f getPosition() { + return position; + } + + void setPosition(sf::Vector2f newPosition) { + position = newPosition; + } + //New methods void takeDamage() {}; void move() {}; @@ -41,6 +49,14 @@ int main() // Create a window object with specific dimensions and a title sf::RenderWindow window(videoMode, "My SFML Window"); + //Player object + Player player; + + //Load Textures and sprite + player.player_texture.loadFromFile("assets/textures/player_ship.png"); + + player.player_sprite.setTexture(player.player_texture); + while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { @@ -49,11 +65,22 @@ int main() window.close(); } + // Handle keyboard input + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + player.move(); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + player.move(); + } // Clear the window--- window.clear(sf::Color::Black); - Player player; + player.player_sprite.setPosition(player.getPosition()); // Set the position of the player sprite + + window.draw(player.player_sprite); // Draw the player sprite + + window.display(); // Display what was drawn // Display whatever you draw window.display(); From f7c02e37d4686c4b0c40f22870c5180a40718833 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 16:01:16 +0530 Subject: [PATCH 08/41] Added move functions --- Space-Invaders/main.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 1c0f08fff..8bf8be270 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -9,7 +9,7 @@ class Player // Private Properties int health = 3; sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - int movement_speed = 5; + int movement_speed = 1; int player_score = 0; public: @@ -18,7 +18,15 @@ class Player sf::Texture player_texture; sf::Sprite player_sprite; - //Public Getter & Setter methods + //Public Functions, Getter & Setter methods + void move(float offsetX) { + position.x += offsetX; + } + + int getMoveSpeed() { + return movement_speed; + } + int getScore() { return player_score; }; @@ -35,7 +43,7 @@ class Player position = newPosition; } - //New methods + //New methods to be added void takeDamage() {}; void move() {}; void shootBullets() {}; @@ -67,20 +75,19 @@ int main() // Handle keyboard input if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - player.move(); + player.move(-1.0f* player.getMoveSpeed()); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - player.move(); + player.move(1.0f * player.getMoveSpeed()); } // Clear the window--- window.clear(sf::Color::Black); - player.player_sprite.setPosition(player.getPosition()); // Set the position of the player sprite - - window.draw(player.player_sprite); // Draw the player sprite + // Set and draw player + player.player_sprite.setPosition(player.getPosition()); - window.display(); // Display what was drawn + window.draw(player.player_sprite); // Display whatever you draw window.display(); From 45ea81265b2cef8b95f19842cf6a5780a16c62ad Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 13:23:19 +0530 Subject: [PATCH 09/41] Added Header and Source folders with GameService --- Space-Invaders/Header/GameService.h | 18 ++++++++++++++ Space-Invaders/Source/GameService.cpp | 34 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Space-Invaders/Header/GameService.h create mode 100644 Space-Invaders/Source/GameService.cpp diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h new file mode 100644 index 000000000..be6de25a8 --- /dev/null +++ b/Space-Invaders/Header/GameService.h @@ -0,0 +1,18 @@ +#pragma once + + +class GameService +{ +private: + void initialize(); // 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/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp new file mode 100644 index 000000000..0dc9832af --- /dev/null +++ b/Space-Invaders/Source/GameService.cpp @@ -0,0 +1,34 @@ +#include "../Header/GameService.h" + +void GameService::initialize() +{ +} + +void GameService::destroy() +{ +} + +GameService::GameService() +{ +} + +GameService::~GameService() +{ +} + +void GameService::ignite() +{ +} + +void GameService::update() +{ +} + +void GameService::render() +{ +} + +bool GameService::isRunning() +{ + return false; +} From e527f50120c9f729c8a745776f0b58df66e7100b Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 13:32:04 +0530 Subject: [PATCH 10/41] comments and gameloop code shift --- Space-Invaders/main.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 8bf8be270..3659330ce 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,8 +1,9 @@ #include #include +#include "Header/GameService.h" -class Player +/*class Player { private: @@ -47,10 +48,11 @@ class Player void takeDamage() {}; void move() {}; void shootBullets() {}; -}; +};*/ int main() { + /* // Define the video mode (dimensions) sf::VideoMode videoMode = sf::VideoMode(800, 600); @@ -91,7 +93,13 @@ int main() // Display whatever you draw window.display(); + }*/ + GameService game_service; + game_service.ignite(); + + while (game_service.isRunning()) + { + game_service.update(); + game_service.render(); } - - return 0; } From a5529a711a782e280382708638e0e48bfb128f9c Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 13:55:15 +0530 Subject: [PATCH 11/41] Added Service Locator header and cpp --- Space-Invaders/Header/GameService.h | 1 - Space-Invaders/Header/ServiceLocator.h | 28 ++++++++++++++++++++++ Space-Invaders/Source/ServiceLocator.cpp | 30 ++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 Space-Invaders/Header/ServiceLocator.h create mode 100644 Space-Invaders/Source/ServiceLocator.cpp diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h index be6de25a8..674677d04 100644 --- a/Space-Invaders/Header/GameService.h +++ b/Space-Invaders/Header/GameService.h @@ -1,6 +1,5 @@ #pragma once - class GameService { private: diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h new file mode 100644 index 000000000..64e5d6126 --- /dev/null +++ b/Space-Invaders/Header/ServiceLocator.h @@ -0,0 +1,28 @@ +#pragma once +// ServiceLocator Class Summary: This class manages access to various services in the application. +// include relevant headers files + +class ServiceLocator +{ +private: + + // Public Methods + ServiceLocator(); + ~ServiceLocator(); + + // Private Methods: + void createServices(); + void clearAllServices(); + +public: + + // Public Methods: + static ServiceLocator* getInstance(); + void initialize(); // Initializes the ServiceLocator. + void update(); // Updates all services. + void render(); // Renders using the services. + + // Methods to Get Specific Services: + //EventService* getEventService(); // Retrieve the EventService instance + //GraphicService* getGraphicService(); // Retrieve the GraphicService instance +}; \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp new file mode 100644 index 000000000..079366424 --- /dev/null +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -0,0 +1,30 @@ +#include "../Header/ServiceLocator.h" + +ServiceLocator::~ServiceLocator() +{ +} + +void ServiceLocator::createServices() +{ +} + +void ServiceLocator::clearAllServices() +{ +} + +ServiceLocator* ServiceLocator::getInstance() +{ + return nullptr; +} + +void ServiceLocator::initialize() +{ +} + +void ServiceLocator::update() +{ +} + +void ServiceLocator::render() +{ +} From 9c1cd5ada61a695938bb3d0927eff154137a46f9 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 15:11:59 +0530 Subject: [PATCH 12/41] Changed Architecture and added links between units --- Space-Invaders/Header/GameService.h | 8 ++++ Space-Invaders/Header/GraphicService.h | 36 ++++++++++++++++ Space-Invaders/Header/ServiceLocator.h | 7 ++- Space-Invaders/Source/GameService.cpp | 51 ++++++++++++++++------ Space-Invaders/Source/GraphicService.cpp | 55 ++++++++++++++++++++++++ Space-Invaders/Source/ServiceLocator.cpp | 47 +++++++++++++------- Space-Invaders/main.cpp | 11 ++--- 7 files changed, 180 insertions(+), 35 deletions(-) create mode 100644 Space-Invaders/Header/GraphicService.h create mode 100644 Space-Invaders/Source/GraphicService.cpp diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h index 674677d04..32230d67d 100644 --- a/Space-Invaders/Header/GameService.h +++ b/Space-Invaders/Header/GameService.h @@ -1,9 +1,17 @@ #pragma once +#include +#include "../Header/ServiceLocator.h" class GameService { private: + + ServiceLocator* service_locator; + sf::RenderWindow* game_window; + + void initialize(); // Handles game initialization. + void initializeVariables();// Handles game initialization. void destroy(); // Handles cleanup tasks. public: diff --git a/Space-Invaders/Header/GraphicService.h b/Space-Invaders/Header/GraphicService.h new file mode 100644 index 000000000..5f719bb1c --- /dev/null +++ b/Space-Invaders/Header/GraphicService.h @@ -0,0 +1,36 @@ +#pragma once +#include + +class GraphicService +{ +private: + + const std::string game_window_title = "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/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 64e5d6126..78dfe507e 100644 --- a/Space-Invaders/Header/ServiceLocator.h +++ b/Space-Invaders/Header/ServiceLocator.h @@ -1,4 +1,6 @@ #pragma once +#include "../Header/GraphicService.h" + // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -6,6 +8,9 @@ class ServiceLocator { private: + // Private Attributes: + GraphicService* graphic_service; + // Public Methods ServiceLocator(); ~ServiceLocator(); @@ -24,5 +29,5 @@ class ServiceLocator // Methods to Get Specific Services: //EventService* getEventService(); // Retrieve the EventService instance - //GraphicService* getGraphicService(); // Retrieve the GraphicService instance + GraphicService* getGraphicService(); // Retrieve the GraphicService instance }; \ No newline at end of file diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp index 0dc9832af..bf1f6f464 100644 --- a/Space-Invaders/Source/GameService.cpp +++ b/Space-Invaders/Source/GameService.cpp @@ -1,34 +1,57 @@ #include "../Header/GameService.h" +#include "../Header/GraphicService.h" -void GameService::initialize() -{ +// Constructor: Initializes pointers to null. +GameService::GameService() { + service_locator = nullptr; // Set service locator to null + game_window = nullptr; // Set game window to null } -void GameService::destroy() -{ +// Destructor: Calls the destroy function to clean up resources. +GameService::~GameService() { + destroy(); // Clean up and release resources } -GameService::GameService() -{ +// 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. } -GameService::~GameService() +//initialize service locator and other variables +void GameService::initialize() { + service_locator->initialize(); + initializeVariables(); } -void GameService::ignite() +void GameService::initializeVariables() { + game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) } -void GameService::update() +void GameService::destroy() { + // don't need to do anything here for now. } -void GameService::render() -{ +// 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 } -bool GameService::isRunning() -{ - return false; +// 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 new file mode 100644 index 000000000..8f3aae085 --- /dev/null +++ b/Space-Invaders/Source/GraphicService.cpp @@ -0,0 +1,55 @@ +#include "../Header/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; +} \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp index 079366424..b3e690efa 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -1,30 +1,47 @@ #include "../Header/ServiceLocator.h" -ServiceLocator::~ServiceLocator() -{ +// 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 } -void ServiceLocator::createServices() -{ +// Destructor: Cleans up resources by clearing all services. +ServiceLocator::~ServiceLocator() { + clearAllServices(); // Call clearAllServices to delete any allocated services } -void ServiceLocator::clearAllServices() -{ +// Creates service instances, specifically the graphic service in this case. +void ServiceLocator::createServices() { + graphic_service = new GraphicService(); // Dynamically create a GraphicService instance } -ServiceLocator* ServiceLocator::getInstance() -{ - return nullptr; +// 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 } -void ServiceLocator::initialize() -{ +// 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 } -void ServiceLocator::update() -{ +// Calls initialize on the graphic service, readying it for use. +void ServiceLocator::initialize() { + graphic_service->initialize(); // Initialize graphic service } -void ServiceLocator::render() -{ +// 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/main.cpp b/Space-Invaders/main.cpp index 3659330ce..900795c1d 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -94,12 +94,13 @@ int main() // Display whatever you draw window.display(); }*/ - GameService game_service; - game_service.ignite(); + GameService* game_service = new GameService(); - while (game_service.isRunning()) + game_service->ignite(); + + while (game_service->isRunning()) { - game_service.update(); - game_service.render(); + game_service->update(); + game_service->render(); } } From 452befa033bbd789689c2aa65aac5971af57d02e Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 15:28:20 +0530 Subject: [PATCH 13/41] added event service header and cpp --- Space-Invaders/Header/EventService.h | 28 +++++++++++ Space-Invaders/Header/ServiceLocator.h | 4 +- Space-Invaders/Source/EventService.cpp | 42 ++++++++++++++++ Space-Invaders/Source/GameService.cpp | 2 + Space-Invaders/Source/ServiceLocator.cpp | 64 +++++++++++++----------- 5 files changed, 109 insertions(+), 31 deletions(-) create mode 100644 Space-Invaders/Header/EventService.h create mode 100644 Space-Invaders/Source/EventService.cpp diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h new file mode 100644 index 000000000..80d016bb2 --- /dev/null +++ b/Space-Invaders/Header/EventService.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +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(); + +}; \ No newline at end of file diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 78dfe507e..19d7a008d 100644 --- a/Space-Invaders/Header/ServiceLocator.h +++ b/Space-Invaders/Header/ServiceLocator.h @@ -1,5 +1,6 @@ #pragma once #include "../Header/GraphicService.h" +#include "../Header/EventService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -10,6 +11,7 @@ class ServiceLocator // Private Attributes: GraphicService* graphic_service; + EventService* event_service; // Public Methods ServiceLocator(); @@ -28,6 +30,6 @@ class ServiceLocator void render(); // Renders using the services. // Methods to Get Specific Services: - //EventService* getEventService(); // Retrieve the EventService instance + EventService* getEventService(); // Retrieve the EventService instance GraphicService* getGraphicService(); // Retrieve the GraphicService instance }; \ No newline at end of file diff --git a/Space-Invaders/Source/EventService.cpp b/Space-Invaders/Source/EventService.cpp new file mode 100644 index 000000000..d8fc0d68b --- /dev/null +++ b/Space-Invaders/Source/EventService.cpp @@ -0,0 +1,42 @@ +#include "../Header/EventService.h" +#include "../Header/GameService.h" +#include "../Header/GraphicService.h" + +EventService::EventService() { game_window = nullptr; } + +EventService::~EventService() = default; //calls the default destructor + +void EventService::initialize() +{ + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); +} + +void EventService::update() +{ + //for later +} + +void EventService::processEvents() +{ + if (isGameWindowOpen()) { + while (game_window->pollEvent(game_event)) { + // Check for window closure + if (gameWindowWasClosed() || hasQuitGame()) + { + game_window->close(); + } + } + } +} + +bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered + +//checks for if a keyboard key has been pressed +bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } + +//control click on the SFML functions to see what they do internally +bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } + +bool EventService::isGameWindowOpen() { return game_window != nullptr; } + +bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } \ No newline at end of file diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp index bf1f6f464..e0fe519e8 100644 --- a/Space-Invaders/Source/GameService.cpp +++ b/Space-Invaders/Source/GameService.cpp @@ -38,6 +38,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/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp index b3e690efa..af461c730 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -1,47 +1,51 @@ #include "../Header/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 +ServiceLocator::ServiceLocator() +{ + graphic_service = nullptr; + event_service = nullptr; + createServices(); } - -// Destructor: Cleans up resources by clearing all services. -ServiceLocator::~ServiceLocator() { - clearAllServices(); // Call clearAllServices to delete any allocated services +ServiceLocator::~ServiceLocator() +{ + clearAllServices(); } -// Creates service instances, specifically the graphic service in this case. -void ServiceLocator::createServices() { - graphic_service = new GraphicService(); // Dynamically create a GraphicService instance +void ServiceLocator::createServices() +{ + graphic_service = new GraphicService(); + event_service = new EventService(); } -// 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 +void ServiceLocator::clearAllServices() +{ + delete(graphic_service); + delete(event_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 +ServiceLocator* ServiceLocator::getInstance() +{ + static ServiceLocator instance; + return &instance; } -// Calls initialize on the graphic service, readying it for use. -void ServiceLocator::initialize() { - graphic_service->initialize(); // Initialize graphic service +void ServiceLocator::initialize() +{ + graphic_service->initialize(); + event_service->initialize(); } -// Updates the state of the graphic service. -void ServiceLocator::update() { - graphic_service->update(); // Update graphic service +void ServiceLocator::update() +{ + graphic_service->update(); + event_service->update(); } -// Renders using the graphic service. -void ServiceLocator::render() { - graphic_service->render(); // Render graphic service +void ServiceLocator::render() +{ + graphic_service->render(); + //no event service because nothing to 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; } From abdf09b6af4166d62085762cb955722d13a60e9f Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 31 Jul 2024 13:30:41 +0530 Subject: [PATCH 14/41] Added Player Service Header and cpp --- Space-Invaders/Header/EventService.h | 2 + Space-Invaders/Header/PlayerService.h | 37 ++++++++++++++ Space-Invaders/Header/ServiceLocator.h | 3 ++ Space-Invaders/Source/EventService.cpp | 6 ++- Space-Invaders/Source/PlayerService.cpp | 64 ++++++++++++++++++++++++ Space-Invaders/Source/ServiceLocator.cpp | 7 +++ 6 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 Space-Invaders/Header/PlayerService.h create mode 100644 Space-Invaders/Source/PlayerService.cpp diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h index 80d016bb2..ce9882ddc 100644 --- a/Space-Invaders/Header/EventService.h +++ b/Space-Invaders/Header/EventService.h @@ -24,5 +24,7 @@ class EventService void processEvents(); // while window is open we will check for events bool pressedEscapeKey(); bool isKeyboardEvent(); + bool pressedLeftKey(); // getting inputs for the player + bool pressedRightKey(); }; \ No newline at end of file diff --git a/Space-Invaders/Header/PlayerService.h b/Space-Invaders/Header/PlayerService.h new file mode 100644 index 000000000..9aadf0a0c --- /dev/null +++ b/Space-Invaders/Header/PlayerService.h @@ -0,0 +1,37 @@ +#pragma once +#include + +class PlayerService +{ + +private: + + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 300.0f); + int movement_speed = 1; + 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(); + +}; \ No newline at end of file diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 19d7a008d..7c9a1d6ac 100644 --- a/Space-Invaders/Header/ServiceLocator.h +++ b/Space-Invaders/Header/ServiceLocator.h @@ -1,6 +1,7 @@ #pragma once #include "../Header/GraphicService.h" #include "../Header/EventService.h" +#include "../Header/PlayerService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -12,6 +13,7 @@ class ServiceLocator // Private Attributes: GraphicService* graphic_service; EventService* event_service; + PlayerService* player_service; // Public Methods ServiceLocator(); @@ -32,4 +34,5 @@ class ServiceLocator // Methods to Get Specific Services: EventService* getEventService(); // Retrieve the EventService instance GraphicService* getGraphicService(); // Retrieve the GraphicService instance + PlayerService* getPlayerService(); // Retrieve the PlayerService instance }; \ No newline at end of file diff --git a/Space-Invaders/Source/EventService.cpp b/Space-Invaders/Source/EventService.cpp index d8fc0d68b..6f2941e93 100644 --- a/Space-Invaders/Source/EventService.cpp +++ b/Space-Invaders/Source/EventService.cpp @@ -36,7 +36,9 @@ bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyP //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::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } \ No newline at end of file +// Player inputs +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/PlayerService.cpp b/Space-Invaders/Source/PlayerService.cpp new file mode 100644 index 000000000..e64c0dc1a --- /dev/null +++ b/Space-Invaders/Source/PlayerService.cpp @@ -0,0 +1,64 @@ +#include "../Header/PlayerService.h" +#include "../Header/ServiceLocator.h" + +PlayerService::PlayerService() +{ + game_window = nullptr; +} + +PlayerService::~PlayerService() = default; + +//init +void PlayerService::initialize() +{ + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +//take our players input in update, then set the position. +//order is important here +void PlayerService::update() +{ + processPlayerInput(); + player_sprite.setPosition(getPlayerPosition()); +} + +void PlayerService::render() +{ + game_window->draw(player_sprite); +} + +void PlayerService::processPlayerInput() +{ + // Handle movement - inputs now handled by eventservice + EventService* event_service = ServiceLocator::getInstance()->getEventService(); //get the event service object created in service locator + + if (event_service->isKeyboardEvent()) + { + if (event_service->pressedLeftKey()) + { + move(-1.0f * getMoveSpeed()); + } + + if (event_service->pressedRightKey()) + { + move(1.0f * getMoveSpeed()); + } + } +} + +void PlayerService::initializePlayerSprite() +{ + if (player_texture.loadFromFile(player_texture_path)) + { + player_sprite.setTexture(player_texture); + } +} + +void PlayerService::move(float offsetX) { + position.x += offsetX; +} + +//Getter and Setter Functions +sf::Vector2f PlayerService::getPlayerPosition() { return position; } +int PlayerService::getMoveSpeed() { return movement_speed; } \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp index af461c730..0e95973c7 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -4,6 +4,7 @@ ServiceLocator::ServiceLocator() { graphic_service = nullptr; event_service = nullptr; + player_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -15,12 +16,14 @@ void ServiceLocator::createServices() { graphic_service = new GraphicService(); event_service = new EventService(); + player_service = new PlayerService(); } void ServiceLocator::clearAllServices() { delete(graphic_service); delete(event_service); + delete(player_service); } ServiceLocator* ServiceLocator::getInstance() @@ -33,19 +36,23 @@ void ServiceLocator::initialize() { graphic_service->initialize(); event_service->initialize(); + player_service->initialize(); } void ServiceLocator::update() { graphic_service->update(); event_service->update(); + player_service->update(); } void ServiceLocator::render() { graphic_service->render(); + player_service->render(); //no event service because nothing to render } GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } EventService* ServiceLocator::getEventService() { return event_service; } +PlayerService* ServiceLocator::getPlayerService() { return player_service; } From e759f6e768adf7e03348899f4840dfbcb5a270ea Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 31 Jul 2024 14:26:58 +0530 Subject: [PATCH 15/41] Added time Service --- Space-Invaders/Header/GraphicService.h | 2 ++ Space-Invaders/Header/PlayerService.h | 5 ++- Space-Invaders/Header/ServiceLocator.h | 3 ++ Space-Invaders/Header/TimeService.h | 28 +++++++++++++++++ Space-Invaders/Source/GraphicService.cpp | 3 +- Space-Invaders/Source/PlayerService.cpp | 17 +++++++++-- Space-Invaders/Source/ServiceLocator.cpp | 7 +++++ Space-Invaders/Source/TimeService.cpp | 39 ++++++++++++++++++++++++ 8 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 Space-Invaders/Header/TimeService.h create mode 100644 Space-Invaders/Source/TimeService.cpp diff --git a/Space-Invaders/Header/GraphicService.h b/Space-Invaders/Header/GraphicService.h index 5f719bb1c..96ed95751 100644 --- a/Space-Invaders/Header/GraphicService.h +++ b/Space-Invaders/Header/GraphicService.h @@ -9,6 +9,8 @@ class GraphicService const int game_window_width = 800; const int game_window_height = 600; + + const int frame_rate = 60; const sf::Color window_color = sf::Color::Blue; diff --git a/Space-Invaders/Header/PlayerService.h b/Space-Invaders/Header/PlayerService.h index 9aadf0a0c..66397a35c 100644 --- a/Space-Invaders/Header/PlayerService.h +++ b/Space-Invaders/Header/PlayerService.h @@ -8,7 +8,7 @@ class PlayerService int health = 3; sf::Vector2f position = sf::Vector2f(200.0f, 300.0f); - int movement_speed = 1; + float movement_speed = 350.f; int player_score = 0; const sf::String player_texture_path = "assets/textures/player_ship.png"; @@ -21,6 +21,9 @@ class PlayerService void initializePlayerSprite(); void processPlayerInput(); + void moveLeft(); + void moveRight(); + public: PlayerService(); diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 7c9a1d6ac..df101d414 100644 --- a/Space-Invaders/Header/ServiceLocator.h +++ b/Space-Invaders/Header/ServiceLocator.h @@ -2,6 +2,7 @@ #include "../Header/GraphicService.h" #include "../Header/EventService.h" #include "../Header/PlayerService.h" +#include "../Header/TimeService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -14,6 +15,7 @@ class ServiceLocator GraphicService* graphic_service; EventService* event_service; PlayerService* player_service; + TimeService* time_service; // Public Methods ServiceLocator(); @@ -35,4 +37,5 @@ class ServiceLocator EventService* getEventService(); // Retrieve the EventService instance GraphicService* getGraphicService(); // Retrieve the GraphicService instance PlayerService* getPlayerService(); // Retrieve the PlayerService instance + TimeService* getTimeService(); // Retrieve the TimeService instance }; \ No newline at end of file diff --git a/Space-Invaders/Header/TimeService.h b/Space-Invaders/Header/TimeService.h new file mode 100644 index 000000000..24ddec14a --- /dev/null +++ b/Space-Invaders/Header/TimeService.h @@ -0,0 +1,28 @@ +#pragma once +#include + + // The TimeService class helps keep track of time in game and calculate delta time. + // Utilizes the library to calculate delta time. +class TimeService +{ +private: + + // A point in time which indicates the starting time of previous frame. + std::chrono::time_point previous_time; + + float delta_time; //to store the detla time + + void updateDeltaTime(); // method to update time + float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time + void updatePreviousTime(); // finally update the current time to be previous time + +public: + + //lifecycle methods + void initialize(); + void update(); + + //getter + float getDeltaTime(); +}; + diff --git a/Space-Invaders/Source/GraphicService.cpp b/Space-Invaders/Source/GraphicService.cpp index 8f3aae085..2cfef211f 100644 --- a/Space-Invaders/Source/GraphicService.cpp +++ b/Space-Invaders/Source/GraphicService.cpp @@ -13,7 +13,8 @@ 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 = createGameWindow(); // Assigns a new game window + game_window->setFramerateLimit(frame_rate); // setting a frame rate limit } // Creates a new SFML RenderWindow object with specified video mode and title. diff --git a/Space-Invaders/Source/PlayerService.cpp b/Space-Invaders/Source/PlayerService.cpp index e64c0dc1a..0d8e4e45e 100644 --- a/Space-Invaders/Source/PlayerService.cpp +++ b/Space-Invaders/Source/PlayerService.cpp @@ -37,16 +37,27 @@ void PlayerService::processPlayerInput() { if (event_service->pressedLeftKey()) { - move(-1.0f * getMoveSpeed()); + moveLeft(); } if (event_service->pressedRightKey()) { - move(1.0f * getMoveSpeed()); + moveRight(); } } } +// New movement methods +void PlayerService::moveLeft() +{ + position.x -= movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); +} + +void PlayerService::moveRight() +{ + position.x += movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); +} + void PlayerService::initializePlayerSprite() { if (player_texture.loadFromFile(player_texture_path)) @@ -56,7 +67,7 @@ void PlayerService::initializePlayerSprite() } void PlayerService::move(float offsetX) { - position.x += offsetX; + position.x += offsetX * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); } //Getter and Setter Functions diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp index 0e95973c7..a8fbc2d90 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -5,6 +5,7 @@ ServiceLocator::ServiceLocator() graphic_service = nullptr; event_service = nullptr; player_service = nullptr; + time_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -17,6 +18,7 @@ void ServiceLocator::createServices() graphic_service = new GraphicService(); event_service = new EventService(); player_service = new PlayerService(); + time_service = new TimeService(); } void ServiceLocator::clearAllServices() @@ -24,6 +26,7 @@ void ServiceLocator::clearAllServices() delete(graphic_service); delete(event_service); delete(player_service); + delete(time_service); } ServiceLocator* ServiceLocator::getInstance() @@ -37,6 +40,7 @@ void ServiceLocator::initialize() graphic_service->initialize(); event_service->initialize(); player_service->initialize(); + time_service->initialize(); } void ServiceLocator::update() @@ -44,6 +48,7 @@ void ServiceLocator::update() graphic_service->update(); event_service->update(); player_service->update(); + time_service->update(); } void ServiceLocator::render() @@ -51,8 +56,10 @@ void ServiceLocator::render() graphic_service->render(); player_service->render(); //no event service because nothing to render + //no time 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 new file mode 100644 index 000000000..7ede82971 --- /dev/null +++ b/Space-Invaders/Source/TimeService.cpp @@ -0,0 +1,39 @@ +#include "../Header/TimeService.h" + +void TimeService::initialize() +{ + previous_time = std::chrono::steady_clock::now(); + delta_time = 0; +} + +void TimeService::update() +{ + updateDeltaTime(); +} + +float TimeService::getDeltaTime() +{ + return delta_time; +} + +void TimeService::updateDeltaTime() +{ + delta_time = calculateDeltaTime(); + updatePreviousTime(); +} + +float TimeService::calculateDeltaTime() +{ + // 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. + return static_cast(delta) / static_cast(1000000); +} + +// Update previous_time to the current time +void TimeService::updatePreviousTime() +{ + previous_time = std::chrono::steady_clock::now(); +} From 943dfd9925012b9808f071d5bd12b06d098ca778 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 31 Jul 2024 15:23:11 +0530 Subject: [PATCH 16/41] Split the player into 3 different units --- .../Header/Player/PlayerController.h | 1 + Space-Invaders/Header/Player/PlayerModel.h | 43 ++++++++++++++++++ Space-Invaders/Header/Player/PlayerView.h | 1 + .../Source/Player/PlayerController.cpp | 0 Space-Invaders/Source/Player/PlayerModel.cpp | 45 +++++++++++++++++++ Space-Invaders/Source/Player/PlayerView.cpp | 0 6 files changed, 90 insertions(+) create mode 100644 Space-Invaders/Header/Player/PlayerController.h create mode 100644 Space-Invaders/Header/Player/PlayerModel.h create mode 100644 Space-Invaders/Header/Player/PlayerView.h create mode 100644 Space-Invaders/Source/Player/PlayerController.cpp create mode 100644 Space-Invaders/Source/Player/PlayerModel.cpp create mode 100644 Space-Invaders/Source/Player/PlayerView.cpp diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h new file mode 100644 index 000000000..50e96676b --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerController.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h new file mode 100644 index 000000000..cf048eca5 --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerModel.h @@ -0,0 +1,43 @@ +#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); + + sf::Vector2f player_position; + PlayerState player_state; //Declaration + int player_score; + +public: + const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); + const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); + + const float player_movement_speed = 200.0f; + + PlayerModel(); + ~PlayerModel(); + + void initialize(); + void reset(); + + sf::Vector2f getPlayerPosition(); + void setPlayerPosition(sf::Vector2f position); + + int getPlayerScore(); + void setPlayerScore(int score); + + //new getter and setter + PlayerState getPlayerState(); + void setPlayerState(PlayerState state); + + +}; diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h new file mode 100644 index 000000000..50e96676b --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerView.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Source/Player/PlayerModel.cpp b/Space-Invaders/Source/Player/PlayerModel.cpp new file mode 100644 index 000000000..0d51db820 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerModel.cpp @@ -0,0 +1,45 @@ +#include "../../Header/Player/PlayerModel.h" + +PlayerModel::PlayerModel() { } + +PlayerModel::~PlayerModel() { } + +void PlayerModel::initialize() { reset(); } // remember to call reset() + +void PlayerModel::reset() +{ + player_state = PlayerState::ALIVE; // set state to alive + player_position = initial_player_position; + player_score = 0; +} + +sf::Vector2f PlayerModel::getPlayerPosition() +{ + return player_position; +} + +void PlayerModel::setPlayerPosition(sf::Vector2f position) +{ + player_position = position; +} + +int PlayerModel::getPlayerScore() +{ + return player_score; +} + +void PlayerModel::setPlayerScore(int score) +{ + player_score = score; +} + +//.. +PlayerState PlayerModel::getPlayerState() +{ + return player_state; +} + +void PlayerModel::setPlayerState(PlayerState state) +{ + player_state = state; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp new file mode 100644 index 000000000..e69de29bb From e01b88b7f463b44ebfa31ca910838490bc97376e Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 31 Jul 2024 15:38:14 +0530 Subject: [PATCH 17/41] Added code to player units --- .../Header/Player/PlayerController.h | 24 +++++++ Space-Invaders/Header/Player/PlayerView.h | 30 ++++++++ Space-Invaders/Header/PlayerService.h | 40 +++-------- .../Source/Player/PlayerController.cpp | 72 +++++++++++++++++++ Space-Invaders/Source/Player/PlayerView.cpp | 50 +++++++++++++ Space-Invaders/Source/PlayerService.cpp | 68 +++--------------- 6 files changed, 194 insertions(+), 90 deletions(-) diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h index 50e96676b..9ea24d95d 100644 --- a/Space-Invaders/Header/Player/PlayerController.h +++ b/Space-Invaders/Header/Player/PlayerController.h @@ -1 +1,25 @@ #pragma once +#include +#include "../Player/PlayerModel.h" +#include "../Player/PlayerView.h" + +class PlayerController +{ +private: + PlayerView* player_view; + PlayerModel* player_model; + + void processPlayerInput(); + void moveLeft(); + void moveRight(); + +public: + PlayerController(); + ~PlayerController(); + + void initialize(); + void update(); + void render(); + + sf::Vector2f getPlayerPosition(); +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h index 50e96676b..8a1aa85e9 100644 --- a/Space-Invaders/Header/Player/PlayerView.h +++ b/Space-Invaders/Header/Player/PlayerView.h @@ -1 +1,31 @@ #pragma once +#include +#include "../../Header/Player/PlayerController.h" + +class PlayerView +{ +private: + + const sf::String player_texture_path = "assets/textures/player_ship.png"; + const float player_sprite_width = 60.f; + const float player_sprite_height = 60.f; + + sf::RenderWindow* game_window; + + sf::Texture player_texture; + sf::Sprite player_sprite; + + void initializePlayerSprite(); + void scalePlayerSprite(); + + PlayerController* player_controller; // ptr to player controller + +public: + PlayerView(); + ~PlayerView(); + + void initialize(); + void update(); + void render(); + void initialize(PlayerController* controller); +}; \ No newline at end of file diff --git a/Space-Invaders/Header/PlayerService.h b/Space-Invaders/Header/PlayerService.h index 66397a35c..2f46d4894 100644 --- a/Space-Invaders/Header/PlayerService.h +++ b/Space-Invaders/Header/PlayerService.h @@ -1,40 +1,16 @@ #pragma once -#include +#include "../../Header/Player/PlayerController.h" class PlayerService { - private: - - int health = 3; - sf::Vector2f position = sf::Vector2f(200.0f, 300.0f); - float movement_speed = 350.f; - 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(); - - void moveLeft(); - void moveRight(); + PlayerController* player_controller; public: + PlayerService(); + ~PlayerService(); - PlayerService(); - ~PlayerService(); - - void initialize(); - void update(); - void render(); - - void move(float offsetX); - int getMoveSpeed(); - sf::Vector2f getPlayerPosition(); - -}; \ No newline at end of file + void initialize(); + void update(); + void render(); +}; diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp index e69de29bb..19181c698 100644 --- a/Space-Invaders/Source/Player/PlayerController.cpp +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -0,0 +1,72 @@ +#include "../../Header/Player/PlayerController.h" +#include "../../Header/EventService.h" +#include "../../Header/ServiceLocator.h" +#include + +PlayerController::PlayerController() +{ + player_view = new PlayerView(); + player_model = new PlayerModel(); +} + +PlayerController::~PlayerController() +{ + delete (player_view); + delete (player_model); +} +//the controller is responsible for calling the lifecycle methods for the other two +void PlayerController::initialize() +{ + player_model->initialize(); + + //This will give an error right now since we haven't included the controller in the view. + player_view->initialize(this); // 'this' refers to the class we are currently inside +} + +void PlayerController::update() +{ + processPlayerInput(); + player_view->update(); // we update() the view +} + +void PlayerController::render() +{ + player_view->render(); // render the view +} + +sf::Vector2f PlayerController::getPlayerPosition() +{ + return player_model->getPlayerPosition(); +} + +void PlayerController::processPlayerInput() +{ + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + { + moveLeft(); + } + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + { + moveRight(); + } +} + +void PlayerController::moveLeft() +{ + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); + player_model->setPlayerPosition(currentPosition); +} + +void PlayerController::moveRight() +{ + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); + player_model->setPlayerPosition(currentPosition); +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp index e69de29bb..93762cd15 100644 --- a/Space-Invaders/Source/Player/PlayerView.cpp +++ b/Space-Invaders/Source/Player/PlayerView.cpp @@ -0,0 +1,50 @@ +#include "../../Header/Player/PlayerView.h" +#include "../../Header/ServiceLocator.h" + +PlayerView::PlayerView() { } + +PlayerView::~PlayerView() { } + +void PlayerView::initialize() +{ + + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +void PlayerView::initialize(PlayerController* controller) +{ + player_controller = controller; //to later use it for setting position + 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() +{ + //set the updated position before we render + player_sprite.setPosition(player_controller->getPlayerPosition()); +} + +void PlayerView::render() +{ + game_window->draw(player_sprite); +} \ No newline at end of file diff --git a/Space-Invaders/Source/PlayerService.cpp b/Space-Invaders/Source/PlayerService.cpp index 0d8e4e45e..8b19d77f9 100644 --- a/Space-Invaders/Source/PlayerService.cpp +++ b/Space-Invaders/Source/PlayerService.cpp @@ -1,75 +1,27 @@ #include "../Header/PlayerService.h" -#include "../Header/ServiceLocator.h" +#include "../Header/Player/PlayerController.h" PlayerService::PlayerService() { - game_window = nullptr; + player_controller = new PlayerController(); } -PlayerService::~PlayerService() = default; +PlayerService::~PlayerService() +{ + delete (player_controller); +} -//init void PlayerService::initialize() { - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); + player_controller->initialize(); } -//take our players input in update, then set the position. -//order is important here void PlayerService::update() { - processPlayerInput(); - player_sprite.setPosition(getPlayerPosition()); + player_controller->update(); } void PlayerService::render() { - game_window->draw(player_sprite); -} - -void PlayerService::processPlayerInput() -{ - // Handle movement - inputs now handled by eventservice - EventService* event_service = ServiceLocator::getInstance()->getEventService(); //get the event service object created in service locator - - if (event_service->isKeyboardEvent()) - { - if (event_service->pressedLeftKey()) - { - moveLeft(); - } - - if (event_service->pressedRightKey()) - { - moveRight(); - } - } -} - -// New movement methods -void PlayerService::moveLeft() -{ - position.x -= movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); -} - -void PlayerService::moveRight() -{ - position.x += movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); -} - -void PlayerService::initializePlayerSprite() -{ - if (player_texture.loadFromFile(player_texture_path)) - { - player_sprite.setTexture(player_texture); - } -} - -void PlayerService::move(float offsetX) { - position.x += offsetX * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); -} - -//Getter and Setter Functions -sf::Vector2f PlayerService::getPlayerPosition() { return position; } -int PlayerService::getMoveSpeed() { return movement_speed; } \ No newline at end of file + player_controller->render(); +} \ No newline at end of file From 621747db4a8ed9a8c99a3409b47593db7dd674bb Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:13:13 +0530 Subject: [PATCH 18/41] Delete Space-Invaders/Header directory --- Space-Invaders/Header/EventService.h | 28 -------------------- Space-Invaders/Header/GameService.h | 25 ------------------ Space-Invaders/Header/GraphicService.h | 36 -------------------------- Space-Invaders/Header/ServiceLocator.h | 35 ------------------------- 4 files changed, 124 deletions(-) delete mode 100644 Space-Invaders/Header/EventService.h delete mode 100644 Space-Invaders/Header/GameService.h delete mode 100644 Space-Invaders/Header/GraphicService.h delete mode 100644 Space-Invaders/Header/ServiceLocator.h diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h deleted file mode 100644 index 80d016bb2..000000000 --- a/Space-Invaders/Header/EventService.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include - -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(); - -}; \ No newline at end of file diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h deleted file mode 100644 index 32230d67d..000000000 --- a/Space-Invaders/Header/GameService.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include "../Header/ServiceLocator.h" - -class GameService -{ -private: - - ServiceLocator* service_locator; - sf::RenderWindow* game_window; - - - void initialize(); // Handles game initialization. - void initializeVariables();// Handles game initialization. - void destroy(); // Handles cleanup tasks. - -public: - GameService(); // Constructor for initializing the GameService object. - ~GameService(); // Destructor for cleaning up resources upon object deletion. - - void ignite(); // Initiates the game. - void update(); // Updates the game logic and game state. - void render(); // Renders each frame of the game. - bool isRunning(); // Checks if the game is currently running. -}; \ No newline at end of file diff --git a/Space-Invaders/Header/GraphicService.h b/Space-Invaders/Header/GraphicService.h deleted file mode 100644 index 5f719bb1c..000000000 --- a/Space-Invaders/Header/GraphicService.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once -#include - -class GraphicService -{ -private: - - const std::string game_window_title = "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/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h deleted file mode 100644 index 19d7a008d..000000000 --- a/Space-Invaders/Header/ServiceLocator.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#include "../Header/GraphicService.h" -#include "../Header/EventService.h" - -// ServiceLocator Class Summary: This class manages access to various services in the application. -// include relevant headers files - -class ServiceLocator -{ -private: - - // Private Attributes: - GraphicService* graphic_service; - EventService* event_service; - - // Public Methods - ServiceLocator(); - ~ServiceLocator(); - - // Private Methods: - void createServices(); - void clearAllServices(); - -public: - - // Public Methods: - static ServiceLocator* getInstance(); - void initialize(); // Initializes the ServiceLocator. - void update(); // Updates all services. - void render(); // Renders using the services. - - // Methods to Get Specific Services: - EventService* getEventService(); // Retrieve the EventService instance - GraphicService* getGraphicService(); // Retrieve the GraphicService instance -}; \ No newline at end of file From 433948443619e10fb6cd4f48589262f61a25e7e2 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:13:29 +0530 Subject: [PATCH 19/41] Delete Space-Invaders/Source directory --- Space-Invaders/Source/EventService.cpp | 42 ----------------- Space-Invaders/Source/GameService.cpp | 59 ------------------------ Space-Invaders/Source/GraphicService.cpp | 55 ---------------------- Space-Invaders/Source/ServiceLocator.cpp | 51 -------------------- 4 files changed, 207 deletions(-) delete mode 100644 Space-Invaders/Source/EventService.cpp delete mode 100644 Space-Invaders/Source/GameService.cpp delete mode 100644 Space-Invaders/Source/GraphicService.cpp delete mode 100644 Space-Invaders/Source/ServiceLocator.cpp diff --git a/Space-Invaders/Source/EventService.cpp b/Space-Invaders/Source/EventService.cpp deleted file mode 100644 index d8fc0d68b..000000000 --- a/Space-Invaders/Source/EventService.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "../Header/EventService.h" -#include "../Header/GameService.h" -#include "../Header/GraphicService.h" - -EventService::EventService() { game_window = nullptr; } - -EventService::~EventService() = default; //calls the default destructor - -void EventService::initialize() -{ - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); -} - -void EventService::update() -{ - //for later -} - -void EventService::processEvents() -{ - if (isGameWindowOpen()) { - while (game_window->pollEvent(game_event)) { - // Check for window closure - if (gameWindowWasClosed() || hasQuitGame()) - { - game_window->close(); - } - } - } -} - -bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered - -//checks for if a keyboard key has been pressed -bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } - -//control click on the SFML functions to see what they do internally -bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } - -bool EventService::isGameWindowOpen() { return game_window != nullptr; } - -bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } \ No newline at end of file diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp deleted file mode 100644 index e0fe519e8..000000000 --- a/Space-Invaders/Source/GameService.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "../Header/GameService.h" -#include "../Header/GraphicService.h" - -// Constructor: Initializes pointers to null. -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->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 deleted file mode 100644 index 8f3aae085..000000000 --- a/Space-Invaders/Source/GraphicService.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "../Header/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; -} \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp deleted file mode 100644 index af461c730..000000000 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "../Header/ServiceLocator.h" - -ServiceLocator::ServiceLocator() -{ - graphic_service = nullptr; - event_service = nullptr; - createServices(); -} -ServiceLocator::~ServiceLocator() -{ - clearAllServices(); -} - -void ServiceLocator::createServices() -{ - graphic_service = new GraphicService(); - event_service = new EventService(); -} - -void ServiceLocator::clearAllServices() -{ - delete(graphic_service); - delete(event_service); -} - -ServiceLocator* ServiceLocator::getInstance() -{ - static ServiceLocator instance; - return &instance; -} - -void ServiceLocator::initialize() -{ - graphic_service->initialize(); - event_service->initialize(); -} - -void ServiceLocator::update() -{ - graphic_service->update(); - event_service->update(); -} - -void ServiceLocator::render() -{ - graphic_service->render(); - //no event service because nothing to render -} - -GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } -EventService* ServiceLocator::getEventService() { return event_service; } From 8df483d9826b1438e05817de5ef6aef66245fe52 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:14:16 +0530 Subject: [PATCH 20/41] Delete Space-Invaders/main.cpp --- Space-Invaders/main.cpp | 106 ---------------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 Space-Invaders/main.cpp diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp deleted file mode 100644 index 900795c1d..000000000 --- a/Space-Invaders/main.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include -#include -#include "Header/GameService.h" - - -/*class Player -{ -private: - - // Private Properties - int health = 3; - sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - int movement_speed = 1; - int player_score = 0; - -public: - - // Public Properties - sf::Texture player_texture; - sf::Sprite player_sprite; - - //Public Functions, Getter & Setter methods - void move(float offsetX) { - position.x += offsetX; - } - - int getMoveSpeed() { - return movement_speed; - } - - int getScore() { - return player_score; - }; - - void setScore(int newScore) { - player_score = newScore; - }; - - sf::Vector2f getPosition() { - return position; - } - - void setPosition(sf::Vector2f newPosition) { - position = newPosition; - } - - //New methods to be added - void takeDamage() {}; - void move() {}; - void shootBullets() {}; -};*/ - -int main() -{ - /* - // Define the video mode (dimensions) - sf::VideoMode videoMode = sf::VideoMode(800, 600); - - // Create a window object with specific dimensions and a title - sf::RenderWindow window(videoMode, "My SFML Window"); - - //Player object - Player player; - - //Load Textures and sprite - player.player_texture.loadFromFile("assets/textures/player_ship.png"); - - player.player_sprite.setTexture(player.player_texture); - - while (window.isOpen()) { - sf::Event event; - while (window.pollEvent(event)) { - // Check for window closure - if (event.type == sf::Event::Closed) - window.close(); - } - - // Handle keyboard input - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - player.move(-1.0f* player.getMoveSpeed()); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - player.move(1.0f * player.getMoveSpeed()); - } - - // Clear the window--- - window.clear(sf::Color::Black); - - // Set and draw player - player.player_sprite.setPosition(player.getPosition()); - - window.draw(player.player_sprite); - - // Display whatever you draw - window.display(); - }*/ - GameService* game_service = new GameService(); - - game_service->ignite(); - - while (game_service->isRunning()) - { - game_service->update(); - game_service->render(); - } -} From f01bc02802256f090ce092c68b287ac91f84192c Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:14:55 +0530 Subject: [PATCH 21/41] Delete Space-Invaders/Space-Invaders.sln --- Space-Invaders/Space-Invaders.sln | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 Space-Invaders/Space-Invaders.sln diff --git a/Space-Invaders/Space-Invaders.sln b/Space-Invaders/Space-Invaders.sln deleted file mode 100644 index 21ec086fc..000000000 --- a/Space-Invaders/Space-Invaders.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.7.34024.191 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Space-Invaders", "Space-Invaders.vcxproj", "{AB3664CD-9870-4359-8811-B481DB3B55A0}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x64.ActiveCfg = Debug|x64 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x64.Build.0 = Debug|x64 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x86.ActiveCfg = Debug|Win32 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x86.Build.0 = Debug|Win32 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x64.ActiveCfg = Release|x64 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x64.Build.0 = Release|x64 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x86.ActiveCfg = Release|Win32 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {F8206701-92F6-4A4D-B0E5-F40E32131C64} - EndGlobalSection -EndGlobal From 5844287564978c1a5be646e8a9dd43e40be5f1ab Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:16:54 +0530 Subject: [PATCH 22/41] New Feature 3 Arch branch --- Space-Invaders/Header/Event/EventService.h | 30 ++ Space-Invaders/Header/Global/ServiceLocator.h | 41 +++ .../Header/Graphic/GraphicService.h | 38 +++ Space-Invaders/Header/Main/GameService.h | 25 ++ .../Header/Player/PlayerController.h | 28 ++ Space-Invaders/Header/Player/PlayerModel.h | 43 +++ Space-Invaders/Header/Player/PlayerService.h | 16 + Space-Invaders/Header/Player/PlayerView.h | 31 ++ Space-Invaders/Header/Time/TimeService.h | 28 ++ Space-Invaders/Source/Event/EventService.cpp | 44 +++ .../Source/Global/ServiceLocator.cpp | 65 ++++ .../Source/Graphic/GraphicService.cpp | 56 ++++ Space-Invaders/Source/Main/GameService.cpp | 59 ++++ .../Source/Player/PlayerController.cpp | 74 +++++ Space-Invaders/Source/Player/PlayerModel.cpp | 45 +++ .../Source/Player/PlayerService.cpp | 27 ++ Space-Invaders/Source/Player/PlayerView.cpp | 50 +++ Space-Invaders/Source/Time/TimeService.cpp | 39 +++ Space-Invaders/Space-Invaders.sln | 31 ++ Space-Invaders/Space-Invaders.vcxproj | 298 ++++++++++-------- Space-Invaders/Space-Invaders.vcxproj.filters | 98 ++++-- Space-Invaders/Space-Invaders.vcxproj.user | 10 +- Space-Invaders/main.cpp | 106 +++++++ 23 files changed, 1117 insertions(+), 165 deletions(-) create mode 100644 Space-Invaders/Header/Event/EventService.h create mode 100644 Space-Invaders/Header/Global/ServiceLocator.h create mode 100644 Space-Invaders/Header/Graphic/GraphicService.h create mode 100644 Space-Invaders/Header/Main/GameService.h create mode 100644 Space-Invaders/Header/Player/PlayerController.h create mode 100644 Space-Invaders/Header/Player/PlayerModel.h create mode 100644 Space-Invaders/Header/Player/PlayerService.h create mode 100644 Space-Invaders/Header/Player/PlayerView.h create mode 100644 Space-Invaders/Header/Time/TimeService.h create mode 100644 Space-Invaders/Source/Event/EventService.cpp create mode 100644 Space-Invaders/Source/Global/ServiceLocator.cpp create mode 100644 Space-Invaders/Source/Graphic/GraphicService.cpp create mode 100644 Space-Invaders/Source/Main/GameService.cpp create mode 100644 Space-Invaders/Source/Player/PlayerController.cpp create mode 100644 Space-Invaders/Source/Player/PlayerModel.cpp create mode 100644 Space-Invaders/Source/Player/PlayerService.cpp create mode 100644 Space-Invaders/Source/Player/PlayerView.cpp create mode 100644 Space-Invaders/Source/Time/TimeService.cpp create mode 100644 Space-Invaders/Space-Invaders.sln create mode 100644 Space-Invaders/main.cpp diff --git a/Space-Invaders/Header/Event/EventService.h b/Space-Invaders/Header/Event/EventService.h new file mode 100644 index 000000000..ce9882ddc --- /dev/null +++ b/Space-Invaders/Header/Event/EventService.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +class EventService +{ +private: + sf::Event game_event; //event var + sf::RenderWindow* game_window; //ptr to our game window + + bool isGameWindowOpen(); + bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. + bool hasQuitGame(); //for our new 'ESC' condition + + + +public: + EventService(); + ~EventService(); + + void initialize(); + void update(); + void processEvents(); // while window is open we will check for events + bool pressedEscapeKey(); + bool isKeyboardEvent(); + bool pressedLeftKey(); // getting inputs for the player + bool pressedRightKey(); + +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h new file mode 100644 index 000000000..332e1549f --- /dev/null +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -0,0 +1,41 @@ +#pragma once +#include "../../Header/Graphic/GraphicService.h" +#include "../../Header/Event/EventService.h" +#include "../../Header/Player/PlayerService.h" +#include "../../Header/Time/TimeService.h" + +// ServiceLocator Class Summary: This class manages access to various services in the application. +// include relevant headers files + +class ServiceLocator +{ +private: + + // Private Attributes: + GraphicService* graphic_service; + EventService* event_service; + PlayerService* player_service; + TimeService* time_service; + + // Public Methods + ServiceLocator(); + ~ServiceLocator(); + + // Private Methods: + void createServices(); + void clearAllServices(); + +public: + + // Public Methods: + static ServiceLocator* getInstance(); + void initialize(); // Initializes the ServiceLocator. + void update(); // Updates all services. + void render(); // Renders using the services. + + // Methods to Get Specific Services: + EventService* getEventService(); // Retrieve the EventService instance + GraphicService* getGraphicService(); // Retrieve the GraphicService instance + PlayerService* getPlayerService(); // Retrieve the PlayerService instance + TimeService* getTimeService(); // Retrieve the TimeService instance +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Graphic/GraphicService.h b/Space-Invaders/Header/Graphic/GraphicService.h new file mode 100644 index 000000000..83674fde3 --- /dev/null +++ b/Space-Invaders/Header/Graphic/GraphicService.h @@ -0,0 +1,38 @@ +#pragma once +#include + +class GraphicService +{ +private: + + const std::string game_window_title = "Alien Invader"; + + const int game_window_width = 800; + const int game_window_height = 600; + + const int frame_rate = 60; + + const sf::Color window_color = sf::Color::Black; + + sf::VideoMode* video_mode; // ptr to video mode + sf::RenderWindow* game_window; // ptr to a RenderWindow + + void setVideoMode(); // Method for setting our video mode + void onDestroy(); // method to run when window is deleted + +public: + GraphicService(); + ~GraphicService(); //cleanup + + //method to create the game window. returns a pointer to an instance of the game window + sf::RenderWindow* createGameWindow(); + + + void initialize(); //lifecycle functions + void update(); //.. + void render(); //.. + bool isGameWindowOpen(); //check if the window is open + + sf::RenderWindow* getGameWindow(); //getter for the game window instance + sf::Color getWindowColor();//get the color +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h new file mode 100644 index 000000000..53f95bee5 --- /dev/null +++ b/Space-Invaders/Header/Main/GameService.h @@ -0,0 +1,25 @@ +#pragma once +#include +#include "../../Header/Global/ServiceLocator.h" + +class GameService +{ +private: + + ServiceLocator* service_locator; + sf::RenderWindow* game_window; + + + void initialize(); // Handles game initialization. + void initializeVariables();// Handles game initialization. + void destroy(); // Handles cleanup tasks. + +public: + GameService(); // Constructor for initializing the GameService object. + ~GameService(); // Destructor for cleaning up resources upon object deletion. + + void ignite(); // Initiates the game. + void update(); // Updates the game logic and game state. + void render(); // Renders each frame of the game. + bool isRunning(); // Checks if the game is currently running. +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h new file mode 100644 index 000000000..36fda230a --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerController.h @@ -0,0 +1,28 @@ +#pragma once +#include + +enum class PlayerState; +class PlayerView; +class PlayerModel; + +class PlayerController +{ +private: + + PlayerView* player_view; + PlayerModel* player_model; + + void processPlayerInput(); + void moveLeft(); + void moveRight(); + +public: + PlayerController(); + ~PlayerController(); + + void initialize(); + void update(); + void render(); + + sf::Vector2f getPlayerPosition(); +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h new file mode 100644 index 000000000..13d61e0ea --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerModel.h @@ -0,0 +1,43 @@ +#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(400.f, 400.f); + + sf::Vector2f player_position; + PlayerState player_state; //Declaration + int player_score; + +public: + const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); + const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); + + const float player_movement_speed = 300.0f; + + PlayerModel(); + ~PlayerModel(); + + void initialize(); + void reset(); + + sf::Vector2f getPlayerPosition(); + void setPlayerPosition(sf::Vector2f position); + + int getPlayerScore(); + void setPlayerScore(int score); + + //new getter and setter + PlayerState getPlayerState(); + void setPlayerState(PlayerState state); + + +}; diff --git a/Space-Invaders/Header/Player/PlayerService.h b/Space-Invaders/Header/Player/PlayerService.h new file mode 100644 index 000000000..2f46d4894 --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerService.h @@ -0,0 +1,16 @@ +#pragma once +#include "../../Header/Player/PlayerController.h" + +class PlayerService +{ +private: + PlayerController* player_controller; + +public: + PlayerService(); + ~PlayerService(); + + void initialize(); + void update(); + void render(); +}; diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h new file mode 100644 index 000000000..8a1aa85e9 --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerView.h @@ -0,0 +1,31 @@ +#pragma once +#include +#include "../../Header/Player/PlayerController.h" + +class PlayerView +{ +private: + + const sf::String player_texture_path = "assets/textures/player_ship.png"; + const float player_sprite_width = 60.f; + const float player_sprite_height = 60.f; + + sf::RenderWindow* game_window; + + sf::Texture player_texture; + sf::Sprite player_sprite; + + void initializePlayerSprite(); + void scalePlayerSprite(); + + PlayerController* player_controller; // ptr to player controller + +public: + PlayerView(); + ~PlayerView(); + + void initialize(); + void update(); + void render(); + void initialize(PlayerController* controller); +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Time/TimeService.h b/Space-Invaders/Header/Time/TimeService.h new file mode 100644 index 000000000..24ddec14a --- /dev/null +++ b/Space-Invaders/Header/Time/TimeService.h @@ -0,0 +1,28 @@ +#pragma once +#include + + // The TimeService class helps keep track of time in game and calculate delta time. + // Utilizes the library to calculate delta time. +class TimeService +{ +private: + + // A point in time which indicates the starting time of previous frame. + std::chrono::time_point previous_time; + + float delta_time; //to store the detla time + + void updateDeltaTime(); // method to update time + float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time + void updatePreviousTime(); // finally update the current time to be previous time + +public: + + //lifecycle methods + void initialize(); + void update(); + + //getter + float getDeltaTime(); +}; + diff --git a/Space-Invaders/Source/Event/EventService.cpp b/Space-Invaders/Source/Event/EventService.cpp new file mode 100644 index 000000000..9be3a667d --- /dev/null +++ b/Space-Invaders/Source/Event/EventService.cpp @@ -0,0 +1,44 @@ +#include "../../Header/Event/EventService.h" +#include "../../Header/Main/GameService.h" +#include "../../Header/Graphic/GraphicService.h" + +EventService::EventService() { game_window = nullptr; } + +EventService::~EventService() = default; //calls the default destructor + +void EventService::initialize() +{ + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); +} + +void EventService::update() +{ + //for later +} + +void EventService::processEvents() +{ + if (isGameWindowOpen()) { + while (game_window->pollEvent(game_event)) { + // Check for window closure + if (gameWindowWasClosed() || hasQuitGame()) + { + game_window->close(); + } + } + } +} + +bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered + +//checks for if a keyboard key has been pressed +bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } + +//control click on the SFML functions to see what they do internally +bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } +bool EventService::isGameWindowOpen() { return game_window != nullptr; } +bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } + +// Player inputs +bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } +bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } \ No newline at end of file diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp new file mode 100644 index 000000000..4c239d74b --- /dev/null +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -0,0 +1,65 @@ +#include "../../Header/Global/ServiceLocator.h" + +ServiceLocator::ServiceLocator() +{ + graphic_service = nullptr; + event_service = nullptr; + player_service = nullptr; + time_service = nullptr; + createServices(); +} +ServiceLocator::~ServiceLocator() +{ + clearAllServices(); +} + +void ServiceLocator::createServices() +{ + graphic_service = new GraphicService(); + event_service = new EventService(); + player_service = new PlayerService(); + time_service = new TimeService(); +} + +void ServiceLocator::clearAllServices() +{ + delete(graphic_service); + delete(event_service); + delete(player_service); + delete(time_service); +} + +ServiceLocator* ServiceLocator::getInstance() +{ + static ServiceLocator instance; + return &instance; +} + +void ServiceLocator::initialize() +{ + graphic_service->initialize(); + event_service->initialize(); + player_service->initialize(); + time_service->initialize(); +} + +void ServiceLocator::update() +{ + graphic_service->update(); + event_service->update(); + player_service->update(); + time_service->update(); +} + +void ServiceLocator::render() +{ + graphic_service->render(); + player_service->render(); + //no event service because nothing to render + //no time 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/Graphic/GraphicService.cpp b/Space-Invaders/Source/Graphic/GraphicService.cpp new file mode 100644 index 000000000..4c8bb04ab --- /dev/null +++ b/Space-Invaders/Source/Graphic/GraphicService.cpp @@ -0,0 +1,56 @@ +#include "../../Header/Graphic/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 + game_window->setFramerateLimit(frame_rate); // setting a frame rate limit +} + +// 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; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp new file mode 100644 index 000000000..12918afe4 --- /dev/null +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -0,0 +1,59 @@ +#include "../../Header/Main/GameService.h" +#include "../../Header/Graphic/GraphicService.h" + +// Constructor: Initializes pointers to null. +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->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/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp new file mode 100644 index 000000000..14b9754c9 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -0,0 +1,74 @@ +#include "../../Header/Player/PlayerController.h" +#include "../../Header/Player/PlayerModel.h" +#include "../../Header/Player/PlayerView.h" +#include "../../Header/Event/EventService.h" +#include "../../Header/Global/ServiceLocator.h" +#include + +PlayerController::PlayerController() +{ + player_view = new PlayerView(); + player_model = new PlayerModel(); +} + +PlayerController::~PlayerController() +{ + delete (player_view); + delete (player_model); +} +//the controller is responsible for calling the lifecycle methods for the other two +void PlayerController::initialize() +{ + player_model->initialize(); + + //This will give an error right now since we haven't included the controller in the view. + player_view->initialize(this); // 'this' refers to the class we are currently inside +} + +void PlayerController::update() +{ + processPlayerInput(); + player_view->update(); // we update() the view +} + +void PlayerController::render() +{ + player_view->render(); // render the view +} + +sf::Vector2f PlayerController::getPlayerPosition() +{ + return player_model->getPlayerPosition(); +} + +void PlayerController::processPlayerInput() +{ + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + { + moveLeft(); + } + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + { + moveRight(); + } +} + +void PlayerController::moveLeft() +{ + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); + player_model->setPlayerPosition(currentPosition); +} + +void PlayerController::moveRight() +{ + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); + player_model->setPlayerPosition(currentPosition); +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerModel.cpp b/Space-Invaders/Source/Player/PlayerModel.cpp new file mode 100644 index 000000000..0d51db820 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerModel.cpp @@ -0,0 +1,45 @@ +#include "../../Header/Player/PlayerModel.h" + +PlayerModel::PlayerModel() { } + +PlayerModel::~PlayerModel() { } + +void PlayerModel::initialize() { reset(); } // remember to call reset() + +void PlayerModel::reset() +{ + player_state = PlayerState::ALIVE; // set state to alive + player_position = initial_player_position; + player_score = 0; +} + +sf::Vector2f PlayerModel::getPlayerPosition() +{ + return player_position; +} + +void PlayerModel::setPlayerPosition(sf::Vector2f position) +{ + player_position = position; +} + +int PlayerModel::getPlayerScore() +{ + return player_score; +} + +void PlayerModel::setPlayerScore(int score) +{ + player_score = score; +} + +//.. +PlayerState PlayerModel::getPlayerState() +{ + return player_state; +} + +void PlayerModel::setPlayerState(PlayerState state) +{ + player_state = state; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerService.cpp b/Space-Invaders/Source/Player/PlayerService.cpp new file mode 100644 index 000000000..af4e3d1e8 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerService.cpp @@ -0,0 +1,27 @@ +#include "../../Header/Player/PlayerService.h" +#include "../../Header/Player/PlayerController.h" + +PlayerService::PlayerService() +{ + player_controller = new PlayerController(); +} + +PlayerService::~PlayerService() +{ + delete (player_controller); +} + +void PlayerService::initialize() +{ + player_controller->initialize(); +} + +void PlayerService::update() +{ + player_controller->update(); +} + +void PlayerService::render() +{ + player_controller->render(); +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp new file mode 100644 index 000000000..b3abf9070 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerView.cpp @@ -0,0 +1,50 @@ +#include "../../Header/Player/PlayerView.h" +#include "../../Header/Global/ServiceLocator.h" + +PlayerView::PlayerView() { } + +PlayerView::~PlayerView() { } + +void PlayerView::initialize() +{ + + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +void PlayerView::initialize(PlayerController* controller) +{ + player_controller = controller; //to later use it for setting position + 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() +{ + //set the updated position before we render + player_sprite.setPosition(player_controller->getPlayerPosition()); +} + +void PlayerView::render() +{ + game_window->draw(player_sprite); +} \ No newline at end of file diff --git a/Space-Invaders/Source/Time/TimeService.cpp b/Space-Invaders/Source/Time/TimeService.cpp new file mode 100644 index 000000000..0bfc0d679 --- /dev/null +++ b/Space-Invaders/Source/Time/TimeService.cpp @@ -0,0 +1,39 @@ +#include "../../Header/Time/TimeService.h" + +void TimeService::initialize() +{ + previous_time = std::chrono::steady_clock::now(); + delta_time = 0; +} + +void TimeService::update() +{ + updateDeltaTime(); +} + +float TimeService::getDeltaTime() +{ + return delta_time; +} + +void TimeService::updateDeltaTime() +{ + delta_time = calculateDeltaTime(); + updatePreviousTime(); +} + +float TimeService::calculateDeltaTime() +{ + // 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. + return static_cast(delta) / static_cast(1000000); +} + +// Update previous_time to the current time +void TimeService::updatePreviousTime() +{ + previous_time = std::chrono::steady_clock::now(); +} diff --git a/Space-Invaders/Space-Invaders.sln b/Space-Invaders/Space-Invaders.sln new file mode 100644 index 000000000..21ec086fc --- /dev/null +++ b/Space-Invaders/Space-Invaders.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34024.191 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Space-Invaders", "Space-Invaders.vcxproj", "{AB3664CD-9870-4359-8811-B481DB3B55A0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x64.ActiveCfg = Debug|x64 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x64.Build.0 = Debug|x64 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x86.ActiveCfg = Debug|Win32 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x86.Build.0 = Debug|Win32 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x64.ActiveCfg = Release|x64 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x64.Build.0 = Release|x64 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x86.ActiveCfg = Release|Win32 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F8206701-92F6-4A4D-B0E5-F40E32131C64} + EndGlobalSection +EndGlobal diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 6f7fa388d..3dd5235bd 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -1,140 +1,160 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 17.0 - Win32Proj - {ab3664cd-9870-4359-8811-b481db3b55a0} - SpaceInvaders - 10.0 - - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - - - - - - - - - - - - - - - - - - - - Level3 - true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - Level3 - true - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - $(SolutionDir)sfml\include;%(AdditionalIncludeDirectories) - - - Console - true - $(SolutionDir)sfml\lib;%(AdditionalLibraryDirectories) - sfml-graphics-d.lib;sfml-window-d.lib;sfml-network-d.lib;sfml-audio-d.lib;sfml-system-d.lib;%(AdditionalDependencies) - - - - - Level3 - true - true - true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - $(SolutionDir)sfml\include;%(AdditionalIncludeDirectories) - - - Console - true - true - true - $(SolutionDir)sfml\lib;%(AdditionalLibraryDirectories) - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {ab3664cd-9870-4359-8811-b481db3b55a0} + SpaceInvaders + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)sfml\include;%(AdditionalIncludeDirectories) + + + Console + true + $(SolutionDir)sfml\lib;%(AdditionalLibraryDirectories) + sfml-graphics-d.lib;sfml-window-d.lib;sfml-network-d.lib;sfml-audio-d.lib;sfml-system-d.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)sfml\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + $(SolutionDir)sfml\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index ce0c35ccf..8ae369c39 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -1,22 +1,78 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Source Files - - + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 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 + + \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj.user b/Space-Invaders/Space-Invaders.vcxproj.user index 966b4ffb6..429333de9 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.user +++ b/Space-Invaders/Space-Invaders.vcxproj.user @@ -1,6 +1,6 @@ - - - - true - + + + + true + \ No newline at end of file diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp new file mode 100644 index 000000000..434cdaeb5 --- /dev/null +++ b/Space-Invaders/main.cpp @@ -0,0 +1,106 @@ +#include +#include +#include "Header/Main/GameService.h" + + +/*class Player +{ +private: + + // Private Properties + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); + int movement_speed = 1; + int player_score = 0; + +public: + + // Public Properties + sf::Texture player_texture; + sf::Sprite player_sprite; + + //Public Functions, Getter & Setter methods + void move(float offsetX) { + position.x += offsetX; + } + + int getMoveSpeed() { + return movement_speed; + } + + int getScore() { + return player_score; + }; + + void setScore(int newScore) { + player_score = newScore; + }; + + sf::Vector2f getPosition() { + return position; + } + + void setPosition(sf::Vector2f newPosition) { + position = newPosition; + } + + //New methods to be added + void takeDamage() {}; + void move() {}; + void shootBullets() {}; +};*/ + +int main() +{ + /* + // Define the video mode (dimensions) + sf::VideoMode videoMode = sf::VideoMode(800, 600); + + // Create a window object with specific dimensions and a title + sf::RenderWindow window(videoMode, "My SFML Window"); + + //Player object + Player player; + + //Load Textures and sprite + player.player_texture.loadFromFile("assets/textures/player_ship.png"); + + player.player_sprite.setTexture(player.player_texture); + + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + // Check for window closure + if (event.type == sf::Event::Closed) + window.close(); + } + + // Handle keyboard input + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + player.move(-1.0f* player.getMoveSpeed()); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + player.move(1.0f * player.getMoveSpeed()); + } + + // Clear the window--- + window.clear(sf::Color::Black); + + // Set and draw player + player.player_sprite.setPosition(player.getPosition()); + + window.draw(player.player_sprite); + + // Display whatever you draw + window.display(); + }*/ + GameService* game_service = new GameService(); + + game_service->ignite(); + + while (game_service->isRunning()) + { + game_service->update(); + game_service->render(); + } +} From 58622f519b5551a0979c3eda676a741893668248 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:30:45 +0530 Subject: [PATCH 23/41] Delete Space-Invaders/Header directory --- Space-Invaders/Header/EventService.h | 30 ------------- Space-Invaders/Header/GameService.h | 25 ----------- Space-Invaders/Header/GraphicService.h | 38 ---------------- .../Header/Player/PlayerController.h | 25 ----------- Space-Invaders/Header/Player/PlayerModel.h | 43 ------------------- Space-Invaders/Header/Player/PlayerView.h | 31 ------------- Space-Invaders/Header/PlayerService.h | 16 ------- Space-Invaders/Header/ServiceLocator.h | 41 ------------------ Space-Invaders/Header/TimeService.h | 28 ------------ 9 files changed, 277 deletions(-) delete mode 100644 Space-Invaders/Header/EventService.h delete mode 100644 Space-Invaders/Header/GameService.h delete mode 100644 Space-Invaders/Header/GraphicService.h delete mode 100644 Space-Invaders/Header/Player/PlayerController.h delete mode 100644 Space-Invaders/Header/Player/PlayerModel.h delete mode 100644 Space-Invaders/Header/Player/PlayerView.h delete mode 100644 Space-Invaders/Header/PlayerService.h delete mode 100644 Space-Invaders/Header/ServiceLocator.h delete mode 100644 Space-Invaders/Header/TimeService.h diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h deleted file mode 100644 index ce9882ddc..000000000 --- a/Space-Invaders/Header/EventService.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include -#include - -class EventService -{ -private: - sf::Event game_event; //event var - sf::RenderWindow* game_window; //ptr to our game window - - bool isGameWindowOpen(); - bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. - bool hasQuitGame(); //for our new 'ESC' condition - - - -public: - EventService(); - ~EventService(); - - void initialize(); - void update(); - void processEvents(); // while window is open we will check for events - bool pressedEscapeKey(); - bool isKeyboardEvent(); - bool pressedLeftKey(); // getting inputs for the player - bool pressedRightKey(); - -}; \ No newline at end of file diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h deleted file mode 100644 index 32230d67d..000000000 --- a/Space-Invaders/Header/GameService.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include "../Header/ServiceLocator.h" - -class GameService -{ -private: - - ServiceLocator* service_locator; - sf::RenderWindow* game_window; - - - void initialize(); // Handles game initialization. - void initializeVariables();// Handles game initialization. - void destroy(); // Handles cleanup tasks. - -public: - GameService(); // Constructor for initializing the GameService object. - ~GameService(); // Destructor for cleaning up resources upon object deletion. - - void ignite(); // Initiates the game. - void update(); // Updates the game logic and game state. - void render(); // Renders each frame of the game. - bool isRunning(); // Checks if the game is currently running. -}; \ No newline at end of file diff --git a/Space-Invaders/Header/GraphicService.h b/Space-Invaders/Header/GraphicService.h deleted file mode 100644 index 96ed95751..000000000 --- a/Space-Invaders/Header/GraphicService.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include - -class GraphicService -{ -private: - - const std::string game_window_title = "Alien Invader"; - - const int game_window_width = 800; - const int game_window_height = 600; - - const int frame_rate = 60; - - const sf::Color window_color = sf::Color::Blue; - - sf::VideoMode* video_mode; // ptr to video mode - sf::RenderWindow* game_window; // ptr to a RenderWindow - - void setVideoMode(); // Method for setting our video mode - void onDestroy(); // method to run when window is deleted - -public: - GraphicService(); - ~GraphicService(); //cleanup - - //method to create the game window. returns a pointer to an instance of the game window - sf::RenderWindow* createGameWindow(); - - - void initialize(); //lifecycle functions - void update(); //.. - void render(); //.. - bool isGameWindowOpen(); //check if the window is open - - sf::RenderWindow* getGameWindow(); //getter for the game window instance - sf::Color getWindowColor();//get the color -}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h deleted file mode 100644 index 9ea24d95d..000000000 --- a/Space-Invaders/Header/Player/PlayerController.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include "../Player/PlayerModel.h" -#include "../Player/PlayerView.h" - -class PlayerController -{ -private: - PlayerView* player_view; - PlayerModel* player_model; - - void processPlayerInput(); - void moveLeft(); - void moveRight(); - -public: - PlayerController(); - ~PlayerController(); - - void initialize(); - void update(); - void render(); - - sf::Vector2f getPlayerPosition(); -}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h deleted file mode 100644 index cf048eca5..000000000 --- a/Space-Invaders/Header/Player/PlayerModel.h +++ /dev/null @@ -1,43 +0,0 @@ -#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); - - sf::Vector2f player_position; - PlayerState player_state; //Declaration - int player_score; - -public: - const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); - const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); - - const float player_movement_speed = 200.0f; - - PlayerModel(); - ~PlayerModel(); - - void initialize(); - void reset(); - - sf::Vector2f getPlayerPosition(); - void setPlayerPosition(sf::Vector2f position); - - int getPlayerScore(); - void setPlayerScore(int score); - - //new getter and setter - PlayerState getPlayerState(); - void setPlayerState(PlayerState state); - - -}; diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h deleted file mode 100644 index 8a1aa85e9..000000000 --- a/Space-Invaders/Header/Player/PlayerView.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include -#include "../../Header/Player/PlayerController.h" - -class PlayerView -{ -private: - - const sf::String player_texture_path = "assets/textures/player_ship.png"; - const float player_sprite_width = 60.f; - const float player_sprite_height = 60.f; - - sf::RenderWindow* game_window; - - sf::Texture player_texture; - sf::Sprite player_sprite; - - void initializePlayerSprite(); - void scalePlayerSprite(); - - PlayerController* player_controller; // ptr to player controller - -public: - PlayerView(); - ~PlayerView(); - - void initialize(); - void update(); - void render(); - void initialize(PlayerController* controller); -}; \ No newline at end of file diff --git a/Space-Invaders/Header/PlayerService.h b/Space-Invaders/Header/PlayerService.h deleted file mode 100644 index 2f46d4894..000000000 --- a/Space-Invaders/Header/PlayerService.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "../../Header/Player/PlayerController.h" - -class PlayerService -{ -private: - PlayerController* player_controller; - -public: - PlayerService(); - ~PlayerService(); - - void initialize(); - void update(); - void render(); -}; diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h deleted file mode 100644 index df101d414..000000000 --- a/Space-Invaders/Header/ServiceLocator.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#include "../Header/GraphicService.h" -#include "../Header/EventService.h" -#include "../Header/PlayerService.h" -#include "../Header/TimeService.h" - -// ServiceLocator Class Summary: This class manages access to various services in the application. -// include relevant headers files - -class ServiceLocator -{ -private: - - // Private Attributes: - GraphicService* graphic_service; - EventService* event_service; - PlayerService* player_service; - TimeService* time_service; - - // Public Methods - ServiceLocator(); - ~ServiceLocator(); - - // Private Methods: - void createServices(); - void clearAllServices(); - -public: - - // Public Methods: - static ServiceLocator* getInstance(); - void initialize(); // Initializes the ServiceLocator. - void update(); // Updates all services. - void render(); // Renders using the services. - - // Methods to Get Specific Services: - EventService* getEventService(); // Retrieve the EventService instance - GraphicService* getGraphicService(); // Retrieve the GraphicService instance - PlayerService* getPlayerService(); // Retrieve the PlayerService instance - TimeService* getTimeService(); // Retrieve the TimeService instance -}; \ No newline at end of file diff --git a/Space-Invaders/Header/TimeService.h b/Space-Invaders/Header/TimeService.h deleted file mode 100644 index 24ddec14a..000000000 --- a/Space-Invaders/Header/TimeService.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include - - // The TimeService class helps keep track of time in game and calculate delta time. - // Utilizes the library to calculate delta time. -class TimeService -{ -private: - - // A point in time which indicates the starting time of previous frame. - std::chrono::time_point previous_time; - - float delta_time; //to store the detla time - - void updateDeltaTime(); // method to update time - float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time - void updatePreviousTime(); // finally update the current time to be previous time - -public: - - //lifecycle methods - void initialize(); - void update(); - - //getter - float getDeltaTime(); -}; - From 4f64943e274206d11bea0b7811998b1380e34743 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:30:57 +0530 Subject: [PATCH 24/41] Delete Space-Invaders/Source directory --- Space-Invaders/Source/EventService.cpp | 44 ------------ Space-Invaders/Source/GameService.cpp | 59 --------------- Space-Invaders/Source/GraphicService.cpp | 56 --------------- .../Source/Player/PlayerController.cpp | 72 ------------------- Space-Invaders/Source/Player/PlayerModel.cpp | 45 ------------ Space-Invaders/Source/Player/PlayerView.cpp | 50 ------------- Space-Invaders/Source/PlayerService.cpp | 27 ------- Space-Invaders/Source/ServiceLocator.cpp | 65 ----------------- Space-Invaders/Source/TimeService.cpp | 39 ---------- 9 files changed, 457 deletions(-) delete mode 100644 Space-Invaders/Source/EventService.cpp delete mode 100644 Space-Invaders/Source/GameService.cpp delete mode 100644 Space-Invaders/Source/GraphicService.cpp delete mode 100644 Space-Invaders/Source/Player/PlayerController.cpp delete mode 100644 Space-Invaders/Source/Player/PlayerModel.cpp delete mode 100644 Space-Invaders/Source/Player/PlayerView.cpp delete mode 100644 Space-Invaders/Source/PlayerService.cpp delete mode 100644 Space-Invaders/Source/ServiceLocator.cpp delete mode 100644 Space-Invaders/Source/TimeService.cpp diff --git a/Space-Invaders/Source/EventService.cpp b/Space-Invaders/Source/EventService.cpp deleted file mode 100644 index 6f2941e93..000000000 --- a/Space-Invaders/Source/EventService.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "../Header/EventService.h" -#include "../Header/GameService.h" -#include "../Header/GraphicService.h" - -EventService::EventService() { game_window = nullptr; } - -EventService::~EventService() = default; //calls the default destructor - -void EventService::initialize() -{ - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); -} - -void EventService::update() -{ - //for later -} - -void EventService::processEvents() -{ - if (isGameWindowOpen()) { - while (game_window->pollEvent(game_event)) { - // Check for window closure - if (gameWindowWasClosed() || hasQuitGame()) - { - game_window->close(); - } - } - } -} - -bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered - -//checks for if a keyboard key has been pressed -bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } - -//control click on the SFML functions to see what they do internally -bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } -bool EventService::isGameWindowOpen() { return game_window != nullptr; } -bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } - -// Player inputs -bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } -bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } \ No newline at end of file diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp deleted file mode 100644 index e0fe519e8..000000000 --- a/Space-Invaders/Source/GameService.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "../Header/GameService.h" -#include "../Header/GraphicService.h" - -// Constructor: Initializes pointers to null. -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->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 deleted file mode 100644 index 2cfef211f..000000000 --- a/Space-Invaders/Source/GraphicService.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "../Header/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 - game_window->setFramerateLimit(frame_rate); // setting a frame rate limit -} - -// 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; -} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp deleted file mode 100644 index 19181c698..000000000 --- a/Space-Invaders/Source/Player/PlayerController.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "../../Header/Player/PlayerController.h" -#include "../../Header/EventService.h" -#include "../../Header/ServiceLocator.h" -#include - -PlayerController::PlayerController() -{ - player_view = new PlayerView(); - player_model = new PlayerModel(); -} - -PlayerController::~PlayerController() -{ - delete (player_view); - delete (player_model); -} -//the controller is responsible for calling the lifecycle methods for the other two -void PlayerController::initialize() -{ - player_model->initialize(); - - //This will give an error right now since we haven't included the controller in the view. - player_view->initialize(this); // 'this' refers to the class we are currently inside -} - -void PlayerController::update() -{ - processPlayerInput(); - player_view->update(); // we update() the view -} - -void PlayerController::render() -{ - player_view->render(); // render the view -} - -sf::Vector2f PlayerController::getPlayerPosition() -{ - return player_model->getPlayerPosition(); -} - -void PlayerController::processPlayerInput() -{ - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) - { - moveLeft(); - } - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) - { - moveRight(); - } -} - -void PlayerController::moveLeft() -{ - sf::Vector2f currentPosition = player_model->getPlayerPosition(); - currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - - currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); - player_model->setPlayerPosition(currentPosition); -} - -void PlayerController::moveRight() -{ - sf::Vector2f currentPosition = player_model->getPlayerPosition(); - currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - - currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); - player_model->setPlayerPosition(currentPosition); -} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerModel.cpp b/Space-Invaders/Source/Player/PlayerModel.cpp deleted file mode 100644 index 0d51db820..000000000 --- a/Space-Invaders/Source/Player/PlayerModel.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "../../Header/Player/PlayerModel.h" - -PlayerModel::PlayerModel() { } - -PlayerModel::~PlayerModel() { } - -void PlayerModel::initialize() { reset(); } // remember to call reset() - -void PlayerModel::reset() -{ - player_state = PlayerState::ALIVE; // set state to alive - player_position = initial_player_position; - player_score = 0; -} - -sf::Vector2f PlayerModel::getPlayerPosition() -{ - return player_position; -} - -void PlayerModel::setPlayerPosition(sf::Vector2f position) -{ - player_position = position; -} - -int PlayerModel::getPlayerScore() -{ - return player_score; -} - -void PlayerModel::setPlayerScore(int score) -{ - player_score = score; -} - -//.. -PlayerState PlayerModel::getPlayerState() -{ - return player_state; -} - -void PlayerModel::setPlayerState(PlayerState state) -{ - player_state = state; -} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp deleted file mode 100644 index 93762cd15..000000000 --- a/Space-Invaders/Source/Player/PlayerView.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "../../Header/Player/PlayerView.h" -#include "../../Header/ServiceLocator.h" - -PlayerView::PlayerView() { } - -PlayerView::~PlayerView() { } - -void PlayerView::initialize() -{ - - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); -} - -void PlayerView::initialize(PlayerController* controller) -{ - player_controller = controller; //to later use it for setting position - 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() -{ - //set the updated position before we render - player_sprite.setPosition(player_controller->getPlayerPosition()); -} - -void PlayerView::render() -{ - game_window->draw(player_sprite); -} \ No newline at end of file diff --git a/Space-Invaders/Source/PlayerService.cpp b/Space-Invaders/Source/PlayerService.cpp deleted file mode 100644 index 8b19d77f9..000000000 --- a/Space-Invaders/Source/PlayerService.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "../Header/PlayerService.h" -#include "../Header/Player/PlayerController.h" - -PlayerService::PlayerService() -{ - player_controller = new PlayerController(); -} - -PlayerService::~PlayerService() -{ - delete (player_controller); -} - -void PlayerService::initialize() -{ - player_controller->initialize(); -} - -void PlayerService::update() -{ - player_controller->update(); -} - -void PlayerService::render() -{ - player_controller->render(); -} \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp deleted file mode 100644 index a8fbc2d90..000000000 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "../Header/ServiceLocator.h" - -ServiceLocator::ServiceLocator() -{ - graphic_service = nullptr; - event_service = nullptr; - player_service = nullptr; - time_service = nullptr; - createServices(); -} -ServiceLocator::~ServiceLocator() -{ - clearAllServices(); -} - -void ServiceLocator::createServices() -{ - graphic_service = new GraphicService(); - event_service = new EventService(); - player_service = new PlayerService(); - time_service = new TimeService(); -} - -void ServiceLocator::clearAllServices() -{ - delete(graphic_service); - delete(event_service); - delete(player_service); - delete(time_service); -} - -ServiceLocator* ServiceLocator::getInstance() -{ - static ServiceLocator instance; - return &instance; -} - -void ServiceLocator::initialize() -{ - graphic_service->initialize(); - event_service->initialize(); - player_service->initialize(); - time_service->initialize(); -} - -void ServiceLocator::update() -{ - graphic_service->update(); - event_service->update(); - player_service->update(); - time_service->update(); -} - -void ServiceLocator::render() -{ - graphic_service->render(); - player_service->render(); - //no event service because nothing to render - //no time 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 deleted file mode 100644 index 7ede82971..000000000 --- a/Space-Invaders/Source/TimeService.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "../Header/TimeService.h" - -void TimeService::initialize() -{ - previous_time = std::chrono::steady_clock::now(); - delta_time = 0; -} - -void TimeService::update() -{ - updateDeltaTime(); -} - -float TimeService::getDeltaTime() -{ - return delta_time; -} - -void TimeService::updateDeltaTime() -{ - delta_time = calculateDeltaTime(); - updatePreviousTime(); -} - -float TimeService::calculateDeltaTime() -{ - // 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. - return static_cast(delta) / static_cast(1000000); -} - -// Update previous_time to the current time -void TimeService::updatePreviousTime() -{ - previous_time = std::chrono::steady_clock::now(); -} From 2f3005b0ede7ff9f3f92f26b6f482523ac09116f Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 15:53:18 +0530 Subject: [PATCH 25/41] Added Namespaces --- Space-Invaders/Header/Event/EventService.h | 40 +++--- Space-Invaders/Header/Global/ServiceLocator.h | 65 +++++----- .../Header/Graphic/GraphicService.h | 51 ++++---- Space-Invaders/Header/Main/GameService.h | 37 +++--- .../Header/Player/PlayerController.h | 43 +++--- Space-Invaders/Header/Player/PlayerModel.h | 59 +++++---- Space-Invaders/Header/Player/PlayerService.h | 23 ++-- Space-Invaders/Header/Player/PlayerView.h | 41 +++--- Space-Invaders/Header/Time/TimeService.h | 36 +++--- Space-Invaders/Source/Event/EventService.cpp | 65 +++++----- .../Source/Global/ServiceLocator.cpp | 122 ++++++++++-------- .../Source/Graphic/GraphicService.cpp | 109 ++++++++-------- Space-Invaders/Source/Main/GameService.cpp | 96 +++++++------- .../Source/Player/PlayerController.cpp | 109 ++++++++-------- Space-Invaders/Source/Player/PlayerModel.cpp | 85 ++++++------ .../Source/Player/PlayerService.cpp | 39 +++--- Space-Invaders/Source/Player/PlayerView.cpp | 78 +++++------ Space-Invaders/Source/Time/TimeService.cpp | 61 ++++----- Space-Invaders/main.cpp | 97 +------------- 19 files changed, 624 insertions(+), 632 deletions(-) diff --git a/Space-Invaders/Header/Event/EventService.h b/Space-Invaders/Header/Event/EventService.h index ce9882ddc..6f5924392 100644 --- a/Space-Invaders/Header/Event/EventService.h +++ b/Space-Invaders/Header/Event/EventService.h @@ -3,28 +3,32 @@ #include #include -class EventService +namespace Event { -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(); // getting inputs for the player - bool pressedRightKey(); + void initialize(); + void update(); + void processEvents(); // while window is open we will check for events + bool pressedEscapeKey(); + bool isKeyboardEvent(); + bool pressedLeftKey(); // getting inputs for the player + bool pressedRightKey(); + + }; +} -}; \ No newline at end of file diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index 332e1549f..a10386a92 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -7,35 +7,38 @@ // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files -class ServiceLocator +namespace Global { -private: - - // Private Attributes: - GraphicService* graphic_service; - EventService* event_service; - PlayerService* player_service; - TimeService* time_service; - - // Public Methods - ServiceLocator(); - ~ServiceLocator(); - - // Private Methods: - void createServices(); - void clearAllServices(); - -public: - - // Public Methods: - static ServiceLocator* getInstance(); - void initialize(); // Initializes the ServiceLocator. - void update(); // Updates all services. - void render(); // Renders using the services. - - // Methods to Get Specific Services: - EventService* getEventService(); // Retrieve the EventService instance - GraphicService* getGraphicService(); // Retrieve the GraphicService instance - PlayerService* getPlayerService(); // Retrieve the PlayerService instance - TimeService* getTimeService(); // Retrieve the TimeService instance -}; \ No newline at end of file + class ServiceLocator + { + private: + + // Private Attributes: + Graphics::GraphicService* graphic_service; + Event::EventService* event_service; + Player::PlayerService* player_service; + Time::TimeService* time_service; + + // Public Methods + ServiceLocator(); + ~ServiceLocator(); + + // Private Methods: + void createServices(); + void clearAllServices(); + + public: + + // Public Methods: + static ServiceLocator* getInstance(); + void initialize(); // Initializes the ServiceLocator. + void update(); // Updates all services. + void render(); // Renders using the services. + + // Methods to Get Specific Services: + Event::EventService* getEventService(); // Retrieve the EventService instance + Graphics::GraphicService* getGraphicService(); // Retrieve the GraphicService instance + Player::PlayerService* getPlayerService(); // Retrieve the PlayerService instance + Time::TimeService* getTimeService(); // Retrieve the TimeService instance + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Graphic/GraphicService.h b/Space-Invaders/Header/Graphic/GraphicService.h index 83674fde3..c87153f16 100644 --- a/Space-Invaders/Header/Graphic/GraphicService.h +++ b/Space-Invaders/Header/Graphic/GraphicService.h @@ -1,38 +1,41 @@ #pragma once #include -class GraphicService +namespace Graphics { -private: + class GraphicService + { + private: - const std::string game_window_title = "Alien Invader"; + const std::string game_window_title = "Alien Invader"; - const int game_window_width = 800; - const int game_window_height = 600; - - const int frame_rate = 60; + const int game_window_width = 800; + const int game_window_height = 600; - const sf::Color window_color = sf::Color::Black; + const int frame_rate = 60; - sf::VideoMode* video_mode; // ptr to video mode - sf::RenderWindow* game_window; // ptr to a RenderWindow + const sf::Color window_color = sf::Color::Black; - 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/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h index 53f95bee5..a4bc449c7 100644 --- a/Space-Invaders/Header/Main/GameService.h +++ b/Space-Invaders/Header/Main/GameService.h @@ -1,25 +1,30 @@ #pragma once #include -#include "../../Header/Global/ServiceLocator.h" +#include "../../header/Global/ServiceLocator.h" -class GameService +namespace Main { -private: + class ServiceLocator; - ServiceLocator* service_locator; - sf::RenderWindow* game_window; + class GameService + { + private: + Global::ServiceLocator* service_locator; + sf::RenderWindow* game_window; - void initialize(); // Handles game initialization. - void initializeVariables();// Handles game initialization. - void destroy(); // Handles cleanup tasks. -public: - GameService(); // Constructor for initializing the GameService object. - ~GameService(); // Destructor for cleaning up resources upon object deletion. + void initialize(); // Handles game initialization. + void initializeVariables();// Handles game initialization. + void destroy(); // Handles cleanup tasks. - 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 + 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/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h index 36fda230a..c3a12b538 100644 --- a/Space-Invaders/Header/Player/PlayerController.h +++ b/Space-Invaders/Header/Player/PlayerController.h @@ -1,28 +1,31 @@ #pragma once #include -enum class PlayerState; -class PlayerView; -class PlayerModel; - -class PlayerController +namespace Player { -private: - - PlayerView* player_view; - PlayerModel* player_model; + class PlayerView; + class PlayerModel; + enum class PlayerState; + + class PlayerController + { + private: + + PlayerView* player_view; + PlayerModel* player_model; - void processPlayerInput(); - void moveLeft(); - void moveRight(); + void processPlayerInput(); + void moveLeft(); + void moveRight(); -public: - PlayerController(); - ~PlayerController(); + public: + PlayerController(); + ~PlayerController(); - void initialize(); - void update(); - void render(); + void initialize(); + void update(); + void render(); - sf::Vector2f getPlayerPosition(); -}; \ No newline at end of file + sf::Vector2f getPlayerPosition(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h index 13d61e0ea..b76424d92 100644 --- a/Space-Invaders/Header/Player/PlayerModel.h +++ b/Space-Invaders/Header/Player/PlayerModel.h @@ -1,43 +1,46 @@ #pragma once #include -enum class PlayerState //Our Enum +namespace Player { - 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(400.f, 400.f); + class PlayerModel + { + private: + const sf::Vector2f initial_player_position = sf::Vector2f(400.f, 400.f); - sf::Vector2f player_position; - PlayerState player_state; //Declaration - int player_score; + sf::Vector2f player_position; + PlayerState player_state; //Declaration + int player_score; -public: - const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); - const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); + public: + const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); + const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); - const float player_movement_speed = 300.0f; + const float player_movement_speed = 300.0f; - PlayerModel(); - ~PlayerModel(); + PlayerModel(); + ~PlayerModel(); - void initialize(); - void reset(); + void initialize(); + void reset(); - sf::Vector2f getPlayerPosition(); - void setPlayerPosition(sf::Vector2f position); + sf::Vector2f getPlayerPosition(); + void setPlayerPosition(sf::Vector2f position); - int getPlayerScore(); - void setPlayerScore(int score); + int getPlayerScore(); + void setPlayerScore(int score); - //new getter and setter - PlayerState getPlayerState(); - void setPlayerState(PlayerState state); + //new getter and setter + PlayerState getPlayerState(); + void setPlayerState(PlayerState state); -}; + }; +} diff --git a/Space-Invaders/Header/Player/PlayerService.h b/Space-Invaders/Header/Player/PlayerService.h index 2f46d4894..a978d53c5 100644 --- a/Space-Invaders/Header/Player/PlayerService.h +++ b/Space-Invaders/Header/Player/PlayerService.h @@ -1,16 +1,19 @@ #pragma once #include "../../Header/Player/PlayerController.h" -class PlayerService +namespace Player { -private: - PlayerController* player_controller; + class PlayerService + { + private: + PlayerController* player_controller; -public: - PlayerService(); - ~PlayerService(); + public: + PlayerService(); + ~PlayerService(); - void initialize(); - void update(); - void render(); -}; + void initialize(); + void update(); + void render(); + }; +} diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h index 8a1aa85e9..8e901f3f4 100644 --- a/Space-Invaders/Header/Player/PlayerView.h +++ b/Space-Invaders/Header/Player/PlayerView.h @@ -2,30 +2,33 @@ #include #include "../../Header/Player/PlayerController.h" -class PlayerView +namespace Player { -private: + 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; // ptr to player controller + PlayerController* player_controller; // ptr to player controller -public: - PlayerView(); - ~PlayerView(); + public: + PlayerView(); + ~PlayerView(); - void initialize(); - void update(); - void render(); - void initialize(PlayerController* controller); -}; \ No newline at end of file + void initialize(); + void update(); + void render(); + void initialize(PlayerController* controller); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Time/TimeService.h b/Space-Invaders/Header/Time/TimeService.h index 24ddec14a..71184c46e 100644 --- a/Space-Invaders/Header/Time/TimeService.h +++ b/Space-Invaders/Header/Time/TimeService.h @@ -3,26 +3,30 @@ // The TimeService class helps keep track of time in game and calculate delta time. // Utilizes the library to calculate delta time. -class TimeService + +namespace Time { -private: + class TimeService + { + private: + + // A point in time which indicates the starting time of previous frame. + std::chrono::time_point previous_time; - // A point in time which indicates the starting time of previous frame. - std::chrono::time_point previous_time; - - float delta_time; //to store the detla time + float delta_time; //to store the detla time - void updateDeltaTime(); // method to update time - float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time - void updatePreviousTime(); // finally update the current time to be previous time + void updateDeltaTime(); // method to update time + float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time + void updatePreviousTime(); // finally update the current time to be previous time -public: + public: - //lifecycle methods - void initialize(); - void update(); + //lifecycle methods + void initialize(); + void update(); - //getter - float getDeltaTime(); -}; + //getter + float getDeltaTime(); + }; +} diff --git a/Space-Invaders/Source/Event/EventService.cpp b/Space-Invaders/Source/Event/EventService.cpp index 9be3a667d..f1622bcd5 100644 --- a/Space-Invaders/Source/Event/EventService.cpp +++ b/Space-Invaders/Source/Event/EventService.cpp @@ -1,44 +1,51 @@ #include "../../Header/Event/EventService.h" #include "../../Header/Main/GameService.h" #include "../../Header/Graphic/GraphicService.h" +#include "../../Header/Global/ServiceLocator.h" -EventService::EventService() { game_window = nullptr; } +namespace Event +{ -EventService::~EventService() = default; //calls the default destructor + EventService::EventService() { game_window = nullptr; } -void EventService::initialize() -{ - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); -} + EventService::~EventService() = default; //calls the default destructor -void EventService::update() -{ - //for later -} + using namespace Global; -void EventService::processEvents() -{ - if (isGameWindowOpen()) { - while (game_window->pollEvent(game_event)) { - // Check for window closure - if (gameWindowWasClosed() || hasQuitGame()) - { - game_window->close(); + 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; } -bool EventService::isGameWindowOpen() { return game_window != nullptr; } -bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } + //control click on the SFML functions to see what they do internally + bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } + bool EventService::isGameWindowOpen() { return game_window != nullptr; } + bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } -// Player inputs -bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } -bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } \ No newline at end of file + // Player inputs + 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/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index 4c239d74b..e4fe451ee 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -1,65 +1,77 @@ #include "../../Header/Global/ServiceLocator.h" +#include "../../header/Main/GameService.h" -ServiceLocator::ServiceLocator() +namespace Global { - graphic_service = nullptr; - event_service = nullptr; - player_service = nullptr; - time_service = nullptr; - createServices(); -} -ServiceLocator::~ServiceLocator() -{ - clearAllServices(); -} + using namespace Main; + using namespace Graphics; + using namespace Event; + using namespace Time; + using namespace Player; + -void ServiceLocator::createServices() -{ - graphic_service = new GraphicService(); - event_service = new EventService(); - player_service = new PlayerService(); - time_service = new TimeService(); -} + ServiceLocator::ServiceLocator() + { + graphic_service = nullptr; + event_service = nullptr; + player_service = nullptr; + time_service = nullptr; + createServices(); + } + ServiceLocator::~ServiceLocator() + { + clearAllServices(); + } -void ServiceLocator::clearAllServices() -{ - delete(graphic_service); - delete(event_service); - delete(player_service); - delete(time_service); -} + void ServiceLocator::createServices() + { + graphic_service = new GraphicService(); + event_service = new EventService(); + player_service = new PlayerService(); + time_service = new TimeService(); + } -ServiceLocator* ServiceLocator::getInstance() -{ - static ServiceLocator instance; - return &instance; -} + void ServiceLocator::clearAllServices() + { + delete(graphic_service); + delete(event_service); + delete(player_service); + delete(time_service); + } -void ServiceLocator::initialize() -{ - graphic_service->initialize(); - event_service->initialize(); - player_service->initialize(); - time_service->initialize(); -} + ServiceLocator* ServiceLocator::getInstance() + { + static ServiceLocator instance; + return &instance; + } -void ServiceLocator::update() -{ - graphic_service->update(); - event_service->update(); - player_service->update(); - time_service->update(); -} + void ServiceLocator::initialize() + { + graphic_service->initialize(); + event_service->initialize(); + player_service->initialize(); + time_service->initialize(); + } -void ServiceLocator::render() -{ - graphic_service->render(); - player_service->render(); - //no event service because nothing to render - //no time service -} + void ServiceLocator::update() + { + graphic_service->update(); + event_service->update(); + player_service->update(); + time_service->update(); + } + + void ServiceLocator::render() + { + graphic_service->render(); + player_service->render(); + //no event service because nothing to render + //no time 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; } -GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } -EventService* ServiceLocator::getEventService() { return event_service; } -PlayerService* ServiceLocator::getPlayerService() { return player_service; } -TimeService* ServiceLocator::getTimeService() { return time_service; } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Graphic/GraphicService.cpp b/Space-Invaders/Source/Graphic/GraphicService.cpp index 4c8bb04ab..b10897d5d 100644 --- a/Space-Invaders/Source/Graphic/GraphicService.cpp +++ b/Space-Invaders/Source/Graphic/GraphicService.cpp @@ -1,56 +1,59 @@ #include "../../Header/Graphic/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 - game_window->setFramerateLimit(frame_rate); // setting a frame rate limit -} - -// 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; +namespace Graphics +{ + // 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 + game_window->setFramerateLimit(frame_rate); // setting a frame rate limit + } + + // 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; + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp index 12918afe4..d61c57973 100644 --- a/Space-Invaders/Source/Main/GameService.cpp +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -1,59 +1,67 @@ #include "../../Header/Main/GameService.h" #include "../../Header/Graphic/GraphicService.h" +#include "../../Header/Global/ServiceLocator.h" -// Constructor: Initializes pointers to null. -GameService::GameService() { - service_locator = nullptr; // Set service locator to null - game_window = nullptr; // Set game window to null -} +namespace Main +{ + using namespace Global; + using namespace Graphics; + using namespace Event; + // Constructor: Initializes pointers to null. + 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 -} + // 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. -} + // Prepares the game service for use by obtaining the service locator instance and initializing services. + void GameService::ignite() { + service_locator = Global::ServiceLocator::getInstance(); // Get ServiceLocator + initialize(); // Initialize services. + } -//initialize service locator and other variables -void GameService::initialize() -{ - service_locator->initialize(); - initializeVariables(); -} + //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::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. -} + 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() { + // Updates the game logic by delegating to the service locator's update method. + void GameService::update() { - service_locator->getEventService()->processEvents(); + service_locator->getEventService()->processEvents(); - service_locator->update(); // Call update on the service locator which then updates all its managed services -} + 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 -} + // 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(); + // 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/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp index 14b9754c9..534114f48 100644 --- a/Space-Invaders/Source/Player/PlayerController.cpp +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -5,70 +5,77 @@ #include "../../Header/Global/ServiceLocator.h" #include -PlayerController::PlayerController() +namespace Player { - player_view = new PlayerView(); - player_model = new PlayerModel(); -} + using namespace Global; + using namespace Event; + using namespace Time; -PlayerController::~PlayerController() -{ - delete (player_view); - delete (player_model); -} -//the controller is responsible for calling the lifecycle methods for the other two -void PlayerController::initialize() -{ - player_model->initialize(); + PlayerController::PlayerController() + { + player_view = new PlayerView(); + player_model = new PlayerModel(); + } - //This will give an error right now since we haven't included the controller in the view. - player_view->initialize(this); // 'this' refers to the class we are currently inside -} + PlayerController::~PlayerController() + { + delete (player_view); + delete (player_model); + } + //the controller is responsible for calling the lifecycle methods for the other two + void PlayerController::initialize() + { + player_model->initialize(); -void PlayerController::update() -{ - processPlayerInput(); - player_view->update(); // we update() the view -} + //This will give an error right now since we haven't included the controller in the view. + player_view->initialize(this); // 'this' refers to the class we are currently inside + } -void PlayerController::render() -{ - player_view->render(); // render the view -} + void PlayerController::update() + { + processPlayerInput(); + player_view->update(); // we update() the view + } -sf::Vector2f PlayerController::getPlayerPosition() -{ - return player_model->getPlayerPosition(); -} + void PlayerController::render() + { + player_view->render(); // render the view + } -void PlayerController::processPlayerInput() -{ - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + sf::Vector2f PlayerController::getPlayerPosition() { - moveLeft(); + return player_model->getPlayerPosition(); } - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + + void PlayerController::processPlayerInput() { - moveRight(); + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + { + moveLeft(); + } + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + { + moveRight(); + } } -} -void PlayerController::moveLeft() -{ - sf::Vector2f currentPosition = player_model->getPlayerPosition(); - currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + void PlayerController::moveLeft() + { + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); - player_model->setPlayerPosition(currentPosition); -} + currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); + player_model->setPlayerPosition(currentPosition); + } -void PlayerController::moveRight() -{ - sf::Vector2f currentPosition = player_model->getPlayerPosition(); - currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + void PlayerController::moveRight() + { + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); - player_model->setPlayerPosition(currentPosition); + currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); + player_model->setPlayerPosition(currentPosition); + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerModel.cpp b/Space-Invaders/Source/Player/PlayerModel.cpp index 0d51db820..3f519eb06 100644 --- a/Space-Invaders/Source/Player/PlayerModel.cpp +++ b/Space-Invaders/Source/Player/PlayerModel.cpp @@ -1,45 +1,48 @@ #include "../../Header/Player/PlayerModel.h" -PlayerModel::PlayerModel() { } - -PlayerModel::~PlayerModel() { } - -void PlayerModel::initialize() { reset(); } // remember to call reset() - -void PlayerModel::reset() -{ - player_state = PlayerState::ALIVE; // set state to alive - player_position = initial_player_position; - player_score = 0; -} - -sf::Vector2f PlayerModel::getPlayerPosition() -{ - return player_position; -} - -void PlayerModel::setPlayerPosition(sf::Vector2f position) -{ - player_position = position; -} - -int PlayerModel::getPlayerScore() -{ - return player_score; -} - -void PlayerModel::setPlayerScore(int score) -{ - player_score = score; -} - -//.. -PlayerState PlayerModel::getPlayerState() -{ - return player_state; -} - -void PlayerModel::setPlayerState(PlayerState state) +namespace Player { - player_state = state; + PlayerModel::PlayerModel() { } + + PlayerModel::~PlayerModel() { } + + void PlayerModel::initialize() { reset(); } // remember to call reset() + + void PlayerModel::reset() + { + player_state = PlayerState::ALIVE; // set state to alive + player_position = initial_player_position; + player_score = 0; + } + + sf::Vector2f PlayerModel::getPlayerPosition() + { + return player_position; + } + + void PlayerModel::setPlayerPosition(sf::Vector2f position) + { + player_position = position; + } + + int PlayerModel::getPlayerScore() + { + return player_score; + } + + void PlayerModel::setPlayerScore(int score) + { + player_score = score; + } + + //.. + PlayerState PlayerModel::getPlayerState() + { + return player_state; + } + + void PlayerModel::setPlayerState(PlayerState state) + { + player_state = state; + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerService.cpp b/Space-Invaders/Source/Player/PlayerService.cpp index af4e3d1e8..e5ccd9d05 100644 --- a/Space-Invaders/Source/Player/PlayerService.cpp +++ b/Space-Invaders/Source/Player/PlayerService.cpp @@ -1,27 +1,30 @@ #include "../../Header/Player/PlayerService.h" #include "../../Header/Player/PlayerController.h" -PlayerService::PlayerService() +namespace Player { - player_controller = new PlayerController(); -} + PlayerService::PlayerService() + { + player_controller = new PlayerController(); + } -PlayerService::~PlayerService() -{ - delete (player_controller); -} + PlayerService::~PlayerService() + { + delete (player_controller); + } -void PlayerService::initialize() -{ - player_controller->initialize(); -} + void PlayerService::initialize() + { + player_controller->initialize(); + } -void PlayerService::update() -{ - player_controller->update(); -} + void PlayerService::update() + { + player_controller->update(); + } -void PlayerService::render() -{ - player_controller->render(); + void PlayerService::render() + { + player_controller->render(); + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp index b3abf9070..bab4189cc 100644 --- a/Space-Invaders/Source/Player/PlayerView.cpp +++ b/Space-Invaders/Source/Player/PlayerView.cpp @@ -1,50 +1,54 @@ #include "../../Header/Player/PlayerView.h" #include "../../Header/Global/ServiceLocator.h" -PlayerView::PlayerView() { } +namespace Player +{ + using namespace Global; + PlayerView::PlayerView() { } -PlayerView::~PlayerView() { } + PlayerView::~PlayerView() { } -void PlayerView::initialize() -{ + void PlayerView::initialize() + { - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); -} + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); + } -void PlayerView::initialize(PlayerController* controller) -{ - player_controller = controller; //to later use it for setting position - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); -} + void PlayerView::initialize(PlayerController* controller) + { + player_controller = controller; //to later use it for setting position + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); + } -void PlayerView::initializePlayerSprite() -{ - if (player_texture.loadFromFile(player_texture_path)) + void PlayerView::initializePlayerSprite() { - player_sprite.setTexture(player_texture); - scalePlayerSprite(); + 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() -{ - //set the updated position before we render - player_sprite.setPosition(player_controller->getPlayerPosition()); -} + 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::render() -{ - game_window->draw(player_sprite); + void PlayerView::update() + { + //set the updated position before we render + player_sprite.setPosition(player_controller->getPlayerPosition()); + } + + void PlayerView::render() + { + game_window->draw(player_sprite); + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Time/TimeService.cpp b/Space-Invaders/Source/Time/TimeService.cpp index 0bfc0d679..718d1fa8c 100644 --- a/Space-Invaders/Source/Time/TimeService.cpp +++ b/Space-Invaders/Source/Time/TimeService.cpp @@ -1,39 +1,42 @@ #include "../../Header/Time/TimeService.h" -void TimeService::initialize() +namespace Time { - previous_time = std::chrono::steady_clock::now(); - delta_time = 0; -} + void TimeService::initialize() + { + previous_time = std::chrono::steady_clock::now(); + delta_time = 0; + } -void TimeService::update() -{ - updateDeltaTime(); -} + void TimeService::update() + { + updateDeltaTime(); + } -float TimeService::getDeltaTime() -{ - return delta_time; -} + float TimeService::getDeltaTime() + { + return delta_time; + } -void TimeService::updateDeltaTime() -{ - delta_time = calculateDeltaTime(); - updatePreviousTime(); -} + void TimeService::updateDeltaTime() + { + delta_time = calculateDeltaTime(); + updatePreviousTime(); + } -float TimeService::calculateDeltaTime() -{ - // 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(); + float TimeService::calculateDeltaTime() + { + // 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. - return static_cast(delta) / static_cast(1000000); -} + // The cast is used to convert delta time from microseconds into seconds. + return static_cast(delta) / static_cast(1000000); + } -// Update previous_time to the current time -void TimeService::updatePreviousTime() -{ - previous_time = std::chrono::steady_clock::now(); + // Update previous_time to the current time + void TimeService::updatePreviousTime() + { + previous_time = std::chrono::steady_clock::now(); + } } diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 434cdaeb5..db819e86b 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,101 +1,10 @@ -#include -#include -#include "Header/Main/GameService.h" - - -/*class Player -{ -private: - - // Private Properties - int health = 3; - sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - int movement_speed = 1; - int player_score = 0; - -public: - - // Public Properties - sf::Texture player_texture; - sf::Sprite player_sprite; - - //Public Functions, Getter & Setter methods - void move(float offsetX) { - position.x += offsetX; - } - - int getMoveSpeed() { - return movement_speed; - } - - int getScore() { - return player_score; - }; - - void setScore(int newScore) { - player_score = newScore; - }; - - sf::Vector2f getPosition() { - return position; - } - - void setPosition(sf::Vector2f newPosition) { - position = newPosition; - } - - //New methods to be added - void takeDamage() {}; - void move() {}; - void shootBullets() {}; -};*/ +#include "../../header/Main/GameService.h" int main() { - /* - // Define the video mode (dimensions) - sf::VideoMode videoMode = sf::VideoMode(800, 600); - - // Create a window object with specific dimensions and a title - sf::RenderWindow window(videoMode, "My SFML Window"); - - //Player object - Player player; - - //Load Textures and sprite - player.player_texture.loadFromFile("assets/textures/player_ship.png"); - - player.player_sprite.setTexture(player.player_texture); + using namespace Main; - while (window.isOpen()) { - sf::Event event; - while (window.pollEvent(event)) { - // Check for window closure - if (event.type == sf::Event::Closed) - window.close(); - } - - // Handle keyboard input - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - player.move(-1.0f* player.getMoveSpeed()); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - player.move(1.0f * player.getMoveSpeed()); - } - - // Clear the window--- - window.clear(sf::Color::Black); - - // Set and draw player - player.player_sprite.setPosition(player.getPosition()); - - window.draw(player.player_sprite); - - // Display whatever you draw - window.display(); - }*/ GameService* game_service = new GameService(); - game_service->ignite(); while (game_service->isRunning()) @@ -103,4 +12,6 @@ int main() game_service->update(); game_service->render(); } + + return 0; } From ff152f2b860f7e3137b179e6b15b4982e2678647 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Fri, 2 Aug 2024 13:09:58 +0530 Subject: [PATCH 26/41] Game State added --- Space-Invaders/Header/Main/GameService.h | 10 ++++++++++ Space-Invaders/Source/Main/GameService.cpp | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h index a4bc449c7..a20e216dc 100644 --- a/Space-Invaders/Header/Main/GameService.h +++ b/Space-Invaders/Header/Main/GameService.h @@ -4,12 +4,20 @@ namespace Main { + enum class GameState + { + BOOT, + MAIN_MENU, + GAMEPLAY, + }; + class ServiceLocator; class GameService { private: + static GameState current_state; Global::ServiceLocator* service_locator; sf::RenderWindow* game_window; @@ -26,5 +34,7 @@ namespace Main 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. + static void setGameState(GameState new_state); + static GameState getGameState(); }; } \ No newline at end of file diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp index d61c57973..6153c00b6 100644 --- a/Space-Invaders/Source/Main/GameService.cpp +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -7,6 +7,9 @@ namespace Main using namespace Global; using namespace Graphics; using namespace Event; + + GameState GameService::current_state = GameState::BOOT; + // Constructor: Initializes pointers to null. GameService::GameService() { service_locator = nullptr; // Set service locator to null @@ -62,6 +65,12 @@ namespace Main // Returns true if the game window is open, indicating the game is still running return service_locator->getGraphicService()->isGameWindowOpen(); } + + // Setter function foro the game state + void GameService::setGameState(GameState new_state) { current_state = new_state; } + + // Getter function for the current game state + GameState GameService::getGameState() { return current_state; } } From 6601dac47b8e3856bd78e9ff49472505043b4809 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Fri, 2 Aug 2024 15:15:06 +0530 Subject: [PATCH 27/41] Added Main Menu and lifecycle --- Space-Invaders/Header/Global/ServiceLocator.h | 3 + .../Header/Graphic/GraphicService.h | 4 +- Space-Invaders/Header/Main/GameService.h | 1 + Space-Invaders/Header/Player/PlayerModel.h | 8 +- .../Header/UI/MainMenu/MainMenuUIController.h | 55 +++++++++ Space-Invaders/Header/UI/UIService.h | 23 ++++ .../Source/Global/ServiceLocator.cpp | 10 +- .../Source/Graphic/GraphicService.cpp | 2 +- .../UI/MainMenu/MainMenuUIController.cpp | 105 ++++++++++++++++++ Space-Invaders/Source/UI/UIService.cpp | 51 +++++++++ 10 files changed, 254 insertions(+), 8 deletions(-) create mode 100644 Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h create mode 100644 Space-Invaders/Header/UI/UIService.h create mode 100644 Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp create mode 100644 Space-Invaders/Source/UI/UIService.cpp diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index a10386a92..fe2ccf055 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -3,6 +3,7 @@ #include "../../Header/Event/EventService.h" #include "../../Header/Player/PlayerService.h" #include "../../Header/Time/TimeService.h" +#include "../../Header/UI/UIService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -18,6 +19,7 @@ namespace Global Event::EventService* event_service; Player::PlayerService* player_service; Time::TimeService* time_service; + UI::UIService* ui_service; // Public Methods ServiceLocator(); @@ -40,5 +42,6 @@ namespace Global Graphics::GraphicService* getGraphicService(); // Retrieve the GraphicService instance Player::PlayerService* getPlayerService(); // Retrieve the PlayerService instance Time::TimeService* getTimeService(); // Retrieve the TimeService instance + UI::UIService* getUIService(); // Retrive the UIService instance }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Graphic/GraphicService.h b/Space-Invaders/Header/Graphic/GraphicService.h index c87153f16..a0514d024 100644 --- a/Space-Invaders/Header/Graphic/GraphicService.h +++ b/Space-Invaders/Header/Graphic/GraphicService.h @@ -9,8 +9,8 @@ namespace Graphics const std::string game_window_title = "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 int frame_rate = 60; diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h index a20e216dc..123bf325a 100644 --- a/Space-Invaders/Header/Main/GameService.h +++ b/Space-Invaders/Header/Main/GameService.h @@ -18,6 +18,7 @@ namespace Main private: static GameState current_state; + Global::ServiceLocator* service_locator; sf::RenderWindow* game_window; diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h index b76424d92..43e8ec35f 100644 --- a/Space-Invaders/Header/Player/PlayerModel.h +++ b/Space-Invaders/Header/Player/PlayerModel.h @@ -13,17 +13,17 @@ namespace Player class PlayerModel { private: - const sf::Vector2f initial_player_position = sf::Vector2f(400.f, 400.f); + const sf::Vector2f initial_player_position = sf::Vector2f(950.f, 950.f); sf::Vector2f player_position; PlayerState player_state; //Declaration int player_score; public: - const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); - const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); + const 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 = 300.0f; + const float player_movement_speed = 600.0f; PlayerModel(); ~PlayerModel(); diff --git a/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h new file mode 100644 index 000000000..32dc26439 --- /dev/null +++ b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h @@ -0,0 +1,55 @@ +#pragma once +#include + +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"; + + // Constants: + const float button_width = 400.f; + const float button_height = 140.f; + + sf::RenderWindow* game_window; + + // Textures: + 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; + + // Buttons and scaling + void initializeBackgroundImage(); + void scaleBackgroundImage(); + void initializeButtons(); + bool loadButtonTexturesFromFile(); + void setButtonSprites(); + void scaleAllButttons(); + void scaleButton(sf::Sprite* button_to_scale); + void positionButtons(); + + public: + MainMenuUIController(); + + void initialize(); + void update(); + void render(); + + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Header/UI/UIService.h b/Space-Invaders/Header/UI/UIService.h new file mode 100644 index 000000000..2f8856ee3 --- /dev/null +++ b/Space-Invaders/Header/UI/UIService.h @@ -0,0 +1,23 @@ +#pragma once +#include "../../Header/UI/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/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index e4fe451ee..9983889db 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -8,6 +8,7 @@ namespace Global using namespace Event; using namespace Time; using namespace Player; + using namespace UI; ServiceLocator::ServiceLocator() @@ -16,6 +17,7 @@ namespace Global event_service = nullptr; player_service = nullptr; time_service = nullptr; + ui_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -29,6 +31,7 @@ namespace Global event_service = new EventService(); player_service = new PlayerService(); time_service = new TimeService(); + ui_service = new UIService(); } void ServiceLocator::clearAllServices() @@ -37,6 +40,7 @@ namespace Global delete(event_service); delete(player_service); delete(time_service); + delete(ui_service); } ServiceLocator* ServiceLocator::getInstance() @@ -51,6 +55,7 @@ namespace Global event_service->initialize(); player_service->initialize(); time_service->initialize(); + ui_service->initialize(); } void ServiceLocator::update() @@ -59,19 +64,22 @@ namespace Global event_service->update(); player_service->update(); time_service->update(); + ui_service->update(); } void ServiceLocator::render() { graphic_service->render(); player_service->render(); + ui_service->render(); //no event service because nothing to render - //no time service + //no time 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; } + UIService* ServiceLocator::getUIService() { return ui_service; } } \ No newline at end of file diff --git a/Space-Invaders/Source/Graphic/GraphicService.cpp b/Space-Invaders/Source/Graphic/GraphicService.cpp index b10897d5d..98ab5cc20 100644 --- a/Space-Invaders/Source/Graphic/GraphicService.cpp +++ b/Space-Invaders/Source/Graphic/GraphicService.cpp @@ -22,7 +22,7 @@ namespace Graphics // 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/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp new file mode 100644 index 000000000..e5accf6e4 --- /dev/null +++ b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp @@ -0,0 +1,105 @@ +#include "../../Header/UI/MainMenu/MainMenuUIController.h" +#include "../../Header/UI/MainMenu/MainMenuUIController.h" +#include "../../Header/Main/GameService.h" +#include "../../Header/Global/ServiceLocator.h" +#include "../../Header/Graphic/GraphicService.h" + +namespace UI +{ + namespace MainMenu //nested namespace since everything in MainMenu exists inside UI + { + using namespace Global; + using namespace Main; + using namespace Graphics; + using namespace Event; + + MainMenuUIController::MainMenuUIController() { game_window = nullptr; } + + void MainMenuUIController::initialize() + { + game_window = Global::ServiceLocator::getInstance()->getGraphicService()->getGameWindow();// added global to access + initializeBackgroundImage(); + initializeButtons(); + } + + void MainMenuUIController::initializeBackgroundImage() + { //check if a texture loaded properly + 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(); + } + } + // only returns true if all tectures are loaded + 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::update() + { + } + + void 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); + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/UI/UIService.cpp b/Space-Invaders/Source/UI/UIService.cpp new file mode 100644 index 000000000..33b445b99 --- /dev/null +++ b/Space-Invaders/Source/UI/UIService.cpp @@ -0,0 +1,51 @@ +#include "../../Header/UI/UIService.h" +#include "../../Header/Main/GameService.h" + +namespace UI +{ + using namespace Main; + using namespace MainMenu; + + UIService::UIService() + { + main_menu_controller = nullptr; + + createControllers(); + } + + void UIService::createControllers() + { + main_menu_controller = new MainMenuUIController(); + } + + UIService::~UIService() + { + destroy(); + } + + void UIService::initialize() + { + initializeControllers(); + + } + + void UIService::update() + { + main_menu_controller->update(); + } + + void UIService::render() + { + main_menu_controller->render(); + } + + void UIService::initializeControllers() + { + main_menu_controller->initialize(); + } + + void UIService::destroy() + { + delete(main_menu_controller); + } +} \ No newline at end of file From 50e219cd90c8baeaf764a50ca776ce476458a60f Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Fri, 2 Aug 2024 15:49:04 +0530 Subject: [PATCH 28/41] Added new control functions --- Space-Invaders/Header/Event/EventService.h | 22 ++++++ Space-Invaders/Header/Main/GameService.h | 1 + .../Header/UI/MainMenu/MainMenuUIController.h | 3 + Space-Invaders/Source/Event/EventService.cpp | 73 ++++++++++++++++++- Space-Invaders/Source/Main/GameService.cpp | 7 ++ .../Source/Player/PlayerController.cpp | 10 ++- .../UI/MainMenu/MainMenuUIController.cpp | 25 +++++++ Space-Invaders/Source/UI/UIService.cpp | 14 +++- 8 files changed, 146 insertions(+), 9 deletions(-) diff --git a/Space-Invaders/Header/Event/EventService.h b/Space-Invaders/Header/Event/EventService.h index 6f5924392..ebecfd5d6 100644 --- a/Space-Invaders/Header/Event/EventService.h +++ b/Space-Invaders/Header/Event/EventService.h @@ -5,6 +5,13 @@ namespace Event { + enum class ButtonState + { + PRESSED, + HELD, + RELEASED, + }; + class EventService { private: @@ -15,6 +22,17 @@ namespace Event bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. bool hasQuitGame(); //for our new 'ESC' condition + //Button States + 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; + + //Button and mouse state functions + void updateMouseButtonsState(ButtonState& current_button_state, sf::Mouse::Button mouse_button); + void updateKeyboardButtonsState(ButtonState& current_button_state, sf::Keyboard::Key keyboard_button); public: @@ -28,6 +46,10 @@ namespace Event bool isKeyboardEvent(); bool pressedLeftKey(); // getting inputs for the player bool pressedRightKey(); + bool pressedLeftMouseButton(); // Mouse inputs for the player + bool pressedRightMouseButton(); + bool pressedAKey(); // AD held check + bool pressedDKey(); }; } diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h index 123bf325a..5671190cb 100644 --- a/Space-Invaders/Header/Main/GameService.h +++ b/Space-Invaders/Header/Main/GameService.h @@ -26,6 +26,7 @@ namespace Main void initialize(); // Handles game initialization. void initializeVariables();// Handles game initialization. void destroy(); // Handles cleanup tasks. + void showMainMenu(); public: GameService(); // Constructor for initializing the GameService object. diff --git a/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h index 32dc26439..0b811811c 100644 --- a/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h @@ -42,6 +42,9 @@ namespace UI void scaleAllButttons(); void scaleButton(sf::Sprite* button_to_scale); void positionButtons(); + // Which button and function + void processButtonInteractions(); + bool clickedButton(sf::Sprite*, sf::Vector2f); public: MainMenuUIController(); diff --git a/Space-Invaders/Source/Event/EventService.cpp b/Space-Invaders/Source/Event/EventService.cpp index f1622bcd5..cd456eff0 100644 --- a/Space-Invaders/Source/Event/EventService.cpp +++ b/Space-Invaders/Source/Event/EventService.cpp @@ -19,7 +19,12 @@ 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() @@ -35,6 +40,46 @@ 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; + } + } + 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 @@ -45,7 +90,29 @@ namespace Event bool EventService::isGameWindowOpen() { return game_window != nullptr; } bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } - // Player inputs - bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } + // Player inputs + bool EventService::pressedLeftKey() { return left_arrow_button_state == ButtonState::HELD; } + + bool EventService::pressedRightKey() { return right_arrow_button_state == ButtonState::HELD; } + + bool EventService::pressedAKey() { return A_button_state == ButtonState::HELD; } + + bool EventService::pressedDKey() { return D_button_state == ButtonState::HELD; } + + bool EventService::pressedLeftMouseButton() { return left_mouse_button_state == ButtonState::PRESSED; } + + bool EventService::pressedRightMouseButton() { return right_mouse_button_state == ButtonState::PRESSED; } + + /*bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } + bool EventService::pressedLeftMouseButton() + { + // check if a mouse button was pressed and which mouse button it was + 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; + }*/ } \ No newline at end of file diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp index 6153c00b6..0118a4dab 100644 --- a/Space-Invaders/Source/Main/GameService.cpp +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -32,6 +32,8 @@ namespace Main { service_locator->initialize(); initializeVariables(); + // set the gamestate to main menu + showMainMenu(); } void GameService::initializeVariables() @@ -44,6 +46,11 @@ namespace Main // don't need to do anything here for now. } + void GameService::showMainMenu() + { + setGameState(GameState::MAIN_MENU); + } + // Updates the game logic by delegating to the service locator's update method. void GameService::update() { diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp index 534114f48..d84805283 100644 --- a/Space-Invaders/Source/Player/PlayerController.cpp +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -49,13 +49,15 @@ namespace Player void PlayerController::processPlayerInput() { - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + + EventService* event_service = ServiceLocator::getInstance()->getEventService(); + + if (event_service->pressedLeftKey() || event_service->pressedAKey()) { moveLeft(); } - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + + if (event_service->pressedRightKey() || event_service->pressedDKey()) { moveRight(); } diff --git a/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp index e5accf6e4..37b309a04 100644 --- a/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp @@ -90,8 +90,33 @@ namespace UI 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 = Global::ServiceLocator::getInstance()->getEventService(); + return event_service->pressedLeftMouseButton() && button_sprite->getGlobalBounds().contains(mouse_position); + } + void MainMenuUIController::update() { + processButtonInteractions(); } void MainMenuUIController::render() diff --git a/Space-Invaders/Source/UI/UIService.cpp b/Space-Invaders/Source/UI/UIService.cpp index 33b445b99..3e60dec73 100644 --- a/Space-Invaders/Source/UI/UIService.cpp +++ b/Space-Invaders/Source/UI/UIService.cpp @@ -31,12 +31,22 @@ namespace UI void UIService::update() { - main_menu_controller->update(); + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->update();; + break; + } } void UIService::render() { - main_menu_controller->render(); + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->render(); + break; + } } void UIService::initializeControllers() From 35c51f603645dd87677c99587fd3ef437e55a480 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Sun, 4 Aug 2024 13:44:00 +0530 Subject: [PATCH 29/41] Added EnemyService // enemy_ship.png - zapper.png --- Space-Invaders/Header/Enemy/EnemyController.h | 27 ++++++++++ Space-Invaders/Header/Enemy/EnemyModel.h | 25 ++++++++++ Space-Invaders/Header/Enemy/EnemyService.h | 25 ++++++++++ Space-Invaders/Header/Enemy/EnemyView.h | 34 +++++++++++++ Space-Invaders/Header/Global/ServiceLocator.h | 3 ++ .../Source/Enemy/EnemyController.cpp | 42 ++++++++++++++++ Space-Invaders/Source/Enemy/EnemyModel.cpp | 34 +++++++++++++ Space-Invaders/Source/Enemy/EnemyService.cpp | 43 ++++++++++++++++ Space-Invaders/Source/Enemy/EnemyView.cpp | 49 +++++++++++++++++++ .../Source/Global/ServiceLocator.cpp | 28 +++++++++-- 10 files changed, 305 insertions(+), 5 deletions(-) create mode 100644 Space-Invaders/Header/Enemy/EnemyController.h create mode 100644 Space-Invaders/Header/Enemy/EnemyModel.h create mode 100644 Space-Invaders/Header/Enemy/EnemyService.h create mode 100644 Space-Invaders/Header/Enemy/EnemyView.h create mode 100644 Space-Invaders/Source/Enemy/EnemyController.cpp create mode 100644 Space-Invaders/Source/Enemy/EnemyModel.cpp create mode 100644 Space-Invaders/Source/Enemy/EnemyService.cpp create mode 100644 Space-Invaders/Source/Enemy/EnemyView.cpp diff --git a/Space-Invaders/Header/Enemy/EnemyController.h b/Space-Invaders/Header/Enemy/EnemyController.h new file mode 100644 index 000000000..7910db832 --- /dev/null +++ b/Space-Invaders/Header/Enemy/EnemyController.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +namespace Enemy +{ + class EnemyView; + class EnemyModel; + + class EnemyController + { + private: + + EnemyView* enemy_view; + 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/Header/Enemy/EnemyModel.h b/Space-Invaders/Header/Enemy/EnemyModel.h new file mode 100644 index 000000000..19f6f84ef --- /dev/null +++ b/Space-Invaders/Header/Enemy/EnemyModel.h @@ -0,0 +1,25 @@ +#pragma once +#include + +namespace Enemy +{ + class EnemyModel + { + private: + sf::Vector2f reference_position = sf::Vector2f(100.f, 100.f); + sf::Vector2f enemy_position; + + public: + EnemyModel(); + ~EnemyModel(); + + void initialize(); + + 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/Header/Enemy/EnemyService.h b/Space-Invaders/Header/Enemy/EnemyService.h new file mode 100644 index 000000000..7ade420ca --- /dev/null +++ b/Space-Invaders/Header/Enemy/EnemyService.h @@ -0,0 +1,25 @@ +#pragma once +namespace Enemy +{ + class EnemyController; + + class EnemyService + { + private: + + void Destroy(); // function to delete enemy + + EnemyController* enemy; // enemy controller ptr + + public: + EnemyService(); + virtual ~EnemyService(); + + void initialize(); + void update(); + void render(); + + EnemyController* spawnEnemy(); // Function to spawn enemy + + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyView.h b/Space-Invaders/Header/Enemy/EnemyView.h new file mode 100644 index 000000000..a2e91275a --- /dev/null +++ b/Space-Invaders/Header/Enemy/EnemyView.h @@ -0,0 +1,34 @@ +#pragma once + +#include + +namespace Enemy +{ + class EnemyController; + + class EnemyView + { + private: + const sf::String enemy_texture_path = "assets/textures/zapper.png"; // cureent ship - zapper + + const float enemy_sprite_width = 60.f; + const float enemy_sprite_height = 60.f; + + EnemyController* enemy_controller; + + sf::RenderWindow* game_window; + sf::Texture enemy_texture; + sf::Sprite enemy_sprite; + + void initializeEnemySprite(); + void scaleEnemySprite(); + + public: + EnemyView(); + ~EnemyView(); + + void initialize(EnemyController* controller); + void update(); + void render(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index fe2ccf055..445b2cd3e 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -4,6 +4,7 @@ #include "../../Header/Player/PlayerService.h" #include "../../Header/Time/TimeService.h" #include "../../Header/UI/UIService.h" +#include "../../Header/Enemy/EnemyService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -20,6 +21,7 @@ namespace Global Player::PlayerService* player_service; Time::TimeService* time_service; UI::UIService* ui_service; + Enemy::EnemyService* enemy_service; // Public Methods ServiceLocator(); @@ -43,5 +45,6 @@ namespace Global Player::PlayerService* getPlayerService(); // Retrieve the PlayerService instance Time::TimeService* getTimeService(); // Retrieve the TimeService instance UI::UIService* getUIService(); // Retrive the UIService instance + Enemy::EnemyService* getEnemyService(); // Retrive the EnemyService instance }; } \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyController.cpp b/Space-Invaders/Source/Enemy/EnemyController.cpp new file mode 100644 index 000000000..646dd18d3 --- /dev/null +++ b/Space-Invaders/Source/Enemy/EnemyController.cpp @@ -0,0 +1,42 @@ +#include "../../Header/Enemy/EnemyController.h" +#include "../../Header/Enemy/EnemyView.h" +#include "../../Header/Enemy/EnemyModel.h" +#include "../../Header/Global/ServiceLocator.h" + +namespace Enemy +{ + using namespace Global; + + EnemyController::EnemyController() + { + enemy_view = new EnemyView(); + enemy_model = new EnemyModel(); + } + + EnemyController::~EnemyController() + { + delete (enemy_view); + delete (enemy_model); + } + + void EnemyController::initialize() + { + enemy_model->initialize(); + enemy_view->initialize(this); // we will discuss this soon + } + + void EnemyController::update() + { + enemy_view->update(); + } + + void EnemyController::render() + { + enemy_view->render(); + } + + sf::Vector2f EnemyController::getEnemyPosition() + { + return enemy_model->getEnemyPosition(); + } +} diff --git a/Space-Invaders/Source/Enemy/EnemyModel.cpp b/Space-Invaders/Source/Enemy/EnemyModel.cpp new file mode 100644 index 000000000..56bbdba7c --- /dev/null +++ b/Space-Invaders/Source/Enemy/EnemyModel.cpp @@ -0,0 +1,34 @@ +#include "../../Header/Enemy/EnemyModel.h" + +namespace Enemy +{ + EnemyModel::EnemyModel() { } + + EnemyModel::~EnemyModel() { } + + void EnemyModel::initialize() + { + enemy_position = reference_position; + } + + sf::Vector2f EnemyModel::getEnemyPosition() + { + return enemy_position; + } + + void EnemyModel::setEnemyPosition(sf::Vector2f position) + { + enemy_position = position; + } + + sf::Vector2f EnemyModel::getReferencePosition() + { + return reference_position; + } + + void EnemyModel::setReferencePosition(sf::Vector2f position) + { + reference_position = position; + } + +} \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyService.cpp b/Space-Invaders/Source/Enemy/EnemyService.cpp new file mode 100644 index 000000000..44548a8d7 --- /dev/null +++ b/Space-Invaders/Source/Enemy/EnemyService.cpp @@ -0,0 +1,43 @@ +#include "../../Header/Enemy/EnemyService.h" +#include "../../Header/Enemy/EnemyController.h" +#include "../../Header/Global/ServiceLocator.h" + +namespace Enemy +{ + using namespace Global; + + + EnemyService::EnemyService() { enemy = nullptr; } + + EnemyService::~EnemyService() { Destroy(); } + + void EnemyService::initialize() + { + //Spawn Enemy + spawnEnemy(); + } + + void EnemyService::update() + { + } + + void EnemyService::render() + { + enemy->render(); + } + + EnemyController* EnemyService::spawnEnemy() + { + //creates and intis an enemy controller + enemy = new EnemyController(); + enemy->initialize(); + + return enemy; + } + + void EnemyService::Destroy() + { + //deallocate memory + delete(enemy); + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyView.cpp b/Space-Invaders/Source/Enemy/EnemyView.cpp new file mode 100644 index 000000000..02c0dddd4 --- /dev/null +++ b/Space-Invaders/Source/Enemy/EnemyView.cpp @@ -0,0 +1,49 @@ +#include "../../Header/Enemy/EnemyView.h" +#include "../../Header/Global/ServiceLocator.h" +#include "../../Header/Graphic/GraphicService.h" +#include "../../Header/Enemy/EnemyController.h" + +namespace Enemy +{ + using namespace Global; + using namespace Graphics; + + EnemyView::EnemyView() { } + + EnemyView::~EnemyView() { } + + void EnemyView::initialize(EnemyController* controller) + { + enemy_controller = controller; + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializeEnemySprite(); + } + + void 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 + } + } + + void EnemyView::scaleEnemySprite() + { + // method to scale the Sprite according to our set dimensions. Don't worry about the static_cast, that will be discussed later. + 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::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/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index 9983889db..4385177fe 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -9,6 +9,7 @@ namespace Global using namespace Time; using namespace Player; using namespace UI; + using namespace Enemy; ServiceLocator::ServiceLocator() @@ -18,6 +19,7 @@ namespace Global player_service = nullptr; time_service = nullptr; ui_service = nullptr; + enemy_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -32,6 +34,7 @@ namespace Global player_service = new PlayerService(); time_service = new TimeService(); ui_service = new UIService(); + enemy_service = new EnemyService(); } void ServiceLocator::clearAllServices() @@ -41,6 +44,7 @@ namespace Global delete(player_service); delete(time_service); delete(ui_service); + delete(enemy_service); } ServiceLocator* ServiceLocator::getInstance() @@ -56,24 +60,37 @@ namespace Global player_service->initialize(); time_service->initialize(); ui_service->initialize(); + enemy_service->initialize(); } void ServiceLocator::update() { graphic_service->update(); - event_service->update(); - player_service->update(); time_service->update(); - ui_service->update(); + event_service->update(); + + if (GameService::getGameState() == GameState::GAMEPLAY) + { + player_service->update(); + enemy_service->update(); + } + + ui_service->update(); } void ServiceLocator::render() { graphic_service->render(); - player_service->render(); + + if (GameService::getGameState() == GameState::GAMEPLAY) + { + player_service->render(); + enemy_service->render(); + } + ui_service->render(); //no event service because nothing to render - //no time service + //no time service because nothing to render } GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } @@ -81,5 +98,6 @@ namespace Global PlayerService* ServiceLocator::getPlayerService() { return player_service; } TimeService* ServiceLocator::getTimeService() { return time_service; } UIService* ServiceLocator::getUIService() { return ui_service; } + EnemyService* ServiceLocator::getEnemyService() { return enemy_service; } } \ No newline at end of file From f515f61003233cc65aa1d47a308aa437acb97da2 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Sun, 4 Aug 2024 14:15:14 +0530 Subject: [PATCH 30/41] Added movement --- Space-Invaders/Header/Enemy/EnemyController.h | 6 ++ Space-Invaders/Header/Enemy/EnemyModel.h | 19 ++++++ .../Source/Enemy/EnemyController.cpp | 60 +++++++++++++++++++ Space-Invaders/Source/Enemy/EnemyModel.cpp | 12 ++++ Space-Invaders/Source/Enemy/EnemyService.cpp | 1 + 5 files changed, 98 insertions(+) diff --git a/Space-Invaders/Header/Enemy/EnemyController.h b/Space-Invaders/Header/Enemy/EnemyController.h index 7910db832..4ebb4e266 100644 --- a/Space-Invaders/Header/Enemy/EnemyController.h +++ b/Space-Invaders/Header/Enemy/EnemyController.h @@ -14,6 +14,12 @@ namespace Enemy EnemyView* enemy_view; EnemyModel* enemy_model; + // manage movement methods + void move(); + void moveLeft(); + void moveRight(); + void moveDown(); + public: EnemyController(); ~EnemyController(); diff --git a/Space-Invaders/Header/Enemy/EnemyModel.h b/Space-Invaders/Header/Enemy/EnemyModel.h index 19f6f84ef..037d2dced 100644 --- a/Space-Invaders/Header/Enemy/EnemyModel.h +++ b/Space-Invaders/Header/Enemy/EnemyModel.h @@ -3,23 +3,42 @@ namespace Enemy { + enum class MovementDirection + { + LEFT, + RIGHT, + DOWN, + }; + class EnemyModel { private: sf::Vector2f reference_position = sf::Vector2f(100.f, 100.f); sf::Vector2f enemy_position; + MovementDirection movement_direction; public: EnemyModel(); ~EnemyModel(); + //const settings for enemy + 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 vertical_travel_distance = 100.f; + const float enemy_movement_speed = 250.0f; + void initialize(); + // Getters and Setters sf::Vector2f getEnemyPosition(); void setEnemyPosition(sf::Vector2f position); 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/Source/Enemy/EnemyController.cpp b/Space-Invaders/Source/Enemy/EnemyController.cpp index 646dd18d3..b7dd7c979 100644 --- a/Space-Invaders/Source/Enemy/EnemyController.cpp +++ b/Space-Invaders/Source/Enemy/EnemyController.cpp @@ -7,6 +7,65 @@ namespace Enemy { using namespace Global; + void EnemyController::move() + { + switch (enemy_model->getMovementDirection()) + { + case::Enemy::MovementDirection::LEFT: + moveLeft(); + break; + + case::Enemy::MovementDirection::RIGHT: + moveRight(); + break; + + case::Enemy::MovementDirection::DOWN: + moveDown(); + break; + } + } + + void EnemyController::moveLeft() + { + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); //get enemy pos + currentPosition.x += enemy_model->enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); //move + + if (currentPosition.x >= enemy_model->left_most_position.x) //check if we reached right most pos + { + enemy_model->setMovementDirection(MovementDirection::DOWN); // move + enemy_model->setReferencePosition(currentPosition);// set ref pos + } + else enemy_model->setEnemyPosition(currentPosition); + } + + void EnemyController::moveRight() + { + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); //get enemy pos + currentPosition.x += enemy_model->enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); //move + + if (currentPosition.x >= enemy_model->right_most_position.x) //check if we reached right most pos + { + enemy_model->setMovementDirection(MovementDirection::DOWN); // move + enemy_model->setReferencePosition(currentPosition);// set ref pos + } + else enemy_model->setEnemyPosition(currentPosition); + } + + void EnemyController::moveDown() + { + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); + currentPosition.y += enemy_model->enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + //check if enemy has moved the specified distance downwards + if (currentPosition.y >= enemy_model->getReferencePosition().y + enemy_model->vertical_travel_distance) + { + //check where to move them based on position + if (enemy_model->getReferencePosition().x <= enemy_model->left_most_position.x) enemy_model->setMovementDirection(MovementDirection::RIGHT); + else enemy_model->setMovementDirection(MovementDirection::LEFT); + } + else enemy_model->setEnemyPosition(currentPosition); + } + EnemyController::EnemyController() { enemy_view = new EnemyView(); @@ -27,6 +86,7 @@ namespace Enemy void EnemyController::update() { + move(); // has all directional moves enemy_view->update(); } diff --git a/Space-Invaders/Source/Enemy/EnemyModel.cpp b/Space-Invaders/Source/Enemy/EnemyModel.cpp index 56bbdba7c..1b5eb8b9f 100644 --- a/Space-Invaders/Source/Enemy/EnemyModel.cpp +++ b/Space-Invaders/Source/Enemy/EnemyModel.cpp @@ -8,6 +8,8 @@ namespace Enemy void EnemyModel::initialize() { + // Starts from the right position + movement_direction = MovementDirection::RIGHT; enemy_position = reference_position; } @@ -31,4 +33,14 @@ 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/Source/Enemy/EnemyService.cpp b/Space-Invaders/Source/Enemy/EnemyService.cpp index 44548a8d7..63d5b5bc2 100644 --- a/Space-Invaders/Source/Enemy/EnemyService.cpp +++ b/Space-Invaders/Source/Enemy/EnemyService.cpp @@ -19,6 +19,7 @@ namespace Enemy void EnemyService::update() { + enemy->update(); } void EnemyService::render() From fb6f2c47698d0fab7ac457481eb6e62c5a437cc5 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Sun, 4 Aug 2024 14:45:07 +0530 Subject: [PATCH 31/41] fixed move and multiple enemies --- Space-Invaders/Header/Enemy/EnemyController.h | 10 +- Space-Invaders/Header/Enemy/EnemyModel.h | 1 + Space-Invaders/Header/Enemy/EnemyService.h | 14 ++- .../Source/Enemy/EnemyController.cpp | 99 ++++++++++--------- Space-Invaders/Source/Enemy/EnemyService.cpp | 44 ++++++--- 5 files changed, 98 insertions(+), 70 deletions(-) diff --git a/Space-Invaders/Header/Enemy/EnemyController.h b/Space-Invaders/Header/Enemy/EnemyController.h index 4ebb4e266..2cd1d2e75 100644 --- a/Space-Invaders/Header/Enemy/EnemyController.h +++ b/Space-Invaders/Header/Enemy/EnemyController.h @@ -1,5 +1,4 @@ #pragma once - #include namespace Enemy @@ -7,14 +6,20 @@ namespace Enemy class EnemyView; class EnemyModel; + enum class EnemyState; + class EnemyController { private: + 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 vertical_travel_distance = 100.f; + const float enemy_movement_speed = 250.0f; EnemyView* enemy_view; EnemyModel* enemy_model; - // manage movement methods void move(); void moveLeft(); void moveRight(); @@ -29,5 +34,6 @@ namespace Enemy void render(); sf::Vector2f getEnemyPosition(); + EnemyState getEnemyState(); }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyModel.h b/Space-Invaders/Header/Enemy/EnemyModel.h index 037d2dced..884a19d29 100644 --- a/Space-Invaders/Header/Enemy/EnemyModel.h +++ b/Space-Invaders/Header/Enemy/EnemyModel.h @@ -40,5 +40,6 @@ namespace Enemy MovementDirection getMovementDirection(); void setMovementDirection(MovementDirection direction); + }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyService.h b/Space-Invaders/Header/Enemy/EnemyService.h index 7ade420ca..17de6dfe3 100644 --- a/Space-Invaders/Header/Enemy/EnemyService.h +++ b/Space-Invaders/Header/Enemy/EnemyService.h @@ -1,4 +1,6 @@ #pragma once +#include + namespace Enemy { class EnemyController; @@ -7,9 +9,14 @@ namespace Enemy { private: - void Destroy(); // function to delete enemy + const float spawn_interval = 3.f; + + std::vector enemy_list; + float spawn_timer; - EnemyController* enemy; // enemy controller ptr + void updateSpawnTimer(); + void processEnemySpawn(); + void destroy(); public: EnemyService(); @@ -18,8 +25,7 @@ namespace Enemy void initialize(); void update(); void render(); - - EnemyController* spawnEnemy(); // Function to spawn enemy + void spawnEnemy(); // Function to spawn enemy }; } \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyController.cpp b/Space-Invaders/Source/Enemy/EnemyController.cpp index b7dd7c979..600361c3c 100644 --- a/Space-Invaders/Source/Enemy/EnemyController.cpp +++ b/Space-Invaders/Source/Enemy/EnemyController.cpp @@ -1,11 +1,43 @@ -#include "../../Header/Enemy/EnemyController.h" -#include "../../Header/Enemy/EnemyView.h" -#include "../../Header/Enemy/EnemyModel.h" -#include "../../Header/Global/ServiceLocator.h" +#include "../../header/Enemy/EnemyController.h" +#include "../../header/Enemy/EnemyView.h" +#include "../../header/Enemy/EnemyModel.h" +#include "../../header/Global/ServiceLocator.h" +#include "../../header/Event/EventService.h" namespace Enemy { using namespace Global; + using namespace Event; + using namespace Time; + + EnemyController::EnemyController() + { + enemy_view = new EnemyView(); + enemy_model = new EnemyModel(); + } + + EnemyController::~EnemyController() + { + delete (enemy_view); + delete (enemy_model); + } + + void EnemyController::initialize() + { + enemy_model->initialize(); + enemy_view->initialize(this); + } + + void EnemyController::update() + { + move(); + enemy_view->update(); + } + + void EnemyController::render() + { + enemy_view->render(); + } void EnemyController::move() { @@ -27,26 +59,26 @@ namespace Enemy void EnemyController::moveLeft() { - sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); //get enemy pos - currentPosition.x += enemy_model->enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); //move + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); + currentPosition.x -= enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - if (currentPosition.x >= enemy_model->left_most_position.x) //check if we reached right most pos + if (currentPosition.x <= left_most_position.x) { - enemy_model->setMovementDirection(MovementDirection::DOWN); // move - enemy_model->setReferencePosition(currentPosition);// set ref pos + enemy_model->setMovementDirection(MovementDirection::DOWN); + enemy_model->setReferencePosition(currentPosition); } else enemy_model->setEnemyPosition(currentPosition); } void EnemyController::moveRight() { - sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); //get enemy pos - currentPosition.x += enemy_model->enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); //move + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); + currentPosition.x += enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - if (currentPosition.x >= enemy_model->right_most_position.x) //check if we reached right most pos + if (currentPosition.x >= right_most_position.x) { - enemy_model->setMovementDirection(MovementDirection::DOWN); // move - enemy_model->setReferencePosition(currentPosition);// set ref pos + enemy_model->setMovementDirection(MovementDirection::DOWN); + enemy_model->setReferencePosition(currentPosition); } else enemy_model->setEnemyPosition(currentPosition); } @@ -54,49 +86,18 @@ namespace Enemy void EnemyController::moveDown() { sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); - currentPosition.y += enemy_model->enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + currentPosition.y += enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - //check if enemy has moved the specified distance downwards - if (currentPosition.y >= enemy_model->getReferencePosition().y + enemy_model->vertical_travel_distance) + if (currentPosition.y >= enemy_model->getReferencePosition().y + vertical_travel_distance) { - //check where to move them based on position - if (enemy_model->getReferencePosition().x <= enemy_model->left_most_position.x) enemy_model->setMovementDirection(MovementDirection::RIGHT); + if (enemy_model->getReferencePosition().x <= left_most_position.x) enemy_model->setMovementDirection(MovementDirection::RIGHT); else enemy_model->setMovementDirection(MovementDirection::LEFT); } else enemy_model->setEnemyPosition(currentPosition); } - EnemyController::EnemyController() - { - enemy_view = new EnemyView(); - enemy_model = new EnemyModel(); - } - - EnemyController::~EnemyController() - { - delete (enemy_view); - delete (enemy_model); - } - - void EnemyController::initialize() - { - enemy_model->initialize(); - enemy_view->initialize(this); // we will discuss this soon - } - - void EnemyController::update() - { - move(); // has all directional moves - enemy_view->update(); - } - - void EnemyController::render() - { - enemy_view->render(); - } - sf::Vector2f EnemyController::getEnemyPosition() { return enemy_model->getEnemyPosition(); } -} +} \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyService.cpp b/Space-Invaders/Source/Enemy/EnemyService.cpp index 63d5b5bc2..ebe71d130 100644 --- a/Space-Invaders/Source/Enemy/EnemyService.cpp +++ b/Space-Invaders/Source/Enemy/EnemyService.cpp @@ -1,44 +1,58 @@ #include "../../Header/Enemy/EnemyService.h" #include "../../Header/Enemy/EnemyController.h" #include "../../Header/Global/ServiceLocator.h" +#include "../../Header/Time/TimeService.h" namespace Enemy { using namespace Global; + using namespace Time; + EnemyService::EnemyService() { } - EnemyService::EnemyService() { enemy = nullptr; } - - EnemyService::~EnemyService() { Destroy(); } + EnemyService::~EnemyService() { destroy(); } void EnemyService::initialize() { - //Spawn Enemy - spawnEnemy(); + spawn_timer = spawn_interval; } void EnemyService::update() { - enemy->update(); + updateSpawnTimer(); + processEnemySpawn(); + + for (int i = 0; i < enemy_list.size(); i++) enemy_list[i]->update(); } void EnemyService::render() { - enemy->render(); + for (int i = 0; i < enemy_list.size(); i++) enemy_list[i]->render(); } - EnemyController* EnemyService::spawnEnemy() + void EnemyService::spawnEnemy() { - //creates and intis an enemy controller - enemy = new EnemyController(); - enemy->initialize(); + EnemyController* enemy_controller = new EnemyController(); + enemy_controller->initialize(); + enemy_list.push_back(enemy_controller); + } - return enemy; + void EnemyService::updateSpawnTimer() + { + spawn_timer += ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); // increase timer + } + + void EnemyService::processEnemySpawn() + { + if (spawn_timer >= spawn_interval) + { + spawnEnemy(); //spawn + spawn_timer = 0.0f; // reset + } } - void EnemyService::Destroy() + void EnemyService::destroy() { - //deallocate memory - delete(enemy); + for (int i = 0; i < enemy_list.size(); i++) delete (enemy_list[i]); //delete all enemies } } \ No newline at end of file From c2cf915abfc0bb04b507f1e90efaa654aff63b70 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Sun, 4 Aug 2024 15:13:03 +0530 Subject: [PATCH 32/41] Added Gameplay Services --- .../Header/Gameplay/GameplayController.h | 21 ++++++++++ .../Header/Gameplay/GameplayService.h | 19 +++++++++ Space-Invaders/Header/Gameplay/GameplayView.h | 26 ++++++++++++ Space-Invaders/Header/Global/ServiceLocator.h | 3 ++ .../Source/Gameplay/GameplayController.cpp | 15 +++++++ .../Source/Gameplay/GameplayService.cpp | 15 +++++++ .../Source/Gameplay/GameplayView.cpp | 40 +++++++++++++++++++ .../Source/Global/ServiceLocator.cpp | 8 ++++ 8 files changed, 147 insertions(+) create mode 100644 Space-Invaders/Header/Gameplay/GameplayController.h create mode 100644 Space-Invaders/Header/Gameplay/GameplayService.h create mode 100644 Space-Invaders/Header/Gameplay/GameplayView.h create mode 100644 Space-Invaders/Source/Gameplay/GameplayController.cpp create mode 100644 Space-Invaders/Source/Gameplay/GameplayService.cpp create mode 100644 Space-Invaders/Source/Gameplay/GameplayView.cpp diff --git a/Space-Invaders/Header/Gameplay/GameplayController.h b/Space-Invaders/Header/Gameplay/GameplayController.h new file mode 100644 index 000000000..76b6fcf24 --- /dev/null +++ b/Space-Invaders/Header/Gameplay/GameplayController.h @@ -0,0 +1,21 @@ +#pragma once +#include + +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/Header/Gameplay/GameplayService.h b/Space-Invaders/Header/Gameplay/GameplayService.h new file mode 100644 index 000000000..53efd34a5 --- /dev/null +++ b/Space-Invaders/Header/Gameplay/GameplayService.h @@ -0,0 +1,19 @@ +#pragma once +namespace Gameplay +{ + class GameplayController; + + class GameplayService + { + private: + GameplayController* gameplay_controller; + + public: + GameplayService(); + ~GameplayService(); + + void initialize(); + void update(); + void render(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Gameplay/GameplayView.h b/Space-Invaders/Header/Gameplay/GameplayView.h new file mode 100644 index 000000000..96ecc2163 --- /dev/null +++ b/Space-Invaders/Header/Gameplay/GameplayView.h @@ -0,0 +1,26 @@ +#pragma once +#include + +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; + + void initializeBackgroundSprite(); + void scaleBackgroundSprite(); + + public: + GameplayView(); + ~GameplayView(); + + void initialize(); + void update(); + void render(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index 445b2cd3e..865b12193 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -5,6 +5,7 @@ #include "../../Header/Time/TimeService.h" #include "../../Header/UI/UIService.h" #include "../../Header/Enemy/EnemyService.h" +#include "../../Header/Gameplay/GameplayService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -22,6 +23,7 @@ namespace Global Time::TimeService* time_service; UI::UIService* ui_service; Enemy::EnemyService* enemy_service; + Gameplay::GameplayService* gameplay_service; // Public Methods ServiceLocator(); @@ -46,5 +48,6 @@ namespace Global Time::TimeService* getTimeService(); // Retrieve the TimeService instance UI::UIService* getUIService(); // Retrive the UIService instance Enemy::EnemyService* getEnemyService(); // Retrive the EnemyService instance + Gameplay::GameplayService* getGameplayService(); // Retrive the GameplayService instance }; } \ No newline at end of file diff --git a/Space-Invaders/Source/Gameplay/GameplayController.cpp b/Space-Invaders/Source/Gameplay/GameplayController.cpp new file mode 100644 index 000000000..ceb231491 --- /dev/null +++ b/Space-Invaders/Source/Gameplay/GameplayController.cpp @@ -0,0 +1,15 @@ +#include "../../header/Gameplay/GameplayController.h" +#include "../../header/Gameplay/GameplayView.h" + +namespace Gameplay +{ + GameplayController::GameplayController() { gameplay_view = new GameplayView(); } + + GameplayController::~GameplayController() { delete (gameplay_view); } + + void GameplayController::initialize() { gameplay_view->initialize(); } + + void GameplayController::update() { gameplay_view->update(); } + + void GameplayController::render() { gameplay_view->render(); } +} diff --git a/Space-Invaders/Source/Gameplay/GameplayService.cpp b/Space-Invaders/Source/Gameplay/GameplayService.cpp new file mode 100644 index 000000000..91b7e9761 --- /dev/null +++ b/Space-Invaders/Source/Gameplay/GameplayService.cpp @@ -0,0 +1,15 @@ +#include "../../header/Gameplay/GameplayService.h" +#include "../../header/Gameplay/GameplayController.h" + +namespace Gameplay +{ + GameplayService::GameplayService() { gameplay_controller = new GameplayController(); } + + GameplayService::~GameplayService() { delete (gameplay_controller); } + + void GameplayService::initialize() { gameplay_controller->initialize(); } + + void GameplayService::update() { gameplay_controller->update(); } + + void GameplayService::render() { gameplay_controller->render(); } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Gameplay/GameplayView.cpp b/Space-Invaders/Source/Gameplay/GameplayView.cpp new file mode 100644 index 000000000..d7c276f92 --- /dev/null +++ b/Space-Invaders/Source/Gameplay/GameplayView.cpp @@ -0,0 +1,40 @@ +#include "../../header/Gameplay/GameplayView.h" +#include "../../header/Global/ServiceLocator.h" +#include "../../header/Graphic/GraphicService.h" + +namespace Gameplay +{ + using namespace Global; + using namespace Graphics; + + GameplayView::GameplayView() { } + + GameplayView::~GameplayView() { } + + void GameplayView::initialize() + { + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializeBackgroundSprite(); + } + + void GameplayView::initializeBackgroundSprite() + { + 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 GameplayView::update() { } + + void GameplayView::render() { game_window->draw(background_sprite); } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index 4385177fe..9a2c46ce5 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -10,6 +10,7 @@ namespace Global using namespace Player; using namespace UI; using namespace Enemy; + using namespace Gameplay; ServiceLocator::ServiceLocator() @@ -20,6 +21,7 @@ namespace Global time_service = nullptr; ui_service = nullptr; enemy_service = nullptr; + gameplay_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -35,6 +37,7 @@ namespace Global time_service = new TimeService(); ui_service = new UIService(); enemy_service = new EnemyService(); + gameplay_service = new GameplayService(); } void ServiceLocator::clearAllServices() @@ -45,6 +48,7 @@ namespace Global delete(time_service); delete(ui_service); delete(enemy_service); + delete(gameplay_service); } ServiceLocator* ServiceLocator::getInstance() @@ -61,6 +65,7 @@ namespace Global time_service->initialize(); ui_service->initialize(); enemy_service->initialize(); + gameplay_service->initialize(); } void ServiceLocator::update() @@ -71,6 +76,7 @@ namespace Global if (GameService::getGameState() == GameState::GAMEPLAY) { + gameplay_service->update(); player_service->update(); enemy_service->update(); } @@ -84,6 +90,7 @@ namespace Global if (GameService::getGameState() == GameState::GAMEPLAY) { + gameplay_service->render(); player_service->render(); enemy_service->render(); } @@ -99,5 +106,6 @@ namespace Global TimeService* ServiceLocator::getTimeService() { return time_service; } UIService* ServiceLocator::getUIService() { return ui_service; } EnemyService* ServiceLocator::getEnemyService() { return enemy_service; } + Gameplay::GameplayService* ServiceLocator::getGameplayService() { return gameplay_service; } } \ No newline at end of file From f3b73832e38221016e20861e7db614e231927d05 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 6 Aug 2024 13:43:00 +0530 Subject: [PATCH 33/41] Enemy Config --- .../Enemy/Controllers/SubZeroController.h | 15 +++++++++++ .../Enemy/Controllers/ZapperController.h | 15 +++++++++++ Space-Invaders/Header/Enemy/EnemyConfig.h | 26 +++++++++++++++++++ Space-Invaders/Header/Enemy/EnemyController.h | 12 +++++---- Space-Invaders/Header/Enemy/EnemyModel.h | 24 ++++++++--------- .../Enemy/Controllers/SubZeroController.cpp | 1 + .../Enemy/Controllers/ZapperController.cpp | 1 + .../Source/Enemy/EnemyController.cpp | 21 +++++++++++++++ Space-Invaders/Source/Enemy/EnemyModel.cpp | 25 ++++++++++++++++-- 9 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 Space-Invaders/Header/Enemy/Controllers/SubZeroController.h create mode 100644 Space-Invaders/Header/Enemy/Controllers/ZapperController.h create mode 100644 Space-Invaders/Header/Enemy/EnemyConfig.h create mode 100644 Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp create mode 100644 Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp diff --git a/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h b/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h new file mode 100644 index 000000000..723909059 --- /dev/null +++ b/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h @@ -0,0 +1,15 @@ +#pragma once + +#include "../../header/Enemy/EnemyController.h" + +namespace Enemy +{ + namespace Controller + { + class SubzeroController : public EnemyController + { + private: + + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/Controllers/ZapperController.h b/Space-Invaders/Header/Enemy/Controllers/ZapperController.h new file mode 100644 index 000000000..2165ed21a --- /dev/null +++ b/Space-Invaders/Header/Enemy/Controllers/ZapperController.h @@ -0,0 +1,15 @@ +#pragma once + +#include "../../header/Enemy/EnemyController.h" + +namespace Enemy +{ + namespace Controller + { + class ZapperController : public EnemyController + { + private: + + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyConfig.h b/Space-Invaders/Header/Enemy/EnemyConfig.h new file mode 100644 index 000000000..0fff00fae --- /dev/null +++ b/Space-Invaders/Header/Enemy/EnemyConfig.h @@ -0,0 +1,26 @@ +#pragma once + +namespace Enemy +{ + enum class EnemyType + { + ZAPPER, + SUBZERO, + UFO, + THUNDER_SNAKE, + }; + + enum class EnemyState + { + PATROLLING, + ATTACK, + DEAD, + }; + + enum class MovementDirection + { + LEFT, + RIGHT, + DOWN, + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyController.h b/Space-Invaders/Header/Enemy/EnemyController.h index 2cd1d2e75..a153eff5e 100644 --- a/Space-Invaders/Header/Enemy/EnemyController.h +++ b/Space-Invaders/Header/Enemy/EnemyController.h @@ -20,20 +20,22 @@ namespace Enemy EnemyView* enemy_view; EnemyModel* enemy_model; - void move(); + virtual void move(); void moveLeft(); void moveRight(); void moveDown(); + sf::Vector2f getRandomInitialPosition(); + void handleOutOfBounds(); + public: - EnemyController(); - ~EnemyController(); + EnemyController(EnemyType type); + virtual ~EnemyController(); - void initialize(); + virtual void initialize(); void update(); void render(); sf::Vector2f getEnemyPosition(); - EnemyState getEnemyState(); }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyModel.h b/Space-Invaders/Header/Enemy/EnemyModel.h index 884a19d29..148bda095 100644 --- a/Space-Invaders/Header/Enemy/EnemyModel.h +++ b/Space-Invaders/Header/Enemy/EnemyModel.h @@ -3,22 +3,22 @@ namespace Enemy { - enum class MovementDirection - { - LEFT, - RIGHT, - DOWN, - }; + enum class EnemyType; + enum class MovementDirection; + enum class EnemyState; class EnemyModel { private: - sf::Vector2f reference_position = sf::Vector2f(100.f, 100.f); + sf::Vector2f reference_position = sf::Vector2f(50.f, 50.f); sf::Vector2f enemy_position; + MovementDirection movement_direction; + EnemyType enemy_type; + EnemyState enemy_state; public: - EnemyModel(); + EnemyModel(EnemyType type); ~EnemyModel(); //const settings for enemy @@ -33,13 +33,13 @@ namespace Enemy // Getters and Setters sf::Vector2f getEnemyPosition(); void setEnemyPosition(sf::Vector2f position); - sf::Vector2f getReferencePosition(); void setReferencePosition(sf::Vector2f position); - + EnemyState getEnemyState(); + void setEnemyState(EnemyState state); + EnemyType getEnemyType(); + void setEnemyType(EnemyType type); MovementDirection getMovementDirection(); void setMovementDirection(MovementDirection direction); - - }; } \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp b/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp new file mode 100644 index 000000000..1b16e8b66 --- /dev/null +++ b/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp @@ -0,0 +1 @@ +#include "../../Header/Enemy/Controllers/SubZeroController.h" diff --git a/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp b/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp new file mode 100644 index 000000000..4d7f68d8f --- /dev/null +++ b/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp @@ -0,0 +1 @@ +#include "../../Header/Enemy/Controllers/ZapperController.h" diff --git a/Space-Invaders/Source/Enemy/EnemyController.cpp b/Space-Invaders/Source/Enemy/EnemyController.cpp index 600361c3c..5ded15ba8 100644 --- a/Space-Invaders/Source/Enemy/EnemyController.cpp +++ b/Space-Invaders/Source/Enemy/EnemyController.cpp @@ -96,6 +96,27 @@ namespace Enemy else enemy_model->setEnemyPosition(currentPosition); } + sf::Vector2f EnemyController::getRandomInitialPosition() + { + float x_offset_distance = (std::rand() % static_cast(enemy_model->right_most_position.x - enemy_model->left_most_position.x)); + float x_position = enemy_model->left_most_position.x + x_offset_distance; + float y_position = enemy_model->left_most_position.y; + + return sf::Vector2f(x_position, y_position); + } + + void EnemyController::handleOutOfBounds() + { + sf::Vector2f enemyPosition = getEnemyPosition(); + sf::Vector2u windowSize = ServiceLocator::getInstance()->getGraphicService()->getGameWindow()->getSize(); + + if (enemyPosition.x < 0 || enemyPosition.x > windowSize.x || + enemyPosition.y < 0 || enemyPosition.y > windowSize.y) + { + ServiceLocator::getInstance()->getEnemyService()->destroyEnemy(this); + } + } + sf::Vector2f EnemyController::getEnemyPosition() { return enemy_model->getEnemyPosition(); diff --git a/Space-Invaders/Source/Enemy/EnemyModel.cpp b/Space-Invaders/Source/Enemy/EnemyModel.cpp index 1b5eb8b9f..052bffabc 100644 --- a/Space-Invaders/Source/Enemy/EnemyModel.cpp +++ b/Space-Invaders/Source/Enemy/EnemyModel.cpp @@ -1,14 +1,15 @@ #include "../../Header/Enemy/EnemyModel.h" +#include "../../Header/Enemy/EnemyConfig.h" namespace Enemy { - EnemyModel::EnemyModel() { } + EnemyModel::EnemyModel(EnemyType type) { enemy_type = type; } EnemyModel::~EnemyModel() { } void EnemyModel::initialize() { - // Starts from the right position + enemy_state = EnemyState::PATROLLING; movement_direction = MovementDirection::RIGHT; enemy_position = reference_position; } @@ -33,6 +34,26 @@ namespace Enemy reference_position = position; } + EnemyState EnemyModel::getEnemyState() + { + return enemy_state; + } + + void EnemyModel::setEnemyState(EnemyState state) + { + enemy_state = state; + } + + EnemyType EnemyModel::getEnemyType() + { + return enemy_type; + } + + void EnemyModel::setEnemyType(EnemyType type) + { + enemy_type = type; + } + MovementDirection EnemyModel::getMovementDirection() { return movement_direction; From 39fdfbec11e51dc3952c20279d44636ad2cff30e Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 6 Aug 2024 15:18:27 +0530 Subject: [PATCH 34/41] Added Zapper and Sub Zero --- .../Enemy/Controllers/SubZeroController.h | 12 ++ .../Enemy/Controllers/ZapperController.h | 14 +++ Space-Invaders/Header/Enemy/EnemyConfig.h | 4 +- Space-Invaders/Header/Enemy/EnemyController.h | 16 +-- Space-Invaders/Header/Enemy/EnemyModel.h | 4 +- Space-Invaders/Header/Enemy/EnemyService.h | 10 +- Space-Invaders/Header/Enemy/EnemyView.h | 6 +- .../Enemy/Controllers/SubZeroController.cpp | 39 ++++++ .../Enemy/Controllers/ZapperController.cpp | 116 ++++++++++++++++++ .../Source/Enemy/EnemyController.cpp | 58 +++------ Space-Invaders/Source/Enemy/EnemyService.cpp | 46 ++++++- Space-Invaders/Source/Enemy/EnemyView.cpp | 23 +++- 12 files changed, 278 insertions(+), 70 deletions(-) diff --git a/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h b/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h index 723909059..51600cb33 100644 --- a/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h +++ b/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h @@ -10,6 +10,18 @@ namespace Enemy { private: + float vertical_movement_speed = 100.f; + + void move() override; + void moveDown(); + + public: + + SubzeroController(EnemyType type); + ~SubzeroController(); + + void initialize() override; + }; } } \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/Controllers/ZapperController.h b/Space-Invaders/Header/Enemy/Controllers/ZapperController.h index 2165ed21a..cade22a40 100644 --- a/Space-Invaders/Header/Enemy/Controllers/ZapperController.h +++ b/Space-Invaders/Header/Enemy/Controllers/ZapperController.h @@ -10,6 +10,20 @@ namespace Enemy { private: + float vertical_travel_distance = 100.f; + + void move() override; + void moveLeft(); + void moveRight(); + void moveDown(); + + public: + + ZapperController(EnemyType type); + ~ZapperController(); + + void initialize() override; + }; } } \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyConfig.h b/Space-Invaders/Header/Enemy/EnemyConfig.h index 0fff00fae..0c54f1ba5 100644 --- a/Space-Invaders/Header/Enemy/EnemyConfig.h +++ b/Space-Invaders/Header/Enemy/EnemyConfig.h @@ -6,8 +6,8 @@ namespace Enemy { ZAPPER, SUBZERO, - UFO, - THUNDER_SNAKE, + //UFO, + //THUNDER_SNAKE, }; enum class EnemyState diff --git a/Space-Invaders/Header/Enemy/EnemyController.h b/Space-Invaders/Header/Enemy/EnemyController.h index a153eff5e..30ce48675 100644 --- a/Space-Invaders/Header/Enemy/EnemyController.h +++ b/Space-Invaders/Header/Enemy/EnemyController.h @@ -6,24 +6,18 @@ namespace Enemy class EnemyView; class EnemyModel; + enum class EnemyType; enum class EnemyState; class EnemyController { - private: - 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 vertical_travel_distance = 100.f; - const float enemy_movement_speed = 250.0f; + protected: + EnemyView* enemy_view; EnemyModel* enemy_model; - virtual void move(); - void moveLeft(); - void moveRight(); - void moveDown(); + virtual void move() = 0; sf::Vector2f getRandomInitialPosition(); void handleOutOfBounds(); @@ -37,5 +31,7 @@ namespace Enemy void render(); sf::Vector2f getEnemyPosition(); + EnemyState getEnemyState(); + EnemyType getEnemyType(); }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyModel.h b/Space-Invaders/Header/Enemy/EnemyModel.h index 148bda095..f7a496405 100644 --- a/Space-Invaders/Header/Enemy/EnemyModel.h +++ b/Space-Invaders/Header/Enemy/EnemyModel.h @@ -22,8 +22,8 @@ namespace Enemy ~EnemyModel(); //const settings for enemy - 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 sf::Vector2f left_most_position = sf::Vector2f(50.f, 50.f); + const sf::Vector2f right_most_position = sf::Vector2f(1800.f, 50.f); const float vertical_travel_distance = 100.f; const float enemy_movement_speed = 250.0f; diff --git a/Space-Invaders/Header/Enemy/EnemyService.h b/Space-Invaders/Header/Enemy/EnemyService.h index 17de6dfe3..8f9dd776f 100644 --- a/Space-Invaders/Header/Enemy/EnemyService.h +++ b/Space-Invaders/Header/Enemy/EnemyService.h @@ -4,28 +4,32 @@ namespace Enemy { class EnemyController; + enum class EnemyType; class EnemyService { private: - const float spawn_interval = 3.f; + const float spawn_interval = 2.f; std::vector enemy_list; float spawn_timer; void updateSpawnTimer(); void processEnemySpawn(); + EnemyType getRandomEnemyType(); + EnemyController* createEnemy(EnemyType enemy_type); void destroy(); public: EnemyService(); - virtual ~EnemyService(); + ~EnemyService(); void initialize(); void update(); void render(); - void spawnEnemy(); // Function to spawn enemy + EnemyController* spawnEnemy(); // Function to spawn enemy + void destroyEnemy(EnemyController* enemy_controller); }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyView.h b/Space-Invaders/Header/Enemy/EnemyView.h index a2e91275a..243b63a44 100644 --- a/Space-Invaders/Header/Enemy/EnemyView.h +++ b/Space-Invaders/Header/Enemy/EnemyView.h @@ -1,6 +1,7 @@ #pragma once #include +#include "../../Header/Enemy/EnemyConfig.h" namespace Enemy { @@ -9,7 +10,8 @@ namespace Enemy class EnemyView { private: - const sf::String enemy_texture_path = "assets/textures/zapper.png"; // cureent ship - zapper + const sf::String subzero_texture_path = "assets/textures/subzero.png"; + const sf::String zapper_texture_path = "assets/textures/zapper.png"; const float enemy_sprite_width = 60.f; const float enemy_sprite_height = 60.f; @@ -20,7 +22,7 @@ namespace Enemy sf::Texture enemy_texture; sf::Sprite enemy_sprite; - void initializeEnemySprite(); + void initializeEnemySprite(EnemyType type); void scaleEnemySprite(); public: diff --git a/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp b/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp index 1b16e8b66..3b212f84c 100644 --- a/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp +++ b/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp @@ -1 +1,40 @@ #include "../../Header/Enemy/Controllers/SubZeroController.h" +#include "../../Header/Enemy/EnemyModel.h" +#include "../../header/Enemy/EnemyConfig.h" +#include "../../Header/Global/ServiceLocator.h" + +namespace Enemy +{ + using namespace Global; + + namespace Controller + { + SubzeroController::SubzeroController(EnemyType type):EnemyController(type) { } + + SubzeroController::~SubzeroController() { } + + void SubzeroController::initialize() + { + EnemyController::initialize(); + enemy_model->setMovementDirection(MovementDirection::DOWN); + } + + void SubzeroController::move() + { + switch (enemy_model->getMovementDirection()) + { + case::Enemy::MovementDirection::DOWN: + moveDown(); + break; + } + } + + void SubzeroController::moveDown() + { + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); + currentPosition.y += vertical_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + enemy_model->setEnemyPosition(currentPosition); + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp b/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp index 4d7f68d8f..ed5dccdf7 100644 --- a/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp +++ b/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp @@ -1 +1,117 @@ #include "../../Header/Enemy/Controllers/ZapperController.h" +#include "../../Header/Enemy/EnemyModel.h" +#include "../../Header/Enemy/EnemyConfig.h" +#include "../../Header/Global/ServiceLocator.h" + +namespace Enemy +{ + using namespace Global; + + namespace Controller + { + ZapperController::ZapperController(EnemyType type) : EnemyController(type){ } + + ZapperController::~ZapperController() { } + + void ZapperController::initialize() + { + EnemyController::initialize();; + } + + void ZapperController::move() + { + // Switch statement based on the movement direction of the enemy + switch (enemy_model->getMovementDirection()) + { + // If the movement direction is LEFT + case::Enemy::MovementDirection::LEFT: + moveLeft(); + break; + + // If the movement direction is RIGHT + case::Enemy::MovementDirection::RIGHT: + moveRight(); + break; + + // If the movement direction is DOWN + case::Enemy::MovementDirection::DOWN: + moveDown(); + break; + } + } + + void ZapperController::moveLeft() + { + // Get the current position of the enemy + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); + + // Update the position to move left + currentPosition.x -= enemy_model->enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + // Check if the enemy reached the leftmost position + if (currentPosition.x <= enemy_model->left_most_position.x) + { + // Set movement direction to DOWN and update reference position + enemy_model->setMovementDirection(MovementDirection::DOWN); + enemy_model->setReferencePosition(currentPosition); + } + else + { + // Update the enemy position + enemy_model->setEnemyPosition(currentPosition); + } + } + + void ZapperController::moveRight() + { + // Get the current position of the enemy + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); + + // Update the position to move right + currentPosition.x += enemy_model->enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + // Check if the enemy reached the rightmost position + if (currentPosition.x >= enemy_model->right_most_position.x) + { + // Set movement direction to DOWN and update reference position + enemy_model->setMovementDirection(MovementDirection::DOWN); + enemy_model->setReferencePosition(currentPosition); + } + else + { + // Update the enemy position + enemy_model->setEnemyPosition(currentPosition); + } + } + + void ZapperController::moveDown() + { + // Get the current position of the enemy + sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); + + // Update the position to move down + currentPosition.y += enemy_model->enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + // Check if the enemy reached the reference position plus vertical travel distance + if (currentPosition.y >= enemy_model->getReferencePosition().y + vertical_travel_distance) + { + // Check if the enemy reference position is on the left side + if (enemy_model->getReferencePosition().x <= enemy_model->left_most_position.x) + { + // Set movement direction to RIGHT + enemy_model->setMovementDirection(MovementDirection::RIGHT); + } + else + { + // Set movement direction to LEFT + enemy_model->setMovementDirection(MovementDirection::LEFT); + } + } + else + { + // Update the enemy position + enemy_model->setEnemyPosition(currentPosition); + } + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyController.cpp b/Space-Invaders/Source/Enemy/EnemyController.cpp index 5ded15ba8..af2cbcd3f 100644 --- a/Space-Invaders/Source/Enemy/EnemyController.cpp +++ b/Space-Invaders/Source/Enemy/EnemyController.cpp @@ -10,10 +10,10 @@ namespace Enemy using namespace Event; using namespace Time; - EnemyController::EnemyController() + EnemyController::EnemyController(EnemyType type) { enemy_view = new EnemyView(); - enemy_model = new EnemyModel(); + enemy_model = new EnemyModel(type); } EnemyController::~EnemyController() @@ -25,6 +25,7 @@ namespace Enemy void EnemyController::initialize() { enemy_model->initialize(); + enemy_model->setEnemyPosition(getRandomInitialPosition()); enemy_view->initialize(this); } @@ -39,7 +40,7 @@ namespace Enemy enemy_view->render(); } - void EnemyController::move() + /*void EnemyController::move() { switch (enemy_model->getMovementDirection()) { @@ -55,46 +56,7 @@ namespace Enemy moveDown(); break; } - } - - void EnemyController::moveLeft() - { - sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); - currentPosition.x -= enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - - if (currentPosition.x <= left_most_position.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_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - - if (currentPosition.x >= right_most_position.x) - { - enemy_model->setMovementDirection(MovementDirection::DOWN); - enemy_model->setReferencePosition(currentPosition); - } - else enemy_model->setEnemyPosition(currentPosition); - } - - void EnemyController::moveDown() - { - sf::Vector2f currentPosition = enemy_model->getEnemyPosition(); - currentPosition.y += enemy_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - - if (currentPosition.y >= enemy_model->getReferencePosition().y + vertical_travel_distance) - { - if (enemy_model->getReferencePosition().x <= left_most_position.x) enemy_model->setMovementDirection(MovementDirection::RIGHT); - else enemy_model->setMovementDirection(MovementDirection::LEFT); - } - else enemy_model->setEnemyPosition(currentPosition); - } + }*/ sf::Vector2f EnemyController::getRandomInitialPosition() { @@ -121,4 +83,14 @@ namespace Enemy { return enemy_model->getEnemyPosition(); } + + EnemyState EnemyController::getEnemyState() + { + return enemy_model->getEnemyState(); + } + + EnemyType EnemyController::getEnemyType() + { + return enemy_model->getEnemyType(); + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyService.cpp b/Space-Invaders/Source/Enemy/EnemyService.cpp index ebe71d130..4dfff0ffe 100644 --- a/Space-Invaders/Source/Enemy/EnemyService.cpp +++ b/Space-Invaders/Source/Enemy/EnemyService.cpp @@ -2,13 +2,17 @@ #include "../../Header/Enemy/EnemyController.h" #include "../../Header/Global/ServiceLocator.h" #include "../../Header/Time/TimeService.h" +#include "../../Header/Enemy/EnemyConfig.h" +#include "../../Header/Enemy/Controllers/SubZeroController.h" +#include "../../Header/Enemy/Controllers/ZapperController.h" namespace Enemy { using namespace Global; using namespace Time; + using namespace Controller; - EnemyService::EnemyService() { } + EnemyService::EnemyService() { std::srand(static_cast(std::time(nullptr))); } EnemyService::~EnemyService() { destroy(); } @@ -30,11 +34,23 @@ namespace Enemy for (int i = 0; i < enemy_list.size(); i++) enemy_list[i]->render(); } - void EnemyService::spawnEnemy() + EnemyController* EnemyService::spawnEnemy() { - EnemyController* enemy_controller = new EnemyController(); + // The base class pointer will be pointing to a child class object + EnemyController* enemy_controller = createEnemy(getRandomEnemyType()); + enemy_controller->initialize(); enemy_list.push_back(enemy_controller); + + return enemy_controller; + } + + void EnemyService::destroyEnemy(EnemyController* enemy_controller) + { + enemy_list.erase(std::remove(enemy_list.begin(), enemy_list.end(), enemy_controller), enemy_list.end()); + + // Delete the enemy_controller object from memory to free up resources. + delete(enemy_controller); } void EnemyService::updateSpawnTimer() @@ -51,6 +67,30 @@ namespace Enemy } } + EnemyType EnemyService::getRandomEnemyType() + { + int randomType = std::rand() % 2; //since we only have 2 enemies right now + return static_cast(randomType); + } + + EnemyController* EnemyService::createEnemy(EnemyType enemy_type) + { + switch (enemy_type) + { + case::Enemy::EnemyType::ZAPPER: + return new ZapperController(Enemy::EnemyType::ZAPPER); + + /*case::Enemy::EnemyType::THUNDER_SNAKE: + return new ThunderSnakeController(Enemy::EnemyType::THUNDER_SNAKE);*/ + + case::Enemy::EnemyType::SUBZERO: + return new SubzeroController(Enemy::EnemyType::SUBZERO); + + /*case::Enemy::EnemyType::UFO: + return new UFOController(Enemy::EnemyType::UFO);*/ + } + } + void EnemyService::destroy() { for (int i = 0; i < enemy_list.size(); i++) delete (enemy_list[i]); //delete all enemies diff --git a/Space-Invaders/Source/Enemy/EnemyView.cpp b/Space-Invaders/Source/Enemy/EnemyView.cpp index 02c0dddd4..e193c16fc 100644 --- a/Space-Invaders/Source/Enemy/EnemyView.cpp +++ b/Space-Invaders/Source/Enemy/EnemyView.cpp @@ -2,6 +2,7 @@ #include "../../Header/Global/ServiceLocator.h" #include "../../Header/Graphic/GraphicService.h" #include "../../Header/Enemy/EnemyController.h" +#include"../../Header/Enemy/EnemyConfig.h" namespace Enemy { @@ -16,15 +17,27 @@ namespace Enemy { enemy_controller = controller; game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializeEnemySprite(); + initializeEnemySprite(enemy_controller->getEnemyType()); } - void EnemyView::initializeEnemySprite() + void EnemyView::initializeEnemySprite(EnemyType type) { - if (enemy_texture.loadFromFile(enemy_texture_path)) //check if the texture loaded + switch (type) { - enemy_sprite.setTexture(enemy_texture); //set the sprite - scaleEnemySprite(); // call the method to scale the sprite + case::Enemy::EnemyType::SUBZERO: + if (enemy_texture.loadFromFile(subzero_texture_path)) + { + enemy_sprite.setTexture(enemy_texture); + scaleEnemySprite(); + } + break; + case::Enemy::EnemyType::ZAPPER: + if (enemy_texture.loadFromFile(zapper_texture_path)) + { + enemy_sprite.setTexture(enemy_texture); + scaleEnemySprite(); + } + break; } } From e448b2c74f859cddede4d368128b0848a5727ffa Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 6 Aug 2024 15:49:40 +0530 Subject: [PATCH 35/41] Added Element folder MVC --- Space-Invaders/Header/Element/Bunker/BunkerController.h | 1 + Space-Invaders/Header/Element/Bunker/BunkerModel.h | 1 + Space-Invaders/Header/Element/Bunker/BunkerView.h | 1 + Space-Invaders/Header/Element/ElementService.h | 8 ++++++++ Space-Invaders/Header/Global/ServiceLocator.h | 3 +++ Space-Invaders/Source/Element/Bunker/BunkerController.cpp | 0 Space-Invaders/Source/Element/Bunker/BunkerModel.cpp | 0 Space-Invaders/Source/Element/Bunker/BunkerView.cpp | 0 Space-Invaders/Source/Element/ElementService.cpp | 4 ++++ Space-Invaders/Source/Global/ServiceLocator.cpp | 5 +++++ 10 files changed, 23 insertions(+) create mode 100644 Space-Invaders/Header/Element/Bunker/BunkerController.h create mode 100644 Space-Invaders/Header/Element/Bunker/BunkerModel.h create mode 100644 Space-Invaders/Header/Element/Bunker/BunkerView.h create mode 100644 Space-Invaders/Header/Element/ElementService.h create mode 100644 Space-Invaders/Source/Element/Bunker/BunkerController.cpp create mode 100644 Space-Invaders/Source/Element/Bunker/BunkerModel.cpp create mode 100644 Space-Invaders/Source/Element/Bunker/BunkerView.cpp create mode 100644 Space-Invaders/Source/Element/ElementService.cpp diff --git a/Space-Invaders/Header/Element/Bunker/BunkerController.h b/Space-Invaders/Header/Element/Bunker/BunkerController.h new file mode 100644 index 000000000..50e96676b --- /dev/null +++ b/Space-Invaders/Header/Element/Bunker/BunkerController.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Header/Element/Bunker/BunkerModel.h b/Space-Invaders/Header/Element/Bunker/BunkerModel.h new file mode 100644 index 000000000..50e96676b --- /dev/null +++ b/Space-Invaders/Header/Element/Bunker/BunkerModel.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Header/Element/Bunker/BunkerView.h b/Space-Invaders/Header/Element/Bunker/BunkerView.h new file mode 100644 index 000000000..50e96676b --- /dev/null +++ b/Space-Invaders/Header/Element/Bunker/BunkerView.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Header/Element/ElementService.h b/Space-Invaders/Header/Element/ElementService.h new file mode 100644 index 000000000..8729bb7af --- /dev/null +++ b/Space-Invaders/Header/Element/ElementService.h @@ -0,0 +1,8 @@ +#pragma once +namespace Element +{ + class ElementService + { + + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index 865b12193..cad6fff7a 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -6,6 +6,7 @@ #include "../../Header/UI/UIService.h" #include "../../Header/Enemy/EnemyService.h" #include "../../Header/Gameplay/GameplayService.h" +#include "../../Header/Element/ElementService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -24,6 +25,7 @@ namespace Global UI::UIService* ui_service; Enemy::EnemyService* enemy_service; Gameplay::GameplayService* gameplay_service; + Element::ElementService* element_service; // Public Methods ServiceLocator(); @@ -49,5 +51,6 @@ namespace Global UI::UIService* getUIService(); // Retrive the UIService instance Enemy::EnemyService* getEnemyService(); // Retrive the EnemyService instance Gameplay::GameplayService* getGameplayService(); // Retrive the GameplayService instance + Element::ElementService* getElementService(); // Retrive the ElementService instance }; } \ No newline at end of file diff --git a/Space-Invaders/Source/Element/Bunker/BunkerController.cpp b/Space-Invaders/Source/Element/Bunker/BunkerController.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Source/Element/Bunker/BunkerModel.cpp b/Space-Invaders/Source/Element/Bunker/BunkerModel.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Source/Element/Bunker/BunkerView.cpp b/Space-Invaders/Source/Element/Bunker/BunkerView.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Source/Element/ElementService.cpp b/Space-Invaders/Source/Element/ElementService.cpp new file mode 100644 index 000000000..f8265ca42 --- /dev/null +++ b/Space-Invaders/Source/Element/ElementService.cpp @@ -0,0 +1,4 @@ +namespace Element +{ + +} \ No newline at end of file diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index 9a2c46ce5..cb6237fd9 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -11,6 +11,7 @@ namespace Global using namespace UI; using namespace Enemy; using namespace Gameplay; + using namespace Element; ServiceLocator::ServiceLocator() @@ -22,6 +23,7 @@ namespace Global ui_service = nullptr; enemy_service = nullptr; gameplay_service = nullptr; + element_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -38,6 +40,7 @@ namespace Global ui_service = new UIService(); enemy_service = new EnemyService(); gameplay_service = new GameplayService(); + element_service = new ElementService(); } void ServiceLocator::clearAllServices() @@ -49,6 +52,7 @@ namespace Global delete(ui_service); delete(enemy_service); delete(gameplay_service); + delete(element_service); } ServiceLocator* ServiceLocator::getInstance() @@ -107,5 +111,6 @@ namespace Global UIService* ServiceLocator::getUIService() { return ui_service; } EnemyService* ServiceLocator::getEnemyService() { return enemy_service; } Gameplay::GameplayService* ServiceLocator::getGameplayService() { return gameplay_service; } + Element::ElementService* ServiceLocator::getElementService() { return element_service; } } \ No newline at end of file From 446a3a066c13abfe1aedef29e4ad57910f37233e Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 7 Aug 2024 12:30:35 +0530 Subject: [PATCH 36/41] MVC and Bunker --- .../Header/Element/Bunker/BunkerController.h | 27 ++++++++++ .../Header/Element/Bunker/BunkerModel.h | 14 +++++ .../Header/Element/Bunker/BunkerView.h | 35 +++++++++++++ .../Element/Bunker/BunkerController.cpp | 24 +++++++++ .../Source/Element/Bunker/BunkerModel.cpp | 14 +++++ .../Source/Element/Bunker/BunkerView.cpp | 52 +++++++++++++++++++ 6 files changed, 166 insertions(+) diff --git a/Space-Invaders/Header/Element/Bunker/BunkerController.h b/Space-Invaders/Header/Element/Bunker/BunkerController.h index 50e96676b..3370da4b2 100644 --- a/Space-Invaders/Header/Element/Bunker/BunkerController.h +++ b/Space-Invaders/Header/Element/Bunker/BunkerController.h @@ -1 +1,28 @@ #pragma once +#include +#include "../../header/Element/Bunker/BunkerModel.h" + +namespace Element +{ + namespace Bunker + { + class BunkerView; + + class BunkerController + { + private: + BunkerView* bunker_view; + BunkerData bunker_data; + + public: + BunkerController(); + ~BunkerController(); + + void initialize(BunkerData data); + void update(); + void render(); + + sf::Vector2f getBunkerPosition(); + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Header/Element/Bunker/BunkerModel.h b/Space-Invaders/Header/Element/Bunker/BunkerModel.h index 50e96676b..7ae912814 100644 --- a/Space-Invaders/Header/Element/Bunker/BunkerModel.h +++ b/Space-Invaders/Header/Element/Bunker/BunkerModel.h @@ -1 +1,15 @@ #pragma once +#include + +namespace Element +{ + namespace Bunker + { + struct BunkerData + { + sf::Vector2f position; + BunkerData(); + BunkerData(sf::Vector2f position); + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Header/Element/Bunker/BunkerView.h b/Space-Invaders/Header/Element/Bunker/BunkerView.h index 50e96676b..cbb63c840 100644 --- a/Space-Invaders/Header/Element/Bunker/BunkerView.h +++ b/Space-Invaders/Header/Element/Bunker/BunkerView.h @@ -1 +1,36 @@ #pragma once +#include + +namespace Element +{ + namespace Bunker + { + class BunkerController; + + class BunkerView + { + private: + const float bunker_sprite_width = 80.f; + const float bunker_sprite_height = 80.f; + + BunkerController* bunker_controller; + sf::RenderWindow* game_window; + + sf::Texture bunker_texture; + sf::Sprite bunker_sprite; + + const sf::String bunker_texture_path = "assets/textures/bunker.png"; + + void scaleSprite(); + void initializeImage(); + + public: + BunkerView(); + ~BunkerView(); + + void initialize(BunkerController* controller); + void update(); + void render(); + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Element/Bunker/BunkerController.cpp b/Space-Invaders/Source/Element/Bunker/BunkerController.cpp index e69de29bb..8e3d95064 100644 --- a/Space-Invaders/Source/Element/Bunker/BunkerController.cpp +++ b/Space-Invaders/Source/Element/Bunker/BunkerController.cpp @@ -0,0 +1,24 @@ +#include "../../header/Element/Bunker/BunkerController.h" +#include "../../header/Element/Bunker/BunkerView.h" + +namespace Element +{ + namespace Bunker + { + BunkerController::BunkerController() { bunker_view = new BunkerView(); } + + BunkerController::~BunkerController() { delete(bunker_view); } + + void BunkerController::initialize(BunkerData data) + { + bunker_data = data; + bunker_view->initialize(this); + } + + void BunkerController::update() { bunker_view->update(); } + + void BunkerController::render() { bunker_view->render(); } + + sf::Vector2f BunkerController::getBunkerPosition() { return bunker_data.position; } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Element/Bunker/BunkerModel.cpp b/Space-Invaders/Source/Element/Bunker/BunkerModel.cpp index e69de29bb..04d29b58d 100644 --- a/Space-Invaders/Source/Element/Bunker/BunkerModel.cpp +++ b/Space-Invaders/Source/Element/Bunker/BunkerModel.cpp @@ -0,0 +1,14 @@ +#include "../../header/Element/Bunker/BunkerModel.h" + +namespace Element +{ + namespace Bunker + { + BunkerData::BunkerData() { }; + + BunkerData::BunkerData(sf::Vector2f position) + { + this->position = position; + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Element/Bunker/BunkerView.cpp b/Space-Invaders/Source/Element/Bunker/BunkerView.cpp index e69de29bb..99a3c5a65 100644 --- a/Space-Invaders/Source/Element/Bunker/BunkerView.cpp +++ b/Space-Invaders/Source/Element/Bunker/BunkerView.cpp @@ -0,0 +1,52 @@ +#include "../../header/Element/Bunker/BunkerView.h" +#include "../../header/Global/ServiceLocator.h" +#include "../../header/Element/Bunker/BunkerController.h" + +namespace Element +{ + namespace Bunker + { + using namespace Global; + + BunkerView::BunkerView() { } + + BunkerView::~BunkerView() { } + + void BunkerView::initialize(BunkerController* controller) + { + bunker_controller = controller; + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializeImage(); + } + + void BunkerView::initializeImage() + { + if (bunker_texture.loadFromFile(bunker_texture_path)) + { + bunker_sprite.setTexture(bunker_texture); + scaleSprite(); + } + } + + + void BunkerView::scaleSprite() + { + bunker_sprite.setScale( + static_cast(bunker_sprite_width) / bunker_sprite.getTexture()->getSize().x, + static_cast(bunker_sprite_height) / bunker_sprite.getTexture()->getSize().y + ); + } + + + void BunkerView::update() + { + bunker_sprite.setPosition(bunker_controller->getBunkerPosition()); + } + + void BunkerView::render() + { + game_window->draw(bunker_sprite); + } + + } +} \ No newline at end of file From 9d4ba43fc326b82b2a239aa8e0eea339f94ffd6c Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 7 Aug 2024 13:13:21 +0530 Subject: [PATCH 37/41] Added Config --- .../Header/Element/Bunker/BunkerView.h | 2 +- .../Header/Element/ElementService.h | 26 +++++++++ Space-Invaders/Header/Enemy/EnemyView.h | 4 +- Space-Invaders/Header/Gameplay/GameplayView.h | 2 +- Space-Invaders/Header/Global/Config.h | 39 +++++++++++++ Space-Invaders/Header/Player/PlayerView.h | 2 +- .../Header/UI/MainMenu/MainMenuUIController.h | 8 +-- .../Source/Element/Bunker/BunkerView.cpp | 3 +- .../Source/Element/ElementService.cpp | 30 ++++++++++ Space-Invaders/Source/Enemy/EnemyView.cpp | 5 +- .../Source/Gameplay/GameplayView.cpp | 3 +- Space-Invaders/Source/Global/Config.cpp | 56 +++++++++++++++++++ .../Source/Global/ServiceLocator.cpp | 8 ++- Space-Invaders/Source/Player/PlayerView.cpp | 3 +- .../UI/MainMenu/MainMenuUIController.cpp | 9 +-- 15 files changed, 179 insertions(+), 21 deletions(-) create mode 100644 Space-Invaders/Header/Global/Config.h create mode 100644 Space-Invaders/Source/Global/Config.cpp diff --git a/Space-Invaders/Header/Element/Bunker/BunkerView.h b/Space-Invaders/Header/Element/Bunker/BunkerView.h index cbb63c840..acc163094 100644 --- a/Space-Invaders/Header/Element/Bunker/BunkerView.h +++ b/Space-Invaders/Header/Element/Bunker/BunkerView.h @@ -19,7 +19,7 @@ namespace Element sf::Texture bunker_texture; sf::Sprite bunker_sprite; - const sf::String bunker_texture_path = "assets/textures/bunker.png"; + //const sf::String bunker_texture_path = "assets/textures/bunker.png"; void scaleSprite(); void initializeImage(); diff --git a/Space-Invaders/Header/Element/ElementService.h b/Space-Invaders/Header/Element/ElementService.h index 8729bb7af..e9c4c8719 100644 --- a/Space-Invaders/Header/Element/ElementService.h +++ b/Space-Invaders/Header/Element/ElementService.h @@ -1,8 +1,34 @@ #pragma once +#include +#include +#include "../../header/Element/Bunker/BunkerController.h" +#include "../../header/Element/Bunker/BunkerModel.h" + namespace Element { + class BunkerController; + class ElementService { + private: + //const vector so that the default values will not be changed down the road by mistake. + const std::vector bunker_data_list = { Bunker::BunkerData(sf::Vector2f(130.f, 800.f)), + Bunker::BunkerData(sf::Vector2f(430.0f, 800.f)), + Bunker::BunkerData(sf::Vector2f(730.0f, 800.f)), + Bunker::BunkerData(sf::Vector2f(1130.0f, 800.f)), + Bunker::BunkerData(sf::Vector2f(1430.0f, 800.f)), + Bunker::BunkerData(sf::Vector2f(1730.0f, 800.f)) }; + + std::vector bunker_list; + + void destroy(); + + public: + ElementService(); + virtual ~ElementService(); + void initialize(); + void update(); + void render(); }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/EnemyView.h b/Space-Invaders/Header/Enemy/EnemyView.h index 243b63a44..b89b046a7 100644 --- a/Space-Invaders/Header/Enemy/EnemyView.h +++ b/Space-Invaders/Header/Enemy/EnemyView.h @@ -10,8 +10,8 @@ namespace Enemy class EnemyView { private: - const sf::String subzero_texture_path = "assets/textures/subzero.png"; - const sf::String zapper_texture_path = "assets/textures/zapper.png"; + //const sf::String subzero_texture_path = "assets/textures/subzero.png"; + //const sf::String zapper_texture_path = "assets/textures/zapper.png"; const float enemy_sprite_width = 60.f; const float enemy_sprite_height = 60.f; diff --git a/Space-Invaders/Header/Gameplay/GameplayView.h b/Space-Invaders/Header/Gameplay/GameplayView.h index 96ecc2163..e6675b5db 100644 --- a/Space-Invaders/Header/Gameplay/GameplayView.h +++ b/Space-Invaders/Header/Gameplay/GameplayView.h @@ -6,7 +6,7 @@ namespace Gameplay class GameplayView { private: - const sf::String background_texture_path = "assets/textures/space_invaders_bg.png"; + //const sf::String background_texture_path = "assets/textures/space_invaders_bg.png"; sf::RenderWindow* game_window; sf::Texture background_texture; diff --git a/Space-Invaders/Header/Global/Config.h b/Space-Invaders/Header/Global/Config.h new file mode 100644 index 000000000..385d119df --- /dev/null +++ b/Space-Invaders/Header/Global/Config.h @@ -0,0 +1,39 @@ +#pragma once +#include + +namespace Global +{ + class Config + { + public: + static const sf::String outscal_logo_texture_path; + static const sf::String background_texture_path; + static const sf::String player_texture_path; + + static const sf::String zapper_texture_path; + static const sf::String thunder_snake_texture_path; + static const sf::String subzero_texture_path; + static const sf::String ufo_texture_path; + static const sf::String bunker_texture_path; + + static const sf::String shield_texture_path; + static const sf::String tripple_laser_texture_path; + static const sf::String rapid_fire_texture_path; + static const sf::String outscal_bomb_texture_path; + + static const sf::String laser_bullet_texture_path; + static const sf::String torpedoe_texture_path; + static const sf::String frost_beam_texture_path; + + static const sf::String play_button_texture_path; + static const sf::String instructions_button_texture_path; + static const sf::String quit_button_texture_path; + static const sf::String menu_button_texture_path; + + static const sf::String bubble_bobble_font_path; + static const sf::String DS_DIGIB_font_path; + + static const sf::String background_music_path; + static const sf::String button_click_sound_path; + }; +} diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h index 8e901f3f4..00fd6efa6 100644 --- a/Space-Invaders/Header/Player/PlayerView.h +++ b/Space-Invaders/Header/Player/PlayerView.h @@ -8,7 +8,7 @@ namespace Player { private: - const sf::String player_texture_path = "assets/textures/player_ship.png"; + //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; diff --git a/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h index 0b811811c..daa45b4bf 100644 --- a/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h @@ -9,10 +9,10 @@ namespace UI { 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"; + //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"; // Constants: const float button_width = 400.f; diff --git a/Space-Invaders/Source/Element/Bunker/BunkerView.cpp b/Space-Invaders/Source/Element/Bunker/BunkerView.cpp index 99a3c5a65..5cd03623a 100644 --- a/Space-Invaders/Source/Element/Bunker/BunkerView.cpp +++ b/Space-Invaders/Source/Element/Bunker/BunkerView.cpp @@ -1,6 +1,7 @@ #include "../../header/Element/Bunker/BunkerView.h" #include "../../header/Global/ServiceLocator.h" #include "../../header/Element/Bunker/BunkerController.h" +#include "../../Header/Global/Config.h" namespace Element { @@ -21,7 +22,7 @@ namespace Element void BunkerView::initializeImage() { - if (bunker_texture.loadFromFile(bunker_texture_path)) + if (bunker_texture.loadFromFile(Config::bunker_texture_path)) { bunker_sprite.setTexture(bunker_texture); scaleSprite(); diff --git a/Space-Invaders/Source/Element/ElementService.cpp b/Space-Invaders/Source/Element/ElementService.cpp index f8265ca42..1aeff50b4 100644 --- a/Space-Invaders/Source/Element/ElementService.cpp +++ b/Space-Invaders/Source/Element/ElementService.cpp @@ -1,4 +1,34 @@ +#include "../../header/Element/ElementService.h" + namespace Element { + ElementService::ElementService() { } + + ElementService::~ElementService() { destroy(); } + + void ElementService::initialize() + { + for (int i = 0; i < bunker_data_list.size(); i++) + { + Bunker::BunkerController* bunker_controller = new Bunker::BunkerController(); + + bunker_controller->initialize(bunker_data_list[i]); + bunker_list.push_back(bunker_controller); + } + } + + void ElementService::update() + { + for (int i = 0; i < bunker_list.size(); i++) bunker_list[i]->update(); + } + + void ElementService::render() + { + for (int i = 0; i < bunker_list.size(); i++) bunker_list[i]->render(); + } + void ElementService::destroy() + { + for (int i = 0; i < bunker_list.size(); i++) delete(bunker_list[i]); + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyView.cpp b/Space-Invaders/Source/Enemy/EnemyView.cpp index e193c16fc..70e3cd142 100644 --- a/Space-Invaders/Source/Enemy/EnemyView.cpp +++ b/Space-Invaders/Source/Enemy/EnemyView.cpp @@ -3,6 +3,7 @@ #include "../../Header/Graphic/GraphicService.h" #include "../../Header/Enemy/EnemyController.h" #include"../../Header/Enemy/EnemyConfig.h" +#include "../../Header/Global/Config.h" namespace Enemy { @@ -25,14 +26,14 @@ namespace Enemy switch (type) { case::Enemy::EnemyType::SUBZERO: - if (enemy_texture.loadFromFile(subzero_texture_path)) + if (enemy_texture.loadFromFile(Config::subzero_texture_path)) { enemy_sprite.setTexture(enemy_texture); scaleEnemySprite(); } break; case::Enemy::EnemyType::ZAPPER: - if (enemy_texture.loadFromFile(zapper_texture_path)) + if (enemy_texture.loadFromFile(Config::zapper_texture_path)) { enemy_sprite.setTexture(enemy_texture); scaleEnemySprite(); diff --git a/Space-Invaders/Source/Gameplay/GameplayView.cpp b/Space-Invaders/Source/Gameplay/GameplayView.cpp index d7c276f92..48af47575 100644 --- a/Space-Invaders/Source/Gameplay/GameplayView.cpp +++ b/Space-Invaders/Source/Gameplay/GameplayView.cpp @@ -1,6 +1,7 @@ #include "../../header/Gameplay/GameplayView.h" #include "../../header/Global/ServiceLocator.h" #include "../../header/Graphic/GraphicService.h" +#include "../../Header/Global/Config.h" namespace Gameplay { @@ -19,7 +20,7 @@ namespace Gameplay void GameplayView::initializeBackgroundSprite() { - if (background_texture.loadFromFile(background_texture_path)) + if (background_texture.loadFromFile(Config::background_texture_path)) { background_sprite.setTexture(background_texture); scaleBackgroundSprite(); diff --git a/Space-Invaders/Source/Global/Config.cpp b/Space-Invaders/Source/Global/Config.cpp new file mode 100644 index 000000000..bc8151e53 --- /dev/null +++ b/Space-Invaders/Source/Global/Config.cpp @@ -0,0 +1,56 @@ +#include "../../header/Global/Config.h" + +namespace Global +{ + const sf::String Config::outscal_logo_texture_path = "assets/textures/outscal_logo.png"; + + const sf::String Config::background_texture_path = "assets/textures/space_invaders_bg.png"; + + const sf::String Config::player_texture_path = "assets/textures/player_ship.png"; + + + const sf::String Config::zapper_texture_path = "assets/textures/zapper.png"; + + const sf::String Config::thunder_snake_texture_path = "assets/textures/thunder_snake.png"; + + const sf::String Config::subzero_texture_path = "assets/textures/subzero.png"; + + const sf::String Config::ufo_texture_path = "assets/textures/ufo.png"; + + const sf::String Config::bunker_texture_path = "assets/textures/bunker.png"; + + + const sf::String Config::shield_texture_path = "assets/textures/shield.png"; + + const sf::String Config::tripple_laser_texture_path = "assets/textures/tripple_laser.png"; + + const sf::String Config::rapid_fire_texture_path = "assets/textures/rapid_fire.png"; + + const sf::String Config::outscal_bomb_texture_path = "assets/textures/outscal_bomb.png"; + + + const sf::String Config::laser_bullet_texture_path = "assets/textures/laser_bullet.png"; + + const sf::String Config::torpedoe_texture_path = "assets/textures/torpedoe.png"; + + const sf::String Config::frost_beam_texture_path = "assets/textures/frost_beam.png"; + + + const sf::String Config::play_button_texture_path = "assets/textures/play_button.png"; + + const sf::String Config::instructions_button_texture_path = "assets/textures/instructions_button.png"; + + const sf::String Config::quit_button_texture_path = "assets/textures/quit_button.png"; + + const sf::String Config::menu_button_texture_path = "assets/textures/menu_button.png"; + + + const sf::String Config::bubble_bobble_font_path = "assets/fonts/bubbleBobble.ttf"; + + const sf::String Config::DS_DIGIB_font_path = "assets/fonts/DS_DIGIB.ttf"; + + + const sf::String Config::background_music_path = "assets/sounds/background_music.mp3"; + + const sf::String Config::button_click_sound_path = "assets/sounds/button_click_sound.wav"; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index cb6237fd9..dbb31fa5f 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -70,6 +70,7 @@ namespace Global ui_service->initialize(); enemy_service->initialize(); gameplay_service->initialize(); + element_service->initialize(); } void ServiceLocator::update() @@ -83,6 +84,7 @@ namespace Global gameplay_service->update(); player_service->update(); enemy_service->update(); + element_service->update(); } ui_service->update(); @@ -97,6 +99,7 @@ namespace Global gameplay_service->render(); player_service->render(); enemy_service->render(); + element_service->render(); } ui_service->render(); @@ -110,7 +113,6 @@ namespace Global TimeService* ServiceLocator::getTimeService() { return time_service; } UIService* ServiceLocator::getUIService() { return ui_service; } EnemyService* ServiceLocator::getEnemyService() { return enemy_service; } - Gameplay::GameplayService* ServiceLocator::getGameplayService() { return gameplay_service; } - Element::ElementService* ServiceLocator::getElementService() { return element_service; } - + GameplayService* ServiceLocator::getGameplayService() { return gameplay_service; } + ElementService* ServiceLocator::getElementService() { return element_service; } } \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp index bab4189cc..96e287e72 100644 --- a/Space-Invaders/Source/Player/PlayerView.cpp +++ b/Space-Invaders/Source/Player/PlayerView.cpp @@ -1,5 +1,6 @@ #include "../../Header/Player/PlayerView.h" #include "../../Header/Global/ServiceLocator.h" +#include "../../Header/Global/Config.h" namespace Player { @@ -24,7 +25,7 @@ namespace Player void PlayerView::initializePlayerSprite() { - if (player_texture.loadFromFile(player_texture_path)) + if (player_texture.loadFromFile(Config::player_texture_path)) { player_sprite.setTexture(player_texture); scalePlayerSprite(); diff --git a/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp index 37b309a04..18a847f1a 100644 --- a/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp @@ -3,6 +3,7 @@ #include "../../Header/Main/GameService.h" #include "../../Header/Global/ServiceLocator.h" #include "../../Header/Graphic/GraphicService.h" +#include "../../Header/Global/Config.h" namespace UI { @@ -24,7 +25,7 @@ namespace UI void MainMenuUIController::initializeBackgroundImage() { //check if a texture loaded properly - if (background_texture.loadFromFile(background_texture_path)) + if (background_texture.loadFromFile(Config::background_texture_path)) { //if it did then set the bg image and scale it background_sprite.setTexture(background_texture); scaleBackgroundImage(); @@ -53,9 +54,9 @@ namespace UI // only returns true if all tectures are loaded 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); + return play_button_texture.loadFromFile(Config::play_button_texture_path) && + instructions_button_texture.loadFromFile(Config::instructions_button_texture_path) && + quit_button_texture.loadFromFile(Config::quit_button_texture_path); } void MainMenuUIController::setButtonSprites() From b92bb2153fd9030d4d4210ef5af37d342a92368a Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 7 Aug 2024 14:22:01 +0530 Subject: [PATCH 38/41] Added Sound Playback --- Space-Invaders/Header/Global/ServiceLocator.h | 3 ++ Space-Invaders/Header/Sound/SoundService.h | 29 +++++++++++ .../Source/Global/ServiceLocator.cpp | 6 +++ Space-Invaders/Source/Sound/SoundService.cpp | 50 +++++++++++++++++++ .../UI/MainMenu/MainMenuUIController.cpp | 5 ++ 5 files changed, 93 insertions(+) create mode 100644 Space-Invaders/Header/Sound/SoundService.h create mode 100644 Space-Invaders/Source/Sound/SoundService.cpp diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index cad6fff7a..95fc30f3e 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -7,6 +7,7 @@ #include "../../Header/Enemy/EnemyService.h" #include "../../Header/Gameplay/GameplayService.h" #include "../../Header/Element/ElementService.h" +#include "../../Header/Sound/SoundService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -26,6 +27,7 @@ namespace Global Enemy::EnemyService* enemy_service; Gameplay::GameplayService* gameplay_service; Element::ElementService* element_service; + Sound::SoundService* sound_service; // Public Methods ServiceLocator(); @@ -52,5 +54,6 @@ namespace Global Enemy::EnemyService* getEnemyService(); // Retrive the EnemyService instance Gameplay::GameplayService* getGameplayService(); // Retrive the GameplayService instance Element::ElementService* getElementService(); // Retrive the ElementService instance + Sound::SoundService* getSoundService(); // Retrive the SoundService instance }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Sound/SoundService.h b/Space-Invaders/Header/Sound/SoundService.h new file mode 100644 index 000000000..93b0dd509 --- /dev/null +++ b/Space-Invaders/Header/Sound/SoundService.h @@ -0,0 +1,29 @@ +#pragma once +#include "SFML/Audio.hpp" + +namespace Sound +{ + enum class SoundType + { + BUTTON_CLICK, + }; + + class SoundService + { + private: + const int background_music_volume = 30; + + sf::Music background_music; + sf::Sound sound_effect; + sf::SoundBuffer buffer_button_click; + + void loadBackgroundMusicFromFile(); + void loadSoundFromFile(); + + public: + void initialize(); + + void playSound(SoundType soundType); + void playBackgroundMusic(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index dbb31fa5f..8bed539b4 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -12,6 +12,7 @@ namespace Global using namespace Enemy; using namespace Gameplay; using namespace Element; + using namespace Sound; ServiceLocator::ServiceLocator() @@ -24,6 +25,7 @@ namespace Global enemy_service = nullptr; gameplay_service = nullptr; element_service = nullptr; + sound_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -41,6 +43,7 @@ namespace Global enemy_service = new EnemyService(); gameplay_service = new GameplayService(); element_service = new ElementService(); + sound_service = new SoundService(); } void ServiceLocator::clearAllServices() @@ -53,6 +56,7 @@ namespace Global delete(enemy_service); delete(gameplay_service); delete(element_service); + delete(sound_service); } ServiceLocator* ServiceLocator::getInstance() @@ -71,6 +75,7 @@ namespace Global enemy_service->initialize(); gameplay_service->initialize(); element_service->initialize(); + sound_service->initialize(); } void ServiceLocator::update() @@ -115,4 +120,5 @@ namespace Global EnemyService* ServiceLocator::getEnemyService() { return enemy_service; } GameplayService* ServiceLocator::getGameplayService() { return gameplay_service; } ElementService* ServiceLocator::getElementService() { return element_service; } + Sound::SoundService* ServiceLocator::getSoundService() { return sound_service; } } \ No newline at end of file diff --git a/Space-Invaders/Source/Sound/SoundService.cpp b/Space-Invaders/Source/Sound/SoundService.cpp new file mode 100644 index 000000000..2db4a00bd --- /dev/null +++ b/Space-Invaders/Source/Sound/SoundService.cpp @@ -0,0 +1,50 @@ +#include "../../header/Sound/SoundService.h" +#include "../../header/Global/Config.h" + +namespace Sound +{ + using namespace Global; + + void SoundService::initialize() + { + loadBackgroundMusicFromFile(); + loadSoundFromFile(); + } + + void SoundService::loadBackgroundMusicFromFile() + { + if (!background_music.openFromFile(Config::background_music_path)) + { + printf("Error loading background music file"); + } + } + + void SoundService::loadSoundFromFile() + { + if (!buffer_button_click.loadFromFile(Config::button_click_sound_path)) + { + printf("Error loading background music file"); + } + } + + void SoundService::playSound(SoundType soundType) + { + switch (soundType) + { + case SoundType::BUTTON_CLICK: + sound_effect.setBuffer(buffer_button_click); + break; + default: + printf("Invalid sound type"); + return; + } + sound_effect.play(); + } + + void SoundService::playBackgroundMusic() + { + background_music.setLoop(true); + background_music.setVolume(background_music_volume); + background_music.play(); + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp index 18a847f1a..4e2a876f6 100644 --- a/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp @@ -13,6 +13,7 @@ namespace UI using namespace Main; using namespace Graphics; using namespace Event; + using namespace Sound; MainMenuUIController::MainMenuUIController() { game_window = nullptr; } @@ -97,6 +98,8 @@ namespace UI if (clickedButton(&play_button_sprite, mouse_position)) { + Global::ServiceLocator::getInstance()->getSoundService()->playSound(SoundType::BUTTON_CLICK); //play button sound + Global::ServiceLocator::getInstance()->getSoundService()->playBackgroundMusic(); //play background music GameService::setGameState(GameState::GAMEPLAY); } @@ -106,7 +109,9 @@ namespace UI } if (clickedButton(&quit_button_sprite, mouse_position)) + { game_window->close(); + } } bool MainMenuUIController::clickedButton(sf::Sprite* button_sprite, sf::Vector2f mouse_position) From 56f32a4b0a3727e61060e90b78abc775c4b7fecf Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 8 Aug 2024 13:08:16 +0530 Subject: [PATCH 39/41] Added Bullet folder and interface --- Space-Invaders/Header/Bullet/BulletService.h | 17 +++++++++++++ .../Header/Projectile/IProjectile.h | 21 ++++++++++++++++ .../Source/Bullet/BulletService.cpp | 24 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 Space-Invaders/Header/Bullet/BulletService.h create mode 100644 Space-Invaders/Header/Projectile/IProjectile.h create mode 100644 Space-Invaders/Source/Bullet/BulletService.cpp diff --git a/Space-Invaders/Header/Bullet/BulletService.h b/Space-Invaders/Header/Bullet/BulletService.h new file mode 100644 index 000000000..cc9b730dd --- /dev/null +++ b/Space-Invaders/Header/Bullet/BulletService.h @@ -0,0 +1,17 @@ +#pragma once +namespace Bullet +{ + class BulletService + { + private: + + + public: + BulletService(); + ~BulletService(); + + void initialize(); + void update(); + void render(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Projectile/IProjectile.h b/Space-Invaders/Header/Projectile/IProjectile.h new file mode 100644 index 000000000..770b0e973 --- /dev/null +++ b/Space-Invaders/Header/Projectile/IProjectile.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include "../../Header/Bullet/BulletConfig.h" + +namespace Projectile +{ + enum class MovementDirection; + + class IProjectile + { + public: + virtual void initialize(sf::Vector2f position, Bullet::MovementDirection direction) = 0; + virtual void update() = 0; + virtual void render() = 0; + + virtual void updateProjectilePosition() = 0; + virtual sf::Vector2f getProjectilePosition() = 0; + + virtual ~IProjectile() {}; + }; +} diff --git a/Space-Invaders/Source/Bullet/BulletService.cpp b/Space-Invaders/Source/Bullet/BulletService.cpp new file mode 100644 index 000000000..f51b53e3c --- /dev/null +++ b/Space-Invaders/Source/Bullet/BulletService.cpp @@ -0,0 +1,24 @@ +#include "../../Header/Bullet/BulletService.h" + +namespace Bullet +{ + Bullet::BulletService::BulletService() + { + } + + Bullet::BulletService::~BulletService() + { + } + + void Bullet::BulletService::initialize() + { + } + + void Bullet::BulletService::update() + { + } + + void Bullet::BulletService::render() + { + } +} \ No newline at end of file From 37a6b316130b2331902f2203f5462bb8f2faf31d Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 8 Aug 2024 15:24:44 +0530 Subject: [PATCH 40/41] FireBullet Added --- Space-Invaders/Header/Bullet/BulletConfig.h | 16 ++++ .../Header/Bullet/BulletController.h | 35 +++++++ Space-Invaders/Header/Bullet/BulletModel.h | 37 ++++++++ Space-Invaders/Header/Bullet/BulletService.h | 17 +++- Space-Invaders/Header/Bullet/BulletView.h | 33 +++++++ .../Controllers/FrostBulletController.h | 20 ++++ .../Controllers/LaserBulletController.h | 17 ++++ .../Bullet/Controllers/TorpedoController.h | 20 ++++ .../Enemy/Controllers/SubZeroController.h | 3 + .../Enemy/Controllers/ZapperController.h | 2 + Space-Invaders/Header/Enemy/EnemyController.h | 10 ++ Space-Invaders/Header/Enemy/EnemyModel.h | 1 + Space-Invaders/Header/Global/ServiceLocator.h | 5 + .../Header/Player/PlayerController.h | 2 + Space-Invaders/Header/Player/PlayerModel.h | 1 + .../Source/Bullet/BulletController.cpp | 92 +++++++++++++++++++ Space-Invaders/Source/Bullet/BulletModel.cpp | 57 ++++++++++++ .../Source/Bullet/BulletService.cpp | 49 +++++++++- Space-Invaders/Source/Bullet/BulletView.cpp | 67 ++++++++++++++ .../Controllers/FrostBulletController.cpp | 18 ++++ .../Controllers/LaserBulletController.cpp | 16 ++++ .../Bullet/Controllers/TorpedoController.cpp | 18 ++++ .../Enemy/Controllers/SubZeroController.cpp | 9 ++ .../Enemy/Controllers/ZapperController.cpp | 9 ++ .../Source/Enemy/EnemyController.cpp | 18 ++++ .../Source/Global/ServiceLocator.cpp | 16 +++- .../Source/Player/PlayerController.cpp | 15 ++- 27 files changed, 595 insertions(+), 8 deletions(-) create mode 100644 Space-Invaders/Header/Bullet/BulletConfig.h create mode 100644 Space-Invaders/Header/Bullet/BulletController.h create mode 100644 Space-Invaders/Header/Bullet/BulletModel.h create mode 100644 Space-Invaders/Header/Bullet/BulletView.h create mode 100644 Space-Invaders/Header/Bullet/Controllers/FrostBulletController.h create mode 100644 Space-Invaders/Header/Bullet/Controllers/LaserBulletController.h create mode 100644 Space-Invaders/Header/Bullet/Controllers/TorpedoController.h create mode 100644 Space-Invaders/Source/Bullet/BulletController.cpp create mode 100644 Space-Invaders/Source/Bullet/BulletModel.cpp create mode 100644 Space-Invaders/Source/Bullet/BulletView.cpp create mode 100644 Space-Invaders/Source/Bullet/Controllers/FrostBulletController.cpp create mode 100644 Space-Invaders/Source/Bullet/Controllers/LaserBulletController.cpp create mode 100644 Space-Invaders/Source/Bullet/Controllers/TorpedoController.cpp diff --git a/Space-Invaders/Header/Bullet/BulletConfig.h b/Space-Invaders/Header/Bullet/BulletConfig.h new file mode 100644 index 000000000..3d40244af --- /dev/null +++ b/Space-Invaders/Header/Bullet/BulletConfig.h @@ -0,0 +1,16 @@ +#pragma once +namespace Bullet +{ + enum class BulletType + { + LASER_BULLET, + TORPEDO, + FROST_BULLET, + }; + + enum class MovementDirection + { + UP, //player needs to shoot in upward direction + DOWN, // enemies always shoot in downward direction + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Bullet/BulletController.h b/Space-Invaders/Header/Bullet/BulletController.h new file mode 100644 index 000000000..d8c97559b --- /dev/null +++ b/Space-Invaders/Header/Bullet/BulletController.h @@ -0,0 +1,35 @@ +#pragma once +#include "../../Header/Projectile/IProjectile.h" +#include "../../Header/Bullet/BulletConfig.h" + +namespace Bullet +{ + class BulletView; + class BulletModel; + enum class BulletType; + + class BulletController : public Projectile::IProjectile + { + protected: + + BulletView* bullet_view; + BulletModel* bullet_model; + + void updateProjectilePosition() override; + + void moveUp(); + void moveDown(); + void handleOutOfBounds(); + + public: + + BulletController(BulletType type); + virtual ~BulletController() override; + void initialize(sf::Vector2f position, Bullet::MovementDirection direction) override; + void update() override; + void render() override; + + sf::Vector2f getProjectilePosition() override; + BulletType getBulletType(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Bullet/BulletModel.h b/Space-Invaders/Header/Bullet/BulletModel.h new file mode 100644 index 000000000..a6d9a45e2 --- /dev/null +++ b/Space-Invaders/Header/Bullet/BulletModel.h @@ -0,0 +1,37 @@ +#pragma once +#include + +namespace Bullet +{ + enum class BulletType; + enum class MovementDirection; + + class BulletModel + { + private: + float movement_speed = 300.f; + sf::Vector2f bullet_position; + + BulletType bullet_type; + MovementDirection movement_direction; + + public: + + BulletModel(BulletType type); + ~BulletModel(); + + void initialize(sf::Vector2f position, MovementDirection direction); + + sf::Vector2f getBulletPosition(); + void setBulletPosition(sf::Vector2f position); + + BulletType getBulletType(); + void setBulletType(BulletType type); + + MovementDirection getMovementDirection(); + void setMovementDirection(MovementDirection direction); + + float getMovementSpeed(); + void setMovementSpeed(float speed); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Bullet/BulletService.h b/Space-Invaders/Header/Bullet/BulletService.h index cc9b730dd..cc8939bb6 100644 --- a/Space-Invaders/Header/Bullet/BulletService.h +++ b/Space-Invaders/Header/Bullet/BulletService.h @@ -1,17 +1,32 @@ #pragma once +#include +#include "SFML/System/Vector2.hpp" +#include "../../Header/Projectile/IProjectile.h" + namespace Bullet { + class BulletController; + enum class BulletType; + enum class MovementDirection; + class BulletService { private: + std::vector bullet_list; + + BulletController* createBullet(BulletType bullet_type); + void destroy(); public: BulletService(); - ~BulletService(); + virtual ~BulletService(); void initialize(); void update(); void render(); + + BulletController* spawnBullet(BulletType bullet_type, sf::Vector2f position, MovementDirection direction); + void destroyBullet(BulletController* bullet_controller); }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Bullet/BulletView.h b/Space-Invaders/Header/Bullet/BulletView.h new file mode 100644 index 000000000..bbe639119 --- /dev/null +++ b/Space-Invaders/Header/Bullet/BulletView.h @@ -0,0 +1,33 @@ +#pragma once +#include +#include "../../Header/Bullet/BulletController.h" + +namespace Bullet +{ + class BulletController; + enum class BulletType; + + class BulletView + { + private: + const float bullet_sprite_width = 18.f; + const float bullet_sprite_height = 18.f; + + sf::RenderWindow* game_window; + sf::Texture bullet_texture; + sf::Sprite bullet_sprite; + + BulletController* bullet_controller; + + void initializeImage(BulletType type); + void scaleImage(); + + public: + BulletView(); + ~BulletView(); + + void initialize(BulletController* controller); + void update(); + void render(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Bullet/Controllers/FrostBulletController.h b/Space-Invaders/Header/Bullet/Controllers/FrostBulletController.h new file mode 100644 index 000000000..3188333d9 --- /dev/null +++ b/Space-Invaders/Header/Bullet/Controllers/FrostBulletController.h @@ -0,0 +1,20 @@ +#pragma once +#include "../../Header/Bullet/BulletController.h" + +namespace Bullet +{ + namespace Controller + { + class FrostBulletController : public BulletController + { + private: + const float torpedo_movement_speed = 500.f; + + public: + FrostBulletController(BulletType type); + ~FrostBulletController(); + + void initialize(sf::Vector2f position, MovementDirection direction) override; + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Header/Bullet/Controllers/LaserBulletController.h b/Space-Invaders/Header/Bullet/Controllers/LaserBulletController.h new file mode 100644 index 000000000..67a48b89f --- /dev/null +++ b/Space-Invaders/Header/Bullet/Controllers/LaserBulletController.h @@ -0,0 +1,17 @@ +#pragma once +#include "../../Header/Bullet/BulletController.h" + +namespace Bullet +{ + namespace Controller + { + class LaserBulletController : public BulletController + { + public: + LaserBulletController(BulletType type); + ~LaserBulletController(); + + void initialize(sf::Vector2f position, MovementDirection direction) override; + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Header/Bullet/Controllers/TorpedoController.h b/Space-Invaders/Header/Bullet/Controllers/TorpedoController.h new file mode 100644 index 000000000..99ac2d88a --- /dev/null +++ b/Space-Invaders/Header/Bullet/Controllers/TorpedoController.h @@ -0,0 +1,20 @@ +#pragma once +#include "../../Header/Bullet/BulletController.h" + +namespace Bullet +{ + namespace Controller + { + class TorpedoController : public BulletController + { + private: + const float torpedo_movement_speed = 200.f; + + public: + TorpedoController(BulletType type); + ~TorpedoController(); + + void initialize(sf::Vector2f position, MovementDirection direction) override; + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h b/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h index 51600cb33..d27ae5a7f 100644 --- a/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h +++ b/Space-Invaders/Header/Enemy/Controllers/SubZeroController.h @@ -11,10 +11,13 @@ namespace Enemy private: float vertical_movement_speed = 100.f; + const float subzero_rate_of_fire = 2.f; void move() override; void moveDown(); + void fireBullet() override; + public: SubzeroController(EnemyType type); diff --git a/Space-Invaders/Header/Enemy/Controllers/ZapperController.h b/Space-Invaders/Header/Enemy/Controllers/ZapperController.h index cade22a40..fc5a3b414 100644 --- a/Space-Invaders/Header/Enemy/Controllers/ZapperController.h +++ b/Space-Invaders/Header/Enemy/Controllers/ZapperController.h @@ -17,6 +17,8 @@ namespace Enemy void moveRight(); void moveDown(); + void fireBullet() override; + public: ZapperController(EnemyType type); diff --git a/Space-Invaders/Header/Enemy/EnemyController.h b/Space-Invaders/Header/Enemy/EnemyController.h index 30ce48675..7b2a2a3c0 100644 --- a/Space-Invaders/Header/Enemy/EnemyController.h +++ b/Space-Invaders/Header/Enemy/EnemyController.h @@ -14,9 +14,19 @@ namespace Enemy protected: + float vertical_movement_speed = 30.f; + float horizontal_movement_speed = 200.0f; + + float rate_of_fire = 3.f; //we want to fire the bullet every 3 seconds + float elapsed_fire_duration = 0.f; //variable to check how long it has been since we last fired + EnemyView* enemy_view; EnemyModel* enemy_model; + void updateFireTimer(); + void processBulletFire(); + virtual void fireBullet() = 0; + virtual void move() = 0; sf::Vector2f getRandomInitialPosition(); diff --git a/Space-Invaders/Header/Enemy/EnemyModel.h b/Space-Invaders/Header/Enemy/EnemyModel.h index f7a496405..57a4cdb4f 100644 --- a/Space-Invaders/Header/Enemy/EnemyModel.h +++ b/Space-Invaders/Header/Enemy/EnemyModel.h @@ -24,6 +24,7 @@ namespace Enemy //const settings for enemy const sf::Vector2f left_most_position = sf::Vector2f(50.f, 50.f); const sf::Vector2f right_most_position = sf::Vector2f(1800.f, 50.f); + const sf::Vector2f barrel_position_offset = sf::Vector2f(20.f, 50.f); const float vertical_travel_distance = 100.f; const float enemy_movement_speed = 250.0f; diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index 95fc30f3e..14f4faf36 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -8,6 +8,8 @@ #include "../../Header/Gameplay/GameplayService.h" #include "../../Header/Element/ElementService.h" #include "../../Header/Sound/SoundService.h" +#include "../../Header/Bullet/BulletService.h" + // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -28,6 +30,7 @@ namespace Global Gameplay::GameplayService* gameplay_service; Element::ElementService* element_service; Sound::SoundService* sound_service; + Bullet::BulletService* bullet_service; // Public Methods ServiceLocator(); @@ -55,5 +58,7 @@ namespace Global Gameplay::GameplayService* getGameplayService(); // Retrive the GameplayService instance Element::ElementService* getElementService(); // Retrive the ElementService instance Sound::SoundService* getSoundService(); // Retrive the SoundService instance + Bullet::BulletService* getBulletService(); // Retrive the BulletService instance + void deleteServiceLocator(); }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h index c3a12b538..09c3098fd 100644 --- a/Space-Invaders/Header/Player/PlayerController.h +++ b/Space-Invaders/Header/Player/PlayerController.h @@ -18,6 +18,8 @@ namespace Player void moveLeft(); void moveRight(); + void fireBullet(); + public: PlayerController(); ~PlayerController(); diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h index 43e8ec35f..4cb5016dc 100644 --- a/Space-Invaders/Header/Player/PlayerModel.h +++ b/Space-Invaders/Header/Player/PlayerModel.h @@ -22,6 +22,7 @@ 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 sf::Vector2f barrel_position_offset = sf::Vector2f(20.f, 50.f); const float player_movement_speed = 600.0f; diff --git a/Space-Invaders/Source/Bullet/BulletController.cpp b/Space-Invaders/Source/Bullet/BulletController.cpp new file mode 100644 index 000000000..d7f4f8500 --- /dev/null +++ b/Space-Invaders/Source/Bullet/BulletController.cpp @@ -0,0 +1,92 @@ +#include "../../Header/Bullet/BulletController.h" +#include "../../Header/Bullet/BulletView.h" +#include "../../Header/Bullet/BulletModel.h" +#include "../../Header/Bullet/BulletConfig.h" +#include "../../Header/Global/ServiceLocator.h" + +namespace Bullet +{ + using namespace Global; + + void Bullet::BulletController::updateProjectilePosition() + { + switch (bullet_model->getMovementDirection()) + { + case::Bullet::MovementDirection::UP: + moveUp(); + break; + + case::Bullet::MovementDirection::DOWN: + moveDown(); + break; + } + } + + void Bullet::BulletController::moveUp() + { + sf::Vector2f currentPosition = bullet_model->getBulletPosition(); + currentPosition.y -= bullet_model->getMovementSpeed() * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + bullet_model->setBulletPosition(currentPosition); + } + + void Bullet::BulletController::moveDown() + { + sf::Vector2f currentPosition = bullet_model->getBulletPosition(); + currentPosition.y += bullet_model->getMovementSpeed() * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + bullet_model->setBulletPosition(currentPosition); + } + + void Bullet::BulletController::handleOutOfBounds() + { + sf::Vector2f bulletPosition = getProjectilePosition(); + sf::Vector2u windowSize = ServiceLocator::getInstance()->getGraphicService()->getGameWindow()->getSize(); + + if (bulletPosition.x < 0 || bulletPosition.x > windowSize.x || + bulletPosition.y < 0 || bulletPosition.y > windowSize.y) + { + ServiceLocator::getInstance()->getBulletService()->destroyBullet(this); + } + } + + BulletController::BulletController(BulletType type) + { + bullet_view = new BulletView(); + bullet_model = new BulletModel(type); + } + + BulletController::~BulletController() + { + delete (bullet_view); + delete (bullet_model); + } + + void BulletController::initialize(sf::Vector2f position, Bullet::MovementDirection direction) + { + bullet_view->initialize(this); + bullet_model->initialize(position, direction); + } + + void BulletController::update() + { + updateProjectilePosition(); + bullet_view->update(); + handleOutOfBounds(); + } + + void BulletController::render() + { + bullet_view->render(); + } + + sf::Vector2f BulletController::getProjectilePosition() + { + return bullet_model->getBulletPosition(); + } + + BulletType BulletController::getBulletType() + { + return bullet_model->getBulletType(); + } +} diff --git a/Space-Invaders/Source/Bullet/BulletModel.cpp b/Space-Invaders/Source/Bullet/BulletModel.cpp new file mode 100644 index 000000000..ed80e5b04 --- /dev/null +++ b/Space-Invaders/Source/Bullet/BulletModel.cpp @@ -0,0 +1,57 @@ +#include "../../Header/Bullet/BulletModel.h" + +namespace Bullet +{ + BulletModel::BulletModel(BulletType type) + { + bullet_type = type; + } + + BulletModel::~BulletModel() { } + + void BulletModel::initialize(sf::Vector2f position, MovementDirection direction) + { + movement_direction = direction; + bullet_position = position; + } + + sf::Vector2f BulletModel::getBulletPosition() + { + return bullet_position; + } + + void BulletModel::setBulletPosition(sf::Vector2f position) + { + bullet_position = position; + } + + BulletType BulletModel::getBulletType() + { + return bullet_type; + } + + void BulletModel::setBulletType(BulletType type) + { + bullet_type = type; + } + + MovementDirection BulletModel::getMovementDirection() + { + return movement_direction; + } + + void BulletModel::setMovementDirection(MovementDirection direction) + { + movement_direction = direction; + } + + float BulletModel::getMovementSpeed() + { + return movement_speed; + } + + void BulletModel::setMovementSpeed(float speed) + { + movement_speed = speed; + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Bullet/BulletService.cpp b/Space-Invaders/Source/Bullet/BulletService.cpp index f51b53e3c..897d61a83 100644 --- a/Space-Invaders/Source/Bullet/BulletService.cpp +++ b/Space-Invaders/Source/Bullet/BulletService.cpp @@ -1,24 +1,63 @@ #include "../../Header/Bullet/BulletService.h" +#include "../../Header/Bullet/BulletController.h" +#include "../../Header/Bullet/BulletConfig.h" +#include "../../Header/Bullet/Controllers/FrostBulletController.h" +#include "../../Header/Bullet/Controllers/LaserBulletController.h" +#include "../../Header/Bullet/Controllers/TorpedoController.h" namespace Bullet { - Bullet::BulletService::BulletService() + using namespace Controller; + using namespace Projectile; + + BulletService::BulletService() { } + + BulletService::~BulletService() { destroy(); } + + void BulletService::initialize() { } + + void BulletService::update() + { + for (int i = 0; i < bullet_list.size(); i++) bullet_list[i]->update(); + } + + void BulletService::render() { + for (int i = 0; i < bullet_list.size(); i++) bullet_list[i]->render(); } - Bullet::BulletService::~BulletService() + BulletController* BulletService::createBullet(BulletType bullet_type) { + switch (bullet_type) + { + case::Bullet::BulletType::LASER_BULLET: + return new LaserBulletController(Bullet::BulletType::LASER_BULLET); + + case::Bullet::BulletType::FROST_BULLET: + return new FrostBulletController(Bullet::BulletType::FROST_BULLET); + + case::Bullet::BulletType::TORPEDO: + return new TorpedoController(Bullet::BulletType::TORPEDO); + } } - void Bullet::BulletService::initialize() + void BulletService::destroy() { + for (int i = 0; i < bullet_list.size(); i++) delete (bullet_list[i]); } - void Bullet::BulletService::update() + BulletController* BulletService::spawnBullet(BulletType bullet_type, sf::Vector2f position, MovementDirection direction) { + BulletController* bullet_controller = createBullet(bullet_type); + + bullet_controller->initialize(position, direction); + bullet_list.push_back(bullet_controller); + return bullet_controller; } - void Bullet::BulletService::render() + void BulletService::destroyBullet(BulletController* bullet_controller) { + bullet_list.erase(std::remove(bullet_list.begin(), bullet_list.end(), bullet_controller), bullet_list.end()); + delete(bullet_controller); } } \ No newline at end of file diff --git a/Space-Invaders/Source/Bullet/BulletView.cpp b/Space-Invaders/Source/Bullet/BulletView.cpp new file mode 100644 index 000000000..4d50a07dd --- /dev/null +++ b/Space-Invaders/Source/Bullet/BulletView.cpp @@ -0,0 +1,67 @@ +#include "../../Header/Bullet/BulletView.h" +#include "../../Header/Bullet/BulletController.h" +#include "../../Header/Global/ServiceLocator.h" +#include "../../Header/Global/Config.h" +#include "../../Header/Bullet/BulletConfig.h" + +namespace Bullet +{ + using namespace Global; + + BulletView::BulletView() { } + + BulletView::~BulletView() { } + + void BulletView::initialize(BulletController* controller) + { + bullet_controller = controller; + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializeImage(bullet_controller->getBulletType()); + } + + void BulletView::initializeImage(BulletType type) + { + switch (type) + { + case::Bullet::BulletType::LASER_BULLET: + if (bullet_texture.loadFromFile(Config::laser_bullet_texture_path)) + { + bullet_sprite.setTexture(bullet_texture); + scaleImage(); + } + break; + case::Bullet::BulletType::FROST_BULLET: + if (bullet_texture.loadFromFile(Config::frost_beam_texture_path)) + { + bullet_sprite.setTexture(bullet_texture); + scaleImage(); + } + break; + case::Bullet::BulletType::TORPEDO: + if (bullet_texture.loadFromFile(Config::torpedoe_texture_path)) + { + bullet_sprite.setTexture(bullet_texture); + scaleImage(); + } + break; + } + } + + void BulletView::scaleImage() + { + bullet_sprite.setScale( + static_cast(bullet_sprite_width) / bullet_sprite.getTexture()->getSize().x, + static_cast(bullet_sprite_height) / bullet_sprite.getTexture()->getSize().y + ); + } + + void BulletView::update() + { + bullet_sprite.setPosition(bullet_controller->getProjectilePosition()); + } + + void BulletView::render() + { + game_window->draw(bullet_sprite); + } +} diff --git a/Space-Invaders/Source/Bullet/Controllers/FrostBulletController.cpp b/Space-Invaders/Source/Bullet/Controllers/FrostBulletController.cpp new file mode 100644 index 000000000..3cd929822 --- /dev/null +++ b/Space-Invaders/Source/Bullet/Controllers/FrostBulletController.cpp @@ -0,0 +1,18 @@ +#include "../../Header/Bullet/Controllers/FrostBulletController.h" +#include "../../Header/Bullet/BulletModel.h" + +namespace Bullet +{ + namespace Controller + { + FrostBulletController::FrostBulletController(BulletType type) : BulletController(type) { } + + FrostBulletController::~FrostBulletController() { } + + void FrostBulletController::initialize(sf::Vector2f position, MovementDirection direction) + { + BulletController::initialize(position, direction); + bullet_model->setMovementSpeed(torpedo_movement_speed); + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Bullet/Controllers/LaserBulletController.cpp b/Space-Invaders/Source/Bullet/Controllers/LaserBulletController.cpp new file mode 100644 index 000000000..170970514 --- /dev/null +++ b/Space-Invaders/Source/Bullet/Controllers/LaserBulletController.cpp @@ -0,0 +1,16 @@ +#include "../../Header/Bullet/Controllers/LaserBulletController.h" + +namespace Bullet +{ + namespace Controller + { + LaserBulletController::LaserBulletController(BulletType type) : BulletController(type) { } + + LaserBulletController::~LaserBulletController() { } + + void LaserBulletController::initialize(sf::Vector2f position, MovementDirection direction) + { + BulletController::initialize(position, direction); + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Bullet/Controllers/TorpedoController.cpp b/Space-Invaders/Source/Bullet/Controllers/TorpedoController.cpp new file mode 100644 index 000000000..712702699 --- /dev/null +++ b/Space-Invaders/Source/Bullet/Controllers/TorpedoController.cpp @@ -0,0 +1,18 @@ +#include "../../Header/Bullet/Controllers/TorpedoController.h" +#include "../../Header/Bullet/BulletModel.h" + +namespace Bullet +{ + namespace Controller + { + TorpedoController::TorpedoController(BulletType type) : BulletController(type) { } + + TorpedoController::~TorpedoController() { } + + void TorpedoController::initialize(sf::Vector2f position, MovementDirection direction) + { + BulletController::initialize(position, direction); + bullet_model->setMovementSpeed(torpedo_movement_speed); + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp b/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp index 3b212f84c..5c6fbcd0e 100644 --- a/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp +++ b/Space-Invaders/Source/Enemy/Controllers/SubZeroController.cpp @@ -6,6 +6,7 @@ namespace Enemy { using namespace Global; + using namespace Bullet; namespace Controller { @@ -17,6 +18,7 @@ namespace Enemy { EnemyController::initialize(); enemy_model->setMovementDirection(MovementDirection::DOWN); + rate_of_fire = subzero_rate_of_fire; } void SubzeroController::move() @@ -36,5 +38,12 @@ namespace Enemy enemy_model->setEnemyPosition(currentPosition); } + + void SubzeroController::fireBullet() + { + ServiceLocator::getInstance()->getBulletService()->spawnBullet(BulletType::FROST_BULLET, + enemy_model->getEnemyPosition() + enemy_model->barrel_position_offset, + Bullet::MovementDirection::DOWN); + } } } \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp b/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp index ed5dccdf7..baa90a730 100644 --- a/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp +++ b/Space-Invaders/Source/Enemy/Controllers/ZapperController.cpp @@ -2,10 +2,12 @@ #include "../../Header/Enemy/EnemyModel.h" #include "../../Header/Enemy/EnemyConfig.h" #include "../../Header/Global/ServiceLocator.h" +#include "../../Header/Bullet/BulletConfig.h" namespace Enemy { using namespace Global; + using namespace Bullet; namespace Controller { @@ -113,5 +115,12 @@ namespace Enemy enemy_model->setEnemyPosition(currentPosition); } } + + void ZapperController::fireBullet() + { + ServiceLocator::getInstance()->getBulletService()->spawnBullet(BulletType::LASER_BULLET, + enemy_model->getEnemyPosition() + enemy_model->barrel_position_offset, + Bullet::MovementDirection::DOWN); + } } } \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyController.cpp b/Space-Invaders/Source/Enemy/EnemyController.cpp index af2cbcd3f..b89da8838 100644 --- a/Space-Invaders/Source/Enemy/EnemyController.cpp +++ b/Space-Invaders/Source/Enemy/EnemyController.cpp @@ -9,6 +9,7 @@ namespace Enemy using namespace Global; using namespace Event; using namespace Time; + using namespace Bullet; EnemyController::EnemyController(EnemyType type) { @@ -32,7 +33,10 @@ namespace Enemy void EnemyController::update() { move(); + updateFireTimer(); //new + processBulletFire(); //new enemy_view->update(); + handleOutOfBounds(); } void EnemyController::render() @@ -58,6 +62,20 @@ namespace Enemy } }*/ + void EnemyController::updateFireTimer() + { + elapsed_fire_duration += ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); //update the elapsed duration + } + + void EnemyController::processBulletFire() //if elapsed duration is equal to or more than the amount of time we want to wait until firing than call the fire method. + { + if (elapsed_fire_duration >= rate_of_fire) + { + fireBullet(); + elapsed_fire_duration = 0.f; //set elapsed duration back to 0. + } + } + sf::Vector2f EnemyController::getRandomInitialPosition() { float x_offset_distance = (std::rand() % static_cast(enemy_model->right_most_position.x - enemy_model->left_most_position.x)); diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index 8bed539b4..e6cd7d7d9 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -13,6 +13,7 @@ namespace Global using namespace Gameplay; using namespace Element; using namespace Sound; + using namespace Bullet; ServiceLocator::ServiceLocator() @@ -26,6 +27,7 @@ namespace Global gameplay_service = nullptr; element_service = nullptr; sound_service = nullptr; + bullet_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -44,6 +46,7 @@ namespace Global gameplay_service = new GameplayService(); element_service = new ElementService(); sound_service = new SoundService(); + bullet_service = new BulletService(); } void ServiceLocator::clearAllServices() @@ -57,6 +60,7 @@ namespace Global delete(gameplay_service); delete(element_service); delete(sound_service); + delete(bullet_service); } ServiceLocator* ServiceLocator::getInstance() @@ -76,6 +80,7 @@ namespace Global gameplay_service->initialize(); element_service->initialize(); sound_service->initialize(); + bullet_service->initialize(); } void ServiceLocator::update() @@ -89,6 +94,7 @@ namespace Global gameplay_service->update(); player_service->update(); enemy_service->update(); + bullet_service->update(); element_service->update(); } @@ -104,6 +110,7 @@ namespace Global gameplay_service->render(); player_service->render(); enemy_service->render(); + bullet_service->render(); element_service->render(); } @@ -120,5 +127,12 @@ namespace Global EnemyService* ServiceLocator::getEnemyService() { return enemy_service; } GameplayService* ServiceLocator::getGameplayService() { return gameplay_service; } ElementService* ServiceLocator::getElementService() { return element_service; } - Sound::SoundService* ServiceLocator::getSoundService() { return sound_service; } + SoundService* ServiceLocator::getSoundService() { return sound_service; } + BulletService* ServiceLocator::getBulletService() { return bullet_service; } + + void ServiceLocator::deleteServiceLocator() + { + delete(this); + } + } \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp index d84805283..49ac671f9 100644 --- a/Space-Invaders/Source/Player/PlayerController.cpp +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -10,6 +10,7 @@ namespace Player using namespace Global; using namespace Event; using namespace Time; + using namespace Bullet; PlayerController::PlayerController() { @@ -61,7 +62,12 @@ namespace Player { moveRight(); } - } + + if (event_service->pressedLeftMouseButton()) + { + fireBullet(); + } + } void PlayerController::moveLeft() { @@ -80,4 +86,11 @@ namespace Player currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); player_model->setPlayerPosition(currentPosition); } + + void PlayerController::fireBullet() + { + ServiceLocator::getInstance()->getBulletService()->spawnBullet(BulletType::LASER_BULLET, + player_model->getPlayerPosition() + player_model->barrel_position_offset, + Bullet::MovementDirection::UP); + } } \ No newline at end of file From 547651cf98e0f20e315de144598d7e5f43277723 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Fri, 9 Aug 2024 13:12:45 +0530 Subject: [PATCH 41/41] Added ICollectible --- .../Header/Collectible/ICollectible.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Space-Invaders/Header/Collectible/ICollectible.h diff --git a/Space-Invaders/Header/Collectible/ICollectible.h b/Space-Invaders/Header/Collectible/ICollectible.h new file mode 100644 index 000000000..d1405aa13 --- /dev/null +++ b/Space-Invaders/Header/Collectible/ICollectible.h @@ -0,0 +1,17 @@ +#pragma once +#include + +namespace Collectible +{ + class ICollectible + { + public: + virtual void onCollected() = 0; + virtual void initialize(sf::Vector2f position) = 0; + virtual void update() = 0; + virtual void render() = 0; + virtual sf::Vector2f getCollectiblePosition() = 0; + + virtual ~ICollectible() {}; + }; +}