Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/missile pos other clients #127

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/Client/Systems/Events/EventsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,31 @@ namespace Systems {
return true;
}

static Types::Position adjustPlayerBulletPosition(Types::Position &playerPos, std::size_t id)
static Types::Position
adjustMissilePosition(Types::Position &pos, missileTypes_e typeOfMissile, std::size_t id)
{
Types::Position newPos = pos;
Json &json = Json::getInstance();
Registry &registry = Registry::getInstance();
Registry::components<Types::CollisionRect> arrCol = registry.getComponents<Types::CollisionRect>();

if (arrCol.exist(id)) {
Types::CollisionRect &col = arrCol[id];
float posX = Maths::intToFloatConservingDecimals(playerPos.x)
float posX = Maths::intToFloatConservingDecimals(pos.x)
+ (Maths::intToFloatConservingDecimals(col.width) / 2.F);
float posY = Maths::intToFloatConservingDecimals(playerPos.y)
float posY = Maths::intToFloatConservingDecimals(pos.y)
+ (Maths::intToFloatConservingDecimals(col.height) / 2.F);
return {Maths::floatToIntConservingDecimals(posX), Maths::floatToIntConservingDecimals(posY)};
} else {
return {playerPos.x, playerPos.y};
newPos.x = Maths::floatToIntConservingDecimals(posX);
newPos.y = Maths::floatToIntConservingDecimals(posY);
}
nlohmann::json bulletData =
json.getJsonObjectById(JsonType::BULLETS, getMissileId(typeOfMissile), "bullets");
Types::CollisionRect collisionRect =
json.getDataFromJson<Types::CollisionRect>(bulletData, "collisionRect");
int halfSprite = Maths::divisionWithTwoIntDecimals(collisionRect.width, 200);
newPos.y = Maths::subtractionWithTwoIntDecimals(newPos.y, halfSprite);
newPos.x = pos.x;
return newPos;
}

void playerShootBullet(std::size_t /*unused*/, std::size_t /*unused*/)
Expand All @@ -119,10 +129,11 @@ namespace Systems {
if (arrHealth.exist(id) && arrHealth[id].hp <= 0) {
continue;
}
Types::Position pos = adjustMissilePosition(arrPosition[id], missile.type, id);
Nitwork::NitworkClient::getInstance().addNewBulletMsg(
{Maths::removeIntDecimals(arrPosition[id].x), Maths::removeIntDecimals(arrPosition[id].y)},
{Maths::removeIntDecimals(pos.x), Maths::removeIntDecimals(pos.y)},
missile.type);
createMissile(adjustPlayerBulletPosition(arrPosition[id], id), missile);
createMissile(pos, missile);
clock_.restart(getClockIdFromMissileType(missile.type));
}
}
Expand Down
13 changes: 2 additions & 11 deletions src/ECS/Systems/BulletsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Systems {
{PERFORANT, "perforant"}
};

static std::string getMissileId(missileTypes_e type)
std::string getMissileId(missileTypes_e type)
{
auto it = missileTypeMap.find(type);
if (it != missileTypeMap.end()) {
Expand Down Expand Up @@ -61,15 +61,6 @@ namespace Systems {
}
#endif

static Types::Position adjustMissilePosition(Types::Position &pos, Types::CollisionRect &collisionRect)
{
Types::Position newPos = pos;
int halfSprite = Maths::divisionWithTwoIntDecimals(collisionRect.width, 200);
newPos.y = Maths::subtractionWithTwoIntDecimals(newPos.y, halfSprite);
newPos.x = pos.x;
return newPos;
}

#ifdef CLIENT
static void addSpriteRectsForBullet(nlohmann::json &bulletData, Types::CollisionRect &collisionRect)
{
Expand Down Expand Up @@ -124,7 +115,7 @@ namespace Systems {
Types::Missiles missileType = typeOfMissile;
Types::Dead deadComp = {};
Types::PlayerAllies playerAlliesComp = {};
Types::Position position = adjustMissilePosition(pos, collisionRect);
Types::Position position = pos;
struct health_s healthComp = {json.getDataFromJson<int>(bulletData, "health")};
Types::Damage damageComp = {json.getDataFromJson<int>(bulletData, "damage")};

Expand Down
1 change: 1 addition & 0 deletions src/ECS/Systems/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Systems {
void entitiesCollision(std::size_t, std::size_t);
void deathChecker(std::size_t, std::size_t);
void updatePhysics(std::size_t, std::size_t);
std::string getMissileId(missileTypes_e type);
void initWave(std::size_t managerId, std::size_t systemId);
void initPlayer(
unsigned int constId,
Expand Down