diff --git a/Inputs.cpp b/Inputs.cpp new file mode 100644 index 0000000..a82c21c --- /dev/null +++ b/Inputs.cpp @@ -0,0 +1,19 @@ +#include "Inputs.h" + +void Inputs::HandleInputs(Event& event) { + if (event.type == Event::Closed) + { + _window.close(); + return; + } + + // 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); + } + } +} \ No newline at end of file diff --git a/Inputs.h b/Inputs.h new file mode 100644 index 0000000..66575cd --- /dev/null +++ b/Inputs.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include "Player.h"; + +using namespace std; +using namespace sf; + +class Inputs +{ +public: + Inputs(RenderWindow& window, Player& player) : _window(window), _player(player) {}; + + void HandleInputs(Event& event); + +private: + RenderWindow& _window; + Player& _player; +}; + diff --git a/Player.cpp b/Player.cpp new file mode 100644 index 0000000..dc544f0 --- /dev/null +++ b/Player.cpp @@ -0,0 +1,25 @@ +#include "Player.h"; + +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) { + int result = _x + _velocity * direction; + + int minX = _window.getSize().x / 2 * -1; + int maxX = _window.getSize().x / 2 - ((int)PlayerShape.getRadius() * 2); + + if (result < minX) { + result = minX; + } + else if (result > maxX) { + result = maxX; + } + else { + _x = result; + } +} \ No newline at end of file diff --git a/Player.h b/Player.h new file mode 100644 index 0000000..3bae875 --- /dev/null +++ b/Player.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +using namespace std; +using namespace sf; + +class Player +{ +public: + Player(RenderWindow& window): _window(window) {} + + CircleShape PlayerShape = CircleShape(20, 3); + + void DrawPlayer(); + void Move(int direction); + +private: + int _x = 0; + int _velocity = 7; + RenderWindow& _window; +}; + diff --git a/Space-Invader.vcxproj b/Space-Invader.vcxproj index c6b558c..d25d422 100644 --- a/Space-Invader.vcxproj +++ b/Space-Invader.vcxproj @@ -133,10 +133,14 @@ + + + + diff --git a/Space-Invader.vcxproj.filters b/Space-Invader.vcxproj.filters index b066ff9..ad9b691 100644 --- a/Space-Invader.vcxproj.filters +++ b/Space-Invader.vcxproj.filters @@ -21,10 +21,22 @@ Fichiers sources + + Fichiers sources + + + Fichiers sources + Fichiers d%27en-tête + + 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 ff13e89..446d19c 100644 --- a/main.cpp +++ b/main.cpp @@ -6,18 +6,20 @@ int main() window.setFramerateLimit(60); window.setVerticalSyncEnabled(true); + Player player(window); + Inputs inputs(window, player); + while (window.isOpen()) { Event event; while (window.pollEvent(event)) { - if (event.type == Event::Closed) - { - window.close(); - } + inputs.HandleInputs(event); window.clear(Color::Black); + player.DrawPlayer(); + window.display(); } } diff --git a/main.h b/main.h index 5f6aa9a..7b8e88f 100644 --- a/main.h +++ b/main.h @@ -4,6 +4,9 @@ #include #include +#include "Player.h"; +#include "Inputs.h"; + using namespace std; using namespace sf;