From 9e3bbd489f690d0ee20e2fc59cc0034ceed27e9d Mon Sep 17 00:00:00 2001 From: Ilya246 Date: Sun, 26 Jun 2022 19:59:49 +0400 Subject: [PATCH] significant bugfixes --- include/entities.hpp | 5 +++-- include/globals.hpp | 2 ++ src/entities.cpp | 22 +++++++++++++++------- src/main.cpp | 27 ++++++++++++++++++++------- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/include/entities.hpp b/include/entities.hpp index 9cca452..d07b9e6 100644 --- a/include/entities.hpp +++ b/include/entities.hpp @@ -38,7 +38,8 @@ struct Entity { virtual ~Entity() noexcept; virtual void control(movement& cont); - virtual void update(); + virtual void update1(); + virtual void update2(); virtual void draw(); virtual void collide(Entity* with, bool collideOther); @@ -123,7 +124,7 @@ struct Attractor: public Entity { Attractor(double radius, double mass); Attractor(bool ghost); - void update() override; + void update2() override; void draw() override; void loadCreatePacket(sf::Packet& packet) override; diff --git a/include/globals.hpp b/include/globals.hpp index fa47f68..ab93c95 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -53,6 +53,7 @@ messageLimit = 50, textCharacterSize = 18, nextID = 0, predictSteps = (int)(30.0 / predictDelta * 60.0); +inline size_t trajectoryOffset = 0; inline bool headless = false, autoConnect = false, debug = false, inputWaiting = false, chatting = false, lockControls = false, @@ -68,6 +69,7 @@ using namespace obf::Types; inline std::map vars {{"port", {Int, &port}}, {"predictSteps", {Int, &predictSteps}}, {"syncSpacing", {Double, &syncSpacing}}, + {"friction", {Double, &friction}}, {"collideRestitution", {Double, &collideRestitution}}, {"gravityStrength", {Double, &G}}, {"blackholeChance", {Double, &blackholeChance}}, diff --git a/src/entities.cpp b/src/entities.cpp index c5f9b54..a676e1c 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -127,9 +127,11 @@ void Entity::syncCreation() { } void Entity::control(movement& cont) {} -void Entity::update() { +void Entity::update1() { x += velX * delta; y += velY * delta; +} +void Entity::update2() { if (globalTime - lastCollideScan > collideScanSpacing) [[unlikely]] { size_t i = 0; for (Entity* e : updateGroup) { @@ -202,12 +204,18 @@ void Entity::update() { void Entity::draw() { sf::Color trajColor(color[0], color[1], color[2]); - if (trajectory && lastTrajectoryRef && trajectory->size() > 0) [[likely]] { + if (trajectory && lastTrajectoryRef && trajectory->size() > trajectoryOffset) [[likely]] { std::vector traj = *trajectory; - sf::VertexArray lines(sf::LineStrip, traj.size()); - for (size_t i = 0; i < traj.size(); i++){ - lines[i].position = sf::Vector2f(lastTrajectoryRef->x + traj[i].x + drawShiftX, lastTrajectoryRef->y + traj[i].y + drawShiftY); + size_t to = traj.size() - trajectoryOffset; + sf::VertexArray lines(sf::LineStrip, to); + float lastAlpha = 255; + float decBy = (255.f - 64.f) / (to); + for (size_t i = 0; i < to; i++){ + Point point = traj[i + trajectoryOffset]; + lines[i].position = sf::Vector2f(lastTrajectoryRef->x + point.x + drawShiftX, lastTrajectoryRef->y + point.y + drawShiftY); lines[i].color = trajColor; + lines[i].color.a = (uint8_t)lastAlpha; + lastAlpha -= decBy; } window->draw(lines); } @@ -515,8 +523,8 @@ void Attractor::unloadSyncPacket(sf::Packet& packet) { packet >> x >> y >> velX >> velY; } -void Attractor::update() { - Entity::update(); +void Attractor::update2() { + Entity::update2(); for (Entity* e : updateGroup) { if (e == this) [[unlikely]] { continue; diff --git a/src/main.cpp b/src/main.cpp index 04548a7..2a2f1a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -339,14 +339,21 @@ int main(int argc, char** argv) { } else { drawShiftX = 0, drawShiftY = 0; } + trajectoryOffset = round((globalTime - lastPredict) / predictDelta * 60.0); for (size_t i = 0; i < ghostTrajectories.size(); i++) { std::vector traj = ghostTrajectories[i]; - if (lastTrajectoryRef && traj.size() > 0) [[likely]] { + if (lastTrajectoryRef && traj.size() > trajectoryOffset) [[likely]] { sf::Color trajColor = ghostTrajectoryColors[i]; - sf::VertexArray lines(sf::LineStrip, traj.size()); - for (size_t i = 0; i < traj.size(); i++){ - lines[i].position = sf::Vector2f(lastTrajectoryRef->x + traj[i].x + drawShiftX, lastTrajectoryRef->y + traj[i].y + drawShiftY); + size_t to = traj.size() - trajectoryOffset; + sf::VertexArray lines(sf::LineStrip, to); + float lastAlpha = 255; + float decBy = (255.f - 64.f) / to; + for (size_t i = 0; i < to; i++) { + Point point = traj[i + trajectoryOffset]; + lines[i].position = sf::Vector2f(lastTrajectoryRef->x + point.x + drawShiftX, lastTrajectoryRef->y + point.y + drawShiftY); lines[i].color = trajColor; + lines[i].color.a = lastAlpha; + lastAlpha -= decBy; } window->draw(lines); } @@ -567,7 +574,10 @@ int main(int argc, char** argv) { } for (size_t i = 0; i < updateGroup.size(); i++) { - updateGroup[i]->update(); + updateGroup[i]->update1(); + } + for (size_t i = 0; i < updateGroup.size(); i++) { + updateGroup[i]->update2(); } if (headless && lastSweep + projectileSweepSpacing < globalTime) { @@ -618,8 +628,11 @@ int main(int argc, char** argv) { for (int i = 0; i < predictSteps; i++) { predictingFor = predictDelta * predictSteps; globalTime += predictDelta / 60.0; - for (Entity* e : updateGroup) { - e->update(); + for (size_t i = 0; i < updateGroup.size(); i++) { + updateGroup[i]->update1(); + } + for (size_t i = 0; i < updateGroup.size(); i++) { + updateGroup[i]->update2(); } if (!stars.empty()) [[likely]] { double x = 0.0, y = 0.0;