diff --git a/Space-Invaders/HeaderFiles/Bullet/BulletConfig.h b/Space-Invaders/HeaderFiles/Bullet/BulletConfig.h new file mode 100644 index 000000000..0cfd35a2f --- /dev/null +++ b/Space-Invaders/HeaderFiles/Bullet/BulletConfig.h @@ -0,0 +1,18 @@ +#pragma once + +namespace Bullet +{ + + enum class MovementDirection + { + UP, + DOWN + }; + + enum class BulletType + { + LASERBULLET, + FROSTBULLET, + TORPEDO + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Bullet/BulletController.h b/Space-Invaders/HeaderFiles/Bullet/BulletController.h new file mode 100644 index 000000000..4ab756a56 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Bullet/BulletController.h @@ -0,0 +1,42 @@ +#pragma once + +#include "../../HeaderFiles/Bullet/BulletConfig.h" +#include "../../HeaderFiles/Projectiles/IProjectile.h" + +namespace Bullet +{ + + enum class BulletType; + class BulletModel; + class BulletView; + + class BulletController : public Projectile::IProjectile + { + protected: + + BulletModel* bulletModel; + BulletView * bulletView; + + 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(); + BulletType getBulletType(); + + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Bullet/BulletModel.h b/Space-Invaders/HeaderFiles/Bullet/BulletModel.h new file mode 100644 index 000000000..a9d8761d2 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Bullet/BulletModel.h @@ -0,0 +1,40 @@ +#pragma once +#include + +namespace Bullet +{ + + enum class BulletType; + enum class MovementDirection; + + class BulletModel + { + private: + + float movementSpeed = 300.0f; + sf::Vector2f bulletPosition; + + BulletType bulletType; + MovementDirection movementDirection; + + public: + + BulletModel(BulletType type); + ~BulletModel(); + + void initialize(sf::Vector2f position, MovementDirection direction); + void update(); + void destroy(); + void render(); + + + MovementDirection getMovementDirection(); + void setMovementDirection(MovementDirection direction); + + BulletType getBulletType(); + void setBulletType(BulletType type); + + float getMovementSpeed(); + void setMovementSpeed(float speed); + }; +} diff --git a/Space-Invaders/HeaderFiles/Bullet/BulletService.h b/Space-Invaders/HeaderFiles/Bullet/BulletService.h new file mode 100644 index 000000000..6fdec71f2 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Bullet/BulletService.h @@ -0,0 +1,11 @@ +#pragma once + +class BulletService +{ +private: + + void initialize(); + void update(); + void render(); + void destroy(); +}; diff --git a/Space-Invaders/HeaderFiles/Bullet/BulletView.h b/Space-Invaders/HeaderFiles/Bullet/BulletView.h new file mode 100644 index 000000000..c285611ad --- /dev/null +++ b/Space-Invaders/HeaderFiles/Bullet/BulletView.h @@ -0,0 +1,45 @@ +#pragma once +#include + + + +namespace Bullet +{ + enum class BulletType; + class BulletController; + + class BulletView + { + + private: + + sf::Sprite bulletSprite; + sf::Texture bulletTexture; + + sf::RenderWindow* window; + + const float bulletSpriteHeight = 18.0f; + const float bulletSpriteWidth = 18.0f; + + BulletController* bulletController; + + void initializeBulletSprite(BulletType type); + void scaleBulletSprite(); + + public: + + BulletView(); + ~BulletView(); + + void initialize(BulletController* controller); + + void update(); + + void render(); + + + + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Bullet/Controllers/FrostBullet.h b/Space-Invaders/HeaderFiles/Bullet/Controllers/FrostBullet.h new file mode 100644 index 000000000..b79f13c65 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Bullet/Controllers/FrostBullet.h @@ -0,0 +1,23 @@ +#pragma once +#include "../../HeaderFiles/Bullet/BulletController.h" + +namespace Bullet +{ + namespace BulletControllers + { + class FrostBullet : public BulletController + { + private: + + const float frostbulletMovementSpeed = 500.0f; + + public: + + FrostBullet(BulletType type); + + ~FrostBullet(); + + void initialize(sf::Vector2f position, MovementDirection direction); + }; + } +} diff --git a/Space-Invaders/HeaderFiles/Bullet/Controllers/LaserBullet.h b/Space-Invaders/HeaderFiles/Bullet/Controllers/LaserBullet.h new file mode 100644 index 000000000..7a10ec83a --- /dev/null +++ b/Space-Invaders/HeaderFiles/Bullet/Controllers/LaserBullet.h @@ -0,0 +1,24 @@ +#pragma once +#include +#include "../../HeaderFiles/Bullet/BulletController.h" + +namespace Bullet +{ + namespace BulletControllers + { + + class LaserBullet: public BulletController + { + private: + + + public: + + LaserBullet(BulletType type); + ~LaserBullet(); + + void initialize(sf::Vector2f position, MovementDirection direction) override; + + }; + } +} diff --git a/Space-Invaders/HeaderFiles/Bullet/Controllers/TorpedoController.h b/Space-Invaders/HeaderFiles/Bullet/Controllers/TorpedoController.h new file mode 100644 index 000000000..0dcf41c02 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Bullet/Controllers/TorpedoController.h @@ -0,0 +1,22 @@ +#pragma once +#include "../../HeaderFiles/Bullet/BulletController.h" + +namespace Bullet +{ + namespace BulletControllers + { + class Torpedo : public BulletController + { + private: + + const float torpedoMovementSpeed = 200.0f; + + public: + + void initialize(sf::Vector2f position, MovementDirection direction) override; + + Torpedo(BulletType type); + ~Torpedo(); + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Element/Bunker/BunkerController.h b/Space-Invaders/HeaderFiles/Element/Bunker/BunkerController.h new file mode 100644 index 000000000..eac714581 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Element/Bunker/BunkerController.h @@ -0,0 +1,34 @@ +#pragma once +#include +#include "../../HeaderFiles/Element/Bunker/BunkerModel.h" + +namespace Element +{ + + namespace Bunker + { + class BunkerView; + +class BunkerController + +{ + private: + + BunkerData bunkerData; + BunkerView* bunkerView; + + public: + + BunkerController(); + ~BunkerController(); + + void update(); + void initialize(BunkerData data); + void render(); + + sf::Vector2f getBunkerPosition(); + + +}; + } +} diff --git a/Space-Invaders/HeaderFiles/Element/Bunker/BunkerModel.h b/Space-Invaders/HeaderFiles/Element/Bunker/BunkerModel.h new file mode 100644 index 000000000..276db40c3 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Element/Bunker/BunkerModel.h @@ -0,0 +1,21 @@ +#pragma once +#include + +namespace Element +{ + namespace Bunker + { + + + + + struct BunkerData + { + + sf::Vector2f position; + BunkerData(sf::Vector2f position); + BunkerData(); + }; + } +} + diff --git a/Space-Invaders/HeaderFiles/Element/Bunker/BunkerView.h b/Space-Invaders/HeaderFiles/Element/Bunker/BunkerView.h new file mode 100644 index 000000000..58f978acc --- /dev/null +++ b/Space-Invaders/HeaderFiles/Element/Bunker/BunkerView.h @@ -0,0 +1,42 @@ +#pragma once +#include +#include "../../HeaderFiles/Element/Bunker/BunkerModel.h" + + +namespace Element +{ + + namespace Bunker + { + class BunkerController; + + class BunkerView + { + private: + + const float bunkerSpriteWidth = 80.0f; + const float bunkerSpriteHeight = 80.0f; + + const sf::String bunker_texture_path = "assets/textures/bunker.png"; + + BunkerController* bunkerController; + sf::RenderWindow *gameWindow; + + void initializeBunkerSprite(); + void scaleBunkerSprite(); + + sf::Sprite bunkerSprite; + sf::Texture bunkerTexture; + + + public: + BunkerView(); + ~BunkerView(); + + void initialize(BunkerController * controller); + void update(); + void render(); + }; + } +} + diff --git a/Space-Invaders/HeaderFiles/Element/ElementService.h b/Space-Invaders/HeaderFiles/Element/ElementService.h new file mode 100644 index 000000000..2fcef9ade --- /dev/null +++ b/Space-Invaders/HeaderFiles/Element/ElementService.h @@ -0,0 +1,32 @@ +#pragma once +#include +#include +#include "../../HeaderFiles/Element/Bunker/BunkerController.h" +#include"../../HeaderFiles/Element/Bunker/BunkerModel.h" +namespace Element +{ + class ElementService + { + private: + + const std::vector bunkerDataList = { + Bunker::BunkerData(sf::Vector2f(130.0f,800.0f)), + Bunker::BunkerData(sf::Vector2f(430.0f,800.0f)), + 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 bunkerList; + + void destroy(); + + public: + ElementService(); + ~ElementService(); + void initialize(); + void update(); + void render(); + + }; +} diff --git a/Space-Invaders/HeaderFiles/Enemy/Controllers/EnemyConfig.h b/Space-Invaders/HeaderFiles/Enemy/Controllers/EnemyConfig.h new file mode 100644 index 000000000..7c4df6ee8 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Enemy/Controllers/EnemyConfig.h @@ -0,0 +1,28 @@ +#pragma once + +namespace Enemy +{ + enum class EnemyState + { + ALIVE, + DEAD, + PATROLLING + }; + + enum class EnemyType + { + ZAPPER, + SUBZERO, + //UFO, + THUNDERSNAKE + }; + + enum class MovementDirection + { + DOWN, + UP, + RIGHT, + LEFT + }; + +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Enemy/Controllers/SubZero.h b/Space-Invaders/HeaderFiles/Enemy/Controllers/SubZero.h new file mode 100644 index 000000000..56ca30ede --- /dev/null +++ b/Space-Invaders/HeaderFiles/Enemy/Controllers/SubZero.h @@ -0,0 +1,28 @@ +#pragma once +#include "../../HeaderFiles/Enemy/EnemyController.h" + +namespace Enemy +{ + namespace Controllers + { + + class SubZero : public EnemyController + { + private: + float movementSpeed = 100.0f; + void move() override; + void moveDown(); + + public: + + SubZero(EnemyType setType); + ~SubZero() override; + + void initialize() override; + + + + }; + + } +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Enemy/Controllers/ThunderSnake.h b/Space-Invaders/HeaderFiles/Enemy/Controllers/ThunderSnake.h new file mode 100644 index 000000000..7b9e40fee --- /dev/null +++ b/Space-Invaders/HeaderFiles/Enemy/Controllers/ThunderSnake.h @@ -0,0 +1,27 @@ +#pragma once +#include "../../HeaderFiles/Enemy/Controllers/EnemyConfig.h" +#include "../../HeaderFiles/Enemy/EnemyController.h" + +namespace Enemy +{ + namespace Controllers + { + class ThunderSnake : public EnemyController + { + private: + float horizontalMovementSpeed = 300.0f; + float verticalMovementSpeed = 20.0f; + void move() override; + void moveRightDiagonal(); + void moveLeftDiagonal(); + public: + + ThunderSnake(EnemyType setType); + ~ThunderSnake() override; + + void initialize() override; + + + }; + } +} diff --git a/Space-Invaders/HeaderFiles/Enemy/Controllers/Zapper.h b/Space-Invaders/HeaderFiles/Enemy/Controllers/Zapper.h new file mode 100644 index 000000000..ba1e15fd3 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Enemy/Controllers/Zapper.h @@ -0,0 +1,31 @@ +#pragma once +#include "../../HeaderFiles/Enemy/EnemyController.h" +namespace Enemy +{ + namespace Controllers + { + + class Zapper : public EnemyController + { + private: + + float verticalTravelDistance = 100.0f; + void moveLeft(); + void moveRight(); + void moveDown(); + void move() override; + public: + Zapper(EnemyType setType); + ~Zapper() override; + + void initialize() override; + + + }; + + + + } + + +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Enemy/EnemyController.h b/Space-Invaders/HeaderFiles/Enemy/EnemyController.h new file mode 100644 index 000000000..9e40f1a2f --- /dev/null +++ b/Space-Invaders/HeaderFiles/Enemy/EnemyController.h @@ -0,0 +1,45 @@ +#pragma once +#include + + +namespace Enemy { + + class EnemyModel; + class EnemyView; + enum class EnemyState; + enum class EnemyType; + + class EnemyController + { + protected: + + EnemyView* enemyView; + EnemyModel* enemyModel; + + virtual void move() = 0; + sf::Vector2f getRandomInitialPosition(); + void handleOutOfBounds(); + //void moveRight(); + //void moveLeft(); + //void moveDown(); + + public: + + EnemyController(EnemyType setType); + virtual ~EnemyController(); + + //Lifecycle + + virtual void initialize(); + void update(); + void render(); + + sf::Vector2f getEnemyPosition(); + EnemyType getEnemyType(); + EnemyState getEnemyState(); + + + + }; + +} diff --git a/Space-Invaders/HeaderFiles/Enemy/EnemyModel.h b/Space-Invaders/HeaderFiles/Enemy/EnemyModel.h new file mode 100644 index 000000000..4fd6f4aeb --- /dev/null +++ b/Space-Invaders/HeaderFiles/Enemy/EnemyModel.h @@ -0,0 +1,62 @@ +#pragma once +#include + +namespace Enemy +{ + enum class EnemyState; + enum class EnemyType; + enum class MovementDirection; + + class EnemyModel + { + private: + + sf::Vector2f referencePosition = (sf::Vector2f(50.0f, 50.f)); + sf::Vector2f enemyPosition; + + EnemyState enemyState; + EnemyType enemyType; + MovementDirection movementDirection; + + + + + public: + + const float enemyMovementSpeed = 250.0f; + const float enemyVerticalDistance = 100.0f; + + const sf::Vector2f leftScreenSide = sf::Vector2f(50.0f, 50.0f); + const sf::Vector2f rightScreenSide = sf::Vector2f(1800.0f, 50.0f); + const sf::Vector2f bottomScreenSide = sf::Vector2f(0.0f, 100.0f); + + EnemyModel(); + EnemyModel(EnemyType setType); + ~EnemyModel(); + + + //Lifecycle + + void initialize(); + //void update(); + //void render(); + + //setters and getters + sf::Vector2f getReferencePosition(); + void setReferencePosition(sf::Vector2f position); + + sf::Vector2f getEnemyPosition(); + void setEnemyPosition(sf::Vector2f position); + + MovementDirection getMovementDirection(); + + void setMovementDirection(MovementDirection direction); + + EnemyState getEnemyState(); + void setEnemyState(EnemyState state); + + EnemyType getEnemyType(); + void setEnemyType(EnemyType setType); + }; + +} diff --git a/Space-Invaders/HeaderFiles/Enemy/EnemyService.h b/Space-Invaders/HeaderFiles/Enemy/EnemyService.h new file mode 100644 index 000000000..917f80454 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Enemy/EnemyService.h @@ -0,0 +1,40 @@ +#pragma once +//#include "../../HeaderFiles/Enemy/EnemyController.h" +#include + + +namespace Enemy { + + class EnemyController; + enum class EnemyType; + + class EnemyService { + + private: + std::vector enemyList; + void destroy(); + + const float enemySpawnDelay = 2.0f; + float spawnTimer; + void updateSpawnTimer(); + void processEnemySpawn(); + EnemyType getRandomEnemyType(); + EnemyController* createEnemy(EnemyType createenemy); + public: + + EnemyService(); + virtual ~EnemyService(); + + + void initialize(); + void update(); + void render(); + + EnemyController* spawnEnemy(); + void destroyEnemy(EnemyController * enemycontroller); + + + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Enemy/EnemyView.h b/Space-Invaders/HeaderFiles/Enemy/EnemyView.h new file mode 100644 index 000000000..41a274467 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Enemy/EnemyView.h @@ -0,0 +1,42 @@ +#pragma once +#include + + +namespace Enemy { + class EnemyController; + enum class EnemyType; + 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 thundersnake_texture_path = "assets/textures/thunder_snake.png"; + const float enemySpriteWidth = 60.f; + const float enemySpriteHeight = 60.f; + + sf::RenderWindow* gameWindow; + sf::Texture enemyTexture; + sf::Sprite enemySprite; + + EnemyController* enemyController; + + void initializeEnemySprites(EnemyType setType); + void scaleEnemySprites(); + + + public: + EnemyView(); + ~EnemyView(); + + //Lifecycle + + void initialize(EnemyController * controller); + void update(); + void render(); + + }; + +} + diff --git a/Space-Invaders/HeaderFiles/Event/EventService.h b/Space-Invaders/HeaderFiles/Event/EventService.h new file mode 100644 index 000000000..f21dcd7e3 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Event/EventService.h @@ -0,0 +1,54 @@ +#pragma once +#include +#include + +namespace Event { + class EventService { + // (Will take the address of the current_button_state var and the pressed mouse or keyboard button. + enum buttonState { + PRESSED, + HELD, + RELEASED + }; + + private: + sf::Event gameEvent; + sf::RenderWindow* gameWindow; + + bool isGameWindowOpen(); + bool gameWindowWasClosed(); + bool hasQuitGame(); + + buttonState leftMouseButtonState; + buttonState rightMouseButtonState; + buttonState leftArrowButtonState; + buttonState rightArrowButtonState; + buttonState A_ButtonState; + buttonState D_ButtonState; + + void updateMouseButtonsState(buttonState& currentButtonState, sf::Mouse::Button mouseButton); + void updateKeyboardButtonsState(buttonState& currentButtonState, sf::Keyboard::Key keyboardButton); + + public: + EventService(); + ~EventService(); + + void initialize(); + void update(); + void processEvents(); + bool pressedEscapeKey(); + bool isKeyboardEvent(); + + + + bool pressedLeftKey(); + bool pressedRightKey(); + + bool pressedAKey(); + bool pressedDKey(); + + bool pressedLeftMouseButton(); + bool pressedRightMouseButton(); + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Gameplay/GameplayController.h b/Space-Invaders/HeaderFiles/Gameplay/GameplayController.h new file mode 100644 index 000000000..d813c7bed --- /dev/null +++ b/Space-Invaders/HeaderFiles/Gameplay/GameplayController.h @@ -0,0 +1,24 @@ +#pragma once + + +namespace Gameplay +{ + class GameplayView; + + class GameplayController + { + private: + + GameplayView* gameplayView; + + public: + + GameplayController(); + ~GameplayController(); + + void initialize(); + void update(); + void render(); + + }; +} diff --git a/Space-Invaders/HeaderFiles/Gameplay/GameplayService.h b/Space-Invaders/HeaderFiles/Gameplay/GameplayService.h new file mode 100644 index 000000000..9bd5b2872 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Gameplay/GameplayService.h @@ -0,0 +1,22 @@ +#pragma once + +namespace Gameplay +{ + class GameplayController; + + class GameplayService + { + private: + + GameplayController* gameplayController; + + public: + GameplayService(); + ~GameplayService(); + + void initialize(); + void update(); + void render(); + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Gameplay/GameplayView.h b/Space-Invaders/HeaderFiles/Gameplay/GameplayView.h new file mode 100644 index 000000000..e24eb32ce --- /dev/null +++ b/Space-Invaders/HeaderFiles/Gameplay/GameplayView.h @@ -0,0 +1,28 @@ +#pragma once +#include + +namespace Gameplay +{ + class GameplayView { + + private: + // To draw it, you need a background texture, background sprite, the game window, the path to the background image file + + sf::Texture backgroundTexture; + sf::Sprite backgroundSprite; + sf::RenderWindow *gameWindow; + const sf::String backgroundTexturePath = "assets/textures/space_invaders_bg.png"; + + public: + + GameplayView(); + ~GameplayView(); + + void initializeBackgroundSprite(); + void scaleBackgroundSprite(); + void initialize(); + void update(); + void render(); + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Global/ServiceLocator.h b/Space-Invaders/HeaderFiles/Global/ServiceLocator.h new file mode 100644 index 000000000..59cb7ead1 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Global/ServiceLocator.h @@ -0,0 +1,64 @@ +#pragma once +#include "../../HeaderFiles/Main/GameService.h" +#include +#include "../../HeaderFiles/Graphic/GraphicService.h" +#include "../../HeaderFiles/Event/EventService.h" +#include "../../HeaderFiles/Player/PlayerService.h" +#include "../../HeaderFiles/Time/TimeService.h" +#include "../../HeaderFiles/UIService/UIService.h" +#include "../../HeaderFiles/Enemy/EnemyService.h" +#include "../../HeaderFiles/Gameplay/GameplayService.h" +#include "../../HeaderFiles/Element/ElementService.h" +#include "../../HeaderFiles/Sound/SoundService.h" + +//namespace Main { + //class GameService; +///} + + namespace Global + { + + class ServiceLocator { + + private: + Graphic::GraphicService* graphic_service; + static ServiceLocator* instance; + Event::EventService* event_service; + Player::PlayerService* player_service; + Time::TimeService* time_service; + UI::UIService* ui_service; + Enemy::EnemyService* enemy_service; + Gameplay::GameplayService* gameplay_service; + Element::ElementService* element_service; + Sound::SoundService* sound_service; + + ServiceLocator(); + ~ServiceLocator(); + + void createServices(); // Creates instances of all services. + void clearAllServices(); // Deletes and deallocates memory for all services. + + + public: + static ServiceLocator* getInstance(); // Provides a method to access the unique ServiceLocator instance (object) + void update(); + void initialize(); + void render(); + + // Methods to Get Specific Services: + Graphic::GraphicService* GetGraphicService(); + Event::EventService* GetEventService(); + Player::PlayerService* GetPlayerService(); + Time::TimeService* GetTimeService(); + UI::UIService* GetUIService(); + Enemy::EnemyService* GetEnemyService(); + Gameplay::GameplayService* GetGameplayService(); + Element::ElementService* GetElementService(); + Sound::SoundService* GetSoundService(); + void deleteServiceLocator(); + + }; + } + + + diff --git a/Space-Invaders/HeaderFiles/Global/config.h b/Space-Invaders/HeaderFiles/Global/config.h new file mode 100644 index 000000000..fa453da00 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Global/config.h @@ -0,0 +1,42 @@ +#pragma once +#include + +namespace Global +{ + class Config + { + public: + + static const sf::String outscallogoTexturePath; + static const sf::String backgroundTexturePath; + static const sf::String playerTexturePath; + + static const sf::String zapperTexturePath; + static const sf::String thundersnakeTexturePath; + static const sf::String subzeroTexturePath; + static const sf::String ufoTexturePath; + static const sf::String bunkerTexturePath; + + static const sf::String shieldTexturePath; + static const sf::String tripplelaserTexturePath; + static const sf::String rapidfireTexturePath; + static const sf::String outscalbombTexturePath; + + static const sf::String laserbulletTexturePath; + static const sf::String torpedoTexturePath; + static const sf::String frostbulletTexturePath; + + static const sf::String playbuttonTexturePath; + static const sf::String instructionsbuttonTexturePath; + static const sf::String quitbuttonTexturePath; + static const sf::String menubuttonTexturePath; + + static const sf::String bubblebobbleFontPath; + static const sf::String DS_DIGIB_font_path; + + static const sf::String backgroundMusicPath; + static const sf::String buttonclickSoundPath; + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Graphic/GraphicService.h b/Space-Invaders/HeaderFiles/Graphic/GraphicService.h new file mode 100644 index 000000000..3d3564489 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Graphic/GraphicService.h @@ -0,0 +1,44 @@ +#pragma once + +#include +using namespace std; +#include + +namespace Graphic +{ + class GraphicService { + + private: + + const string gameWindowTitle = "SPACE INVADERS"; + + const int gameWindowHeight = 1920; + const int gameWindowWidth = 1080; + const int frameRate = 60; + + const sf::Color gameWindowColor = sf::Color::Blue; + + sf::VideoMode* videoMode{ nullptr }; // ptr to video mode + sf::RenderWindow* gameWindow{ nullptr }; //pointer to renderwindow : we have created an object using pointers to manually delete the said object and pointer. + + void SetVideoMode(); // Method for setting our video mode; + + void OnDestroy(); // method to run when window is delete}; + + + public: + GraphicService(); + ~GraphicService(); + sf::RenderWindow* CreateGameWindow(); //method to create the game window. returns a pointer to an instance of the game window + + void initialize(); //lifecycle functions + void update(); + void render(); + bool isGameWindowOpen() const;//check if the window is open + + sf::RenderWindow* GetGameWindow() const;//getter for the game window instance + sf::Color GetWindowColor() const;//get the color + + }; +} + diff --git a/Space-Invaders/HeaderFiles/Main/GameService.h b/Space-Invaders/HeaderFiles/Main/GameService.h new file mode 100644 index 000000000..c934cafea --- /dev/null +++ b/Space-Invaders/HeaderFiles/Main/GameService.h @@ -0,0 +1,52 @@ +#pragma once +#include +#include "../../HeaderFiles/Global/ServiceLocator.h" + +namespace Global +{ + class ServiceLocator; +} +namespace Main +{ + enum GameState { + BOOT, + MAINMENU, + GAMEPLAY + + + }; + + //class ServiceLocator; + + class GameService + { + + private: + static GameState currentState; + + Global::ServiceLocator* service_locator; + sf::RenderWindow* gameWindow; + + void initialize(); // Handle game initialization + void destroy(); // Destroys unneeded objects + void initializeVariables(); + + + public: + + GameService(); // Default Constructor + ~GameService(); // Deconstructor + + void update(); // Updating game logic + void ignite(); // initiates the game + void render(); //Rendering the next frame + bool isRunning(); //check if the game is currently running + //sf::RenderWindow* GetGameWindow() const; // Getter for the game window instance + + static void setGameState(GameState newState); + static GameState getGameState(); + void showMainMenu(); + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Player/PlayerController.h b/Space-Invaders/HeaderFiles/Player/PlayerController.h new file mode 100644 index 000000000..411a5c4f0 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Player/PlayerController.h @@ -0,0 +1,48 @@ +#pragma once +//To handle all the logic of the player +#include +#include +#include "../../HeaderFiles/Player/PlayerModel.h" +#include "../../HeaderFiles/Player/PlayerView.h" + + + +namespace Player +{ + class PlayerModel; + class PlayerView; + enum class PlayerState; + + class PlayerController { + + private: + //float variable to store the current pos of the player + + + //pointers to player view and model + PlayerView* playerView; + PlayerModel* playerModel; + + + void ProcessPlayerInput(); + void moveRight(); + void moveLeft(); + + public: + + //Constructors and deconstructors + PlayerController(); + ~PlayerController(); + + //Lifecycle methpds + void update(); + void initialize(); + void render(); + + //Setters and getters + sf::Vector2f getPlayerPosition(); + + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Player/PlayerModel.h b/Space-Invaders/HeaderFiles/Player/PlayerModel.h new file mode 100644 index 000000000..a20d1a0eb --- /dev/null +++ b/Space-Invaders/HeaderFiles/Player/PlayerModel.h @@ -0,0 +1,51 @@ +#pragma once +#include +#include "../../HeaderFiles/Player/PlayerController.h" +//TO maintain and store data of the player + +namespace Player +{ + enum class PlayerState { + + ALIVE, + DEAD + + }; + + class PlayerModel { + + private: + + const sf::Vector2f initialPlayerPosition = sf::Vector2f(950.0f, 950.0f); + sf::Vector2f playerPosition; + PlayerState playerState; //Declaration + int playerScore; + + public: + + const sf::Vector2f leftScreenSide = sf::Vector2f(50.0f, 950.0f); + const sf::Vector2f rightScreenSide = sf::Vector2f(1800.0f, 950.0f); + + const float playerMovementSpeed = 350.0f; + PlayerModel(); + ~PlayerModel(); + + void Reset(); + void initialize(); + + + //getters and setters + 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/HeaderFiles/Player/PlayerService.h b/Space-Invaders/HeaderFiles/Player/PlayerService.h new file mode 100644 index 000000000..b58c03991 --- /dev/null +++ b/Space-Invaders/HeaderFiles/Player/PlayerService.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +namespace Player +{ + class PlayerController; + + + class PlayerService + { + private: + + Player::PlayerController* playerController; + + public: + + PlayerService(); + ~PlayerService(); + + void update(); + void render(); + void initialize(); + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Player/PlayerView.h b/Space-Invaders/HeaderFiles/Player/PlayerView.h new file mode 100644 index 000000000..7d37325ba --- /dev/null +++ b/Space-Invaders/HeaderFiles/Player/PlayerView.h @@ -0,0 +1,42 @@ +#pragma once +#include +using namespace std; +#include +#include "../../HeaderFiles/Player/PlayerController.h" + +namespace Player +{ + class PlayerController; + + //To render all the player images onto the screen + + class PlayerView { + + private: + const sf::String player_texture_path = "assets/textures/player_ship.png"; + const float spriteHeight = 60.0f; + const float spriteWidth = 60.0f; + + sf::Texture playerTexture; + sf::Sprite playerSprite; + sf::RenderWindow* gameWindow; + void initializePlayerSprite(); + void scalePlayerSprite(); + + PlayerController* playerController; + + public: + PlayerView(); + ~PlayerView(); + + //LIfecycle methods + void initialize(); + void render(); + void update(); + + void initialize(PlayerController* controller); // we pass a pointer of type controller because we need to use this in the view later. + }; +} + + + diff --git a/Space-Invaders/HeaderFiles/Projectiles/IProjectile.h b/Space-Invaders/HeaderFiles/Projectiles/IProjectile.h new file mode 100644 index 000000000..40e03d63b --- /dev/null +++ b/Space-Invaders/HeaderFiles/Projectiles/IProjectile.h @@ -0,0 +1,24 @@ +#pragma once +#include +#include "../../HeaderFiles/Enemy/Controllers/EnemyConfig.h" +#include "../../HeaderFiles/Bullet/BulletConfig.h" + +namespace Projectile +{ + + enum class MovememtDirection; + class IProjectile + { + public: + + virtual void initialize(sf::Vector2f position, Bullet::MovementDirection direction) = 0; + + virtual void update() = 0; + virtual void render() = 0; + + virtual sf::Vector2f getProjectilePosition() = 0; + virtual void updateProjectilePosition() = 0; + virtual ~IProjectile(); + }; + +} \ No newline at end of file diff --git a/Space-Invaders/HeaderFiles/Sound/SoundService.h b/Space-Invaders/HeaderFiles/Sound/SoundService.h new file mode 100644 index 000000000..c826bb30e --- /dev/null +++ b/Space-Invaders/HeaderFiles/Sound/SoundService.h @@ -0,0 +1,39 @@ +#pragma once + +#include "SFML/Audio.hpp" + + +namespace Sound +{ + enum class SoundType + { + BUTTONCLICK + }; + class SoundService + { + + + private: + + const int backgroundMusicVolume = 30; + sf::Music backgroundMusic; + sf::Sound soundEffects; + sf::SoundBuffer buttonClick; + + void loadBackgroundMusic(); + void loadSoundEffects(); + + + public: + SoundService(); + ~SoundService(); + + void playSound(SoundType soundType); + void playBackgroundMusic(); + void initialize(); + + + + + }; +} diff --git a/Space-Invaders/HeaderFiles/Time/TimeService.h b/Space-Invaders/HeaderFiles/Time/TimeService.h new file mode 100644 index 000000000..9d39fccee --- /dev/null +++ b/Space-Invaders/HeaderFiles/Time/TimeService.h @@ -0,0 +1,32 @@ +#pragma once +#include +//set up Timeservice.h and .cpp + + +namespace Time +{ + class TimeService { + + // The TimeService class helps keep track of time in game and calculate delta time. + // Utilizes the library to calculate delta time. + + 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(); + float calculateDeltaTime(); + void updatePreviousTime(); + + public: + void update(); + void initialize(); + + //getter + float getDeltaTime(); + + }; +} diff --git a/Space-Invaders/HeaderFiles/UIService/MainMenuUIController/MainMenuUIController.h b/Space-Invaders/HeaderFiles/UIService/MainMenuUIController/MainMenuUIController.h new file mode 100644 index 000000000..95442a291 --- /dev/null +++ b/Space-Invaders/HeaderFiles/UIService/MainMenuUIController/MainMenuUIController.h @@ -0,0 +1,68 @@ +#pragma once +#include + + +namespace UI { + + namespace MainMenu + { + class MainMenuUIController + { + private: + const sf::String bgTexture_Path = "assets/textures/space_invaders_bg.png"; + const sf::String playButtonTexture_Path = "assets/textures/play_button.png"; + const sf::String instructionTexture_Path = "assets/textures/instructions_button.png"; + const sf::String quitButtonTexture_Path = "assets/textures/quit_button.png"; + + + //pointer to gameWindow + sf::RenderWindow* gameWindow; + + //Textures and sprite variables + sf::Sprite bgSprite; + sf::Texture bgTexture; + sf::Sprite playButtonSprite; + sf::Texture playButtonTexture; + sf::Sprite quitButtonSprite; + sf::Texture quitButtonTexture; + sf::Sprite instructionSprite; + sf::Texture instructionTexture; + + const float buttonHeight = 150.0f; + const float buttonWidth = 300.0f; + + public: + + //Constructors and Deconstructors + MainMenuUIController(); + ~MainMenuUIController(); + + void initializeBGImage(); + void scaleBGImage(); + + // Declare methods for initializing, setting, and positioning button sprites. + + void initializeButtonSprites(); + void setButtonSprites(); + void positionButtonSprites(); + + + void scaleAllButtons(); + void scaleButton(sf::Sprite* buttonToScale); + + //bool to check if image is loaded or not + + bool loadButtonTexturesFromFile(); + + void processButtonInteractions(); + bool clickedButton(sf::Sprite*, sf::Vector2f); + + //Lidecycle methods + void initialize(); + void render(); + void update(); + void destroy(); + }; + + } +} diff --git a/Space-Invaders/HeaderFiles/UIService/UIService.h b/Space-Invaders/HeaderFiles/UIService/UIService.h new file mode 100644 index 000000000..244e5cb07 --- /dev/null +++ b/Space-Invaders/HeaderFiles/UIService/UIService.h @@ -0,0 +1,30 @@ +#pragma once +#include "../../HeaderFiles/UIService/MainMenuUIController/MainMenuUIController.h" +#include +using namespace std; + +namespace UI { + class UIService + { + + MainMenu::MainMenuUIController* mainMenuUIController; + + private: + void destroy(); + void createUIControllers(); + void initializeControllers(); + + + public: + + UIService(); + ~UIService(); + + void update(); + void initialize(); + void render(); + + + + }; +} diff --git a/Space-Invaders/Source/Bullet/BulletController.cpp b/Space-Invaders/Source/Bullet/BulletController.cpp new file mode 100644 index 000000000..73f68b4bc --- /dev/null +++ b/Space-Invaders/Source/Bullet/BulletController.cpp @@ -0,0 +1,69 @@ +#include "../../HeaderFiles/Bullet/BulletController.h" + + + + +namespace Bullet +{ + + BulletController::BulletController(BulletType type) + { + + } + + BulletController::~BulletController() + { + + } + + void BulletController::moveUp() + { + + } + + void BulletController::moveDown() + { + + } + + void BulletController::handleOutOfBounds() + { + + } + + void BulletController::updateProjectilePosition() + { + + } + + void BulletController::initialize(sf::Vector2f position, Bullet::MovementDirection direction) + { + + } + + void BulletController::update() + { + + } + + void BulletController::render() + { + + } + + sf::Vector2f BulletController::getProjectilePosition() + { + + } + + BulletType BulletController::getBulletType() + { + + } + + + + + + +} \ No newline at end of file diff --git a/Space-Invaders/Source/Bullet/BulletModel.cpp b/Space-Invaders/Source/Bullet/BulletModel.cpp new file mode 100644 index 000000000..40e6c2674 --- /dev/null +++ b/Space-Invaders/Source/Bullet/BulletModel.cpp @@ -0,0 +1,66 @@ +#include "../../HeaderFiles/Bullet/BulletModel.h" + +namespace Bullet +{ + BulletModel::BulletModel(BulletType type) + { + bulletType = type; + } + + BulletModel::~BulletModel() + { + + } + + void BulletModel::initialize(sf::Vector2f position, MovementDirection direction) + { + movementDirection = direction; + + bulletPosition = position; + } + + void BulletModel::update() + { + + } + + void BulletModel::render() + { + + } + + void BulletModel::destroy() + { + + } + + BulletType BulletModel::getBulletType() + { + return bulletType; + } + + void BulletModel::setBulletType(BulletType type) + { + bulletType = type; + } + + float BulletModel::getMovementSpeed() + { + return movementSpeed; + } + + void BulletModel::setMovementSpeed(float speed) + { + movementSpeed = speed; + } + + MovementDirection BulletModel::getMovementDirection() + { + return movementDirection; + } + + void BulletModel::setMovementDirection(MovementDirection direction) + { + movementDirection = direction; + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Bullet/BulletService.cpp b/Space-Invaders/Source/Bullet/BulletService.cpp new file mode 100644 index 000000000..19b97542b --- /dev/null +++ b/Space-Invaders/Source/Bullet/BulletService.cpp @@ -0,0 +1,20 @@ +#include "../../HeaderFiles/Bullet/BulletService.h" + + +void BulletService::initialize() +{ + +} + +void BulletService::update() +{ + +} +void BulletService::render() +{ + +} +void BulletService::destroy() +{ + +} \ 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..b24bc1f2b --- /dev/null +++ b/Space-Invaders/Source/Bullet/BulletView.cpp @@ -0,0 +1,75 @@ +#include "../../HeaderFiles/Bullet/BulletConfig.h" +#include "../../HeaderFiles/Bullet/BulletController.h" +#include "../../HeaderFiles/Bullet/BulletView.h" +#include "../../HeaderFiles/Global/config.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" + +namespace Bullet +{ + using namespace Global; + BulletView::BulletView() + { + + } + + BulletView::~BulletView() + { + + } + + void BulletView::initialize(BulletController* controller) + { + controller = bulletController; + window = ServiceLocator::getInstance()->GetGraphicService()->GetGameWindow(); + scaleBulletSprite(); + } + + void BulletView::update() + { + + } + void BulletView::render() + { + + } + + void BulletView::initializeBulletSprite(BulletType type) + { + switch (type) + { + case Bullet::BulletType::LASERBULLET: + + if (bulletTexture.loadFromFile(Config::laserbulletTexturePath)) + { + bulletSprite.setTexture(bulletTexture); + scaleBulletSprite(); + } + break; + case Bullet::BulletType::FROSTBULLET: + if (bulletTexture.loadFromFile(Config::frostbulletTexturePath)) + { + bulletSprite.setTexture(bulletTexture); + scaleBulletSprite(); + } + break; + case Bullet::BulletType::TORPEDO: + if (bulletTexture.loadFromFile(Config::torpedoTexturePath)) + { + bulletSprite.setTexture(bulletTexture); + scaleBulletSprite(); + } + break; + default: + break; + } + } + + void BulletView::scaleBulletSprite() + { + bulletSprite.setScale( + static_cast(bulletSpriteHeight) / bulletSprite.getTexture()->getSize().y, + static_cast(bulletSpriteWidth) / bulletSprite.getTexture()->getSize().x); + } + + +} \ No newline at end of file diff --git a/Space-Invaders/Source/Bullet/Controller/FrostBullet.cpp b/Space-Invaders/Source/Bullet/Controller/FrostBullet.cpp new file mode 100644 index 000000000..a9454b4af --- /dev/null +++ b/Space-Invaders/Source/Bullet/Controller/FrostBullet.cpp @@ -0,0 +1,25 @@ +#include "../../HeaderFiles/Bullet/BulletModel.h" +#include "../../HeaderFiles/Bullet/Controllers/FrostBullet.h" + +namespace Bullet +{ + namespace BulletControllers + { + FrostBullet::FrostBullet(BulletType type) : BulletController(type) + { + + } + + FrostBullet::~FrostBullet() + { + + } + + void FrostBullet::initialize(sf::Vector2f position, MovementDirection direction) + { + BulletController::initialize(position, direction); + bulletModel->setMovementSpeed(frostbulletMovementSpeed); + } + + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Bullet/Controller/LaserBullet.cpp b/Space-Invaders/Source/Bullet/Controller/LaserBullet.cpp new file mode 100644 index 000000000..5eb4720a1 --- /dev/null +++ b/Space-Invaders/Source/Bullet/Controller/LaserBullet.cpp @@ -0,0 +1,22 @@ +#include "../../HeaderFiles/Bullet/Controllers/LaserBullet.h" + +namespace Bullet +{ + namespace BulletControllers + { + LaserBullet::LaserBullet(BulletType type) : BulletController(type) + { + + } + + LaserBullet::~LaserBullet() + { + + } + + void LaserBullet::initialize(sf::Vector2f position, MovementDirection direction) + { + BulletController::initialize(position, direction); + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Bullet/Controller/Torpedo.cpp b/Space-Invaders/Source/Bullet/Controller/Torpedo.cpp new file mode 100644 index 000000000..524ff859f --- /dev/null +++ b/Space-Invaders/Source/Bullet/Controller/Torpedo.cpp @@ -0,0 +1,24 @@ +#include "../../HeaderFiles/Bullet/Controllers/TorpedoController.h" +#include "../../HeaderFiles/Bullet/BulletModel.h" + +namespace Bullet +{ + namespace BulletControllers + { + Torpedo::Torpedo(BulletType type) : BulletController(type) + { + + } + + Torpedo::~Torpedo() + { + + } + + void Torpedo::initialize(sf::Vector2f position, MovementDirection direction) + { + BulletController::initialize(position, direction); + bulletModel->setMovementSpeed(torpedoMovementSpeed); + } + } +} \ 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..036fe5361 --- /dev/null +++ b/Space-Invaders/Source/Element/Bunker/BunkerController.cpp @@ -0,0 +1,40 @@ +#include "../../HeaderFiles/Element/Bunker/BunkerController.h" +#include "../../HeaderFiles/Element/Bunker/BunkerView.h" + + + +namespace Element +{ + namespace Bunker + { + BunkerController::BunkerController() + { + bunkerView = new BunkerView(); + } + + BunkerController::~BunkerController() + { + delete(bunkerView); + } + + void BunkerController::initialize(BunkerData data) + { + bunkerData = data; + bunkerView->initialize(this); + } + void BunkerController::update() + { + bunkerView->update(); + } + + void BunkerController::render() + { + bunkerView->render(); + } + + sf::Vector2f BunkerController::getBunkerPosition() + { + return bunkerData.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 new file mode 100644 index 000000000..06407a5fa --- /dev/null +++ b/Space-Invaders/Source/Element/Bunker/BunkerModel.cpp @@ -0,0 +1,16 @@ +#include"../../HeaderFiles/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/BunkerService.cpp b/Space-Invaders/Source/Element/Bunker/BunkerService.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..059f0a8d9 --- /dev/null +++ b/Space-Invaders/Source/Element/Bunker/BunkerView.cpp @@ -0,0 +1,59 @@ +#include "../../HeaderFiles/Element/Bunker/BunkerView.h" +#include "../../HeaderFiles/Element/Bunker/BunkerController.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" + + +namespace Element +{ + namespace Bunker + { + using namespace Global; + + + BunkerView::BunkerView() + { + + } + + BunkerView::~BunkerView() + { + + } + + void BunkerView::initialize(BunkerController* controller) + { + bunkerController = controller; + gameWindow = ServiceLocator::getInstance()->GetGraphicService()->GetGameWindow(); + initializeBunkerSprite(); + } + + void BunkerView::update() + { + bunkerSprite.setPosition(bunkerController->getBunkerPosition()); + } + + void BunkerView::render() + { + gameWindow->draw(bunkerSprite); + } + + void BunkerView::initializeBunkerSprite() + { + if (bunkerTexture.loadFromFile(bunker_texture_path)) + { + bunkerSprite.setTexture(bunkerTexture); + scaleBunkerSprite(); + } + } + + void BunkerView::scaleBunkerSprite() + { + bunkerSprite.setScale( + static_cast(bunkerSpriteWidth) / bunkerSprite.getTexture()->getSize().x, + static_cast(bunkerSpriteHeight) / bunkerSprite.getTexture()->getSize().y); + } + + + + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Element/ElementService.cpp b/Space-Invaders/Source/Element/ElementService.cpp new file mode 100644 index 000000000..3615f61b6 --- /dev/null +++ b/Space-Invaders/Source/Element/ElementService.cpp @@ -0,0 +1,51 @@ +#include "../../HeaderFiles/Element/ElementService.h" + +namespace Element +{ + ElementService::ElementService() + { + + } + + ElementService::~ElementService() + { + destroy(); + } + + void ElementService::initialize() + { + for ( int i = 0; i < bunkerDataList.size(); i++) + { + Bunker::BunkerController* bunkerController = new Bunker::BunkerController(); + + bunkerController->initialize(bunkerDataList[i]); + bunkerList.push_back(bunkerController); + } + } + + void ElementService::update() + { + for (int i = 0; i < bunkerDataList.size(); i++) + { + bunkerList[i]->update(); + } + } + + void ElementService::render() + { + for (int i = 0; i < bunkerDataList.size(); i++) + { + bunkerList[i]->render(); + } + } + + void ElementService::destroy() + { + for (int i = 0; i < bunkerDataList.size(); i++) + { + delete(bunkerList[i]); + } + } + + +} \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/Controllers/SubZero.cpp b/Space-Invaders/Source/Enemy/Controllers/SubZero.cpp new file mode 100644 index 000000000..aeb65ad2e --- /dev/null +++ b/Space-Invaders/Source/Enemy/Controllers/SubZero.cpp @@ -0,0 +1,51 @@ +#include "../../HeaderFiles/Enemy/Controllers/SubZero.h" +//#include "../../HeaderFiles/Enemy/EnemyController.h" +#include "../../HeaderFiles/Enemy/EnemyModel.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include "../../HeaderFiles/Enemy/Controllers/EnemyConfig.h" + +namespace Enemy +{ + using namespace Global; + + namespace Controllers + { + SubZero::SubZero(EnemyType setType) : EnemyController(setType) + { + } + void SubZero::initialize() + { + EnemyController::initialize(); + enemyModel->setMovementDirection(MovementDirection::DOWN); + + } + + + SubZero::~SubZero() + { + + } + + void SubZero::move() + { + switch (MovementDirection::DOWN) + { + case Enemy::MovementDirection::DOWN: + moveDown(); + break; + + } + } + + void SubZero::moveDown() + { + sf::Vector2f currentPosition = enemyModel->getEnemyPosition(); + + currentPosition.y += movementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + enemyModel->setEnemyPosition(currentPosition); + + } + + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/Controllers/ThunderSnake.cpp b/Space-Invaders/Source/Enemy/Controllers/ThunderSnake.cpp new file mode 100644 index 000000000..5876bddda --- /dev/null +++ b/Space-Invaders/Source/Enemy/Controllers/ThunderSnake.cpp @@ -0,0 +1,75 @@ +#include "../../HeaderFiles/Enemy/Controllers/ThunderSnake.h" +#include "../../HeaderFiles/Enemy/EnemyModel.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include "../../HeaderFiles/Enemy/Controllers/EnemyConfig.h" + +namespace Enemy +{ + using namespace Global; + namespace Controllers + { + ThunderSnake::ThunderSnake(EnemyType setType) : EnemyController(setType) + { + + } + + ThunderSnake::~ThunderSnake() + { + + } + + void ThunderSnake::moveRightDiagonal() + { + sf::Vector2f currentPosition = enemyModel->getEnemyPosition(); + currentPosition.x += horizontalMovementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + currentPosition.y += verticalMovementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + if (currentPosition.x >= enemyModel->rightScreenSide.x ) + { + enemyModel->setMovementDirection(MovementDirection::LEFT); + enemyModel->setReferencePosition(currentPosition); + } + else + { + enemyModel->setEnemyPosition(currentPosition); + } + + } + void ThunderSnake::moveLeftDiagonal() + { + sf::Vector2f currentPosition = enemyModel->getEnemyPosition(); + currentPosition.x -= horizontalMovementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + currentPosition.y += verticalMovementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + if (currentPosition.x <= enemyModel->leftScreenSide.x ) + { + enemyModel->setMovementDirection(MovementDirection::RIGHT); + enemyModel->setReferencePosition(currentPosition); + } + else + { + enemyModel->setEnemyPosition(currentPosition); + } + + } + void ThunderSnake::initialize() + { + EnemyController::initialize(); + } + + void ThunderSnake::move() + { + switch (enemyModel->getMovementDirection()) + { + case Enemy::MovementDirection::RIGHT: + moveRightDiagonal(); + break; + case Enemy::MovementDirection::LEFT: + moveLeftDiagonal(); + break; + } + } + + + } +} diff --git a/Space-Invaders/Source/Enemy/Controllers/Zapper.cpp b/Space-Invaders/Source/Enemy/Controllers/Zapper.cpp new file mode 100644 index 000000000..49087e70d --- /dev/null +++ b/Space-Invaders/Source/Enemy/Controllers/Zapper.cpp @@ -0,0 +1,100 @@ +#include "../../HeaderFiles/Enemy/Controllers/Zapper.h" + +#include "../../HeaderFiles/Enemy/EnemyModel.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include "../../HeaderFiles/Enemy/Controllers/EnemyConfig.h" + +namespace Enemy +{ + using namespace Global; + namespace Controllers + { + Zapper::Zapper(EnemyType setType) :EnemyController(setType) { + + } + + Zapper::~Zapper() + { + + } + + void Zapper::initialize() + { + EnemyController::initialize(); + //enemyModel->setMovementDirection(MovementDirection::RIGHT); + } + + void Zapper::move() + { + + switch (enemyModel->getMovementDirection()) + { + case Enemy::MovementDirection::DOWN: + moveDown(); + break; + case Enemy::MovementDirection::RIGHT: + moveRight(); + break; + case Enemy::MovementDirection::LEFT: + moveLeft(); + break; + } + } + + void Zapper::moveDown() + { + sf::Vector2f currentPosition = enemyModel->getEnemyPosition(); + currentPosition.y += enemyModel->enemyMovementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + if (currentPosition.y >= enemyModel->getReferencePosition().y + verticalTravelDistance) + { + if (enemyModel->getReferencePosition().x <= enemyModel->leftScreenSide.x) + { + enemyModel->setMovementDirection(MovementDirection::RIGHT); + } + else + { + enemyModel->setMovementDirection(MovementDirection::LEFT); + } + } + else + { + enemyModel->setEnemyPosition(currentPosition); + } + + } + + void Zapper::moveLeft() + { + sf::Vector2f currentPosition = enemyModel->getEnemyPosition(); + currentPosition.x -= enemyModel->enemyMovementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + if (currentPosition.x <= enemyModel->leftScreenSide.x) + { + enemyModel->setMovementDirection(MovementDirection::DOWN); + enemyModel->setReferencePosition(currentPosition); + } + else + { + enemyModel->setEnemyPosition(currentPosition); + } + + } + + void Zapper::moveRight() + { + sf::Vector2f currentPosition = enemyModel->getEnemyPosition(); + currentPosition.x += enemyModel->enemyMovementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + if (currentPosition.x >= enemyModel->rightScreenSide.x) + { + enemyModel->setMovementDirection(MovementDirection::DOWN); + enemyModel->setReferencePosition(currentPosition); + } + else + { + enemyModel->setEnemyPosition(currentPosition); + } + } + } +} \ 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..c3f5b81bd --- /dev/null +++ b/Space-Invaders/Source/Enemy/EnemyController.cpp @@ -0,0 +1,120 @@ +#include "../../HeaderFiles/Enemy/EnemyController.h" +#include "../../HeaderFiles/Enemy/EnemyModel.h" +#include "../../HeaderFiles/Enemy/EnemyView.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +//#include "../../HeaderFiles/Event/EventService.h" +//#include "../../HeaderFiles/Enemy/Controllers/EnemyConfig.h" +#include +using namespace std; + +namespace Enemy +{ + using namespace Global; + //using namespace Time; + //using namespace Event; + + EnemyController::EnemyController(EnemyType setType) { + enemyModel = new EnemyModel(setType); + enemyView = new EnemyView(); + } + + EnemyController::~EnemyController() { + delete (enemyView); + delete (enemyModel); + } + + void EnemyController::initialize() { + enemyModel->initialize(); + enemyView->initialize(this); + //enemyModel->setMovementDirection(MovementDirection::DOWN); + enemyModel->setEnemyPosition(getRandomInitialPosition()); + } + + void EnemyController::update() { + move(); + enemyView->update(); + handleOutOfBounds(); + } + void EnemyController::render() { + enemyView->render(); + } + + sf::Vector2f EnemyController::getEnemyPosition() { + return enemyModel->getEnemyPosition(); + } + EnemyType EnemyController::getEnemyType() + { + return enemyModel->getEnemyType(); + } + + EnemyState EnemyController::getEnemyState() + { + return enemyModel->getEnemyState(); + } + + sf::Vector2f EnemyController::getRandomInitialPosition() + { + float x_offset_distance = (std::rand() % static_cast(enemyModel->rightScreenSide.x - enemyModel->leftScreenSide.x)); + float x_position = x_offset_distance + enemyModel->leftScreenSide.x; + float y_position = enemyModel->leftScreenSide.x; + + return sf::Vector2f(x_position, y_position); + + } + + void EnemyController::handleOutOfBounds() + { + sf::Vector2f enemyPosition = enemyModel->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); + } + } + /*void EnemyController::moveRight() + { + sf::Vector2f currentPosition = enemyModel->getEnemyPosition(); + currentPosition.x += enemyModel->enemyMovementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + if (currentPosition.x >= enemyModel->rightScreenSide.x) + { + enemyModel->setMovementDirection(MovementDirection::DOWN); + enemyModel->setReferencePosition(currentPosition); + } + else (enemyModel->setEnemyPosition(currentPosition)); + } + + void EnemyController::moveLeft() + { + sf::Vector2f currentPosition = enemyModel->getEnemyPosition(); + currentPosition.x -= enemyModel->enemyMovementSpeed * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + if (currentPosition.x <= enemyModel->leftScreenSide.x) + { + enemyModel->setMovementDirection(MovementDirection::DOWN); + enemyModel->setReferencePosition(currentPosition); + } + else (enemyModel->setEnemyPosition(currentPosition)); + } + + void EnemyController::moveDown() + { + sf::Vector2f currentPosition = enemyModel->getEnemyPosition(); + currentPosition.y += enemyModel->enemyVerticalDistance * ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + if (currentPosition.y >= enemyModel->getReferencePosition().y + enemyModel->enemyVerticalDistance) + { + if (enemyModel->getReferencePosition().x <= enemyModel->leftScreenSide.x) + { + enemyModel->setMovementDirection(MovementDirection::RIGHT); + } + else (enemyModel->setMovementDirection(MovementDirection::LEFT)); + } + else (enemyModel->setEnemyPosition(currentPosition)); + + + + }*/ +} \ No newline at end of file diff --git a/Space-Invaders/Source/Enemy/EnemyModel.cpp b/Space-Invaders/Source/Enemy/EnemyModel.cpp new file mode 100644 index 000000000..bc0f14da9 --- /dev/null +++ b/Space-Invaders/Source/Enemy/EnemyModel.cpp @@ -0,0 +1,73 @@ +#include "../../HeaderFiles/Enemy/Controllers/EnemyConfig.h" +#include "../../HeaderFiles/Enemy/EnemyModel.h" +#include +using namespace std; + +namespace Enemy +{ + + EnemyModel::EnemyModel(EnemyType setType) { + enemyType = setType; + } + + EnemyModel::~EnemyModel() { + + } + + void EnemyModel::initialize() + { + enemyState = EnemyState::PATROLLING; + movementDirection = MovementDirection::RIGHT; + enemyPosition = referencePosition; + } + + + void EnemyModel::setEnemyPosition(sf::Vector2f position) + { + enemyPosition = position; + } + + sf::Vector2f EnemyModel::getEnemyPosition() { + return enemyPosition; + } + + void EnemyModel::setReferencePosition(sf::Vector2f position) { + referencePosition = position; + } + + sf::Vector2f EnemyModel::getReferencePosition() { + return referencePosition; + } + + MovementDirection EnemyModel::getMovementDirection() { + return movementDirection; + } + + void EnemyModel::setMovementDirection(MovementDirection direction) + { + movementDirection = direction; + } + + EnemyState EnemyModel::getEnemyState() + { + return enemyState; + } + + void EnemyModel::setEnemyState(EnemyState state) + { + enemyState = state; + } + + EnemyType EnemyModel::getEnemyType() + { + return enemyType; + } + + void EnemyModel::setEnemyType(EnemyType setType) + { + enemyType = setType; + } + + + +} \ 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..9f4ea9ae8 --- /dev/null +++ b/Space-Invaders/Source/Enemy/EnemyService.cpp @@ -0,0 +1,116 @@ +#include "../../HeaderFiles/Enemy/EnemyService.h" +#include "../../HeaderFiles/Enemy/EnemyController.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include "../../HeaderFiles/Time/TimeService.h" +#include "../../HeaderFiles/Enemy/Controllers/SubZero.h" +#include "../../HeaderFiles/Enemy/Controllers/EnemyConfig.h" +#include "../../HeaderFiles/Enemy/Controllers/Zapper.h" +#include "../../HeaderFiles/Enemy/Controllers/ThunderSnake.h" +#include +namespace Enemy { + using namespace Global; + using namespace Time; + using namespace Controllers; + + + EnemyService::EnemyService() + { + std::srand(static_cast(std::time(nullptr))); + } + + EnemyService::~EnemyService() { + destroy(); + } + + void EnemyService::initialize() { + spawnTimer = enemySpawnDelay; + spawnEnemy(); + } + + void EnemyService::update() { + updateSpawnTimer(); + processEnemySpawn(); + + for (int i = 0; i < enemyList.size(); i++) + { + enemyList[i]->update(); + } + } + + void EnemyService::render() { + for (int i = 0; i < enemyList.size(); i++) + { + enemyList[i]->render(); + } + } + + void EnemyService::destroy() { + for (int i = 0; i < enemyList.size(); i++) + { + delete enemyList[i]; + } + } + + EnemyController * EnemyService::spawnEnemy() { + EnemyController* enemycontroller = createEnemy(getRandomEnemyType()); + + enemycontroller->initialize(); + + enemyList.push_back(enemycontroller); + return enemycontroller; + + } + void EnemyService::updateSpawnTimer() { + spawnTimer += ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + } + + // Inside the method to process enemy spawn, we will spawn an enemy if the spawn timer is more than the the interval. Then we will set time timer back to 0. + void EnemyService::processEnemySpawn() + { + if (spawnTimer >= enemySpawnDelay) + { + spawnEnemy(); + spawnTimer = 0.0f; + } + } + + EnemyController* EnemyService::createEnemy(EnemyType createenemy) + { + switch (createenemy) + { + case Enemy::EnemyType::ZAPPER: + //EnemyController* createenemy = new Zapper(); + return new Zapper(Enemy::EnemyType::ZAPPER); + + case Enemy::EnemyType::SUBZERO: + return new SubZero(Enemy::EnemyType::SUBZERO); + + case Enemy::EnemyType::THUNDERSNAKE: + return new ThunderSnake(Enemy::EnemyType::THUNDERSNAKE); + + //case Enemy::EnemyType::UFO: + //return new UFO(Enemy::EnemyType::UFO); + + + } + } + + EnemyType EnemyService::getRandomEnemyType() + { + int randomType = std::rand() % 3; + return static_cast (randomType); + } + + void EnemyService::destroyEnemy(EnemyController* enemycontroller) + { + // Erase the enemy_controller object from the enemy_list vector. + // std::remove rearranges the elements in the vector so that all elements + // that are equal to enemy_controller are moved to the end of the vector, + // then it returns an iterator pointing to the start of the removed elements. + // The erase function then removes those elements from the vector. + enemyList.erase(std::remove(enemyList.begin(), enemyList.end(), enemycontroller),enemyList.end()); + + delete (enemycontroller); + } + +} \ 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..f8e2dd454 --- /dev/null +++ b/Space-Invaders/Source/Enemy/EnemyView.cpp @@ -0,0 +1,74 @@ +#include "../../HeaderFiles/Enemy/EnemyView.h" +#include "../../HeaderFiles/Graphic/GraphicService.h" +#include "../../HeaderFiles/Enemy/EnemyController.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include "../../HeaderFiles/Enemy/Controllers/EnemyConfig.h" +#include "../../HeaderFiles/Enemy/EnemyModel.h" + +#include +using namespace std; + +namespace Enemy +{ + using namespace Global; + using namespace Graphic; + + EnemyView::EnemyView() + { + + } + + EnemyView::~EnemyView() { + + } + + void EnemyView::initialize(EnemyController * controller) + { + enemyController = controller; + gameWindow = ServiceLocator::getInstance()->GetGraphicService()->GetGameWindow(); + initializeEnemySprites(enemyController->getEnemyType()); + } + + void EnemyView::initializeEnemySprites(EnemyType setType) + { + switch (setType) + { + case Enemy::EnemyType::SUBZERO: + if (enemyTexture.loadFromFile(subzero_texture_path)) + { + enemySprite.setTexture(enemyTexture); + scaleEnemySprites(); + } + break; + case Enemy::EnemyType::ZAPPER: + if (enemyTexture.loadFromFile(zapper_texture_path)) + { + enemySprite.setTexture(enemyTexture); + scaleEnemySprites(); + } + break; + case Enemy::EnemyType::THUNDERSNAKE: + if (enemyTexture.loadFromFile(thundersnake_texture_path)) + { + enemySprite.setTexture(enemyTexture); + scaleEnemySprites(); + } + } + } + + void EnemyView::scaleEnemySprites() + { + enemySprite.setScale( + static_cast(enemySpriteWidth) / enemySprite.getTexture()->getSize().x, + static_cast(enemySpriteHeight) / enemySprite.getTexture()->getSize().y); + } + void EnemyView::update() { + enemySprite.setPosition(enemyController->getEnemyPosition()); + } + + void EnemyView::render() { + gameWindow->draw(enemySprite); + } + + +} \ No newline at end of file diff --git a/Space-Invaders/Source/Event/EventService.cpp b/Space-Invaders/Source/Event/EventService.cpp new file mode 100644 index 000000000..8d807b6c0 --- /dev/null +++ b/Space-Invaders/Source/Event/EventService.cpp @@ -0,0 +1,147 @@ +#include +#include "../../HeaderFiles/Main/GameService.h" +using namespace std; +#include "../../HeaderFiles/Event/EventService.h" +#include "../../HeaderFiles/Graphic/GraphicService.h" + +namespace Event +{ + using namespace Global; + + EventService::EventService() + { + //gameEvent = nullptr; + gameWindow = nullptr; + } + EventService::~EventService() + { + + } + void EventService::initialize() + { + gameWindow = ServiceLocator::getInstance()->GetGraphicService()->GetGameWindow(); + } + void EventService::update() + { + updateMouseButtonsState(leftMouseButtonState, sf::Mouse::Left); + updateMouseButtonsState(rightMouseButtonState, sf::Mouse::Right); + updateKeyboardButtonsState(A_ButtonState, sf::Keyboard::A); + updateKeyboardButtonsState(D_ButtonState, sf::Keyboard::D); + updateKeyboardButtonsState(leftArrowButtonState, sf::Keyboard::Left); + updateKeyboardButtonsState(rightArrowButtonState, sf::Keyboard::Right); + + + processEvents(); + } + void EventService::processEvents() + { + if (isGameWindowOpen()) { + while (gameWindow->pollEvent(gameEvent)) + { + cout << gameEvent.type; + if (gameWindowWasClosed() || hasQuitGame()) + { + gameWindow->close(); + } + } + } + } + + bool EventService::hasQuitGame() + { // only true if the ESC key is pressed and a keyboard event has been registered + //return (isKeyboardEvent() && pressedEscapeKey()); + if (isKeyboardEvent()) { + return pressedEscapeKey(); + } + + return false; + + } + bool EventService::pressedEscapeKey() { + return (gameEvent.key.code == sf::Keyboard::Escape); + + } + bool EventService::isKeyboardEvent() { + return (gameEvent.type == sf::Event::KeyPressed); + } + bool EventService::isGameWindowOpen() + { + return gameWindow != nullptr; + } + bool EventService::gameWindowWasClosed() + { + return gameEvent.type == sf::Event::Closed; + } + + bool EventService::pressedLeftKey() { + + return leftArrowButtonState == buttonState::HELD; + + + } + bool EventService::pressedRightKey() + { + return rightArrowButtonState == buttonState::HELD; + } + + bool EventService::pressedAKey() { + return A_ButtonState == buttonState::HELD; + } + + bool EventService::pressedDKey() { + return D_ButtonState == buttonState::HELD; + } + bool EventService::pressedLeftMouseButton() { + return leftMouseButtonState == buttonState::PRESSED; + } + + bool EventService::pressedRightMouseButton() { + return rightMouseButtonState == buttonState::PRESSED; + } + + void EventService::updateMouseButtonsState(buttonState &curentButtonState, sf::Mouse::Button mouseButton) + { + if (sf::Mouse::isButtonPressed(mouseButton)) + { + switch (curentButtonState) + { + case buttonState::RELEASED: + curentButtonState = buttonState::PRESSED; + break; + + case buttonState::PRESSED: + curentButtonState = buttonState::HELD; + break; + } + } + else + { + curentButtonState = buttonState::RELEASED; + } + + } + + void EventService::updateKeyboardButtonsState(buttonState ¤tButtonState, sf::Keyboard::Key keyboardButton) + { + if (sf::Keyboard::isKeyPressed(keyboardButton)) + { + switch (currentButtonState) + { + case buttonState::RELEASED: + currentButtonState = buttonState::PRESSED; + break; + + case buttonState::PRESSED: + currentButtonState = buttonState::HELD; + break; + } + } + else + { + currentButtonState = buttonState::RELEASED; + } + + + + } +} diff --git a/Space-Invaders/Source/Gameplay/GameplayController.cpp b/Space-Invaders/Source/Gameplay/GameplayController.cpp new file mode 100644 index 000000000..f611306c1 --- /dev/null +++ b/Space-Invaders/Source/Gameplay/GameplayController.cpp @@ -0,0 +1,32 @@ +#include "../../HeaderFiles/Gameplay/GameplayView.h" +#include "../../HeaderFiles/Gameplay/GameplayController.h" + +namespace Gameplay +{ + + GameplayController::GameplayController() + { + gameplayView = new GameplayView(); + } + + GameplayController::~GameplayController() + { + delete(gameplayView); + } + + void GameplayController::initialize() + { + gameplayView->initialize(); + } + + void GameplayController::update() + { + gameplayView->update(); + } + + void GameplayController::render() + { + gameplayView->render(); + } + +} \ No newline at end of file diff --git a/Space-Invaders/Source/Gameplay/GameplayService.cpp b/Space-Invaders/Source/Gameplay/GameplayService.cpp new file mode 100644 index 000000000..e3819f0b7 --- /dev/null +++ b/Space-Invaders/Source/Gameplay/GameplayService.cpp @@ -0,0 +1,30 @@ +#include "../../HeaderFiles/Gameplay/GameplayController.h" +#include "../../HeaderFiles/Gameplay/GameplayService.h" + +namespace Gameplay +{ + GameplayService::GameplayService() + { + gameplayController = new GameplayController(); + } + + GameplayService::~GameplayService() + { + delete (gameplayController); + } + + void GameplayService::initialize() + { + gameplayController->initialize(); + } + + void GameplayService::update() + { + gameplayController->update(); + } + + void GameplayService::render() + { + gameplayController->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..7e57ab21c --- /dev/null +++ b/Space-Invaders/Source/Gameplay/GameplayView.cpp @@ -0,0 +1,57 @@ +#include "../../HeaderFiles/Gameplay/GameplayView.h" +#include +#include "../../HeaderFiles/Graphic/GraphicService.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include +using namespace std; + +namespace Gameplay +{ + using namespace Global; + using namespace Graphic; + + GameplayView::GameplayView() + { + + } + + GameplayView::~GameplayView() + { + + } + + void GameplayView::initialize() + { + gameWindow = ServiceLocator::getInstance()->GetGraphicService()->GetGameWindow(); + initializeBackgroundSprite(); + + } + + void GameplayView::update() + { + + } + + void GameplayView::render() + { + gameWindow->draw(backgroundSprite); + } + + void GameplayView::initializeBackgroundSprite() + { + if (backgroundTexture.loadFromFile(backgroundTexturePath)) + { + backgroundSprite.setTexture(backgroundTexture); + scaleBackgroundSprite(); + } + } + void GameplayView::scaleBackgroundSprite() + { + backgroundSprite.setScale + ( + static_cast (gameWindow->getSize().x) / backgroundSprite.getTexture()->getSize().x, + static_cast (gameWindow->getSize().y) / backgroundSprite.getTexture()->getSize().y + ); + } + +} \ 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..e4974dc33 --- /dev/null +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -0,0 +1,157 @@ +#include +using namespace std; +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include "../../HeaderFiles/Main/GameService.h" + +namespace Global +{ + + using namespace Graphic; + using namespace Time; + using namespace Event; + using namespace Player; + using namespace UI; + using namespace Enemy; + using namespace Main; + using namespace Gameplay; + using namespace Element; + using namespace Sound; +// Initialize the static instance pointer +ServiceLocator* ServiceLocator::instance = nullptr; + +ServiceLocator::ServiceLocator(){ + graphic_service = nullptr; + event_service = nullptr; + player_service = nullptr; + time_service = nullptr; + ui_service = nullptr; + enemy_service = nullptr; + gameplay_service = nullptr; + element_service = nullptr; + sound_service = nullptr; + + + createServices(); +} +ServiceLocator::~ServiceLocator() { + + clearAllServices(); +} +// Get the single instance of ServiceLocator +ServiceLocator* ServiceLocator::getInstance() { + static ServiceLocator instance; + return &instance; +} +void ServiceLocator::initialize(){ + //initializes the service locator class + graphic_service->initialize(); + event_service->initialize(); + player_service->initialize(); + time_service->initialize(); + ui_service->initialize(); + enemy_service->initialize(); + gameplay_service->initialize(); + element_service->initialize(); + sound_service->initialize(); +} +void ServiceLocator::update() { + //Keeps on updating services required and updates the game state + graphic_service->update(); + time_service->update(); + event_service->update(); + + + if (GameService::getGameState() == GameState::GAMEPLAY) + { + gameplay_service->update(); + player_service->update(); + enemy_service->update(); + element_service->update(); + + } + ui_service->update(); +} +void ServiceLocator::render() { + //Keeps on rendering the new services + graphic_service->render(); + if (GameService::getGameState() == GameState::GAMEPLAY) + { + gameplay_service->render(); + player_service->render(); + enemy_service->render(); + element_service->render(); + + } + ui_service->render(); + + +} +void ServiceLocator::createServices() { + graphic_service = new GraphicService(); + event_service = new EventService(); + player_service = new PlayerService(); + time_service = new TimeService(); + ui_service = new UIService(); + enemy_service = new EnemyService(); + gameplay_service = new GameplayService(); + element_service = new ElementService(); + sound_service = new SoundService(); + ; +} +void ServiceLocator::clearAllServices() { + delete(graphic_service); // Delete the graphic_service instance + graphic_service = nullptr; // Reset pointer to null to avoid dangling pointer + delete (event_service); + event_service = nullptr; + delete (player_service); + player_service = nullptr; + delete(time_service); + time_service = nullptr; + delete (ui_service); + ui_service = nullptr; + delete(enemy_service); + enemy_service = nullptr; + delete(gameplay_service); + gameplay_service = nullptr; + delete(element_service); + element_service = nullptr; + delete(sound_service); + sound_service = nullptr; +} + +// Returns a pointer to the currently set graphic service. + + GraphicService* ServiceLocator::GetGraphicService() +{ return graphic_service; } + EventService* ServiceLocator::GetEventService() +{ return event_service; } + PlayerService* ServiceLocator::GetPlayerService() +{ return player_service; } + TimeService* ServiceLocator::GetTimeService() +{ return time_service; } + UIService* ServiceLocator::GetUIService(){ + return ui_service; + } + EnemyService* ServiceLocator::GetEnemyService() { + return enemy_service; + } + GameplayService* ServiceLocator::GetGameplayService() { + return gameplay_service; + } + + ElementService* ServiceLocator::GetElementService() + { + return element_service; + } + + SoundService* ServiceLocator::GetSoundService() + { + return sound_service; + } + + +} + + //ServiceLocator:: static ServiceLocator* getInstance() { + +//} diff --git a/Space-Invaders/Source/Global/config.cpp b/Space-Invaders/Source/Global/config.cpp new file mode 100644 index 000000000..ba7f027c4 --- /dev/null +++ b/Space-Invaders/Source/Global/config.cpp @@ -0,0 +1,56 @@ +#include "../../HeaderFiles/Global/config.h" + +namespace Global +{ + const sf::String Config::outscallogoTexturePath = "assets/textures/outscal_logo.png"; + + const sf::String Config::backgroundTexturePath = "assets/textures/space_invaders_bg.png"; + + const sf::String Config::playerTexturePath = "assets/textures/player_ship.png"; + + + const sf::String Config::zapperTexturePath = "assets/textures/zapper.png"; + + const sf::String Config::thundersnakeTexturePath = "assets/textures/thunder_snake.png"; + + const sf::String Config::subzeroTexturePath = "assets/textures/subzero.png"; + + const sf::String Config::ufoTexturePath = "assets/textures/ufo.png"; + + const sf::String Config::bunkerTexturePath = "assets/textures/bunker.png"; + + + const sf::String Config::shieldTexturePath = "assets/textures/shield.png"; + + const sf::String Config::tripplelaserTexturePath = "assets/textures/tripple_laser.png"; + + const sf::String Config::rapidfireTexturePath = "assets/textures/rapid_fire.png"; + + const sf::String Config::outscalbombTexturePath = "assets/textures/outscal_bomb.png"; + + + const sf::String Config::laserbulletTexturePath = "assets/textures/laser_bullet.png"; + + const sf::String Config::torpedoTexturePath = "assets/textures/torpedoe.png"; + + const sf::String Config::frostbulletTexturePath = "assets/textures/frost_beam.png"; + + + const sf::String Config::playbuttonTexturePath = "assets/textures/play_button.png"; + + const sf::String Config::instructionsbuttonTexturePath = "assets/textures/instructions_button.png"; + + const sf::String Config::quitbuttonTexturePath = "assets/textures/quit_button.png"; + + const sf::String Config::menubuttonTexturePath = "assets/textures/menu_button.png"; + + + const sf::String Config::bubblebobbleFontPath = "assets/fonts/bubbleBobble.ttf"; + + const sf::String Config::DS_DIGIB_font_path = "assets/fonts/DS_DIGIB.ttf"; + + + const sf::String Config::backgroundMusicPath = "assets/sounds/background_music.mp3"; + + const sf::String Config::buttonclickSoundPath = "assets/sounds/button_click_sound.wav"; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Graphic/GraphicService.cpp b/Space-Invaders/Source/Graphic/GraphicService.cpp new file mode 100644 index 000000000..578553de8 --- /dev/null +++ b/Space-Invaders/Source/Graphic/GraphicService.cpp @@ -0,0 +1,49 @@ +#include +#include "../../HeaderFiles/Graphic/GraphicService.h" +#include +using namespace std; + +namespace Graphic { + //Constructor + GraphicService::GraphicService() { + gameWindow = nullptr; // Initializes game window pointer to null + videoMode = nullptr; // Initializes video mode pointer to null + } + GraphicService::~GraphicService() { + OnDestroy(); // Calls onDestroy method to clean up resources + } + void GraphicService::initialize() { + gameWindow = CreateGameWindow(); // Assigns a new game window to the game_window pointer + gameWindow->setFramerateLimit(frameRate); + } + void GraphicService::OnDestroy() { + delete videoMode; // Deletes the video mode object + delete gameWindow; // Deletes the game window object + + } + sf::RenderWindow* GraphicService::CreateGameWindow() { + SetVideoMode(); + return new sf::RenderWindow(*videoMode, gameWindowTitle,sf::Style::Fullscreen); + } + // Sets up the video mode for the game window using specified dimensions and system's color depth. + void GraphicService::SetVideoMode() { + videoMode = new sf::VideoMode(gameWindowHeight, gameWindowWidth, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode + } + sf::RenderWindow* GraphicService::GetGameWindow() const { + return gameWindow; + } + sf::Color GraphicService::GetWindowColor() const { + return gameWindowColor; + } + + void GraphicService::update() { + + } + void GraphicService::render() { + + } + bool GraphicService::isGameWindowOpen() const { + + return gameWindow->isOpen(); + } +} diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp new file mode 100644 index 000000000..9f743be6b --- /dev/null +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -0,0 +1,83 @@ +#include +using namespace std; +#include "../../HeaderFiles/Graphic/GraphicService.h" +#include "../../HeaderFiles/Main/GameService.h" + + +namespace Main +{ + using namespace Global; + + GameState GameService::currentState = GameState::BOOT; + + void GameService::initialize() // initialization of game + { + service_locator->initialize(); + initializeVariables();//INitialize variables + showMainMenu(); + + + } + void GameService::destroy() // Destroys game objects when destroyed + { + delete(gameWindow); + } + void GameService::initializeVariables() { + gameWindow = service_locator->GetGraphicService()->GetGameWindow(); //set game window (it was null before this) + + } + GameService::GameService() // Default Constructor + { + service_locator = nullptr; + gameWindow = nullptr; // Initializes game window pointer to null + //gameloop + } + GameService::~GameService() // Deconstructor + { + // end of game when player exits + destroy(); + } + + void GameService::update() //Updates the game objects each frame + { + service_locator->GetEventService()->processEvents(); + service_locator->update(); + + } + void GameService::ignite() { // Initiates the game + service_locator = Global::ServiceLocator::getInstance(); + initialize(); + + + } + void GameService::render() { //Renders each object each frame + gameWindow->clear(service_locator->GetGraphicService()->GetWindowColor()); + service_locator->render(); + gameWindow->display(); + + + } + bool GameService::isRunning() // Keeps on checking if the game is running + { + return service_locator->GetGraphicService()->isGameWindowOpen(); + } + + void GameService::setGameState(GameState newState) { + + currentState = newState; + + } + GameState GameService::getGameState() { + return currentState; + } + + void GameService::showMainMenu() { + setGameState(GameState::MAINMENU); + } +} + + + + + + diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp new file mode 100644 index 000000000..6cd9830e7 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -0,0 +1,82 @@ +#include +#include "../../HeaderFiles/Player/PlayerController.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include "../../HeaderFiles/Event/EventService.h" +#include "../../HeaderFiles/Player/PlayerModel.h" +#include"../../HeaderFiles/Player/PlayerView.h" +#include + +namespace Player { + + + PlayerController::PlayerController() { + playerView = new PlayerView(); + playerModel = new PlayerModel(); + } + PlayerController::~PlayerController() { + delete (playerView); + delete (playerModel); + } + + void PlayerController::initialize() { + + playerModel->initialize(); + playerView->initialize(this); + } + + void PlayerController::update() { + + playerView->update(); + ProcessPlayerInput(); + + } + + void PlayerController::render() { + playerView->render(); + + } + sf::Vector2f PlayerController::getPlayerPosition() { + return playerModel->getPlayerPosition(); + } + + + void PlayerController::ProcessPlayerInput() { + + Event::EventService* eventService = Global::ServiceLocator::getInstance()->GetEventService(); + + if (eventService->pressedLeftKey() || eventService->pressedAKey()) + { + moveLeft(); + } + + if (eventService->pressedRightKey() || eventService->pressedDKey()) + { + moveRight(); + } + + } + + void PlayerController::moveLeft() { + + sf::Vector2f currentPosition = playerModel->getPlayerPosition(); + currentPosition.x -= playerModel->playerMovementSpeed * Global::ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + currentPosition.x = std::max(currentPosition.x, playerModel->leftScreenSide.x); + playerModel->setPlayerPosition(currentPosition); + + + } + + void PlayerController::moveRight() { + sf::Vector2f currentPosition = playerModel->getPlayerPosition(); + currentPosition.x += playerModel->playerMovementSpeed * Global::ServiceLocator::getInstance()->GetTimeService()->getDeltaTime(); + + currentPosition.x = std::min(currentPosition.x, playerModel->rightScreenSide.x); + playerModel->setPlayerPosition(currentPosition); + } +} + + + + + diff --git a/Space-Invaders/Source/Player/PlayerModel.cpp b/Space-Invaders/Source/Player/PlayerModel.cpp new file mode 100644 index 000000000..86365495c --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerModel.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; +#include "../../HeaderFiles/Player/PlayerModel.h" + +namespace Player { + PlayerModel::PlayerModel() { + + } + PlayerModel::~PlayerModel() { + + } + void PlayerModel::initialize() + { + Reset(); + } + void PlayerModel::Reset() { + playerState = PlayerState::ALIVE; //set state to alive + playerPosition = initialPlayerPosition; + playerScore = 0; + + } + + sf::Vector2f PlayerModel::getPlayerPosition() + { + return playerPosition; + + } + void PlayerModel::setPlayerPosition(sf::Vector2f position) { + playerPosition = position; + } + + int PlayerModel::getPlayerScore() { + return playerScore; + } + + void PlayerModel::setPlayerScore(int score) { + playerScore = score; + } + PlayerState PlayerModel::getPlayerState() { + return playerState; + } + void PlayerModel::setPlayerState(PlayerState state) + { + playerState = 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..23693ad67 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerService.cpp @@ -0,0 +1,33 @@ +#include "../../HeaderFiles/Player/PlayerService.h" +#include "../../HeaderFiles/Player/PlayerController.h" + +namespace Player { + + PlayerService::PlayerService() + { + playerController = new PlayerController(); + } + + PlayerService::~PlayerService() + { + delete (playerController); + } + + void PlayerService::initialize() + { + playerController->initialize(); + + } + + void PlayerService::update() + { + playerController->update(); + + } + + void PlayerService::render() + { + playerController->render(); + } +} + diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp new file mode 100644 index 000000000..13cad33ae --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerView.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +#include "../../HeaderFiles/Player/PlayerView.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include "../../HeaderFiles/Player/PlayerController.h" + +namespace Player { + + using namespace Global; + + void PlayerView::initialize() { + gameWindow = ServiceLocator::getInstance()->GetGraphicService()->GetGameWindow(); + initializePlayerSprite(); + } + + void PlayerView::initializePlayerSprite() + { + if (playerTexture.loadFromFile(player_texture_path)) + { + playerSprite.setTexture(playerTexture); + } + scalePlayerSprite(); + } + + void PlayerView::scalePlayerSprite() { + //Here we find the factor to scale our sprites with. Ignore the static_cast for now, we will discuss it later. + + playerSprite.setScale( + static_cast(spriteWidth) / playerSprite.getTexture()->getSize().x, + static_cast(spriteHeight) / playerSprite.getTexture()->getSize().y); + } + + PlayerView::PlayerView() { + + } + PlayerView::~PlayerView() { + + } + + void PlayerView::initialize(PlayerController* controller) { + + playerController = controller; + gameWindow = ServiceLocator::getInstance()->GetGraphicService()->GetGameWindow(); + initializePlayerSprite(); + } + + void PlayerView::update() { + playerSprite.setPosition(playerController->getPlayerPosition()); + + } + + void PlayerView::render() { + gameWindow->draw(playerSprite); + } +} + diff --git a/Space-Invaders/Source/Sound/SoundService.cpp b/Space-Invaders/Source/Sound/SoundService.cpp new file mode 100644 index 000000000..6b2e53ca4 --- /dev/null +++ b/Space-Invaders/Source/Sound/SoundService.cpp @@ -0,0 +1,64 @@ +#include "../../HeaderFiles/Sound/SoundService.h" +#include "../../HeaderFiles/Global/config.h" +#include +using namespace std; + +namespace Sound +{ + using namespace Global; + + SoundService::SoundService() + { + + } + + SoundService::~SoundService() + { + + } + + void SoundService::initialize() + { + loadBackgroundMusic(); + loadSoundEffects(); + } + + void SoundService::loadBackgroundMusic() + { + if (!backgroundMusic.openFromFile(Config::backgroundMusicPath)) + { + cout << "Eror while loading background music file " << endl; + } + } + + void SoundService::loadSoundEffects() + { + if (!buttonClick.loadFromFile(Config::buttonclickSoundPath)) + { + cout << "Error while loading button click sound file " << endl; + } + } + + void SoundService::playBackgroundMusic() + { + backgroundMusic.setLoop(true); + backgroundMusic.setVolume(backgroundMusicVolume); + backgroundMusic.play(); + } + + void SoundService::playSound(SoundType soundType) + { + switch (soundType) + { + case SoundType::BUTTONCLICK: + soundEffects.setBuffer(buttonClick); + break; + default: + cout << "Invalid sound type" << endl; + return; + } + + soundEffects.play(); + } + +} \ 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..8ed339492 --- /dev/null +++ b/Space-Invaders/Source/Time/TimeService.cpp @@ -0,0 +1,40 @@ +#include "../../HeaderFiles/Time/TimeService.h" + +namespace Time { + + void TimeService::initialize() { + previous_time = std::chrono::steady_clock::now(); + delta_time = 0; + } + + void TimeService::update() { + updateDeltaTime(); + } + + void TimeService::updateDeltaTime() { + delta_time = calculateDeltaTime(); + updatePreviousTime(); + } + float TimeService::getDeltaTime() { + return delta_time; + } + + float TimeService::calculateDeltaTime() + { + int delta = std::chrono::duration_cast( + std::chrono::steady_clock::now() - previous_time).count(); + + // The cast is used to convert delta time from microseconds into seconds. + // We will learn aboit how this works in detail later. + return static_cast(delta) / static_cast(1000000); + } + + + void TimeService::updatePreviousTime() { //Updated the previous time to a new time. To keep the difference between frames same the second delta time becomes the first + previous_time = std::chrono::steady_clock::now(); + } +} + + + + diff --git a/Space-Invaders/Source/UIService/MainMenuUIController/MainMenuUIController.cpp b/Space-Invaders/Source/UIService/MainMenuUIController/MainMenuUIController.cpp new file mode 100644 index 000000000..811e7f871 --- /dev/null +++ b/Space-Invaders/Source/UIService/MainMenuUIController/MainMenuUIController.cpp @@ -0,0 +1,167 @@ +#include "../../HeaderFiles/UIService/MainMenuUIController/MainMenuUIController.h" +#include "../../HeaderFiles/Main/GameService.h" +#include "../../HeaderFiles/Graphic/GraphicService.h" +#include "../../HeaderFiles/Global/ServiceLocator.h" +#include + + +using namespace std; + +namespace UI { + + namespace MainMenu { + + using namespace Global; + using namespace Main; + using namespace Graphic; + using namespace Event; + using namespace Sound; + + MainMenuUIController::MainMenuUIController() + { + gameWindow = nullptr; + } + + MainMenuUIController::~MainMenuUIController() + { + + } + + void MainMenuUIController::initialize() + { + gameWindow = ServiceLocator::getInstance()->GetGraphicService()->GetGameWindow(); + initializeBGImage(); + initializeButtonSprites(); + + } + + void MainMenuUIController::initializeBGImage() { + + if (bgTexture.loadFromFile(bgTexture_Path)) + { + bgSprite.setTexture(bgTexture); + scaleBGImage(); + } + } + void MainMenuUIController::scaleBGImage() + { + // Don't worry about the static_cast we will discuss it later. For now know that this function is + // just scaling our background image based on the size of the game window + + bgSprite.setScale( + static_cast(gameWindow->getSize().x) / bgSprite.getTexture()->getSize().x, + static_cast(gameWindow->getSize().y) / bgSprite.getTexture()->getSize().y + ); + } + + bool MainMenuUIController::loadButtonTexturesFromFile() + { + return playButtonTexture.loadFromFile(playButtonTexture_Path) && + instructionTexture.loadFromFile(instructionTexture_Path) && + quitButtonTexture.loadFromFile(quitButtonTexture_Path); + + } + + + void MainMenuUIController::initializeButtonSprites() { + if (loadButtonTexturesFromFile()) + { + setButtonSprites(); + scaleAllButtons(); + positionButtonSprites(); + } + + } + + void MainMenuUIController::setButtonSprites() + { + playButtonSprite.setTexture(playButtonTexture); + instructionSprite.setTexture(instructionTexture); + quitButtonSprite.setTexture(quitButtonTexture); + + } + + void MainMenuUIController::positionButtonSprites() + { + float xPosition = (static_cast(gameWindow->getSize().x) / 2) - buttonWidth / 2; + + playButtonSprite.setPosition( { xPosition, 500.0f }); + quitButtonSprite.setPosition( { xPosition, 900.0f }); + instructionSprite.setPosition({ xPosition, 700.0f }); + + } + + void MainMenuUIController::scaleAllButtons() + { + scaleButton(&playButtonSprite); + scaleButton(&instructionSprite); + scaleButton(&quitButtonSprite); + + } + + void MainMenuUIController::scaleButton(sf::Sprite* buttonToScale) + { + buttonToScale->setScale + ( buttonWidth / buttonToScale->getTexture()->getSize().x, + buttonHeight/ buttonToScale->getTexture()->getSize().y + ); + + + } + void MainMenuUIController::processButtonInteractions() + { + + sf::Vector2f mousePosition = sf::Vector2f(sf::Mouse::getPosition()); + if (clickedButton(&playButtonSprite, mousePosition)) + { + ServiceLocator::getInstance()->GetSoundService()->playBackgroundMusic(); + ServiceLocator::getInstance()->GetSoundService()->playSound(SoundType::BUTTONCLICK); + + GameService::setGameState(GameState::GAMEPLAY); + + } + + if (clickedButton(&instructionSprite, mousePosition)) + { + cout << "You are seeing the game instructions " << endl; + ServiceLocator::getInstance()->GetSoundService()->playSound(SoundType::BUTTONCLICK); + } + + if (clickedButton(&quitButtonSprite, mousePosition)) + { + ServiceLocator::getInstance()->GetSoundService()->playSound(SoundType::BUTTONCLICK); + gameWindow->close(); + } + + + + + + + } + bool MainMenuUIController::clickedButton(sf::Sprite* buttonSprite, sf::Vector2f mousePositon) { + EventService* event_service = ServiceLocator::getInstance()->GetEventService(); + return event_service->pressedLeftMouseButton() && buttonSprite->getGlobalBounds().contains(mousePositon); + + } + + void MainMenuUIController::update() + { + processButtonInteractions(); + + } + + void MainMenuUIController::render() + { + gameWindow->draw(bgSprite); + gameWindow->draw(quitButtonSprite); + gameWindow->draw(instructionSprite); + gameWindow->draw(playButtonSprite); + + + } + + } + + +} \ No newline at end of file diff --git a/Space-Invaders/Source/UIService/UIService.cpp b/Space-Invaders/Source/UIService/UIService.cpp new file mode 100644 index 000000000..90cee71f8 --- /dev/null +++ b/Space-Invaders/Source/UIService/UIService.cpp @@ -0,0 +1,61 @@ +#include +#include "../../HeaderFiles/UIService/UIService.h" +//#include "../../HeaderFiles/UIService/MainMenuUIController/MainMenuUIController.h" +#include "../../HeaderFiles/Main/GameService.h" + + +namespace UI +{ + using namespace MainMenu; + using namespace Main; + + UIService::UIService() + { + mainMenuUIController = nullptr; + createUIControllers(); + } + + UIService::~UIService() + { + destroy(); + } + void UIService::createUIControllers() + { + mainMenuUIController = new MainMenuUIController(); + } + void UIService::initializeControllers() + { + mainMenuUIController->initialize(); + } + + void UIService::update() { + switch (GameService::getGameState()) + { + case GameState::MAINMENU: + return mainMenuUIController->update(); + break; + + } + } + + void UIService::render() + { + switch (GameService::getGameState()) + { + case GameState::MAINMENU: + return mainMenuUIController->render(); + break; + } + } + + void UIService::initialize() + { + createUIControllers(); + mainMenuUIController->initialize(); + } + + void UIService::destroy() + { + delete (mainMenuUIController); + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/trial.cpp b/Space-Invaders/Source/trial.cpp new file mode 100644 index 000000000..ff9d428e6 --- /dev/null +++ b/Space-Invaders/Source/trial.cpp @@ -0,0 +1,105 @@ +/* class Player { + +private: + int playerScore = 0; + int health = 1; + int movementSpeed = 10; + sf::Vector2f position = sf::Vector2f(300.0f, 450.0f); + + +public: + sf::Texture playershiptexture; + sf::Sprite playershipsprite; + + int getScore() + { + return playerScore; + } + void setScore(int newScore) { + playerScore = newScore; + } + int getMoveSpeed() + { + return movementSpeed; + } + void setMoveSpeed(int newMoveSpeed) + { + movementSpeed = newMoveSpeed; + } + + void takeDamage() { + + cout << "Player took damage" << endl; + }; + + void move(float offsetX) { + position.x += offsetX; + }; + + void shootBullets() { + cout << "Player is shooting bullets" << endl; + }; + + sf::Vector2f getPosition() + { + return position; + } + + + +}; */ + +int main() +{ + /* //render a blue colored window + sf::VideoMode videomode = sf::VideoMode(800, 600); + sf::RenderWindow window(videomode, "SFML Window"); + Player player; + + player.playershiptexture.loadFromFile("assets/textures/player_ship.png"); + + player.playershipsprite.setTexture(player.playershiptexture); // sprite.setTexture(texture); + // set position, rotation, scale and more + + // player.playershipsprite.setPosition(300, 450); + player.playershipsprite.setRotation(0); + player.playershipsprite.setScale(1, 1); + cout << "Player score is " << player.getScore() << endl;; + player.setScore(100); + cout <<"New player score: "< + @@ -133,6 +133,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index ce0c35ccf..52ca778da 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -18,5 +18,223 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + 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 + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header 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/main.cpp b/Space-Invaders/main.cpp index 7d5f90dff..9ec26ed95 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,24 @@ -int main() -{ - return 0; -} \ No newline at end of file +#include +using namespace std; +#include "../../HeaderFiles/Main/GameService.h" +#include + +using namespace Main; + +int main() { + + GameService* game_service = new GameService(); + // EventService* event_service = new EventService(); + + game_service->ignite(); + + while (game_service->isRunning()) + { + game_service->update(); + game_service->render(); + + } +} + +