From 9b0a7a78bba1c8c1bc573b28fd898629bffeb39d Mon Sep 17 00:00:00 2001 From: UnEpicier Date: Tue, 9 Jan 2024 12:26:42 +0100 Subject: [PATCH] feat(laser): created laser object Created laser object without any collision detection --- Inputs.cpp | 32 +++++++++++++++++----------- Inputs.h | 10 ++++++--- Laser.cpp | 11 ++++++++++ Laser.h | 21 +++++++++++++++++++ Player.cpp | 4 ++-- Player.h | 4 ++-- Space-Invader.vcxproj | 2 ++ Space-Invader.vcxproj.filters | 6 ++++++ main.cpp | 39 ++++++++++++++++++++++++++++------- main.h | 1 + 10 files changed, 103 insertions(+), 27 deletions(-) create mode 100644 Laser.cpp create mode 100644 Laser.h diff --git a/Inputs.cpp b/Inputs.cpp index a82c21c..f3dab5d 100644 --- a/Inputs.cpp +++ b/Inputs.cpp @@ -1,19 +1,27 @@ #include "Inputs.h" -void Inputs::HandleInputs(Event& event) { - if (event.type == Event::Closed) - { - _window.close(); - return; +void Inputs::HandleInputs() { + // Player movements + if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) { + _player.move(-1); } - // Player movements - if (event.type == Event::KeyPressed) { - if (event.key.code == Keyboard::Left || event.key.code == Keyboard::Q) { - _player.Move(-1); - } - if (event.key.code == Keyboard::Right || event.key.code == Keyboard::D) { - _player.Move(1); + if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) { + _player.move(1); + } + + if (Keyboard::isKeyPressed(Keyboard::Space)) { + if (_clock.getElapsedTime().asMilliseconds() > 500) { + const Vector2f playerPos = _player.PlayerShape.getPosition(); + const float playerRadius = _player.PlayerShape.getRadius(); + + Laser laser; + laser.setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius)); + + _lasers.push_back(laser.getLaser()); + + _clock.restart(); } + } } \ No newline at end of file diff --git a/Inputs.h b/Inputs.h index 66575cd..0d874b6 100644 --- a/Inputs.h +++ b/Inputs.h @@ -1,7 +1,10 @@ #pragma once +#include #include +#include #include "Player.h"; +#include "Laser.h"; using namespace std; using namespace sf; @@ -9,12 +12,13 @@ using namespace sf; class Inputs { public: - Inputs(RenderWindow& window, Player& player) : _window(window), _player(player) {}; + Inputs(Player& player, vector& lasers) : _player(player), _lasers(lasers) {}; - void HandleInputs(Event& event); + void HandleInputs(); private: - RenderWindow& _window; Player& _player; + Clock _clock; + vector& _lasers; }; diff --git a/Laser.cpp b/Laser.cpp new file mode 100644 index 0000000..0c1132b --- /dev/null +++ b/Laser.cpp @@ -0,0 +1,11 @@ +#include "Laser.h" + +Laser::Laser() +{ + _laser = RectangleShape(Vector2f(2, 20)); + _laser.setFillColor(Color::White); +} + +void Laser::setInitialPosition(Vector2f position) { + _laser.setPosition(Vector2f(position.x - (_laser.getScale().x / 2), position.y)); +} \ No newline at end of file diff --git a/Laser.h b/Laser.h new file mode 100644 index 0000000..2051b6a --- /dev/null +++ b/Laser.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +using namespace std; +using namespace sf; + +class Laser +{ +public: + Laser(); + + RectangleShape getLaser() { return _laser; } + + void setInitialPosition(Vector2f position); + +private: + RectangleShape _laser; +}; + diff --git a/Player.cpp b/Player.cpp index dc544f0..cfb68af 100644 --- a/Player.cpp +++ b/Player.cpp @@ -1,13 +1,13 @@ #include "Player.h"; -void Player::DrawPlayer() { +void Player::drawPlayer() { PlayerShape.setFillColor(Color(52, 252, 5)); PlayerShape.setPosition(Vector2f(_window.getSize().x / 2 + _x, _window.getSize().y - PlayerShape.getScale().y - 50)); _window.draw(PlayerShape); } -void Player::Move(int direction) { +void Player::move(int direction) { int result = _x + _velocity * direction; int minX = _window.getSize().x / 2 * -1; diff --git a/Player.h b/Player.h index 3bae875..cd6da16 100644 --- a/Player.h +++ b/Player.h @@ -13,8 +13,8 @@ public: CircleShape PlayerShape = CircleShape(20, 3); - void DrawPlayer(); - void Move(int direction); + void drawPlayer(); + void move(int direction); private: int _x = 0; diff --git a/Space-Invader.vcxproj b/Space-Invader.vcxproj index d25d422..1517bb8 100644 --- a/Space-Invader.vcxproj +++ b/Space-Invader.vcxproj @@ -134,11 +134,13 @@ + + diff --git a/Space-Invader.vcxproj.filters b/Space-Invader.vcxproj.filters index ad9b691..bf5a3a1 100644 --- a/Space-Invader.vcxproj.filters +++ b/Space-Invader.vcxproj.filters @@ -27,6 +27,9 @@ Fichiers sources + + Fichiers sources + @@ -38,5 +41,8 @@ Fichiers d%27en-tĂȘte + + Fichiers d%27en-tĂȘte + \ No newline at end of file diff --git a/main.cpp b/main.cpp index 446d19c..ba15fd8 100644 --- a/main.cpp +++ b/main.cpp @@ -7,21 +7,44 @@ int main() window.setVerticalSyncEnabled(true); Player player(window); - Inputs inputs(window, player); + vector lasers; + + Inputs inputs(player, lasers); while (window.isOpen()) { Event event; while (window.pollEvent(event)) { - inputs.HandleInputs(event); - - window.clear(Color::Black); - - player.DrawPlayer(); - - window.display(); + if (event.type == sf::Event::Closed) + { + window.close(); + } } + + // Handle user's inputs + inputs.HandleInputs(); + + window.clear(Color::Black); + + // Draw everything + player.drawPlayer(); + + for (vector::iterator it = lasers.begin(); it < lasers.end();) { + window.draw(*it); + it->move(0, -5); + + if (it->getPosition().y < -it->getScale().y) { + it = lasers.erase(it); + } + else { + lasers[distance(lasers.begin(), it)] = *it; + ++it; + } + } + + // Display window + window.display(); } return 0; diff --git a/main.h b/main.h index 7b8e88f..eb7a5e8 100644 --- a/main.h +++ b/main.h @@ -5,6 +5,7 @@ #include #include "Player.h"; +#include "Laser.h"; #include "Inputs.h"; using namespace std;