feat(laser): created laser object
Created laser object without any collision detection
This commit is contained in:
+19
-11
@@ -1,19 +1,27 @@
|
||||
#include "Inputs.h"
|
||||
|
||||
void Inputs::HandleInputs(Event& event) {
|
||||
if (event.type == Event::Closed)
|
||||
{
|
||||
_window.close();
|
||||
return;
|
||||
void Inputs::HandleInputs() {
|
||||
// Player movements
|
||||
if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) {
|
||||
_player.move(-1);
|
||||
}
|
||||
|
||||
// Player movements
|
||||
if (event.type == Event::KeyPressed) {
|
||||
if (event.key.code == Keyboard::Left || event.key.code == Keyboard::Q) {
|
||||
_player.Move(-1);
|
||||
if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) {
|
||||
_player.move(1);
|
||||
}
|
||||
if (event.key.code == Keyboard::Right || event.key.code == Keyboard::D) {
|
||||
_player.Move(1);
|
||||
|
||||
if (Keyboard::isKeyPressed(Keyboard::Space)) {
|
||||
if (_clock.getElapsedTime().asMilliseconds() > 500) {
|
||||
const Vector2f playerPos = _player.PlayerShape.getPosition();
|
||||
const float playerRadius = _player.PlayerShape.getRadius();
|
||||
|
||||
Laser laser;
|
||||
laser.setInitialPosition(Vector2f(playerPos.x + playerRadius, playerPos.y - playerRadius));
|
||||
|
||||
_lasers.push_back(laser.getLaser());
|
||||
|
||||
_clock.restart();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Main.hpp>
|
||||
#include "Player.h";
|
||||
#include "Laser.h";
|
||||
|
||||
using namespace std;
|
||||
using namespace sf;
|
||||
@@ -9,12 +12,13 @@ using namespace sf;
|
||||
class Inputs
|
||||
{
|
||||
public:
|
||||
Inputs(RenderWindow& window, Player& player) : _window(window), _player(player) {};
|
||||
Inputs(Player& player, vector<RectangleShape>& lasers) : _player(player), _lasers(lasers) {};
|
||||
|
||||
void HandleInputs(Event& event);
|
||||
void HandleInputs();
|
||||
|
||||
private:
|
||||
RenderWindow& _window;
|
||||
Player& _player;
|
||||
Clock _clock;
|
||||
vector<RectangleShape>& _lasers;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "Laser.h"
|
||||
|
||||
Laser::Laser()
|
||||
{
|
||||
_laser = RectangleShape(Vector2f(2, 20));
|
||||
_laser.setFillColor(Color::White);
|
||||
}
|
||||
|
||||
void Laser::setInitialPosition(Vector2f position) {
|
||||
_laser.setPosition(Vector2f(position.x - (_laser.getScale().x / 2), position.y));
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace sf;
|
||||
|
||||
class Laser
|
||||
{
|
||||
public:
|
||||
Laser();
|
||||
|
||||
RectangleShape getLaser() { return _laser; }
|
||||
|
||||
void setInitialPosition(Vector2f position);
|
||||
|
||||
private:
|
||||
RectangleShape _laser;
|
||||
};
|
||||
|
||||
+2
-2
@@ -1,13 +1,13 @@
|
||||
#include "Player.h";
|
||||
|
||||
void Player::DrawPlayer() {
|
||||
void Player::drawPlayer() {
|
||||
PlayerShape.setFillColor(Color(52, 252, 5));
|
||||
PlayerShape.setPosition(Vector2f(_window.getSize().x / 2 + _x, _window.getSize().y - PlayerShape.getScale().y - 50));
|
||||
|
||||
_window.draw(PlayerShape);
|
||||
}
|
||||
|
||||
void Player::Move(int direction) {
|
||||
void Player::move(int direction) {
|
||||
int result = _x + _velocity * direction;
|
||||
|
||||
int minX = _window.getSize().x / 2 * -1;
|
||||
|
||||
@@ -13,8 +13,8 @@ public:
|
||||
|
||||
CircleShape PlayerShape = CircleShape(20, 3);
|
||||
|
||||
void DrawPlayer();
|
||||
void Move(int direction);
|
||||
void drawPlayer();
|
||||
void move(int direction);
|
||||
|
||||
private:
|
||||
int _x = 0;
|
||||
|
||||
@@ -134,11 +134,13 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Inputs.cpp" />
|
||||
<ClCompile Include="Laser.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="Player.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Inputs.h" />
|
||||
<ClInclude Include="Laser.h" />
|
||||
<ClInclude Include="main.h" />
|
||||
<ClInclude Include="Player.h" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -27,6 +27,9 @@
|
||||
<ClCompile Include="Inputs.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Laser.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="main.h">
|
||||
@@ -38,5 +41,8 @@
|
||||
<ClInclude Include="Inputs.h">
|
||||
<Filter>Fichiers d%27en-tête</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Laser.h">
|
||||
<Filter>Fichiers d%27en-tête</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -7,21 +7,44 @@ int main()
|
||||
window.setVerticalSyncEnabled(true);
|
||||
|
||||
Player player(window);
|
||||
Inputs inputs(window, player);
|
||||
vector<RectangleShape> lasers;
|
||||
|
||||
Inputs inputs(player, lasers);
|
||||
|
||||
while (window.isOpen())
|
||||
{
|
||||
Event event;
|
||||
while (window.pollEvent(event))
|
||||
{
|
||||
inputs.HandleInputs(event);
|
||||
if (event.type == sf::Event::Closed)
|
||||
{
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Handle user's inputs
|
||||
inputs.HandleInputs();
|
||||
|
||||
window.clear(Color::Black);
|
||||
|
||||
player.DrawPlayer();
|
||||
// Draw everything
|
||||
player.drawPlayer();
|
||||
|
||||
window.display();
|
||||
for (vector<RectangleShape>::iterator it = lasers.begin(); it < lasers.end();) {
|
||||
window.draw(*it);
|
||||
it->move(0, -5);
|
||||
|
||||
if (it->getPosition().y < -it->getScale().y) {
|
||||
it = lasers.erase(it);
|
||||
}
|
||||
else {
|
||||
lasers[distance(lasers.begin(), it)] = *it;
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
// Display window
|
||||
window.display();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user