Added laser collision on aliens
This commit is contained in:
@@ -141,6 +141,7 @@
|
||||
<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" />
|
||||
@@ -148,6 +149,7 @@
|
||||
<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" />
|
||||
|
||||
@@ -33,6 +33,9 @@
|
||||
<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">
|
||||
@@ -47,5 +50,8 @@
|
||||
<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>
|
||||
@@ -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;
|
||||
};
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "Player.h"
|
||||
#include "Laser.h"
|
||||
#include "Shield.h"
|
||||
#include "Alien.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace sf;
|
||||
@@ -21,6 +22,8 @@ public:
|
||||
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};
|
||||
|
||||
@@ -28,5 +31,6 @@ private:
|
||||
|
||||
vector<Laser*> _lasers;
|
||||
vector<Shield*> _shields;
|
||||
vector<Alien*> _aliens;
|
||||
};
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ using namespace sf;
|
||||
class Laser
|
||||
{
|
||||
public:
|
||||
Laser() {};
|
||||
Laser(Window& window);
|
||||
|
||||
RectangleShape& getShape() { return _laser; }
|
||||
|
||||
@@ -10,7 +10,6 @@ using namespace std;
|
||||
class Shield
|
||||
{
|
||||
public:
|
||||
Shield() {};
|
||||
Shield(Window& window);
|
||||
|
||||
void setPosition(Vector2f position);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
+57
-2
@@ -1,17 +1,38 @@
|
||||
#include "GameManager.h";
|
||||
#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));
|
||||
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;
|
||||
@@ -33,6 +54,30 @@ void GameManager::run() {
|
||||
_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());
|
||||
|
||||
@@ -101,6 +146,16 @@ void GameManager::handleLasersCollision() {
|
||||
++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);
|
||||
|
||||
Reference in New Issue
Block a user