Skip to content

Commit

Permalink
a lot of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya246 committed Jun 25, 2022
1 parent 6ec2f71 commit ae6ce2d
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 89 deletions.
14 changes: 8 additions & 6 deletions include/entities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,17 @@ struct Triangle: public Entity {
lastBoosted = -boostCooldown, lastShot = -reload, hyperboostCharge = 0.0,
resLastBoosted, resLastShot, resHyperboostCharge, resRotation;

bool burning, resBurning;
bool burning = false, resBurning;
std::string name = "";

std::unique_ptr<std::vector<Point>> inertTrajectory;
std::unique_ptr<sf::CircleShape> shape, forwards;
float rotation = 0;
float rotation = 0.f;
};

struct Attractor: public Entity {
Attractor(double radius);
Attractor(double radius, double mass);
Attractor(bool ghost);

void update() override;
void draw() override;
Expand All @@ -128,7 +128,9 @@ struct Attractor: public Entity {

uint8_t type() override;

std::unique_ptr<sf::CircleShape> shape;
bool star = false, blackhole = false;

std::unique_ptr<sf::CircleShape> shape, warning;
};

struct Projectile: public Entity {
Expand Down Expand Up @@ -157,11 +159,11 @@ struct Player {

sf::TcpSocket tcpSocket;
std::vector<sf::Packet> tcpQueue;
std::string username = "", ip;
std::string username = "", ip = "";
double lastAck = 0.0, lastPingSent = 0.0, lastSynced = 0.0, lastFullsynced = 0.0, ping = 0.0,
viewW = 500.0, viewH = 500.0;
movement controls;
unsigned short port;
unsigned short port = 0;
};

}
12 changes: 8 additions & 4 deletions include/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ inline std::vector<Entity*> updateGroup;
inline std::vector<Player*> playerGroup;
inline std::vector<Entity*> entityDeleteBuffer;
inline std::vector<Attractor*> planets;
inline std::vector<std::vector<Point>> ghostTrajectories;
inline std::vector<sf::Color> ghostTrajectoryColors;
inline sf::Vector2i mousePos;
inline sf::Clock deltaClock, globalClock;
inline std::future<void> inputReader;
Expand All @@ -37,6 +39,7 @@ inline double delta = 1.0 / 60.0,
collideScanSpacing = 0.5, collideScanDistance2 = 60.0 * 60.0,
collideRestitution = 1.2, // how "bouncy" collisions should be
friction = 0.002, // friction of colliding bodies, stops infinite sliding
extraStarChance = 0.3, blackholeChance = 1.0 / 3.0, starMass = 5.0e20, starR = 6.0e4,
syncCullThreshold = 0.6, syncCullOffset = 100000.0, sweepThreshold = 4e6 * 4e6,
predictSpacing = 0.2, predictDelta = 6.0,
G = 6.67e-11,
Expand All @@ -52,8 +55,7 @@ predictSteps = (int)(30.0 / predictDelta * 60.0);
inline bool headless = false, autoConnect = false, debug = false,
inputWaiting = false,
chatting = false,
simulating = false,
blackholeSystem = false;
simulating = false;

struct Var {
uint8_t type;
Expand All @@ -66,17 +68,19 @@ inline std::map<std::string, Var> vars {{"port", {Int, &port}},
{"syncSpacing", {Double, &syncSpacing}},
{"collideRestitution", {Double, &collideRestitution}},
{"gravityStrength", {Double, &G}},
{"blackholeChance", {Double, &blackholeChance}},
{"extraStarChance", {Double, &extraStarChance}},
{"name", {String, &name}},
{"serverAddress", {String, &serverAddress}},
{"autoConnect", {Bool, &autoConnect}},
{"DEBUG", {Bool, &debug}}};

inline sf::String displayMessages[displayMessageCount];

inline Attractor* star = nullptr;
inline Triangle* ghost = nullptr;
inline std::vector<Attractor*> stars;
inline Entity* trajectoryRef = nullptr;
inline Entity* lastTrajectoryRef = nullptr;
inline Entity* systemCenter = nullptr;

inline const std::string configFile = "config.txt", configDocFile = "confighelp.txt";

Expand Down
3 changes: 1 addition & 2 deletions include/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ constexpr uint16_t Ping = 0,
PingInfo = 9,
ResizeView = 10,
Name = 11,
PlanetCollision = 12,
SystemInfo = 13;
PlanetCollision = 12;
}

namespace obf::Entities {
Expand Down
58 changes: 36 additions & 22 deletions src/entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ Entity::Entity() {
}

Entity::~Entity() noexcept {
// swap remove this from updateGroup
// O(n) complexity since iterates whole thing at worst
if (debug) {
printf("Deleting entity id %u\n", this->id);
}
Expand Down Expand Up @@ -143,13 +141,13 @@ void Entity::update() {
if (dst2(x - e->x, y - e->y) <= (radius + e->radius) * (radius + e->radius)) [[unlikely]] {
collide(e, true);
if (type() == Entities::Attractor) {
if (this == star && e->type() == Entities::Triangle) [[unlikely]] {
if (((Attractor*)this)->star && e->type() == Entities::Triangle) [[unlikely]] {
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.");
sendMessage.append("<").append(p->name()).append("> has been incinerated.");
std::cout << sendMessage << std::endl;
chatPacket << Packets::Chat << sendMessage;
p->tcpSocket.disconnect();
Expand Down Expand Up @@ -258,7 +256,6 @@ Triangle::Triangle() : Entity() {
icon = std::make_unique<sf::CircleShape>(3.f, 3);
icon->setOrigin(3.f, 3.f);
icon->setFillColor(sf::Color(255, 255, 255));
inertTrajectory = std::make_unique<std::vector<Point>>();
}
}
Triangle::Triangle(bool ghost) : Entity() {
Expand Down Expand Up @@ -380,16 +377,6 @@ void Triangle::control(movement& cont) {

void Triangle::draw() {
Entity::draw();
if (inertTrajectory && lastTrajectoryRef && inertTrajectory->size() > 0) [[likely]] {
sf::Color trajColor((unsigned char)(color[0] * 0.7), (unsigned char)(color[1] * 0.7), (unsigned char)(color[2] * 0.7));
std::vector<Point> traj = *inertTrajectory;
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);
lines[i].color = trajColor;
}
window->draw(lines);
}
shape->setPosition(x + drawShiftX, y + drawShiftY);
shape->setRotation(90.f - rotation);
shape->setFillColor(sf::Color(color[0], color[1], color[2]));
Expand Down Expand Up @@ -454,12 +441,17 @@ uint8_t Triangle::type() {

Attractor::Attractor(double radius) : Entity() {
this->radius = radius;
this->mass = 100.0;
this->mass = 1.0e18;
if (!headless) {
shape = std::make_unique<sf::CircleShape>(radius, std::max(4, (int)(sqrt(radius))));
shape->setOrigin(radius, radius);
icon = std::make_unique<sf::CircleShape>(2.f, 6);
icon->setOrigin(2.f, 2.f);
warning = std::make_unique<sf::CircleShape>(5.f, 4);
warning->setOrigin(5.f, 5.f);
warning->setFillColor(sf::Color(0, 0, 0, 0));
warning->setOutlineColor(sf::Color(255, 0, 0));
warning->setOutlineThickness(1.f);
}
}
Attractor::Attractor(double radius, double mass) : Entity() {
Expand All @@ -470,17 +462,32 @@ Attractor::Attractor(double radius, double mass) : Entity() {
shape->setOrigin(radius, radius);
icon = std::make_unique<sf::CircleShape>(2.f, 6);
icon->setOrigin(2.f, 2.f);
warning = std::make_unique<sf::CircleShape>(5.f, 4);
warning->setOrigin(5.f, 5.f);
warning->setFillColor(sf::Color(0, 0, 0, 0));
warning->setOutlineColor(sf::Color(255, 0, 0));
warning->setOutlineThickness(1.f);
}
}
Attractor::Attractor(bool ghost) {
for (size_t i = 0; i < updateGroup.size(); i++) {
Entity* e = updateGroup[i];
if (e == this) [[unlikely]] {
updateGroup[i] = updateGroup[updateGroup.size() - 1];
updateGroup.pop_back();
break;
}
}
}

void Attractor::loadCreatePacket(sf::Packet& packet) {
packet << type() << radius << id << x << y << velX << velY << mass << color[0] << color[1] << color[2];
packet << type() << radius << id << x << y << velX << velY << mass << star << blackhole << color[0] << color[1] << color[2];
if(debug){
printf("Sent id %d: %g %g %g %g\n", id, x, y, velX, velY);
}
}
void Attractor::unloadCreatePacket(sf::Packet& packet) {
packet >> id >> x >> y >> velX >> velY >> mass >> color[0] >> color[1] >> color[2];
packet >> id >> x >> y >> velX >> velY >> mass >> star >> blackhole >> color[0] >> color[1] >> color[2];
if(debug){
printf(", id %d: %g %g %g %g\n", id, x, y, velX, velY);
}
Expand Down Expand Up @@ -514,11 +521,18 @@ void Attractor::draw() {
shape->setPosition(x + drawShiftX, y + drawShiftY);
shape->setFillColor(sf::Color(color[0], color[1], color[2]));
window->draw(*shape);
if (g_camera.scale > radius && ownEntity) {
if (ownEntity) {
g_camera.bindUI();
icon->setPosition(g_camera.w * 0.5 + (x - ownEntity->x) / g_camera.scale, g_camera.h * 0.5 + (y - ownEntity->y) / g_camera.scale);
icon->setFillColor(sf::Color(color[0], color[1], color[2]));
window->draw(*icon);
double uiX = g_camera.w * 0.5 + (x - ownEntity->x) / g_camera.scale, uiY = g_camera.h * 0.5 + (y - ownEntity->y) / g_camera.scale;
if (g_camera.scale > radius) {
icon->setPosition(uiX, uiY);
icon->setFillColor(sf::Color(color[0], color[1], color[2]));
window->draw(*icon);
}
if (blackhole && this != lastTrajectoryRef) {
warning->setPosition(uiX, uiY);
window->draw(*warning);
}
g_camera.bindWorld();
}
}
Expand Down
Loading

0 comments on commit ae6ce2d

Please sign in to comment.