From 281ea37fc6f5e958a725167784eee6fb78e864ae Mon Sep 17 00:00:00 2001 From: TheOne41799 Date: Mon, 10 Jun 2024 02:03:13 +0530 Subject: [PATCH 1/7] Created splash screen --- Space-Invaders/Header/Main/GameService.h | 2 +- .../SplashScreen/SplashScreenUIController.h | 35 ++++++++ Space-Invaders/Header/UI/UIService.h | 2 + Space-Invaders/Source/Main/GameService.cpp | 12 +-- .../SplashScreen/SplashScreenUIController.cpp | 83 +++++++++++++++++++ Space-Invaders/Source/UI/UIService.cpp | 9 +- Space-Invaders/Space-Invaders.vcxproj | 2 + Space-Invaders/Space-Invaders.vcxproj.filters | 6 ++ 8 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 Space-Invaders/Header/UI/SplashScreen/SplashScreenUIController.h create mode 100644 Space-Invaders/Source/UI/SplashScreen/SplashScreenUIController.cpp diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h index 466575ba2..94ca9933c 100644 --- a/Space-Invaders/Header/Main/GameService.h +++ b/Space-Invaders/Header/Main/GameService.h @@ -27,7 +27,7 @@ namespace Main void Initialize(); void InitializeVariables(); - void ShowMainMenu(); + void ShowSplashScreen(); public: GameService(); diff --git a/Space-Invaders/Header/UI/SplashScreen/SplashScreenUIController.h b/Space-Invaders/Header/UI/SplashScreen/SplashScreenUIController.h new file mode 100644 index 000000000..58814fe87 --- /dev/null +++ b/Space-Invaders/Header/UI/SplashScreen/SplashScreenUIController.h @@ -0,0 +1,35 @@ +#pragma once +#include "../../header/UI/Interface/IUIController.h" +#include "../../Header/UI/UIElement/ImageView.h" + + +namespace UI +{ + namespace SplashScreen + { + class SplashScreenUIController : public Interface::IUIController + { + private: + const float splashScreenDuration = 2.0f; + float elapsedDuration = 0.0f; + const float logoWidth = 600.f; + const float logoHeight = 134.f; + + UIElement::ImageView* outscalLogoView; + + void InitializeOutscalLogo(); + void UpdateTimer(); + void ShowMainMenu(); + sf::Vector2f GetLogoPosition(); + + public: + SplashScreenUIController(); + ~SplashScreenUIController(); + + void Initialize() override; + void Update() override; + void Render() override; + void Show() override; + }; + } +} diff --git a/Space-Invaders/Header/UI/UIService.h b/Space-Invaders/Header/UI/UIService.h index b497dcb9f..c2339c18c 100644 --- a/Space-Invaders/Header/UI/UIService.h +++ b/Space-Invaders/Header/UI/UIService.h @@ -1,6 +1,7 @@ #pragma once #include "MainMenu/MainMenuUIController.h" #include "GameplayUI/GameplayUIController.h" +#include "SplashScreen/SplashScreenUIController.h" #include "Interface/IUIController.h" @@ -11,6 +12,7 @@ namespace UI private: MainMenu::MainMenuUIController* mainMenuUIController; GameplayUI::GameplayUIController* gameplayUIController; + SplashScreen::SplashScreenUIController* splashScreenUIController; void CreateControllers(); void InitializeControllers(); diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp index 04d432db9..0959b8340 100644 --- a/Space-Invaders/Source/Main/GameService.cpp +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -1,6 +1,7 @@ #include "../../Header/Main/GameService.h" #include "../../Header/Graphics/GraphicService.h" #include "../../Header/Event/EventService.h" +#include "../../Header/UI/UIService.h" namespace Main @@ -18,14 +19,14 @@ namespace Main GameService::~GameService() { - //Destroy(); + } void GameService::Initialize() { serviceLocator->Initialize(); InitializeVariables(); - ShowMainMenu(); + ShowSplashScreen(); } void GameService::InitializeVariables() @@ -50,7 +51,7 @@ namespace Main gameWindow->clear(); serviceLocator->Render(); gameWindow->display(); - } + } bool GameService::IsRunning() { @@ -67,9 +68,10 @@ namespace Main return currentState; } - void GameService::ShowMainMenu() + void GameService::ShowSplashScreen() { - SetGameState(GameState::MAIN_MENU); + SetGameState(GameState::SPLASH_SCREEN); + ServiceLocator::GetInstance()->GetUIService()->ShowScreen(); } } diff --git a/Space-Invaders/Source/UI/SplashScreen/SplashScreenUIController.cpp b/Space-Invaders/Source/UI/SplashScreen/SplashScreenUIController.cpp new file mode 100644 index 000000000..85b96e89c --- /dev/null +++ b/Space-Invaders/Source/UI/SplashScreen/SplashScreenUIController.cpp @@ -0,0 +1,83 @@ +#include "../../Header/UI/SplashScreen/SplashScreenUIController.h" +#include "../../Header/Main/GameService.h" +#include "../../Header/Global/ServiceLocator.h" +#include "../../Header/Graphics/GraphicService.h" +#include "../../Header/Sound/SoundService.h" +#include "../../Header/Global/Config.h" + + +namespace UI +{ + namespace SplashScreen + { + using namespace Main; + using namespace Graphics; + using namespace Global; + using namespace UIElement; + using namespace Sound; + + + SplashScreenUIController::SplashScreenUIController() + { + outscalLogoView = new ImageView(); + } + + SplashScreenUIController::~SplashScreenUIController() + { + delete (outscalLogoView); + } + + void SplashScreenUIController::Initialize() + { + InitializeOutscalLogo(); + } + + void SplashScreenUIController::Update() + { + UpdateTimer(); + ShowMainMenu(); + } + + void SplashScreenUIController::Render() + { + outscalLogoView->Render(); + } + + void SplashScreenUIController::InitializeOutscalLogo() + { + sf::Vector2f position = GetLogoPosition(); + outscalLogoView->Initialize(Config::outscalLogoTexturePath, logoWidth, logoHeight, position); + + } + + void SplashScreenUIController::ShowMainMenu() + { + if (elapsedDuration >= splashScreenDuration) + { + ServiceLocator::GetInstance()->GetSoundService()->PlayBackgroundMusic(); + GameService::SetGameState(GameState::MAIN_MENU); + } + + } + + void SplashScreenUIController::UpdateTimer() + { + elapsedDuration += ServiceLocator::GetInstance()->GetTimeService()->GetDeltaTime(); + } + + sf::Vector2f SplashScreenUIController::GetLogoPosition() + { + sf::RenderWindow* game_window = ServiceLocator::GetInstance()->GetGraphicService()->GetGameWindow(); + + float xPosition = (game_window->getSize().x - logoWidth) / 2.0f; + float yPosition = (game_window->getSize().y - logoHeight) / 2.0f; + + return sf::Vector2f(xPosition, yPosition); + } + + void SplashScreenUIController::Show() + { + + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/UI/UIService.cpp b/Space-Invaders/Source/UI/UIService.cpp index fae116159..681572cb1 100644 --- a/Space-Invaders/Source/UI/UIService.cpp +++ b/Space-Invaders/Source/UI/UIService.cpp @@ -10,12 +10,14 @@ namespace UI using namespace UIElement; using namespace Interface; using namespace GameplayUI; + using namespace SplashScreen; UIService::UIService() { mainMenuUIController = nullptr; gameplayUIController = nullptr; + splashScreenUIController = nullptr; CreateControllers(); } @@ -23,6 +25,7 @@ namespace UI { mainMenuUIController = new MainMenuUIController(); gameplayUIController = new GameplayUIController(); + splashScreenUIController = new SplashScreenUIController(); } UIService::~UIService() @@ -67,16 +70,19 @@ namespace UI { mainMenuUIController->Initialize(); gameplayUIController->Initialize(); + splashScreenUIController->Initialize(); } IUIController* UIService::GetCurrentUIController() { switch (GameService::GetGameState()) { + case GameState::SPLASH_SCREEN: + return splashScreenUIController; case GameState::MAIN_MENU: return mainMenuUIController; case GameState::GAMEPLAY: - return gameplayUIController; + return gameplayUIController; default: return nullptr; } @@ -86,5 +92,6 @@ namespace UI { delete(mainMenuUIController); delete(gameplayUIController); + delete(splashScreenUIController); } } \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index af4ed4d33..4c8c62e14 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -180,6 +180,7 @@ + @@ -239,6 +240,7 @@ + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index d282ae164..e3d28c4e5 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -174,6 +174,9 @@ Source Files + + Source Files + @@ -347,5 +350,8 @@ Header Files + + Header Files + \ No newline at end of file From 47d80d797dba001710f2772178fcf23255583a8d Mon Sep 17 00:00:00 2001 From: TheOne41799 Date: Mon, 10 Jun 2024 02:18:17 +0530 Subject: [PATCH 2/7] Created animated image view ui element --- .../Header/UI/UIElement/AnimatedImageView.h | 59 ++++++++++ .../Source/UI/UIElement/AnimatedImageView.cpp | 109 ++++++++++++++++++ Space-Invaders/Space-Invaders.vcxproj | 2 + Space-Invaders/Space-Invaders.vcxproj.filters | 6 + 4 files changed, 176 insertions(+) create mode 100644 Space-Invaders/Header/UI/UIElement/AnimatedImageView.h create mode 100644 Space-Invaders/Source/UI/UIElement/AnimatedImageView.cpp diff --git a/Space-Invaders/Header/UI/UIElement/AnimatedImageView.h b/Space-Invaders/Header/UI/UIElement/AnimatedImageView.h new file mode 100644 index 000000000..a36793c67 --- /dev/null +++ b/Space-Invaders/Header/UI/UIElement/AnimatedImageView.h @@ -0,0 +1,59 @@ +#pragma once +#include "../../Header/UI/UIElement/ImageView.h" +#include + + +namespace UI +{ + namespace UIElement + { + enum class AnimationType + { + FADE_IN, + FADE_OUT, + }; + + + class AnimatedImageView : public ImageView + { + private: + using CallbackFunction = std::function; + + CallbackFunction callbackFunction = nullptr; + + void UpdateElapsedDuration(); + void HandleAnimationProgress(); + void UpdateAnimation(); + + protected: + const float defaultAnimationDuration = 2.0f; + + AnimationType animationType; + float animationDuration; + float elapsedDuration; + sf::Clock clock; + + virtual void Reset(); + + void SetAnimationDuration(float duration); + void SetAnimationType(AnimationType type); + + virtual void FadeIn(); + virtual void FadeOut(); + + public: + AnimatedImageView(); + virtual ~AnimatedImageView(); + + virtual void Initialize(sf::String texturePath, + float imageWidth, float imageHeight, + sf::Vector2f position) override; + virtual void Update() override; + virtual void Render() override; + + virtual void PlayAnimation(AnimationType type, float duration, CallbackFunction animationEndCallback); + + void RegisterCallbackFuntion(CallbackFunction animationEndCallback); + }; + } +} diff --git a/Space-Invaders/Source/UI/UIElement/AnimatedImageView.cpp b/Space-Invaders/Source/UI/UIElement/AnimatedImageView.cpp new file mode 100644 index 000000000..937f5e754 --- /dev/null +++ b/Space-Invaders/Source/UI/UIElement/AnimatedImageView.cpp @@ -0,0 +1,109 @@ +#include "../../Header/UI/UIElement/AnimatedImageView.h" + + +namespace UI +{ + namespace UIElement + { + AnimatedImageView::AnimatedImageView() = default; + + AnimatedImageView::~AnimatedImageView() = default; + + void AnimatedImageView::Initialize(sf::String texturePath, + float imageWidth, float imageHeight, + sf::Vector2f position) + { + ImageView::Initialize(texturePath, imageWidth, imageHeight, position); + } + + void AnimatedImageView::RegisterCallbackFuntion(CallbackFunction animationEndCallback) + { + callbackFunction = animationEndCallback; + } + + void AnimatedImageView::Update() + { + ImageView::Update(); + + if (uiState == UIState::VISIBLE) + { + UpdateElapsedDuration(); + HandleAnimationProgress(); + UpdateAnimation(); + } + } + + void AnimatedImageView::Render() + { + ImageView::Render(); + } + + void AnimatedImageView::PlayAnimation(AnimationType type, + float duration, + CallbackFunction animationEndCallback) + { + ImageView::Show(); + Reset(); + SetAnimationType(type); + SetAnimationDuration(duration); + RegisterCallbackFuntion(animationEndCallback); + } + + void AnimatedImageView::UpdateElapsedDuration() + { + float deltaTime = clock.restart().asSeconds(); + elapsedDuration += deltaTime; + } + + void AnimatedImageView::HandleAnimationProgress() + { + if (elapsedDuration >= animationDuration && callbackFunction) + { + callbackFunction(); + } + } + + void AnimatedImageView::UpdateAnimation() + { + switch (animationType) + { + case AnimationType::FADE_IN: + FadeIn(); + break; + case AnimationType::FADE_OUT: + FadeOut(); + break; + } + } + + void AnimatedImageView::SetAnimationDuration(float duration) + { + animationDuration = duration; + } + + void AnimatedImageView::SetAnimationType(AnimationType type) + { + animationType = type; + } + + void AnimatedImageView::FadeIn() + { + float alpha = std::min(1.0f, elapsedDuration / animationDuration); + imageSprite.setColor(sf::Color(255, 255, 255, static_cast(alpha * 255))); + } + + void AnimatedImageView::FadeOut() + { + float alpha = std::max(0.0f, 1.0f - (elapsedDuration / animationDuration)); + imageSprite.setColor(sf::Color(255, 255, 255, static_cast(alpha * 255))); + } + + void AnimatedImageView::Reset() + { + animationDuration = defaultAnimationDuration; + animationType = AnimationType::FADE_IN; + clock.restart(); + elapsedDuration = 0.0f; + } + } +} diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 4c8c62e14..d752a8794 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -181,6 +181,7 @@ + @@ -241,6 +242,7 @@ + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index e3d28c4e5..30b8c9986 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -177,6 +177,9 @@ Source Files + + Source Files + @@ -353,5 +356,8 @@ Header Files + + Header Files + \ No newline at end of file From bfa835a4b9f74d62c9082687e018e7c2e1111444 Mon Sep 17 00:00:00 2001 From: TheOne41799 Date: Mon, 10 Jun 2024 02:38:42 +0530 Subject: [PATCH 3/7] Created fade in and fade out effects on splash screen --- .../SplashScreen/SplashScreenUIController.h | 13 +++++----- .../SplashScreen/SplashScreenUIController.cpp | 25 +++++++++---------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Space-Invaders/Header/UI/SplashScreen/SplashScreenUIController.h b/Space-Invaders/Header/UI/SplashScreen/SplashScreenUIController.h index 58814fe87..af5b7c1f8 100644 --- a/Space-Invaders/Header/UI/SplashScreen/SplashScreenUIController.h +++ b/Space-Invaders/Header/UI/SplashScreen/SplashScreenUIController.h @@ -1,6 +1,6 @@ #pragma once -#include "../../header/UI/Interface/IUIController.h" -#include "../../Header/UI/UIElement/ImageView.h" +#include "../../Header/UI/Interface/IUIController.h" +#include "../../Header/UI/UIElement/AnimatedImageView.h" namespace UI @@ -10,16 +10,15 @@ namespace UI class SplashScreenUIController : public Interface::IUIController { private: - const float splashScreenDuration = 2.0f; - float elapsedDuration = 0.0f; + const float logoAnimationDuration = 2.0f; const float logoWidth = 600.f; const float logoHeight = 134.f; - UIElement::ImageView* outscalLogoView; + UIElement::AnimatedImageView* outscalLogoView; void InitializeOutscalLogo(); - void UpdateTimer(); - void ShowMainMenu(); + void FadeInAnimationCallback(); + void FadeOutAnimationCallback(); sf::Vector2f GetLogoPosition(); public: diff --git a/Space-Invaders/Source/UI/SplashScreen/SplashScreenUIController.cpp b/Space-Invaders/Source/UI/SplashScreen/SplashScreenUIController.cpp index 85b96e89c..81813d369 100644 --- a/Space-Invaders/Source/UI/SplashScreen/SplashScreenUIController.cpp +++ b/Space-Invaders/Source/UI/SplashScreen/SplashScreenUIController.cpp @@ -19,7 +19,7 @@ namespace UI SplashScreenUIController::SplashScreenUIController() { - outscalLogoView = new ImageView(); + outscalLogoView = new AnimatedImageView(); } SplashScreenUIController::~SplashScreenUIController() @@ -34,8 +34,7 @@ namespace UI void SplashScreenUIController::Update() { - UpdateTimer(); - ShowMainMenu(); + outscalLogoView->Update(); } void SplashScreenUIController::Render() @@ -50,19 +49,17 @@ namespace UI } - void SplashScreenUIController::ShowMainMenu() + void SplashScreenUIController::FadeInAnimationCallback() { - if (elapsedDuration >= splashScreenDuration) - { - ServiceLocator::GetInstance()->GetSoundService()->PlayBackgroundMusic(); - GameService::SetGameState(GameState::MAIN_MENU); - } - + outscalLogoView->PlayAnimation(AnimationType::FADE_OUT, + logoAnimationDuration, + std::bind(&SplashScreenUIController::FadeOutAnimationCallback, this)); } - void SplashScreenUIController::UpdateTimer() + void SplashScreenUIController::FadeOutAnimationCallback() { - elapsedDuration += ServiceLocator::GetInstance()->GetTimeService()->GetDeltaTime(); + ServiceLocator::GetInstance()->GetSoundService()->PlayBackgroundMusic(); + GameService::SetGameState(GameState::MAIN_MENU); } sf::Vector2f SplashScreenUIController::GetLogoPosition() @@ -77,7 +74,9 @@ namespace UI void SplashScreenUIController::Show() { - + outscalLogoView->PlayAnimation(AnimationType::FADE_IN, + logoAnimationDuration, + std::bind(&SplashScreenUIController::FadeInAnimationCallback, this)); } } } \ No newline at end of file From e11d1a5e9d36e7a534739bf3754630bd2f358f86 Mon Sep 17 00:00:00 2001 From: TheOne41799 Date: Mon, 10 Jun 2024 03:54:31 +0530 Subject: [PATCH 4/7] Created data for animations --- .../AnimationSystem/AnimationSystemConfig.h | 32 +++++++++++++++++++ .../AnimationSystemConfigData.h | 10 ++++++ 2 files changed, 42 insertions(+) create mode 100644 Space-Invaders/Header/AnimationSystem/AnimationSystemConfig.h create mode 100644 Space-Invaders/Header/AnimationSystem/AnimationSystemConfigData.h diff --git a/Space-Invaders/Header/AnimationSystem/AnimationSystemConfig.h b/Space-Invaders/Header/AnimationSystem/AnimationSystemConfig.h new file mode 100644 index 000000000..99106aaf5 --- /dev/null +++ b/Space-Invaders/Header/AnimationSystem/AnimationSystemConfig.h @@ -0,0 +1,32 @@ +#pragma once +#include + + +namespace Animation +{ + struct AnimationSystemConfig + { + sf::String animationTexturePath; + + float spriteSheetWidth; + float spriteSheetHeight; + + float tileWidth; + float tileHeight; + + int numberOfAnimationFrames; + float frameDuration; + + AnimationSystemConfig() = default; + + AnimationSystemConfig(sf::String texturePath, float spriteWidth, float spriteHeight, + float tWidth, float tHeight, int frames, float duration) : + animationTexturePath(texturePath), + spriteSheetWidth(spriteWidth), + spriteSheetHeight(spriteHeight), + tileWidth(tWidth), + tileHeight(tHeight), + numberOfAnimationFrames(frames), + frameDuration(duration) {} + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/AnimationSystem/AnimationSystemConfigData.h b/Space-Invaders/Header/AnimationSystem/AnimationSystemConfigData.h new file mode 100644 index 000000000..079540f36 --- /dev/null +++ b/Space-Invaders/Header/AnimationSystem/AnimationSystemConfigData.h @@ -0,0 +1,10 @@ +#pragma once +#include "AnimationSystemConfig.h" +#include "../../Header/Global/Config.h" + + +namespace Animation +{ + const AnimationSystemConfig explosionAnimationConfig(Global::Config::explosionTexturePath, + 70.0f, 80.0f, 14.28f, 20.0f, 7, 0.03f); +} From d84b89c0dda4eb56375ba3dcf1abafcbe9a5a12d Mon Sep 17 00:00:00 2001 From: TheOne41799 Date: Mon, 10 Jun 2024 04:12:34 +0530 Subject: [PATCH 5/7] Created animation system --- .../Header/AnimationSystem/AnimationSystem.h | 35 ++++++++ .../Header/UI/UIElement/ImageView.h | 2 + .../AnimationSystem/AnimationSystem.cpp | 81 +++++++++++++++++++ .../Source/UI/UIElement/ImageView.cpp | 13 +++ Space-Invaders/Space-Invaders.vcxproj | 3 + Space-Invaders/Space-Invaders.vcxproj.filters | 9 +++ 6 files changed, 143 insertions(+) create mode 100644 Space-Invaders/Header/AnimationSystem/AnimationSystem.h create mode 100644 Space-Invaders/Source/AnimationSystem/AnimationSystem.cpp diff --git a/Space-Invaders/Header/AnimationSystem/AnimationSystem.h b/Space-Invaders/Header/AnimationSystem/AnimationSystem.h new file mode 100644 index 000000000..e6512e1e2 --- /dev/null +++ b/Space-Invaders/Header/AnimationSystem/AnimationSystem.h @@ -0,0 +1,35 @@ +#pragma once +#include +#include "../../Header/AnimationSystem/AnimationSystemConfig.h" +#include "../../Header/UI/UIElement/ImageView.h" + + +namespace Animation +{ + class AnimationSystem + { + private: + AnimationSystemConfig animationSystemConfig; + + sf::Vector2f animationPosition; + UI::UIElement::ImageView* animationImage; + + void CreateUIElements(); + void InitializeImage(); + + int currentFrame; + sf::Clock clock; + sf::Time frameTime; + + public: + AnimationSystem(AnimationSystemConfig config); + ~AnimationSystem(); + + void Initialize(sf::Vector2f position); + void Update(); + void Render(); + + void Destroy(); + }; +} + diff --git a/Space-Invaders/Header/UI/UIElement/ImageView.h b/Space-Invaders/Header/UI/UIElement/ImageView.h index e6bf6e89a..6e53dbc54 100644 --- a/Space-Invaders/Header/UI/UIElement/ImageView.h +++ b/Space-Invaders/Header/UI/UIElement/ImageView.h @@ -22,7 +22,9 @@ namespace UI virtual void Render() override; virtual void SetTexture(sf::String texturePath); + virtual void SetTextureRect(sf::IntRect textureRect); virtual void SetScale(float width, float height); + virtual void SetScale(float width, float height, float tileWidth, float tileHeight); virtual void SetPosition(sf::Vector2f position); virtual void SetRotation(float rotationAngle); virtual void SetOriginAtCentre(); diff --git a/Space-Invaders/Source/AnimationSystem/AnimationSystem.cpp b/Space-Invaders/Source/AnimationSystem/AnimationSystem.cpp new file mode 100644 index 000000000..7a3ca8712 --- /dev/null +++ b/Space-Invaders/Source/AnimationSystem/AnimationSystem.cpp @@ -0,0 +1,81 @@ +#include "../../Header/AnimationSystem/AnimationSystem.h" +#include "../../Header/Global/Config.h" +#include "../../Header/Global/ServiceLocator.h" +//#include "../../Header/AnimationSystem/AnimationService.h" + + +namespace Animation +{ + using namespace Global; + using namespace UI::UIElement; + + + AnimationSystem::AnimationSystem(AnimationSystemConfig config) + { + animationSystemConfig = config; + CreateUIElements(); + } + + AnimationSystem::~AnimationSystem() + { + delete(animationImage); + } + + void AnimationSystem::Initialize(sf::Vector2f position) + { + animationPosition = position; + currentFrame = 0; + frameTime = sf::seconds(animationSystemConfig.frameDuration); + + InitializeImage(); + } + + void AnimationSystem::CreateUIElements() + { + animationImage = new ImageView(); + } + + void AnimationSystem::InitializeImage() + { + animationImage->Initialize(Config::explosionTexturePath, 0, 0, animationPosition); + + animationImage->SetTextureRect(sf::IntRect(0, 0, + animationSystemConfig.tileWidth, animationSystemConfig.tileHeight)); + + animationImage->SetScale(animationSystemConfig.spriteSheetWidth, + animationSystemConfig.spriteSheetHeight, + animationSystemConfig.tileWidth, + animationSystemConfig.tileHeight); + } + + void AnimationSystem::Update() + { + if (clock.getElapsedTime() >= frameTime) + { + if (currentFrame + 1 >= animationSystemConfig.numberOfAnimationFrames) + { + Destroy(); + } + + currentFrame = (currentFrame + 1) % animationSystemConfig.numberOfAnimationFrames; + clock.restart(); + + animationImage->SetTextureRect(sf::IntRect(currentFrame * animationSystemConfig.tileWidth, + 0, + animationSystemConfig.tileWidth, + animationSystemConfig.tileHeight)); + } + + animationImage->Update(); + } + + void AnimationSystem::Render() + { + animationImage->Render(); + } + + void AnimationSystem::Destroy() + { + //ServiceLocator::GetInstance()->GetAnimationService()->DestroyAnimationSystem(this); + } +} diff --git a/Space-Invaders/Source/UI/UIElement/ImageView.cpp b/Space-Invaders/Source/UI/UIElement/ImageView.cpp index abc9b6a01..dd7d09291 100644 --- a/Space-Invaders/Source/UI/UIElement/ImageView.cpp +++ b/Space-Invaders/Source/UI/UIElement/ImageView.cpp @@ -41,6 +41,11 @@ namespace UI } } + void ImageView::SetTextureRect(sf::IntRect textureRect) + { + imageSprite.setTextureRect(textureRect); + } + void ImageView::SetScale(float width, float height) { float scale_x = width / imageSprite.getTexture()->getSize().x; @@ -49,6 +54,14 @@ namespace UI imageSprite.setScale(scale_x, scale_y); } + void ImageView::SetScale(float width, float height, float tileWidth, float tileHeight) + { + float scaleX = width / tileWidth; + float scaleY = height / tileHeight; + + imageSprite.setScale(scaleX, scaleY); + } + void ImageView::SetPosition(sf::Vector2f position) { imageSprite.setPosition(position); diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index d752a8794..f944fe11c 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -189,6 +189,9 @@ + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 30b8c9986..97e4150fd 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -359,5 +359,14 @@ Header Files + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file From b7fe01d4e226921151f2f04048ed16876c545723 Mon Sep 17 00:00:00 2001 From: TheOne41799 Date: Mon, 10 Jun 2024 04:35:55 +0530 Subject: [PATCH 6/7] Created animation service --- .../Header/AnimationSystem/AnimationService.h | 38 ++++++++++ Space-Invaders/Header/Global/ServiceLocator.h | 3 + .../AnimationSystem/AnimationService.cpp | 76 +++++++++++++++++++ .../AnimationSystem/AnimationSystem.cpp | 4 +- Space-Invaders/Source/Global/Config.cpp | 3 + .../Source/Global/ServiceLocator.cpp | 24 ++++-- Space-Invaders/Space-Invaders.vcxproj | 3 + Space-Invaders/Space-Invaders.vcxproj.filters | 9 +++ 8 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 Space-Invaders/Header/AnimationSystem/AnimationService.h create mode 100644 Space-Invaders/Source/AnimationSystem/AnimationService.cpp diff --git a/Space-Invaders/Header/AnimationSystem/AnimationService.h b/Space-Invaders/Header/AnimationSystem/AnimationService.h new file mode 100644 index 000000000..144ec1a2d --- /dev/null +++ b/Space-Invaders/Header/AnimationSystem/AnimationService.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include +#include "../../Header/AnimationSystem/AnimationSystem.h" + + +namespace Animation +{ + enum class AnimationType + { + EXPLOSION, + }; + + + class AnimationService + { + private: + std::vector animationSystemList; + std::vector flaggedAnimationSystemList; + + AnimationSystemConfig GetAnimationSystemConfig(AnimationType animationType); + void DestroyFlaggedAnimationSystem(); + void Destroy(); + + public: + AnimationService(); + virtual ~AnimationService(); + + void Initialize(); + void Update(); + void Render(); + + void Reset(); + + void SpawnAnimationSystem(sf::Vector2f position, AnimationType animationType); + void DestroyAnimationSystem(AnimationSystem* animationSystem); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index daab3bb89..fd9abba2b 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -11,6 +11,7 @@ #include "../Powerups/PowerupService.h" #include "../Sound/SoundService.h" #include "../Collision/CollisionService.h" +#include "../AnimationSystem/AnimationService.h" namespace Global @@ -31,6 +32,7 @@ namespace Global Powerup::PowerupService* powerupService; Sound::SoundService* soundService; Collision::CollisionService* collisionService; + Animation::AnimationService* animationService; ServiceLocator(); ~ServiceLocator(); @@ -57,5 +59,6 @@ namespace Global Powerup::PowerupService* GetPowerupService(); Sound::SoundService* GetSoundService(); Collision::CollisionService* GetCollisionService(); + Animation::AnimationService* GetAnimationService(); }; } diff --git a/Space-Invaders/Source/AnimationSystem/AnimationService.cpp b/Space-Invaders/Source/AnimationSystem/AnimationService.cpp new file mode 100644 index 000000000..c7cffa036 --- /dev/null +++ b/Space-Invaders/Source/AnimationSystem/AnimationService.cpp @@ -0,0 +1,76 @@ +#include "../../Header/AnimationSystem/AnimationService.h" +#include "../../Header/AnimationSystem/AnimationSystemConfigData.h" + + +namespace Animation +{ + AnimationService::AnimationService() { } + + AnimationService::~AnimationService() + { + Destroy(); + } + + void AnimationService::Initialize() { } + + void AnimationService::Update() + { + for (AnimationSystem* animationSystem : animationSystemList) + { + animationSystem->Update(); + } + + DestroyFlaggedAnimationSystem(); + } + + void AnimationService::Render() + { + for (AnimationSystem* animationSystem : animationSystemList) + { + animationSystem->Render(); + } + } + + void AnimationService::SpawnAnimationSystem(sf::Vector2f position, AnimationType animationType) + { + AnimationSystem* animationSystem = new AnimationSystem(GetAnimationSystemConfig(animationType)); + animationSystem->Initialize(position); + animationSystemList.push_back(animationSystem); + } + + void AnimationService::DestroyAnimationSystem(AnimationSystem* animationSystem) + { + flaggedAnimationSystemList.push_back(animationSystem); + animationSystemList.erase(std::remove(animationSystemList.begin(), animationSystemList.end(), + animationSystem), animationSystemList.end()); + } + + AnimationSystemConfig AnimationService::GetAnimationSystemConfig(AnimationType animationType) + { + switch (animationType) + { + case Animation::AnimationType::EXPLOSION: + return explosionAnimationConfig; + } + } + + void AnimationService::DestroyFlaggedAnimationSystem() + { + for (AnimationSystem* particleSystem : flaggedAnimationSystemList) + { + delete (particleSystem); + } + + flaggedAnimationSystemList.clear(); + } + + void AnimationService::Reset() { Destroy(); } + + void AnimationService::Destroy() + { + for (AnimationSystem* animationSystem : animationSystemList) + { + delete (animationSystem); + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/AnimationSystem/AnimationSystem.cpp b/Space-Invaders/Source/AnimationSystem/AnimationSystem.cpp index 7a3ca8712..1b21e53c8 100644 --- a/Space-Invaders/Source/AnimationSystem/AnimationSystem.cpp +++ b/Space-Invaders/Source/AnimationSystem/AnimationSystem.cpp @@ -1,7 +1,7 @@ #include "../../Header/AnimationSystem/AnimationSystem.h" #include "../../Header/Global/Config.h" #include "../../Header/Global/ServiceLocator.h" -//#include "../../Header/AnimationSystem/AnimationService.h" +#include "../../Header/AnimationSystem/AnimationService.h" namespace Animation @@ -76,6 +76,6 @@ namespace Animation void AnimationSystem::Destroy() { - //ServiceLocator::GetInstance()->GetAnimationService()->DestroyAnimationSystem(this); + ServiceLocator::GetInstance()->GetAnimationService()->DestroyAnimationSystem(this); } } diff --git a/Space-Invaders/Source/Global/Config.cpp b/Space-Invaders/Source/Global/Config.cpp index 0493b1113..dbf841748 100644 --- a/Space-Invaders/Source/Global/Config.cpp +++ b/Space-Invaders/Source/Global/Config.cpp @@ -21,6 +21,9 @@ namespace Global const sf::String Config::bunkerTexturePath = "assets/textures/bunker.png"; + const sf::String Config::explosionTexturePath = "assets/textures/explosion.png"; + + const sf::String Config::shieldTexturePath = "assets/textures/shield.png"; const sf::String Config::trippleLaserTexturePath = "assets/textures/tripple_laser.png"; diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index a148195f1..5a6bc8d4d 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -17,6 +17,7 @@ namespace Global using namespace Bullet; using namespace Powerup; using namespace Collision; + using namespace Animation; ServiceLocator::ServiceLocator() @@ -33,6 +34,7 @@ namespace Global bulletService = nullptr; powerupService = nullptr; collisionService = nullptr; + animationService = nullptr; CreateServices(); } @@ -56,6 +58,7 @@ namespace Global powerupService = new PowerupService(); collisionService = new CollisionService(); soundService = new SoundService(); + animationService = new AnimationService(); } void ServiceLocator::ClearAllServices() @@ -72,6 +75,7 @@ namespace Global delete(bulletService); delete(powerupService); delete(collisionService); + delete(animationService); } ServiceLocator* ServiceLocator::GetInstance() @@ -94,6 +98,7 @@ namespace Global bulletService->Initialize(); powerupService->Initialize(); collisionService->Initialize(); + animationService->Initialize(); } void ServiceLocator::Update() @@ -111,6 +116,7 @@ namespace Global bulletService->Update(); powerupService->Update(); collisionService->Update(); + animationService->Update(); } uiService->Update(); @@ -128,6 +134,7 @@ namespace Global elementService->Render(); bulletService->Render(); powerupService->Render(); + animationService->Render(); } uiService->Render(); @@ -153,27 +160,27 @@ namespace Global return timeService; } - UI::UIService* ServiceLocator::GetUIService() + UIService* ServiceLocator::GetUIService() { return uiService; } - Enemy::EnemyService* ServiceLocator::GetEnemyService() + EnemyService* ServiceLocator::GetEnemyService() { return enemyService; } - Gameplay::GameplayService* ServiceLocator::GetGameplayService() + GameplayService* ServiceLocator::GetGameplayService() { return gameplayService; } - Elements::ElementService* ServiceLocator::GetElementService() + ElementService* ServiceLocator::GetElementService() { return elementService; } - Sound::SoundService* ServiceLocator::GetSoundService() + SoundService* ServiceLocator::GetSoundService() { return soundService; } @@ -188,10 +195,15 @@ namespace Global return powerupService; } - Collision::CollisionService* ServiceLocator::GetCollisionService() + CollisionService* ServiceLocator::GetCollisionService() { return collisionService; } + + AnimationService* ServiceLocator::GetAnimationService() + { + return animationService; + } } diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index f944fe11c..8a08e1244 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -133,6 +133,8 @@ + + @@ -189,6 +191,7 @@ + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 97e4150fd..855810744 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -180,6 +180,12 @@ Source Files + + Source Files + + + Source Files + @@ -368,5 +374,8 @@ Header Files + + Header Files + \ No newline at end of file From b429c6828a5788ab0ada7626327b36ee3746e779 Mon Sep 17 00:00:00 2001 From: TheOne41799 Date: Mon, 10 Jun 2024 04:57:23 +0530 Subject: [PATCH 7/7] Created explosions --- Space-Invaders/Header/Global/Config.h | 1 + Space-Invaders/Header/Sound/SoundService.h | 3 +++ Space-Invaders/Source/Enemy/EnemyController.cpp | 5 +++++ Space-Invaders/Source/Global/Config.cpp | 2 ++ Space-Invaders/Source/Sound/SoundService.cpp | 16 ++++++++++++---- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Space-Invaders/Header/Global/Config.h b/Space-Invaders/Header/Global/Config.h index cbb12dd9f..33ecab50e 100644 --- a/Space-Invaders/Header/Global/Config.h +++ b/Space-Invaders/Header/Global/Config.h @@ -36,5 +36,6 @@ namespace Global static const sf::String bulletFireSoundPath; static const sf::String powerupEnabledSoundPath; static const sf::String powerupDisabledSoundPath; + static const sf::String explosionSoundPath; }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Sound/SoundService.h b/Space-Invaders/Header/Sound/SoundService.h index ef5d40b13..5f1fa6c29 100644 --- a/Space-Invaders/Header/Sound/SoundService.h +++ b/Space-Invaders/Header/Sound/SoundService.h @@ -23,6 +23,7 @@ namespace Sound sf::Sound soundEffect; sf::Sound powerupSoundEffect; + sf::Sound explosionSoundEffect; sf::SoundBuffer bufferButtonClick; sf::SoundBuffer bufferBulletFire; @@ -30,6 +31,8 @@ namespace Sound sf::SoundBuffer bufferPowerupEnabled; sf::SoundBuffer bufferPowerupDisabled; + sf::SoundBuffer bufferExplosionSound; + void LoadBackgroundMusicFromFile(); void LoadSoundFromFile(); diff --git a/Space-Invaders/Source/Enemy/EnemyController.cpp b/Space-Invaders/Source/Enemy/EnemyController.cpp index 61cac2f6d..284c1fa04 100644 --- a/Space-Invaders/Source/Enemy/EnemyController.cpp +++ b/Space-Invaders/Source/Enemy/EnemyController.cpp @@ -132,6 +132,11 @@ namespace Enemy void EnemyController::Destroy() { + ServiceLocator::GetInstance()->GetAnimationService() + ->SpawnAnimationSystem(enemyModel->GetEnemyPosition(), Animation::AnimationType::EXPLOSION); + + ServiceLocator::GetInstance()->GetSoundService()->PlaySound(SoundType::EXPLOSION); + ServiceLocator::GetInstance()->GetPlayerService()->IncreaseEnemiesKilled(1); ServiceLocator::GetInstance()->GetEnemyService()->DestroyEnemy(this); } diff --git a/Space-Invaders/Source/Global/Config.cpp b/Space-Invaders/Source/Global/Config.cpp index dbf841748..3f1f321b6 100644 --- a/Space-Invaders/Source/Global/Config.cpp +++ b/Space-Invaders/Source/Global/Config.cpp @@ -56,4 +56,6 @@ namespace Global const sf::String Config::powerupEnabledSoundPath = "assets/sounds/powerup_enabled.ogg"; const sf::String Config::powerupDisabledSoundPath = "assets/sounds/powerup_disabled.ogg"; + + const sf::String Config::explosionSoundPath = "assets/sounds/explosion.wav"; } diff --git a/Space-Invaders/Source/Sound/SoundService.cpp b/Space-Invaders/Source/Sound/SoundService.cpp index 4c6175acb..1a8712540 100644 --- a/Space-Invaders/Source/Sound/SoundService.cpp +++ b/Space-Invaders/Source/Sound/SoundService.cpp @@ -25,19 +25,23 @@ namespace Sound { if (!bufferButtonClick.loadFromFile(Config::buttonClickSoundPath)) { - printf("Error loading background music file"); + printf("Error loading sound file"); } if (!bufferBulletFire.loadFromFile(Config::bulletFireSoundPath)) { - printf("Error loading background music file"); + printf("Error loading sound file"); } if (!bufferPowerupEnabled.loadFromFile(Config::powerupEnabledSoundPath)) { - printf("Error loading background music file"); + printf("Error loading sound file"); } if (!bufferPowerupDisabled.loadFromFile(Config::powerupDisabledSoundPath)) { - printf("Error loading background music file"); + printf("Error loading sound file"); + } + if (!bufferExplosionSound.loadFromFile(Config::explosionSoundPath)) + { + printf("Error loading sound file"); } } @@ -60,6 +64,10 @@ namespace Sound powerupSoundEffect.setBuffer(bufferPowerupDisabled); powerupSoundEffect.play(); break; + case SoundType::EXPLOSION: + explosionSoundEffect.setBuffer(bufferExplosionSound); + explosionSoundEffect.play(); + break; default: printf("Invalid sound type"); return;