refactor: re-organize code

This commit is contained in:
2024-01-09 16:21:57 +01:00
parent 8d542b6484
commit 15c745d7c0
14 changed files with 126 additions and 146 deletions
-27
View File
@@ -1,27 +0,0 @@
#include "Inputs.h"
void Inputs::HandleInputs() {
// Player movements
if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) {
_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.getShape().getPosition();
const float playerRadius = _player.getShape().getRadius();
Laser laser;
laser.setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
_lasers.push_back(laser.getLaser());
_clock.restart();
}
}
}
-24
View File
@@ -1,24 +0,0 @@
#pragma once
#include <list>
#include <SFML/Graphics.hpp>
#include <SFML/Main.hpp>
#include "Player.h";
#include "Laser.h";
using namespace std;
using namespace sf;
class Inputs
{
public:
Inputs(Player& player, vector<RectangleShape>& lasers) : _player(player), _lasers(lasers) {};
void HandleInputs();
private:
Player& _player;
Clock _clock;
vector<RectangleShape>& _lasers;
};
-11
View File
@@ -1,11 +0,0 @@
#include "Shield.h"
Shield::Shield() {
_shield = RectangleShape(Vector2f(30, 20));
_shield.setFillColor(Color(52, 252, 5));
}
void Shield::decreaseLife()
{
_life -= 1;
}
+8 -7
View File
@@ -107,6 +107,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -121,6 +122,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -133,16 +135,15 @@
<None Include="vcpkg.json" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Inputs.cpp" />
<ClCompile Include="Laser.cpp" />
<ClCompile Include="lib\GameManager.cpp" />
<ClCompile Include="lib\Laser.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Player.cpp" />
<ClCompile Include="lib\Player.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Inputs.h" />
<ClInclude Include="Laser.h" />
<ClInclude Include="main.h" />
<ClInclude Include="Player.h" />
<ClInclude Include="include\GameManager.h" />
<ClInclude Include="include\Laser.h" />
<ClInclude Include="include\Player.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
+6 -9
View File
@@ -21,27 +21,24 @@
<ClCompile Include="main.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="Player.cpp">
<ClCompile Include="lib\Player.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="Inputs.cpp">
<ClCompile Include="lib\Laser.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="Laser.cpp">
<ClCompile Include="lib\GameManager.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h">
<ClInclude Include="include\Player.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="Player.h">
<ClInclude Include="include\Laser.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="Inputs.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="Laser.h">
<ClInclude Include="include\GameManager.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup>
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ShowAllFiles>false</ShowAllFiles>
<ShowAllFiles>true</ShowAllFiles>
</PropertyGroup>
</Project>
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include "Player.h"
#include "Laser.h"
using namespace std;
using namespace sf;
class GameManager
{
public:
GameManager();
void run();
void handleInputs();
private:
Clock _clock;
RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen };
Player _player = { _window.getSize() };
vector<Laser *> _lasers;
};
+1 -1
View File
@@ -11,7 +11,7 @@ class Laser
public:
Laser();
RectangleShape getLaser() { return _laser; }
RectangleShape& getShape() { return _laser; }
void setInitialPosition(Vector2f position);
+2 -2
View File
@@ -9,7 +9,7 @@ using namespace sf;
class Player
{
public:
Player(int screenSize);
Player(Vector2u screenSize);
CircleShape getShape() { return _shape; };
void move(int direction);
@@ -17,6 +17,6 @@ public:
private:
CircleShape _shape;
int _screenSize;
int _screenWidth;
};
+65
View File
@@ -0,0 +1,65 @@
#include "GameManager.h";
GameManager::GameManager() {
_window.setVerticalSyncEnabled(true);
}
void GameManager::run() {
while (_window.isOpen()) {
Event event;
while (_window.pollEvent(event)) {
if (event.type == Event::Closed) {
_window.close();
}
}
// Handle user's inputs
handleInputs();
_window.clear(Color::Black);
// Draw everything
_window.draw(_player.getShape());
for (vector<Laser *>::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) {
it = _lasers.erase(it);
continue;
}
++it;
}
_window.display();
}
}
void GameManager::handleInputs() {
// Player movements
if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) {
_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.getShape().getPosition();
const float playerRadius = _player.getShape().getRadius();
Laser* laser = new Laser();
laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
_lasers.push_back(laser);
_clock.restart();
}
}
}
+1 -1
View File
@@ -2,7 +2,7 @@
Laser::Laser()
{
_laser = RectangleShape(Vector2f(2, 20));
_laser = RectangleShape(Vector2f(4, 30));
_laser.setFillColor(Color::White);
}
+5 -5
View File
@@ -1,18 +1,18 @@
#include "Player.h";
Player::Player(int screenSize): _screenSize(screenSize) {
_shape = CircleShape(20, 3);
Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) {
_shape = CircleShape(40, 3);
_shape.setFillColor(Color(52, 252, 5));
_shape.setPosition(Vector2f(_screenSize / 2, _screenSize - _shape.getScale().y - 50));
_shape.setPosition(_screenWidth / 2.f, screenSize.y - _shape.getScale().y - 50);
}
void Player::move(int direction) {
float max = _screenSize - (_shape.getRadius() * 2);
float max = _screenWidth - (_shape.getRadius() * 2);
float result = 5 * direction;
// Prevent leaving the window
if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) {
result = 0;
result = 0;
}
_shape.move(result, 0);
+9 -44
View File
@@ -1,51 +1,16 @@
#include "main.h";
using namespace std;
#include <iostream>
#include "GameManager.h"
int main()
{
RenderWindow window(VideoMode(SCREEN_SIZE, SCREEN_SIZE), "Space Invader", Style::Titlebar | Style::Close);
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
cout << "Starting game..." << endl;
GameManager gameManager;
Player player(SCREEN_SIZE);
vector<RectangleShape> lasers;
Inputs inputs(player, lasers);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
// Handle user's inputs
inputs.HandleInputs();
window.clear(Color::Black);
// Draw everything
window.draw(player.getShape());
for (vector<RectangleShape>::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();
}
cout << "Game started." << endl;
gameManager.run();
return 0;
}
-14
View File
@@ -1,14 +0,0 @@
#pragma once
#include <cstdlib>
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Player.h";
#include "Laser.h";
#include "Inputs.h";
using namespace std;
using namespace sf;
const int SCREEN_SIZE = 1024;