From d67394aaec3e7638e60885799423b8cd47f08e67 Mon Sep 17 00:00:00 2001 From: UnEpicier Date: Tue, 23 Jan 2024 11:40:46 +0100 Subject: [PATCH] feat(shield): added shields --- Space-Invader.vcxproj | 8 +++++ Space-Invader.vcxproj.filters | 6 ++++ include/GameManager.h | 8 +++-- include/Laser.h | 3 +- include/Player.h | 5 +++- include/Shield.h | 26 +++++++++++++++++ lib/GameManager.cpp | 55 ++++++++++++++++++++++++++++++++++- lib/Laser.cpp | 4 +-- lib/Player.cpp | 9 ++++-- lib/Shield.cpp | 13 +++++++++ 10 files changed, 127 insertions(+), 10 deletions(-) create mode 100644 include/Shield.h create mode 100644 lib/Shield.cpp diff --git a/Space-Invader.vcxproj b/Space-Invader.vcxproj index 0813c20..6d61d67 100644 --- a/Space-Invader.vcxproj +++ b/Space-Invader.vcxproj @@ -70,6 +70,12 @@ + + true + + + true + true @@ -137,6 +143,7 @@ + @@ -144,6 +151,7 @@ + diff --git a/Space-Invader.vcxproj.filters b/Space-Invader.vcxproj.filters index 1ac28a3..76cad6b 100644 --- a/Space-Invader.vcxproj.filters +++ b/Space-Invader.vcxproj.filters @@ -30,6 +30,9 @@ Fichiers sources + + Fichiers sources + @@ -41,5 +44,8 @@ Fichiers d%27en-tĂȘte + + Fichiers d%27en-tĂȘte + \ No newline at end of file diff --git a/include/GameManager.h b/include/GameManager.h index 5c1cab0..d67ed5d 100644 --- a/include/GameManager.h +++ b/include/GameManager.h @@ -4,6 +4,7 @@ #include #include "Player.h" #include "Laser.h" +#include "Shield.h" using namespace std; using namespace sf; @@ -15,14 +16,17 @@ public: void run(); void handleInputs(); + void handleLasersCollision(); private: Clock _clock; - RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen }; + //RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen }; + RenderWindow _window{ VideoMode(1024, 800), "Space Invader", Style::Close | Style::Titlebar}; Player _player = { _window.getSize() }; - vector _lasers; + vector _lasers; + vector _shields; }; diff --git a/include/Laser.h b/include/Laser.h index 6dc164c..a25031c 100644 --- a/include/Laser.h +++ b/include/Laser.h @@ -9,7 +9,8 @@ using namespace sf; class Laser { public: - Laser(); + Laser() {}; + Laser(Window& window); RectangleShape& getShape() { return _laser; } diff --git a/include/Player.h b/include/Player.h index 59019e8..1d54e42 100644 --- a/include/Player.h +++ b/include/Player.h @@ -1,7 +1,10 @@ #pragma once +#define _USE_MATH_DEFINES + #include #include +#include using namespace std; using namespace sf; @@ -11,7 +14,7 @@ class Player public: Player(Vector2u screenSize); - CircleShape getShape() { return _shape; }; + CircleShape& getShape() { return _shape; }; void move(int direction); private: diff --git a/include/Shield.h b/include/Shield.h new file mode 100644 index 0000000..861b3ec --- /dev/null +++ b/include/Shield.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include + +using namespace sf; +using namespace std; + +class Shield +{ +public: + Shield() {}; + Shield(Window& window); + + void setPosition(Vector2f position); + + RectangleShape& getShape() { return _shape; }; + + int getLife() const { return _life; }; + void decreaseLife() { _life -= 1; }; + +private: + int _life = 10; + RectangleShape _shape; +}; \ No newline at end of file diff --git a/lib/GameManager.cpp b/lib/GameManager.cpp index 23bd138..3b3f95d 100644 --- a/lib/GameManager.cpp +++ b/lib/GameManager.cpp @@ -2,6 +2,13 @@ GameManager::GameManager() { _window.setVerticalSyncEnabled(true); + + for (int i = 0; i < 4; i++) { + Shield* shield = new Shield(_window); + shield->setPosition(Vector2f(_window.getSize().x / 4.25 * i, _window.getSize().y * 0.9)); + + _shields.push_back(shield); + } } void GameManager::run() { @@ -22,12 +29,17 @@ void GameManager::run() { // Draw everything _window.draw(_player.getShape()); + for (auto shield : _shields) { + _window.draw(shield->getShape()); + } + for (vector::iterator it = _lasers.begin(); it < _lasers.end();) { _window.draw((*it)->getShape()); (*it)->getShape().move(0, -5); if ((*it)->getShape().getPosition().y < -(*it)->getShape().getScale().y) { + delete* it; it = _lasers.erase(it); continue; } @@ -35,11 +47,18 @@ void GameManager::run() { ++it; } + handleLasersCollision(); + _window.display(); } } void GameManager::handleInputs() { + // Exit event + if (Keyboard::isKeyPressed(Keyboard::Escape)) { + _window.close(); + } + // Player movements if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) { _player.move(-1); @@ -54,7 +73,7 @@ void GameManager::handleInputs() { const Vector2f playerPos = _player.getShape().getPosition(); const float playerRadius = _player.getShape().getRadius(); - Laser* laser = new Laser(); + Laser* laser = new Laser(_window); laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius)); _lasers.push_back(laser); @@ -62,4 +81,38 @@ void GameManager::handleInputs() { _clock.restart(); } } +} + +void GameManager::handleLasersCollision() { + bool restartClock = false; + for (vector::iterator laserIt = _lasers.begin(); laserIt < _lasers.end();) { + bool deleteLaser = false; + for (vector::iterator shieldIt = _shields.begin(); shieldIt < _shields.end();) { + if ((*laserIt)->getShape().getGlobalBounds().intersects((*shieldIt)->getShape().getGlobalBounds())) { + if ((*shieldIt)->getLife() - 1 == 0) { + delete* shieldIt; + shieldIt = _shields.erase(shieldIt); + } + + (*shieldIt)->decreaseLife(); + deleteLaser = true; + break; + } + ++shieldIt; + } + + if (deleteLaser) { + delete* laserIt; + laserIt = _lasers.erase(laserIt); + + restartClock = true; + continue; + } + + ++laserIt; + } + + if (restartClock) { + _clock.restart(); + } } \ No newline at end of file diff --git a/lib/Laser.cpp b/lib/Laser.cpp index 8d287b4..62703f1 100644 --- a/lib/Laser.cpp +++ b/lib/Laser.cpp @@ -1,8 +1,8 @@ #include "Laser.h" -Laser::Laser() +Laser::Laser(Window& _window) { - _laser = RectangleShape(Vector2f(4, 30)); + _laser = RectangleShape(Vector2f(_window.getSize().x / 600, _window.getSize().y / 30)); _laser.setFillColor(Color::White); } diff --git a/lib/Player.cpp b/lib/Player.cpp index 235e078..6fe7ac0 100644 --- a/lib/Player.cpp +++ b/lib/Player.cpp @@ -1,14 +1,17 @@ #include "Player.h"; Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) { - _shape = CircleShape(40, 3); + _shape = CircleShape(screenSize.x / 70, 3); _shape.setFillColor(Color(52, 252, 5)); - _shape.setPosition(_screenWidth / 2.f, screenSize.y - _shape.getScale().y - 50); + + float shapeHeight = _shape.getRadius() * 1.5; + + _shape.setPosition(_screenWidth / 2.f, screenSize.y - shapeHeight); } void Player::move(int direction) { float max = _screenWidth - (_shape.getRadius() * 2); - float result = 5 * direction; + float result = (_screenWidth / 724.0)*direction; // Prevent leaving the window if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) { diff --git a/lib/Shield.cpp b/lib/Shield.cpp new file mode 100644 index 0000000..a877c88 --- /dev/null +++ b/lib/Shield.cpp @@ -0,0 +1,13 @@ +#include "Shield.h" + +Shield::Shield(Window& window) { + int shapeWidth = window.getSize().x / 10; + int shapeHeight = window.getSize().y / 35; + + _shape = RectangleShape(Vector2f(shapeWidth, shapeHeight)); + _shape.setFillColor(Color(Color(52, 252, 5))); +} + +void Shield::setPosition(Vector2f position) { + _shape.setPosition(position.x + _shape.getSize().x, position.y - _shape.getScale().y); +} \ No newline at end of file