Skip to content

Commit

Permalink
significant bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya246 committed Jun 26, 2022
1 parent 78819bc commit 9e3bbd4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
5 changes: 3 additions & 2 deletions include/entities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions include/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -68,6 +69,7 @@ using namespace obf::Types;
inline std::map<std::string, Var> vars {{"port", {Int, &port}},
{"predictSteps", {Int, &predictSteps}},
{"syncSpacing", {Double, &syncSpacing}},
{"friction", {Double, &friction}},
{"collideRestitution", {Double, &collideRestitution}},
{"gravityStrength", {Double, &G}},
{"blackholeChance", {Double, &blackholeChance}},
Expand Down
22 changes: 15 additions & 7 deletions src/entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Point> 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);
}
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 20 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point> 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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9e3bbd4

Please sign in to comment.