Files
factory-hole-core/include/Components/Misc.hpp
2026-02-20 22:50:05 +09:00

88 lines
1.9 KiB
C++

#pragma once
#include "flecs.h"
#include <vector>
struct Vector2
{
Vector2() = default;
Vector2(int32_t x, int32_t y): X{ x }, Y{ y } {};
int32_t X{}, Y{};
Vector2 operator+(const Vector2& other) const { return { X + other.X, Y + other.Y }; }
Vector2 operator-(const Vector2& other) const { return { X - other.X, Y - other.Y }; }
};
struct TilePosition
{
Vector2 Position;
};
struct Bounds
{
Vector2 Min;
Vector2 Max;
};
inline bool BoundsHasPoint(const Bounds& b, int x, int y)
{
return x >= b.Min.X && x < b.Max.X && y >= b.Min.Y && y < b.Max.Y;
}
inline Bounds BoundsGrow(const Bounds& b, int32_t amount)
{
return Bounds{
Vector2{b.Min.X - amount, b.Min.Y - amount},
Vector2{b.Max.X + amount, b.Max.Y + amount}
};
}
struct Level
{
Level() = default;
Level(uint8_t level) : Val{ level } {};
uint8_t Val;
};
template <typename Elem, typename Vector = std::vector<Elem>>
flecs::opaque<Vector, Elem> std_vector_support(flecs::world& world) {
return flecs::opaque<Vector, Elem>()
.as_type(world.vector<Elem>())
.serialize([](const flecs::serializer *s, const Vector *data) {
for (const auto& el : *data)
s->value(el);
return 0;
})
.count([](const Vector *data) {
return data->size();
})
.resize([](Vector *data, size_t size) {
data->resize(size);
})
.ensure_element([](Vector *data, size_t elem) {
if (data->size() <= elem)
data->resize(elem + 1);
return &data->data()[elem];
});
}
inline void Flecs_Misc(flecs::world& world)
{
world.component<Vector2>()
.member<int32_t>("x")
.member<int32_t>("y");
world.component<TilePosition>()
.member<Vector2>("Position");
world.component<Bounds>()
.member<Vector2>("Min")
.member<Vector2>("Max");
world.component<Level>()
.member<uint8_t>("Val");
}