Skip to content

Commit

Permalink
finally fix star collision crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya246 committed Jun 22, 2022
1 parent a3cef43 commit 433b0f3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion include/entities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct Triangle: public Entity {
void unloadSyncPacket(sf::Packet& packet) override;

uint8_t type() override;
double accel = 0.004, rotateSpeed = 2.0, boostCooldown = 15.0, boostStrength = 2.0, reload = 6.0, shootPower = 2.0,
double accel = 6, rotateSpeed = 2.0, boostCooldown = 15.0, boostStrength = 2.0, reload = 6.0, shootPower = 2.0,
lastBoosted = -boostCooldown, lastShot = -reload;

std::string name = "";
Expand Down
41 changes: 32 additions & 9 deletions src/entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,31 @@ void Entity::update() {
}
lastCollideScan = globalTime;
}
for (Entity* e : near) {
for (size_t i = 0; i < near.size(); i++) {
Entity* e = near[i];
if (dst2(x - e->x, y - e->y) <= (radius + e->radius) * (radius + e->radius)) [[unlikely]] {
collide(e, true);
collide(near[i], true);
if (this == star) {
bool found = false;
for (Player* p : playerGroup) {
if (p->entity == e) [[unlikely]] {
sf::Packet chatPacket;
std::string sendMessage;
sendMessage.append("<").append(p->name()).append("> has been incinerated by the star.");
std::cout << sendMessage << std::endl;
chatPacket << Packets::Chat << sendMessage;
p->tcpSocket.disconnect();
delete p;
for (Player* p : playerGroup) {
p->tcpSocket.send(chatPacket);
}
found = true;
}
}
if (!found) {
delete e;
}
}
}
}
}
Expand All @@ -128,11 +150,7 @@ void Entity::collide(Entity* with, bool collideOther) {
if (debug && dst2(with->velX - velX, with->velY - velY) > 0.1) [[unlikely]] {
printf("collision: %u-%u\n", id, with->id);
}
if (this == star) [[unlikely]] {
delete with;
return;
}
if (with->type() == Entities::Projectile || with == star) {
if (with->type() == Entities::Projectile) {
return;
}
double dVx = velX - with->velX, dVy = with->velY - velY;
Expand Down Expand Up @@ -316,8 +334,13 @@ void Attractor::update() {
}

double xdiff = e->x - x, ydiff = y - e->y;
double factor = -mass * delta * G / pow(xdiff * xdiff + ydiff * ydiff, 1.5);
e->addVelocity(xdiff * factor, ydiff * factor);
double factor = delta * G / pow(xdiff * xdiff + ydiff * ydiff, 1.5);
double factorm = -factor * mass;
e->addVelocity(xdiff * factorm, ydiff * factorm);
if (e->type() != Entities::Attractor) {
double factortm = factor * e->mass;
addVelocity(xdiff * factortm, ydiff * factortm);
}
}
}
void Attractor::draw() {
Expand Down
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ int main(int argc, char** argv) {

window->clear(sf::Color(20, 16, 50));
g_camera.bindWorld();
for (auto* entity : updateGroup) {
entity->draw();
for (size_t i = 0; i < updateGroup.size(); i++) {
updateGroup[i]->draw();
}
g_camera.bindUI();
if (ownEntity) {
Expand Down Expand Up @@ -424,8 +424,8 @@ int main(int argc, char** argv) {
*(unsigned char*) &lastControls = *(unsigned char*) &controls;
}

for (auto* entity : updateGroup) {
entity->update();
for (size_t i = 0; i < updateGroup.size(); i++) {
updateGroup[i]->update();
}
if (headless) {
int to = playerGroup.size();
Expand Down

0 comments on commit 433b0f3

Please sign in to comment.