83 lines
1.9 KiB
C++
83 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;
|
|
|
|
bool HasPoint(int x, int y) const { return x >= Min.X && x < Max.X && y >= Min.Y && y < Max.Y; }
|
|
Bounds Grow(int32_t amount) const {
|
|
return Bounds{
|
|
Vector2{Min.X - amount, Min.Y - amount},
|
|
Vector2{Max.X + amount, 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");
|
|
}
|