refactor: re-organize code
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#include "GameManager.h";
|
||||
|
||||
GameManager::GameManager() {
|
||||
_window.setVerticalSyncEnabled(true);
|
||||
}
|
||||
|
||||
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 (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) {
|
||||
it = _lasers.erase(it);
|
||||
continue;
|
||||
}
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
_window.display();
|
||||
}
|
||||
}
|
||||
|
||||
void GameManager::handleInputs() {
|
||||
// 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();
|
||||
laser->setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
|
||||
|
||||
_lasers.push_back(laser);
|
||||
|
||||
_clock.restart();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "Laser.h"
|
||||
|
||||
Laser::Laser()
|
||||
{
|
||||
_laser = RectangleShape(Vector2f(4, 30));
|
||||
_laser.setFillColor(Color::White);
|
||||
}
|
||||
|
||||
void Laser::setInitialPosition(Vector2f position) {
|
||||
_laser.setPosition(Vector2f(position.x - (_laser.getScale().x / 2), position.y));
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "Player.h";
|
||||
|
||||
Player::Player(Vector2u screenSize): _screenWidth(screenSize.x) {
|
||||
_shape = CircleShape(40, 3);
|
||||
_shape.setFillColor(Color(52, 252, 5));
|
||||
_shape.setPosition(_screenWidth / 2.f, screenSize.y - _shape.getScale().y - 50);
|
||||
}
|
||||
|
||||
void Player::move(int direction) {
|
||||
float max = _screenWidth - (_shape.getRadius() * 2);
|
||||
float result = 5 * direction;
|
||||
|
||||
// Prevent leaving the window
|
||||
if (_shape.getPosition().x + result < 0 || _shape.getPosition().x + result > max) {
|
||||
result = 0;
|
||||
}
|
||||
|
||||
_shape.move(result, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user