feat(shield): added shields
This commit is contained in:
+54
-1
@@ -2,6 +2,13 @@
|
||||
|
||||
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() {
|
||||
@@ -22,12 +29,17 @@ void GameManager::run() {
|
||||
// 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;
|
||||
}
|
||||
@@ -35,11 +47,18 @@ void GameManager::run() {
|
||||
++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);
|
||||
@@ -54,7 +73,7 @@ void GameManager::handleInputs() {
|
||||
const Vector2f playerPos = _player.getShape().getPosition();
|
||||
const float playerRadius = _player.getShape().getRadius();
|
||||
|
||||
Laser* laser = new Laser();
|
||||
Laser* laser = new Laser(_window);
|
||||
laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
|
||||
|
||||
_lasers.push_back(laser);
|
||||
@@ -62,4 +81,38 @@ void GameManager::handleInputs() {
|
||||
_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();
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
|
||||
+6
-3
@@ -1,14 +1,17 @@
|
||||
#include "Player.h";
|
||||
|
||||
Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) {
|
||||
_shape = CircleShape(40, 3);
|
||||
_shape = CircleShape(screenSize.x / 70, 3);
|
||||
_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) {
|
||||
float max = _screenWidth - (_shape.getRadius() * 2);
|
||||
float result = 5 * direction;
|
||||
float result = (_screenWidth / 724.0)*direction;
|
||||
|
||||
// Prevent leaving the window
|
||||
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