summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-05-15 15:37:59 +0200
committerAki <please@ignore.pl>2022-05-15 15:37:59 +0200
commit345bb237a7f682670a6107ae75afd948cc1f6ab6 (patch)
treefd4d2982d33c837bd7c4079e41434bbbf3ec8f8a
parent1b61e9b2b8c3b2218ec5a908a547180a642719e6 (diff)
downloadderelict-345bb237a7f682670a6107ae75afd948cc1f6ab6.zip
derelict-345bb237a7f682670a6107ae75afd948cc1f6ab6.tar.gz
derelict-345bb237a7f682670a6107ae75afd948cc1f6ab6.tar.bz2
Separated Wreck from Killmail
-rw-r--r--CMakeLists.txt1
-rw-r--r--DumpSource.cpp62
-rw-r--r--DumpSource.h6
-rw-r--r--Grid.h6
-rw-r--r--Killmail.h5
-rw-r--r--Reader.cpp67
-rw-r--r--Reader.h12
-rw-r--r--Source.h4
-rw-r--r--View.cpp27
-rw-r--r--View.h7
-rw-r--r--Wreck.h12
-rw-r--r--main.cpp5
12 files changed, 125 insertions, 89 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 94427fa..30ff0fe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -9,6 +9,7 @@ add_executable(
${PROJECT_NAME}
DumpSource.cpp
main.cpp
+ Reader.cpp
View.cpp
)
target_link_libraries(
diff --git a/DumpSource.cpp b/DumpSource.cpp
index 56b3336..1fdf7a8 100644
--- a/DumpSource.cpp
+++ b/DumpSource.cpp
@@ -1,6 +1,5 @@
#include "DumpSource.h"
-#include <cmath>
#include <string>
#include <utility>
#include <vector>
@@ -8,23 +7,14 @@
#include <nlohmann/json.hpp>
#include <raylib.h>
-#include "Grid.h"
#include "Killmail.h"
-#include "LongVector3.h"
using json = nlohmann::json;
-static constexpr long double SCALE {.0001};
-static constexpr long double EXTENT {1000 * 50000};
-
-
-static Grid& find_grid_for(std::vector<Grid>& grids, const LongVector3& original);
-
-
DumpSource::DumpSource(const char* filename) :
- m_grids {}
+ m_killmails {}
{
if (!FileExists(filename))
throw "File does not exist";
@@ -32,7 +22,7 @@ DumpSource::DumpSource(const char* filename) :
auto dump = json::parse(text);
for (const auto& info : dump["killmails"]) {
Killmail km;
- km.original = {
+ km.position = {
info["victim"]["position"]["x"].get<long double>(),
info["victim"]["position"]["y"].get<long double>(),
info["victim"]["position"]["z"].get<long double>(),
@@ -42,54 +32,14 @@ DumpSource::DumpSource(const char* filename) :
km.location.system = location["name"].get<std::string>().data();
km.location.constellation = location["constellation"].get<std::string>().data();
km.location.region = location["region"].get<std::string>().data();
- auto& grid = find_grid_for(m_grids, km.original);
- grid.killmails.push_back(std::move(km));
+ m_killmails.push_back(std::move(km));
}
UnloadFileText(text);
- for (auto& grid : m_grids) {
- LongVector3 average {0, 0, 0};
- for (const auto& km : grid.killmails) {
- average.x += km.original.x;
- average.y += km.original.y;
- average.z += km.original.z;
- }
- const auto killmails = grid.killmails.size();
- average.x /= killmails;
- average.y /= killmails;
- average.z /= killmails;
- for (auto& km : grid.killmails) {
- km.position = {
- static_cast<float>((km.original.x - average.x) * SCALE),
- static_cast<float>((km.original.y - average.y) * SCALE),
- static_cast<float>((km.original.z - average.z) * SCALE),
- };
- }
- grid.origin = average;
- }
}
-std::vector<Grid>
-DumpSource::grids() const
+std::vector<Killmail>
+DumpSource::killmails() const
{
- return m_grids;
-}
-
-
-Grid&
-find_grid_for(std::vector<Grid>& grids, const LongVector3& original)
-{
- for (auto& grid : grids) {
- for (auto& km : grid.killmails) {
- const long double dist =
- std::sqrt(
- std::pow(original.x - km.original.x, 2) +
- std::pow(original.y - km.original.y, 2) +
- std::pow(original.z - km.original.z, 2));
- if (dist < EXTENT)
- return grid;
- }
- }
- grids.push_back(Grid{});
- return grids.back();
+ return m_killmails;
}
diff --git a/DumpSource.h b/DumpSource.h
index e90c8af..3c18b69 100644
--- a/DumpSource.h
+++ b/DumpSource.h
@@ -2,7 +2,7 @@
#include <vector>
-#include "Grid.h"
+#include "Killmail.h"
#include "Source.h"
@@ -10,7 +10,7 @@ class DumpSource : public Source
{
public:
explicit DumpSource(const char* filename);
- std::vector<Grid> grids() const override;
+ std::vector<Killmail> killmails() const override;
private:
- std::vector<Grid> m_grids;
+ std::vector<Killmail> m_killmails;
};
diff --git a/Grid.h b/Grid.h
index 8a6c3e7..e9f45d7 100644
--- a/Grid.h
+++ b/Grid.h
@@ -2,14 +2,12 @@
#include <vector>
-#include <raylib.h>
-
-#include "Killmail.h"
#include "LongVector3.h"
+#include "Wreck.h"
struct Grid
{
LongVector3 origin;
- std::vector<Killmail> killmails;
+ std::vector<Wreck> wrecks;
};
diff --git a/Killmail.h b/Killmail.h
index cc0f06b..6550dc9 100644
--- a/Killmail.h
+++ b/Killmail.h
@@ -1,7 +1,5 @@
#pragma once
-#include <raylib.h>
-
#include "Location.h"
#include "LongVector3.h"
#include "Owner.h"
@@ -13,6 +11,5 @@ struct Killmail
Location location;
Owner owner;
Ship ship;
- Vector3 position;
- LongVector3 original;
+ LongVector3 position;
};
diff --git a/Reader.cpp b/Reader.cpp
new file mode 100644
index 0000000..f5c8de3
--- /dev/null
+++ b/Reader.cpp
@@ -0,0 +1,67 @@
+#include "Reader.h"
+
+#include <cmath>
+#include <vector>
+
+#include "Grid.h"
+#include "LongVector3.h"
+#include "Source.h"
+#include "Wreck.h"
+
+
+static constexpr long double SCALE {.0001};
+static constexpr long double EXTENT {1000 * 50000};
+
+
+static Grid& find_grid_for(std::vector<Grid>& grids, const LongVector3& position);
+
+
+std::vector<Grid>
+Reader::read(Source& source)
+{
+ std::vector<Grid> grids;
+ for (auto& km : source.killmails()) {
+ auto& grid = find_grid_for(grids, km.position);
+ grid.wrecks.push_back(Wreck{Vector3{0, 0, 0}, km});
+ }
+ for (auto& grid : grids) {
+ LongVector3 average {0, 0, 0};
+ for (const auto& wreck : grid.wrecks) {
+ average.x += wreck.killmail.position.x;
+ average.y += wreck.killmail.position.y;
+ average.z += wreck.killmail.position.z;
+ }
+ const auto killmails = grid.wrecks.size();
+ average.x /= killmails;
+ average.y /= killmails;
+ average.z /= killmails;
+ for (auto& wreck : grid.wrecks) {
+ wreck.position = {
+ static_cast<float>((wreck.killmail.position.x - average.x) * SCALE),
+ static_cast<float>((wreck.killmail.position.y - average.y) * SCALE),
+ static_cast<float>((wreck.killmail.position.z - average.z) * SCALE),
+ };
+ }
+ grid.origin = average;
+ }
+ return grids;
+}
+
+
+Grid&
+find_grid_for(std::vector<Grid>& grids, const LongVector3& position)
+{
+ for (auto& grid : grids) {
+ for (auto& wreck : grid.wrecks) {
+ const long double dist =
+ std::sqrt(
+ std::pow(position.x - wreck.killmail.position.x, 2) +
+ std::pow(position.y - wreck.killmail.position.y, 2) +
+ std::pow(position.z - wreck.killmail.position.z, 2));
+ if (dist < EXTENT)
+ return grid;
+ }
+ }
+ grids.push_back(Grid{});
+ return grids.back();
+}
diff --git a/Reader.h b/Reader.h
new file mode 100644
index 0000000..6149958
--- /dev/null
+++ b/Reader.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <vector>
+
+#include "Grid.h"
+#include "Source.h"
+
+
+struct Reader
+{
+ static std::vector<Grid> read(Source& source);
+};
diff --git a/Source.h b/Source.h
index c1d0876..1dba9ea 100644
--- a/Source.h
+++ b/Source.h
@@ -2,11 +2,11 @@
#include <vector>
-#include "Grid.h"
+#include "Killmail.h"
struct Source
{
virtual ~Source() = default;
- virtual std::vector<Grid> grids() const = 0;
+ virtual std::vector<Killmail> killmails() const = 0;
};
diff --git a/View.cpp b/View.cpp
index c4b0e5a..77ab271 100644
--- a/View.cpp
+++ b/View.cpp
@@ -2,18 +2,16 @@
#include <algorithm>
#include <cmath>
-#include <memory>
-#include <utility>
#include <raylib.h>
+#include "Grid.h"
#include "Label.h"
-#include "Source.h"
-View::View(std::unique_ptr<Source> source) :
+View::View(std::vector<Grid> grids) :
m_camera {},
- m_source {std::move(source)},
+ m_grids {grids},
m_labels {},
m_grid {0}
{
@@ -29,28 +27,27 @@ View::View(std::unique_ptr<Source> source) :
void
View::update(const float dt)
{
- const auto grids = m_source->grids();
if (IsKeyPressed(KEY_SPACE)) {
m_grid++;
- if (m_grid >= grids.size())
+ if (m_grid >= m_grids.size())
m_grid = 0;
}
UpdateCamera(&m_camera);
const int height = GetScreenHeight();
const int width = GetScreenWidth();
- const auto killmails = grids.at(m_grid).killmails;
+ const auto wrecks = m_grids.at(m_grid).wrecks;
m_labels.clear();
- m_labels.reserve(killmails.size());
- for (const auto& km : killmails) {
- const auto pos = GetWorldToScreen(km.position, m_camera);
+ m_labels.reserve(wrecks.size());
+ for (const auto& wreck : wrecks) {
+ const auto pos = GetWorldToScreen(wreck.position, m_camera);
const float depth =
std::sqrt(
- std::pow(m_camera.position.x - km.position.x, 2) +
- std::pow(m_camera.position.y - km.position.y, 2) +
- std::pow(m_camera.position.z - km.position.z, 2));
+ std::pow(m_camera.position.x - wreck.position.x, 2) +
+ std::pow(m_camera.position.y - wreck.position.y, 2) +
+ std::pow(m_camera.position.z - wreck.position.z, 2));
if (0 > pos.x || width < pos.x || 0 > pos.y || height < pos.y)
continue;
- const auto base = GetWorldToScreen({km.position.x, 0.0f, km.position.z}, m_camera);
+ const auto base = GetWorldToScreen({wreck.position.x, 0.0f, wreck.position.z}, m_camera);
m_labels.push_back(Label{pos, base, depth});
}
std::sort(m_labels.begin(), m_labels.end(), [](auto& a, auto& b){ return a.depth > b.depth; });
diff --git a/View.h b/View.h
index 8e247d1..1928726 100644
--- a/View.h
+++ b/View.h
@@ -1,23 +1,22 @@
#pragma once
-#include <memory>
#include <vector>
#include <raylib.h>
+#include "Grid.h"
#include "Label.h"
-#include "Source.h"
class View
{
public:
- explicit View(std::unique_ptr<Source> source);
+ explicit View(std::vector<Grid> grids);
void update(float dt);
void draw() const;
private:
Camera m_camera;
- std::unique_ptr<Source> m_source;
+ std::vector<Grid> m_grids;
std::vector<Label> m_labels;
int m_grid;
float m_timer;
diff --git a/Wreck.h b/Wreck.h
new file mode 100644
index 0000000..f7b3d1b
--- /dev/null
+++ b/Wreck.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <raylib.h>
+
+#include "Killmail.h"
+
+
+struct Wreck
+{
+ Vector3 position;
+ Killmail killmail;
+};
diff --git a/main.cpp b/main.cpp
index d1c9b86..c21aa0d 100644
--- a/main.cpp
+++ b/main.cpp
@@ -3,6 +3,7 @@
#include <raylib.h>
#include "DumpSource.h"
+#include "Reader.h"
#include "View.h"
@@ -12,7 +13,9 @@ main(int argc, char* argv[])
InitWindow(800, 600, "Derelict");
SetWindowState(FLAG_WINDOW_RESIZABLE);
SetTargetFPS(60);
- View view(std::make_unique<DumpSource>("sample.json"));
+ DumpSource source("sample.json");
+ auto grids = Reader::read(source);
+ View view(grids);
while (!WindowShouldClose())
{
view.update(GetFrameTime());