Skip to content

Commit

Permalink
Merge pull request #153 from FppEpitech/143-fix-clock-the-velocity
Browse files Browse the repository at this point in the history
FIX: Clock the velocity
  • Loading branch information
mathieurobert1 authored Oct 11, 2024
2 parents b22da0e + 6b2dd2b commit c809361
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Engine/Client/Src/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ void Application::_keyboardHandler(std::size_t key)
try {
if (key == KEY_NULL)
return;
_sceneManager->processInput(KEY_MAP(key), this->_client->getIdxPlayerComponent());
if (!_sceneManager->processInput(KEY_MAP(key), this->_client->getIdxPlayerComponent()))
return;
_client->sendKeyPacket(KEY_MAP(key));
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions Engine/Server/Src/Network/NetworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ void Network::Server::_startReceive(ECS::Registry& reg)
if (it != _clients.end()) {
if (it->second == asio::ip::udp::endpoint())
_clients[packet.getToken()] = _remote_endpoint;
if (this->_messageHandler)
this->_messageHandler(packet, this->_remote_endpoint, reg);
// if (this->_messageHandler)
// this->_messageHandler(packet, this->_remote_endpoint, reg);
}
} catch (const std::exception& e) {
_startReceive(reg);
Expand Down
2 changes: 1 addition & 1 deletion Game/GameData/Entities/Shoot.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"VelocityComponent":
{
"vx": 5,
"vx": 600,
"vy": 0
},
"ShootComponent":
Expand Down
1 change: 1 addition & 0 deletions R-TypeGame/Components/Velocity/VelocityComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
VelocityComponent::VelocityComponent(float vx, float vy) :
vx(vx), vy(vy), AComponent("VelocityComponent")
{
frameRate = std::chrono::high_resolution_clock::now();
}
3 changes: 3 additions & 0 deletions R-TypeGame/Components/Velocity/VelocityComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#pragma once

#include <chrono>

#include "../../../Engine/Shared/Interface/IComponent.hpp"

/**
Expand All @@ -19,6 +21,7 @@ class VelocityComponent : public AComponent {

float vx; // Velocity x.
float vy; // Velocity y.
std::chrono::high_resolution_clock::time_point frameRate; // Velocity frameRate.

/**
* @brief Construct a new Velocity Component object.
Expand Down
3 changes: 3 additions & 0 deletions R-TypeGame/Parser/Position2DParser/Position2DParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ std::shared_ptr<Position2DComponent> parsePosition2D(std::string pathFile)
Json::Reader reader;
Json::Value root;

if (!file.is_open())
return nullptr;

if (!reader.parse(file, root, false))
return nullptr;

Expand Down
3 changes: 3 additions & 0 deletions R-TypeGame/Parser/ScaleParser/ScaleParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ std::shared_ptr<ScaleComponent> parseScale(std::string pathFile)
Json::Reader reader;
Json::Value root;

if (!file.is_open())
return nullptr;

if (!reader.parse(file, root, false))
return nullptr;

Expand Down
3 changes: 3 additions & 0 deletions R-TypeGame/Parser/ShootParser/ShootParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ std::shared_ptr<ShootComponent> parseShoot(std::string pathFile)
Json::Reader reader;
Json::Value root;

if (!file.is_open())
return nullptr;

if (!reader.parse(file, root, false))
return nullptr;

Expand Down
3 changes: 3 additions & 0 deletions R-TypeGame/Parser/SpeedParser/SpeedParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ std::shared_ptr<SpeedComponent> parseSpeed(std::string pathFile)
Json::Reader reader;
Json::Value root;

if (!file.is_open())
return nullptr;

if (!reader.parse(file, root, false))
return nullptr;

Expand Down
3 changes: 3 additions & 0 deletions R-TypeGame/Parser/TextureRectParser/TextureRectParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ std::shared_ptr<TextureRectComponent> parseTextureRect(std::string pathFile)
Json::Reader reader;
Json::Value root;

if (!file.is_open())
return nullptr;

if (!reader.parse(file, root, false))
return nullptr;

Expand Down
3 changes: 3 additions & 0 deletions R-TypeGame/Parser/VelocityParser/VelocityParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ std::shared_ptr<VelocityComponent> parseVelocity(std::string pathFile)
Json::Reader reader;
Json::Value root;

if (!file.is_open())
return nullptr;

if (!reader.parse(file, root, false))
return nullptr;

Expand Down
2 changes: 1 addition & 1 deletion R-TypeGame/Systems/Background/PlanetInitSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void PlanetInitSystem::_initPlanet(ECS::Registry& reg, int idxPacketEntities)
std::uniform_int_distribution<unsigned> distrib(0, NB_JSON - 1);
std::uniform_int_distribution<unsigned> distribX(1920, 5000);
std::uniform_int_distribution<unsigned> distribY(0, 1080);
std::uniform_real_distribution<float> distribVelocity(0.3, 1.5);
std::uniform_real_distribution<float> distribVelocity(30, 150);

int randomJson = distrib(gen);

Expand Down
5 changes: 0 additions & 5 deletions R-TypeGame/Systems/Background/PlanetRestartSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "PlanetRestartSystem.hpp"
#include "PlanetComponent.hpp"
#include "GetGraphicalLibrary.hpp"
#include "UpdatePlanet.hpp"

PlanetRestartSystem::PlanetRestartSystem() :
Expand All @@ -18,10 +17,6 @@ PlanetRestartSystem::PlanetRestartSystem() :
void PlanetRestartSystem::_restartPlanet(ECS::Registry& reg, int idxPacketEntities)
{
try {
std::shared_ptr<IGraphic> libGraphic = getGraphicalLibrary();
if (!libGraphic)
return;

ECS::SparseArray<IComponent> positions = reg.get_components<IComponent>("Position2DComponent");
ECS::SparseArray<IComponent> velocities = reg.get_components<IComponent>("VelocityComponent");
ECS::SparseArray<IComponent> planets = reg.get_components<IComponent>("PlanetComponent");
Expand Down
2 changes: 1 addition & 1 deletion R-TypeGame/Systems/Background/UpdatePlanet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void updateNewPositions(std::shared_ptr<VelocityComponent> velocity, std::shared
std::mt19937 gen(getRandomSeed());
std::uniform_int_distribution<unsigned> distribX(1920, 5000);
std::uniform_int_distribution<unsigned> distribY(0, 1080);
std::uniform_real_distribution<float> distribVelocity(0.3, 1.5);
std::uniform_real_distribution<float> distribVelocity(30, 150);

position->x = distribX(gen);
position->y = distribY(gen);
Expand Down
3 changes: 1 addition & 2 deletions R-TypeGame/Systems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ add_library(system_initPlayerOne SHARED
${CMAKE_SOURCE_DIR}/Engine/Shared/Component/SpriteSheetAnimationComponent.cpp
${CMAKE_SOURCE_DIR}/Engine/Shared/Component/Entities/Player/PlayerComponent.cpp
../Components/Speed/SpeedComponent.cpp
../Components/Shoot/ShootComponent.cpp
../Parser/TextureRectParser/TextureRectParser.cpp
../Parser/Position2DParser/Position2DParser.cpp
../Parser/LifeParser/LifeParser.cpp
Expand Down Expand Up @@ -182,7 +183,6 @@ target_link_libraries(system_shoot PRIVATE JsonCpp::JsonCpp)

add_library(system_shootDestroy SHARED
Shoot/ShootDestroySystem.cpp
${CMAKE_SOURCE_DIR}/Engine/Client/Src/GetGraphicalLibrary/GetGraphicalLibrary.cpp
${CMAKE_SOURCE_DIR}/Engine/Shared/Ecs/Src/Registry.cpp
)

Expand Down Expand Up @@ -215,7 +215,6 @@ target_link_libraries(system_initPlanet PRIVATE JsonCpp::JsonCpp)

add_library(system_restartPlanet SHARED
Background/PlanetRestartSystem.cpp
${CMAKE_SOURCE_DIR}/Engine/Client/Src/GetGraphicalLibrary/GetGraphicalLibrary.cpp
Background/UpdatePlanet.cpp
)

Expand Down
13 changes: 11 additions & 2 deletions R-TypeGame/Systems/Move/MoveEntities/MoveEntitiesSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "VelocityComponent.hpp"
#include "Position2DComponent.hpp"

#include <chrono>

MoveEntitiesSystem::MoveEntitiesSystem() :
ASystem("MoveEntitiesSystem")
{
Expand All @@ -25,8 +27,15 @@ void MoveEntitiesSystem::_moveEntities(ECS::Registry& reg, int idxPacketEntities
std::shared_ptr<VelocityComponent> velocity = std::dynamic_pointer_cast<VelocityComponent>(velocities[entity]);
if (!position || !velocity)
continue;
position->x += velocity->vx;
position->y += velocity->vy;

const std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
double timeElapsed = std::chrono::duration<double, std::milli>(now - velocity->frameRate).count() / 1000;

if (0.005 < timeElapsed) {
position->x += velocity->vx * timeElapsed;
position->y += velocity->vy;
velocity->frameRate = std::chrono::high_resolution_clock::now();
}
}
} catch (std::exception e) {
}
Expand Down
4 changes: 4 additions & 0 deletions R-TypeGame/Systems/Players/PlayerInitOneSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "PlayerParser.hpp"
#include "SpeedParser.hpp"
#include "SpriteSheetAnimationParser.hpp"
#include "ShootComponent.hpp"

#include <fstream>
#include <json/json.h>
Expand All @@ -33,6 +34,9 @@ PlayerInitSystem::PlayerInitSystem() :

void PlayerInitSystem::_initPlayer(ECS::Registry& reg, int idxPacketEntities)
{

reg.register_component<IComponent>("ShootComponent");

std::shared_ptr<TextureRectComponent> textureRect = parseTextureRect(PATH_JSON);
if (textureRect) {
reg.register_component<IComponent>(textureRect->getType());
Expand Down
7 changes: 1 addition & 6 deletions R-TypeGame/Systems/Shoot/ShootDestroySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "ShootDestroySystem.hpp"
#include "Position2DComponent.hpp"
#include "ShootComponent.hpp"
#include "GetGraphicalLibrary.hpp"

ShootDestroySystem::ShootDestroySystem() :
ASystem("ShootDestroySystem")
Expand All @@ -18,9 +17,6 @@ ShootDestroySystem::ShootDestroySystem() :
void ShootDestroySystem::_shootDestroy(ECS::Registry& reg, int idxPacketEntities)
{
try {
std::shared_ptr<IGraphic> libGraphic = getGraphicalLibrary();
if (!libGraphic)
return;

ECS::SparseArray<IComponent> positions = reg.get_components<IComponent>("Position2DComponent");
ECS::SparseArray<IComponent> shoots = reg.get_components<IComponent>("ShootComponent");
Expand All @@ -34,8 +30,7 @@ void ShootDestroySystem::_shootDestroy(ECS::Registry& reg, int idxPacketEntities
std::shared_ptr<Position2DComponent> position = std::dynamic_pointer_cast<Position2DComponent>(positions[entity]);

if (position && shoot->friendlyFire) {
if (position->x >= -100 && position->x <= libGraphic->getWindowSize().first &&
position->y >= 0 && position->y <= libGraphic->getWindowSize().second)
if (position->x >= -100 && position->x <= 1920 && position->y >= 0 && position->y <= 1080)
continue;
reg.kill_entity(entity);
}
Expand Down
4 changes: 2 additions & 2 deletions R-TypeGame/Systems/Shoot/ShootSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ ShootSystem::ShootSystem() :

void ShootSystem::_shoot(ECS::Registry& reg, int idxPacketEntities)
{
if (idxPacketEntities > reg.getEntities().size())
return;
ECS::entity_t shoot = reg.spawn_entity();
ShootInitSystem().getFunction()(reg, shoot);

Expand All @@ -33,15 +35,13 @@ void ShootSystem::_shoot(ECS::Registry& reg, int idxPacketEntities)
}
}


reg.messageType = 0x02;
reg.payload.clear();

std::string componentType = "ShootSystem";
reg.payload.push_back(static_cast<uint8_t>(componentType.size()));
reg.payload.insert(reg.payload.end(), componentType.begin(), componentType.end());


reg.payload.push_back(static_cast<uint8_t>(idxPacketEntities >> 24) & 0xFF);
reg.payload.push_back(static_cast<uint8_t>((idxPacketEntities >> 16) & 0xFF));
reg.payload.push_back(static_cast<uint8_t>((idxPacketEntities >> 8) & 0xFF));
Expand Down

0 comments on commit c809361

Please sign in to comment.