Lines are under ECS too

This commit is contained in:
2024-10-03 01:35:31 +03:00
parent b0613d9ad1
commit db39715354

View File

@@ -13,89 +13,153 @@
#include "from_string.h"
#include "buildings_data.h"
#include "editor_event.h"
#include "base_data.h"
#include "road_lines_data.h"
class LinesAccessor {
HashMap<String, struct RoadLinesData::road_line> lines_;
struct CLine {
struct RoadLinesData::road_line line;
};
class LinesAccessor {
public:
LinesAccessor()
{
BaseData::get_singleton()->get().component<CLine>();
}
const flecs::world &get() const
{
return BaseData::get_singleton()->get();
}
flecs::world &get()
{
return BaseData::get_singleton()->get();
}
template <typename F> void each(F &&func) const
{
get().each<F>(func);
}
inline flecs::entity lookup(const String &key) const
{
String ename = "line:" + key;
flecs::entity e = get().lookup(ename.ascii().ptr());
assert(e.is_valid());
return e;
}
inline flecs::entity lookup_create(const String &key) const
{
String ename = "line:" + key;
flecs::entity e = get().entity(ename.ascii().ptr());
assert(e.is_valid());
return e;
}
bool has_entity(const String &key) const
{
String ename = "line:" + key;
flecs::entity e = get().lookup(ename.ascii().ptr());
return e.is_valid();
}
inline const struct RoadLinesData::road_line &
get_line(const String &key) const
{
return lines_[key];
flecs::entity e = lookup(key);
const struct CLine *cl = e.get<CLine>();
return cl->line;
}
inline const struct RoadLinesData::road_line *
get_line_ptr(const String &key) const
{
return &lines_[key];
flecs::entity e = lookup(key);
const struct CLine *cl = e.get<CLine>();
return &cl->line;
}
inline struct RoadLinesData::road_line &get_line(const String &key)
{
return lines_[key];
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
return cl->line;
}
inline const struct RoadLinesData::road_line &
operator[](const String &key) const
{
return lines_[key];
flecs::entity e = lookup(key);
const struct CLine *cl = e.get<CLine>();
return cl->line;
}
inline void set_line(const String &key,
const struct RoadLinesData::road_line &line)
{
lines_[key] = line;
flecs::entity e = lookup_create(key);
e.set<CLine>({ line });
}
inline void insert_line_point(const String &key, int index,
const Transform &xform)
{
lines_[key].points.insert(lines_[key].points.begin() + index,
xform);
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
cl->line.points.insert(cl->line.points.begin() + index, xform);
}
inline void erase_line_point(const String &key, int index)
{
lines_[key].points.erase(lines_[key].points.begin() + index);
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
cl->line.points.erase(cl->line.points.begin() + index);
}
inline void set_line_point_position(const String &key, int index,
const Vector3 &position)
{
lines_[key].points[index].origin = position;
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
cl->line.points[index].origin = position;
}
inline void clear_all_line_indices()
{
List<String> keys;
get_key_list(&keys);
List<String>::Element *e = keys.front();
while (e) {
String rkey = e->get();
clear_line_indices(rkey);
e = e->next();
}
BaseData::get_singleton()->get_singleton()->get().each(
[](CLine &cl) { cl.line.indices.clear(); });
}
inline void clear_line_indices(const String &key)
{
lines_[key].indices.clear();
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
cl->line.indices.clear();
}
inline int get_line_points_count(const String &key) const
{
return lines_[key].points.size();
flecs::entity e = lookup(key);
const struct CLine *cl = e.get<CLine>();
return cl->line.points.size();
}
inline const Vector3 &get_line_point_position(const String &key,
int index)
int index) const
{
return lines_[key].points[index].origin;
flecs::entity e = lookup(key);
const struct CLine *cl = e.get<CLine>();
return cl->line.points[index].origin;
}
inline void add_line_index(const String &key, int id)
{
lines_[key].indices.push_back(id);
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
cl->line.indices.push_back(id);
}
inline void insert_line_index(const String &key, int index, int id)
{
lines_[key].indices.insert(lines_[key].indices.begin() + index,
id);
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
cl->line.indices.insert(cl->line.indices.begin() + index, id);
}
inline void clear_line_segments(const String &key)
{
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
cl->line.segments.clear();
}
void update_line_segments(const String &key)
{
int i;
lines_[key].segments.clear();
lines_[key].segments.resize(get_line_points_count(key) - 1);
flecs::entity e = lookup(key);
clear_line_segments(key);
struct CLine *cl = e.get_mut<CLine>();
cl->line.segments.resize(get_line_points_count(key) - 1);
float offset = 0.0f;
for (i = 0; i < (int)get_line_points_count(key) - 1; i++) {
struct RoadLinesData::line_segment segment;
@@ -107,7 +171,7 @@ public:
side.y = 0;
segment.tangent = side.normalized();
segment.offset = offset;
lines_[key].segments[i] = segment;
cl->line.segments[i] = segment;
offset += segment.length;
}
}
@@ -115,6 +179,8 @@ public:
float curve_offset, float normal_offset,
float y_rotation, float y_offset)
{
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
struct RoadLinesData::line_building_data lb;
lb.building_key = bkey;
lb.building_key_hash = bkey.hash64();
@@ -122,29 +188,44 @@ public:
lb.normal_offset = normal_offset;
lb.y_rotation = y_rotation;
lb.y_offset = y_offset;
lines_[key].buildings.push_back(lb);
cl->line.buildings.push_back(lb);
}
void set_line_metadata(const String &key, const Dictionary &metadata)
{
lines_[key].metadata = metadata;
flecs::entity e = lookup(key);
struct CLine *cl = e.get_mut<CLine>();
cl->line.metadata = metadata;
}
inline bool has(const String &key)
{
return lines_.has(key);
return has_entity(key);
}
void get_key_list(List<String> *keys)
{
lines_.get_key_list(keys);
// ugly as fuck
get().each([keys](flecs::entity e, const CLine &cl) {
String name(e.name());
if (name.begins_with("line:")) {
name = name.substr(5, -1);
keys->push_back(name);
}
});
}
const String &get_next(const String &key)
{
const String *r = lines_.next(&key);
return *r;
List<String> keys;
get_key_list(&keys);
List<String>::Element *e = keys.find(key);
assert(e);
e = e->next();
assert(e);
return e->get();
}
void erase(const String &key)
{
lines_.erase(key);
flecs::entity e = lookup(key);
e.destruct();
}
};