feat(shield): added shields
This commit is contained in:
@@ -70,6 +70,12 @@
|
|||||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
</ImportGroup>
|
</ImportGroup>
|
||||||
<PropertyGroup Label="UserMacros" />
|
<PropertyGroup Label="UserMacros" />
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<CopyLocalDeploymentContent>true</CopyLocalDeploymentContent>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<CopyLocalDeploymentContent>true</CopyLocalDeploymentContent>
|
||||||
|
</PropertyGroup>
|
||||||
<PropertyGroup Label="Vcpkg">
|
<PropertyGroup Label="Vcpkg">
|
||||||
<VcpkgEnableManifest>true</VcpkgEnableManifest>
|
<VcpkgEnableManifest>true</VcpkgEnableManifest>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
@@ -137,6 +143,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="lib\GameManager.cpp" />
|
<ClCompile Include="lib\GameManager.cpp" />
|
||||||
<ClCompile Include="lib\Laser.cpp" />
|
<ClCompile Include="lib\Laser.cpp" />
|
||||||
|
<ClCompile Include="lib\Shield.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="lib\Player.cpp" />
|
<ClCompile Include="lib\Player.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@@ -144,6 +151,7 @@
|
|||||||
<ClInclude Include="include\GameManager.h" />
|
<ClInclude Include="include\GameManager.h" />
|
||||||
<ClInclude Include="include\Laser.h" />
|
<ClInclude Include="include\Laser.h" />
|
||||||
<ClInclude Include="include\Player.h" />
|
<ClInclude Include="include\Player.h" />
|
||||||
|
<ClInclude Include="include\Shield.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|||||||
@@ -30,6 +30,9 @@
|
|||||||
<ClCompile Include="lib\GameManager.cpp">
|
<ClCompile Include="lib\GameManager.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="lib\Shield.cpp">
|
||||||
|
<Filter>Fichiers sources</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="include\Player.h">
|
<ClInclude Include="include\Player.h">
|
||||||
@@ -41,5 +44,8 @@
|
|||||||
<ClInclude Include="include\GameManager.h">
|
<ClInclude Include="include\GameManager.h">
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="include\Shield.h">
|
||||||
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
#include "Laser.h"
|
#include "Laser.h"
|
||||||
|
#include "Shield.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace sf;
|
using namespace sf;
|
||||||
@@ -15,14 +16,17 @@ public:
|
|||||||
|
|
||||||
void run();
|
void run();
|
||||||
void handleInputs();
|
void handleInputs();
|
||||||
|
void handleLasersCollision();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Clock _clock;
|
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() };
|
Player _player = { _window.getSize() };
|
||||||
|
|
||||||
vector<Laser*> _lasers;
|
vector<Laser*> _lasers;
|
||||||
|
vector<Shield*> _shields;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -9,7 +9,8 @@ using namespace sf;
|
|||||||
class Laser
|
class Laser
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Laser();
|
Laser() {};
|
||||||
|
Laser(Window& window);
|
||||||
|
|
||||||
RectangleShape& getShape() { return _laser; }
|
RectangleShape& getShape() { return _laser; }
|
||||||
|
|
||||||
|
|||||||
+4
-1
@@ -1,7 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#define _USE_MATH_DEFINES
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace sf;
|
using namespace sf;
|
||||||
@@ -11,7 +14,7 @@ class Player
|
|||||||
public:
|
public:
|
||||||
Player(Vector2u screenSize);
|
Player(Vector2u screenSize);
|
||||||
|
|
||||||
CircleShape getShape() { return _shape; };
|
CircleShape& getShape() { return _shape; };
|
||||||
void move(int direction);
|
void move(int direction);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
+54
-1
@@ -2,6 +2,13 @@
|
|||||||
|
|
||||||
GameManager::GameManager() {
|
GameManager::GameManager() {
|
||||||
_window.setVerticalSyncEnabled(true);
|
_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() {
|
void GameManager::run() {
|
||||||
@@ -22,12 +29,17 @@ void GameManager::run() {
|
|||||||
// Draw everything
|
// Draw everything
|
||||||
_window.draw(_player.getShape());
|
_window.draw(_player.getShape());
|
||||||
|
|
||||||
|
for (auto shield : _shields) {
|
||||||
|
_window.draw(shield->getShape());
|
||||||
|
}
|
||||||
|
|
||||||
for (vector<Laser *>::iterator it = _lasers.begin(); it < _lasers.end();) {
|
for (vector<Laser *>::iterator it = _lasers.begin(); it < _lasers.end();) {
|
||||||
_window.draw((*it)->getShape());
|
_window.draw((*it)->getShape());
|
||||||
|
|
||||||
(*it)->getShape().move(0, -5);
|
(*it)->getShape().move(0, -5);
|
||||||
|
|
||||||
if ((*it)->getShape().getPosition().y < -(*it)->getShape().getScale().y) {
|
if ((*it)->getShape().getPosition().y < -(*it)->getShape().getScale().y) {
|
||||||
|
delete* it;
|
||||||
it = _lasers.erase(it);
|
it = _lasers.erase(it);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -35,11 +47,18 @@ void GameManager::run() {
|
|||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleLasersCollision();
|
||||||
|
|
||||||
_window.display();
|
_window.display();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameManager::handleInputs() {
|
void GameManager::handleInputs() {
|
||||||
|
// Exit event
|
||||||
|
if (Keyboard::isKeyPressed(Keyboard::Escape)) {
|
||||||
|
_window.close();
|
||||||
|
}
|
||||||
|
|
||||||
// Player movements
|
// Player movements
|
||||||
if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) {
|
if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) {
|
||||||
_player.move(-1);
|
_player.move(-1);
|
||||||
@@ -54,7 +73,7 @@ void GameManager::handleInputs() {
|
|||||||
const Vector2f playerPos = _player.getShape().getPosition();
|
const Vector2f playerPos = _player.getShape().getPosition();
|
||||||
const float playerRadius = _player.getShape().getRadius();
|
const float playerRadius = _player.getShape().getRadius();
|
||||||
|
|
||||||
Laser* laser = new Laser();
|
Laser* laser = new Laser(_window);
|
||||||
laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
|
laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
|
||||||
|
|
||||||
_lasers.push_back(laser);
|
_lasers.push_back(laser);
|
||||||
@@ -63,3 +82,37 @@ void GameManager::handleInputs() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GameManager::handleLasersCollision() {
|
||||||
|
bool restartClock = false;
|
||||||
|
for (vector<Laser*>::iterator laserIt = _lasers.begin(); laserIt < _lasers.end();) {
|
||||||
|
bool deleteLaser = false;
|
||||||
|
for (vector<Shield*>::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();
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
#include "Laser.h"
|
#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);
|
_laser.setFillColor(Color::White);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-3
@@ -1,14 +1,17 @@
|
|||||||
#include "Player.h";
|
#include "Player.h";
|
||||||
|
|
||||||
Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) {
|
Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) {
|
||||||
_shape = CircleShape(40, 3);
|
_shape = CircleShape(screenSize.x / 70, 3);
|
||||||
_shape.setFillColor(Color(52, 252, 5));
|
_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) {
|
void Player::move(int direction) {
|
||||||
float max = _screenWidth - (_shape.getRadius() * 2);
|
float max = _screenWidth - (_shape.getRadius() * 2);
|
||||||
float result = 5 * direction;
|
float result = (_screenWidth / 724.0)*direction;
|
||||||
|
|
||||||
// Prevent leaving the window
|
// Prevent leaving the window
|
||||||
if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) {
|
if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user