🚀
This commit is contained in:
@@ -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
@@ -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();
|
||||
}
|
||||
}
|
||||
+10
-10
@@ -1,11 +1,11 @@
|
||||
#include "Laser.h"
|
||||
|
||||
Laser::Laser(Window& _window)
|
||||
{
|
||||
_laser = RectangleShape(Vector2f(_window.getSize().x / 600, _window.getSize().y / 30));
|
||||
_laser.setFillColor(Color::White);
|
||||
}
|
||||
|
||||
void Laser::setInitialPosition(Vector2f position) {
|
||||
_laser.setPosition(Vector2f(position.x - (_laser.getScale().x / 2), position.y));
|
||||
#include "Laser.h"
|
||||
|
||||
Laser::Laser(Window& _window)
|
||||
{
|
||||
_laser = RectangleShape(Vector2f(_window.getSize().x / 600, _window.getSize().y / 30));
|
||||
_laser.setFillColor(Color::White);
|
||||
}
|
||||
|
||||
void Laser::setInitialPosition(Vector2f position) {
|
||||
_laser.setPosition(Vector2f(position.x - (_laser.getScale().x / 2), position.y));
|
||||
}
|
||||
+21
-21
@@ -1,22 +1,22 @@
|
||||
#include "Player.h";
|
||||
|
||||
Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) {
|
||||
_shape = CircleShape(screenSize.x / 70, 3);
|
||||
_shape.setFillColor(Color(52, 252, 5));
|
||||
|
||||
float shapeHeight = _shape.getRadius() * 1.5;
|
||||
|
||||
_shape.setPosition(_screenWidth / 2.f, screenSize.y - shapeHeight);
|
||||
}
|
||||
|
||||
void Player::move(int direction) {
|
||||
float max = _screenWidth - (_shape.getRadius() * 2);
|
||||
float result = (_screenWidth / 724.0)*direction;
|
||||
|
||||
// Prevent leaving the window
|
||||
if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) {
|
||||
result = 0;
|
||||
}
|
||||
|
||||
_shape.move(result, 0);
|
||||
#include "Player.h";
|
||||
|
||||
Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) {
|
||||
_shape = CircleShape(screenSize.x / 70, 3);
|
||||
_shape.setFillColor(Color(52, 252, 5));
|
||||
|
||||
float shapeHeight = _shape.getRadius() * 1.5;
|
||||
|
||||
_shape.setPosition(_screenWidth / 2.f, screenSize.y - shapeHeight);
|
||||
}
|
||||
|
||||
void Player::move(int direction) {
|
||||
float max = _screenWidth - (_shape.getRadius() * 2);
|
||||
float result = (_screenWidth / 724.0)*direction;
|
||||
|
||||
// Prevent leaving the window
|
||||
if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) {
|
||||
result = 0;
|
||||
}
|
||||
|
||||
_shape.move(result, 0);
|
||||
}
|
||||
+12
-12
@@ -1,13 +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);
|
||||
#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