feat(player): added movements
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
|||||||
|
#include "Inputs.h"
|
||||||
|
|
||||||
|
void Inputs::HandleInputs(Event& event) {
|
||||||
|
if (event.type == Event::Closed)
|
||||||
|
{
|
||||||
|
_window.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Player movements
|
||||||
|
if (event.type == Event::KeyPressed) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include "Player.h";
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace sf;
|
||||||
|
|
||||||
|
class Inputs
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Inputs(RenderWindow& window, Player& player) : _window(window), _player(player) {};
|
||||||
|
|
||||||
|
void HandleInputs(Event& event);
|
||||||
|
|
||||||
|
private:
|
||||||
|
RenderWindow& _window;
|
||||||
|
Player& _player;
|
||||||
|
};
|
||||||
|
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
#include "Player.h";
|
||||||
|
|
||||||
|
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) {
|
||||||
|
int result = _x + _velocity * direction;
|
||||||
|
|
||||||
|
int minX = _window.getSize().x / 2 * -1;
|
||||||
|
int maxX = _window.getSize().x / 2 - ((int)PlayerShape.getRadius() * 2);
|
||||||
|
|
||||||
|
if (result < minX) {
|
||||||
|
result = minX;
|
||||||
|
}
|
||||||
|
else if (result > maxX) {
|
||||||
|
result = maxX;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_x = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace sf;
|
||||||
|
|
||||||
|
class Player
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Player(RenderWindow& window): _window(window) {}
|
||||||
|
|
||||||
|
CircleShape PlayerShape = CircleShape(20, 3);
|
||||||
|
|
||||||
|
void DrawPlayer();
|
||||||
|
void Move(int direction);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int _x = 0;
|
||||||
|
int _velocity = 7;
|
||||||
|
RenderWindow& _window;
|
||||||
|
};
|
||||||
|
|
||||||
@@ -133,10 +133,14 @@
|
|||||||
<None Include="vcpkg.json" />
|
<None Include="vcpkg.json" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Inputs.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
|
<ClCompile Include="Player.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Inputs.h" />
|
||||||
<ClInclude Include="main.h" />
|
<ClInclude Include="main.h" />
|
||||||
|
<ClInclude Include="Player.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|||||||
@@ -21,10 +21,22 @@
|
|||||||
<ClCompile Include="main.cpp">
|
<ClCompile Include="main.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Player.cpp">
|
||||||
|
<Filter>Fichiers sources</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Inputs.cpp">
|
||||||
|
<Filter>Fichiers sources</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="main.h">
|
<ClInclude Include="main.h">
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Player.h">
|
||||||
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Inputs.h">
|
||||||
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -6,18 +6,20 @@ int main()
|
|||||||
window.setFramerateLimit(60);
|
window.setFramerateLimit(60);
|
||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
|
|
||||||
|
Player player(window);
|
||||||
|
Inputs inputs(window, player);
|
||||||
|
|
||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
Event event;
|
Event event;
|
||||||
while (window.pollEvent(event))
|
while (window.pollEvent(event))
|
||||||
{
|
{
|
||||||
if (event.type == Event::Closed)
|
inputs.HandleInputs(event);
|
||||||
{
|
|
||||||
window.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
window.clear(Color::Black);
|
window.clear(Color::Black);
|
||||||
|
|
||||||
|
player.DrawPlayer();
|
||||||
|
|
||||||
window.display();
|
window.display();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user