feat(laser): created laser object
Created laser object without any collision detection
This commit is contained in:
+20
-12
@@ -1,19 +1,27 @@
|
|||||||
#include "Inputs.h"
|
#include "Inputs.h"
|
||||||
|
|
||||||
void Inputs::HandleInputs(Event& event) {
|
void Inputs::HandleInputs() {
|
||||||
if (event.type == Event::Closed)
|
// Player movements
|
||||||
{
|
if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::Q)) {
|
||||||
_window.close();
|
_player.move(-1);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Player movements
|
if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) {
|
||||||
if (event.type == Event::KeyPressed) {
|
_player.move(1);
|
||||||
if (event.key.code == Keyboard::Left || event.key.code == Keyboard::Q) {
|
|
||||||
_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
|
#pragma once
|
||||||
|
|
||||||
|
#include <list>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Main.hpp>
|
||||||
#include "Player.h";
|
#include "Player.h";
|
||||||
|
#include "Laser.h";
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace sf;
|
using namespace sf;
|
||||||
@@ -9,12 +12,13 @@ using namespace sf;
|
|||||||
class Inputs
|
class Inputs
|
||||||
{
|
{
|
||||||
public:
|
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:
|
private:
|
||||||
RenderWindow& _window;
|
|
||||||
Player& _player;
|
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";
|
#include "Player.h";
|
||||||
|
|
||||||
void Player::DrawPlayer() {
|
void Player::drawPlayer() {
|
||||||
PlayerShape.setFillColor(Color(52, 252, 5));
|
PlayerShape.setFillColor(Color(52, 252, 5));
|
||||||
PlayerShape.setPosition(Vector2f(_window.getSize().x / 2 + _x, _window.getSize().y - PlayerShape.getScale().y - 50));
|
PlayerShape.setPosition(Vector2f(_window.getSize().x / 2 + _x, _window.getSize().y - PlayerShape.getScale().y - 50));
|
||||||
|
|
||||||
_window.draw(PlayerShape);
|
_window.draw(PlayerShape);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::Move(int direction) {
|
void Player::move(int direction) {
|
||||||
int result = _x + _velocity * direction;
|
int result = _x + _velocity * direction;
|
||||||
|
|
||||||
int minX = _window.getSize().x / 2 * -1;
|
int minX = _window.getSize().x / 2 * -1;
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ public:
|
|||||||
|
|
||||||
CircleShape PlayerShape = CircleShape(20, 3);
|
CircleShape PlayerShape = CircleShape(20, 3);
|
||||||
|
|
||||||
void DrawPlayer();
|
void drawPlayer();
|
||||||
void Move(int direction);
|
void move(int direction);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _x = 0;
|
int _x = 0;
|
||||||
|
|||||||
@@ -134,11 +134,13 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Inputs.cpp" />
|
<ClCompile Include="Inputs.cpp" />
|
||||||
|
<ClCompile Include="Laser.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="Player.cpp" />
|
<ClCompile Include="Player.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Inputs.h" />
|
<ClInclude Include="Inputs.h" />
|
||||||
|
<ClInclude Include="Laser.h" />
|
||||||
<ClInclude Include="main.h" />
|
<ClInclude Include="main.h" />
|
||||||
<ClInclude Include="Player.h" />
|
<ClInclude Include="Player.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -27,6 +27,9 @@
|
|||||||
<ClCompile Include="Inputs.cpp">
|
<ClCompile Include="Inputs.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Laser.cpp">
|
||||||
|
<Filter>Fichiers sources</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="main.h">
|
<ClInclude Include="main.h">
|
||||||
@@ -38,5 +41,8 @@
|
|||||||
<ClInclude Include="Inputs.h">
|
<ClInclude Include="Inputs.h">
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Laser.h">
|
||||||
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -7,21 +7,44 @@ int main()
|
|||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
|
|
||||||
Player player(window);
|
Player player(window);
|
||||||
Inputs inputs(window, player);
|
vector<RectangleShape> lasers;
|
||||||
|
|
||||||
|
Inputs inputs(player, lasers);
|
||||||
|
|
||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
Event event;
|
Event event;
|
||||||
while (window.pollEvent(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);
|
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;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user