refactor: re-organize code
This commit is contained in:
-27
@@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -107,6 +107,7 @@
|
|||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
@@ -121,6 +122,7 @@
|
|||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
@@ -133,16 +135,15 @@
|
|||||||
<None Include="vcpkg.json" />
|
<None Include="vcpkg.json" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Inputs.cpp" />
|
<ClCompile Include="lib\GameManager.cpp" />
|
||||||
<ClCompile Include="Laser.cpp" />
|
<ClCompile Include="lib\Laser.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="Player.cpp" />
|
<ClCompile Include="lib\Player.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Inputs.h" />
|
<ClInclude Include="include\GameManager.h" />
|
||||||
<ClInclude Include="Laser.h" />
|
<ClInclude Include="include\Laser.h" />
|
||||||
<ClInclude Include="main.h" />
|
<ClInclude Include="include\Player.h" />
|
||||||
<ClInclude Include="Player.h" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|||||||
@@ -21,27 +21,24 @@
|
|||||||
<ClCompile Include="main.cpp">
|
<ClCompile Include="main.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="Player.cpp">
|
<ClCompile Include="lib\Player.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="Inputs.cpp">
|
<ClCompile Include="lib\Laser.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="Laser.cpp">
|
<ClCompile Include="lib\GameManager.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="main.h">
|
<ClInclude Include="include\Player.h">
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="Player.h">
|
<ClInclude Include="include\Laser.h">
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="Inputs.h">
|
<ClInclude Include="include\GameManager.h">
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="Laser.h">
|
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<ShowAllFiles>false</ShowAllFiles>
|
<ShowAllFiles>true</ShowAllFiles>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -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;
|
||||||
|
};
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ class Laser
|
|||||||
public:
|
public:
|
||||||
Laser();
|
Laser();
|
||||||
|
|
||||||
RectangleShape getLaser() { return _laser; }
|
RectangleShape& getShape() { return _laser; }
|
||||||
|
|
||||||
void setInitialPosition(Vector2f position);
|
void setInitialPosition(Vector2f position);
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ using namespace sf;
|
|||||||
class Player
|
class Player
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Player(int screenSize);
|
Player(Vector2u screenSize);
|
||||||
|
|
||||||
CircleShape getShape() { return _shape; };
|
CircleShape getShape() { return _shape; };
|
||||||
void move(int direction);
|
void move(int direction);
|
||||||
@@ -17,6 +17,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
CircleShape _shape;
|
CircleShape _shape;
|
||||||
|
|
||||||
int _screenSize;
|
int _screenWidth;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Laser::Laser()
|
Laser::Laser()
|
||||||
{
|
{
|
||||||
_laser = RectangleShape(Vector2f(2, 20));
|
_laser = RectangleShape(Vector2f(4, 30));
|
||||||
_laser.setFillColor(Color::White);
|
_laser.setFillColor(Color::White);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
#include "Player.h";
|
#include "Player.h";
|
||||||
|
|
||||||
Player::Player(int screenSize): _screenSize(screenSize) {
|
Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) {
|
||||||
_shape = CircleShape(20, 3);
|
_shape = CircleShape(40, 3);
|
||||||
_shape.setFillColor(Color(52, 252, 5));
|
_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) {
|
void Player::move(int direction) {
|
||||||
float max = _screenSize - (_shape.getRadius() * 2);
|
float max = _screenWidth - (_shape.getRadius() * 2);
|
||||||
float result = 5 * direction;
|
float result = 5 * direction;
|
||||||
|
|
||||||
// Prevent leaving the window
|
// Prevent leaving the window
|
||||||
@@ -1,51 +1,16 @@
|
|||||||
#include "main.h";
|
using namespace std;
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "GameManager.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
RenderWindow window(VideoMode(SCREEN_SIZE, SCREEN_SIZE), "Space Invader", Style::Titlebar | Style::Close);
|
cout << "Starting game..." << endl;
|
||||||
window.setFramerateLimit(60);
|
GameManager gameManager;
|
||||||
window.setVerticalSyncEnabled(true);
|
|
||||||
|
|
||||||
Player player(SCREEN_SIZE);
|
cout << "Game started." << endl;
|
||||||
vector<RectangleShape> lasers;
|
gameManager.run();
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user