Added laser collision on aliens

This commit is contained in:
2024-01-30 16:22:43 +01:00
parent d67394aaec
commit 0bc7a96c32
9 changed files with 499 additions and 409 deletions
+160 -158
View File
@@ -1,159 +1,161 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{77e81a7a-9e66-4c14-8c8c-2d046feb7de8}</ProjectGuid>
<RootNamespace>SpaceInvader</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<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">
<VcpkgEnableManifest>true</VcpkgEnableManifest>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<None Include="vcpkg.json" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="lib\GameManager.cpp" />
<ClCompile Include="lib\Laser.cpp" />
<ClCompile Include="lib\Shield.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="lib\Player.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\GameManager.h" />
<ClInclude Include="include\Laser.h" />
<ClInclude Include="include\Player.h" />
<ClInclude Include="include\Shield.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{77e81a7a-9e66-4c14-8c8c-2d046feb7de8}</ProjectGuid>
<RootNamespace>SpaceInvader</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<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">
<VcpkgEnableManifest>true</VcpkgEnableManifest>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<None Include="vcpkg.json" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="lib\Alien.cpp" />
<ClCompile Include="lib\GameManager.cpp" />
<ClCompile Include="lib\Laser.cpp" />
<ClCompile Include="lib\Shield.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="lib\Player.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\Alien.h" />
<ClInclude Include="include\GameManager.h" />
<ClInclude Include="include\Laser.h" />
<ClInclude Include="include\Player.h" />
<ClInclude Include="include\Shield.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
+56 -50
View File
@@ -1,51 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Fichiers sources">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Fichiers d%27en-tête">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Fichiers de ressources">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="vcpkg.json" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lib\Player.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lib\Laser.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lib\GameManager.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lib\Shield.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\Player.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="include\Laser.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="include\GameManager.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="include\Shield.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Fichiers sources">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Fichiers d%27en-tête">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Fichiers de ressources">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="vcpkg.json" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lib\Player.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lib\Laser.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lib\GameManager.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lib\Shield.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lib\Alien.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\Player.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="include\Laser.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="include\GameManager.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="include\Shield.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="include\Alien.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup>
</Project>
+5 -5
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>true</ShowAllFiles>
</PropertyGroup>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ShowAllFiles>true</ShowAllFiles>
</PropertyGroup>
</Project>
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
class Alien
{
public:
Alien(Vector2f& position);
RectangleShape& getShape() { return _shape; };
private:
RectangleShape _shape;
};
+36 -32
View File
@@ -1,32 +1,36 @@
#pragma once
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include "Player.h"
#include "Laser.h"
#include "Shield.h"
using namespace std;
using namespace sf;
class GameManager
{
public:
GameManager();
void run();
void handleInputs();
void handleLasersCollision();
private:
Clock _clock;
//RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen };
RenderWindow _window{ VideoMode(1024, 800), "Space Invader", Style::Close | Style::Titlebar};
Player _player = { _window.getSize() };
vector<Laser*> _lasers;
vector<Shield*> _shields;
};
#pragma once
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include "Player.h"
#include "Laser.h"
#include "Shield.h"
#include "Alien.h"
using namespace std;
using namespace sf;
class GameManager
{
public:
GameManager();
void run();
void handleInputs();
void handleLasersCollision();
private:
Clock _clock;
const int alienScreenMargin = 50;
//RenderWindow _window{ VideoMode::getFullscreenModes()[0], "Space Invader", Style::Fullscreen };
RenderWindow _window{ VideoMode(1024, 800), "Space Invader", Style::Close | Style::Titlebar};
Player _player = { _window.getSize() };
vector<Laser*> _lasers;
vector<Shield*> _shields;
vector<Alien*> _aliens;
};
+21 -22
View File
@@ -1,22 +1,21 @@
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
class Laser
{
public:
Laser() {};
Laser(Window& window);
RectangleShape& getShape() { return _laser; }
void setInitialPosition(Vector2f position);
private:
RectangleShape _laser;
};
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
class Laser
{
public:
Laser(Window& window);
RectangleShape& getShape() { return _laser; }
void setInitialPosition(Vector2f position);
private:
RectangleShape _laser;
};
+24 -25
View File
@@ -1,26 +1,25 @@
#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;
#pragma once
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
using namespace sf;
using namespace std;
class Shield
{
public:
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;
};
+7
View File
@@ -0,0 +1,7 @@
#include "Alien.h"
Alien::Alien(Vector2f& position) {
_shape = RectangleShape(Vector2f(100, 50));
_shape.setFillColor(Color::Red);
_shape.setPosition(Vector2f(position.x - (_shape.getScale().x / 2.0), position.y));
}
+172 -117
View File
@@ -1,118 +1,173 @@
#include "GameManager.h";
GameManager::GameManager() {
_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() {
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 (auto shield : _shields) {
_window.draw(shield->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) {
delete* it;
it = _lasers.erase(it);
continue;
}
++it;
}
handleLasersCollision();
_window.display();
}
}
void GameManager::handleInputs() {
// Exit event
if (Keyboard::isKeyPressed(Keyboard::Escape)) {
_window.close();
}
// 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(_window);
laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
_lasers.push_back(laser);
_clock.restart();
}
}
}
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();
}
#include "GameManager.h"
GameManager::GameManager() {
_window.setVerticalSyncEnabled(true);
for (int i = 0; i < 4; i++) {
Shield* shield = new Shield(_window);
shield->setPosition(Vector2f(_window.getSize().x / 4.25f * i, _window.getSize().y * 0.9f));
_shields.push_back(shield);
}
const int rows = 3;
const int cols = 6;
const int distX = 135;
const int distY = 85;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Vector2f position(col * distX + alienScreenMargin + 1, row * distY + alienScreenMargin);
Alien* alien = new Alien(position);
_aliens.push_back(alien);
}
}
}
void GameManager::run() {
int alienDirection = 1;
const float alienXStep = 10;
const int moveDownStep = 5;
int moveDown = 0;
Clock alienClock;
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 (auto shield : _shields) {
_window.draw(shield->getShape());
}
for (Alien* alien : _aliens) {
const FloatRect bounds = alien->getShape().getGlobalBounds();
if (bounds.left + bounds.width >= _window.getSize().x - alienScreenMargin || bounds.left <= alienScreenMargin) {
alienDirection *= -1;
moveDown = moveDownStep;
break;
}
}
for (Alien* alien : _aliens) {
_window.draw(alien->getShape());
}
if (alienClock.getElapsedTime().asMilliseconds() >= 500) {
for (Alien* alien : _aliens) {
alien->getShape().move(alienXStep * alienDirection, moveDown);
}
alienClock.restart();
}
moveDown = 0;
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) {
delete* it;
it = _lasers.erase(it);
continue;
}
++it;
}
handleLasersCollision();
_window.display();
}
}
void GameManager::handleInputs() {
// Exit event
if (Keyboard::isKeyPressed(Keyboard::Escape)) {
_window.close();
}
// 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(_window);
laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
_lasers.push_back(laser);
_clock.restart();
}
}
}
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;
}
for (vector<Alien*>::iterator alienIt = _aliens.begin(); alienIt < _aliens.end();) {
if ((*laserIt)->getShape().getGlobalBounds().intersects((*alienIt)->getShape().getGlobalBounds())) {
delete* alienIt;
alienIt = _aliens.erase(alienIt);
deleteLaser = true;
break;
}
++alienIt;
}
if (deleteLaser) {
delete* laserIt;
laserIt = _lasers.erase(laserIt);
restartClock = true;
continue;
}
++laserIt;
}
if (restartClock) {
_clock.restart();
}
}