minor update
This commit is contained in:
@@ -2,73 +2,110 @@
|
||||
|
||||
#include "engine-variant.hpp"
|
||||
|
||||
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
||||
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
||||
namespace varicle {
|
||||
|
||||
template <class... Ts> struct overloaded : Ts... {
|
||||
using Ts::operator()...;
|
||||
};
|
||||
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
||||
|
||||
enum class OpType {
|
||||
Assign, // Replace the old value entirely
|
||||
Add, // Add to the current value
|
||||
Multiply, // Add to the current value
|
||||
Lerp // Smoothly blend toward a value
|
||||
Assign, // Replace the old value entirely
|
||||
Add, // Add to the current value
|
||||
Multiply, // Add to the current value
|
||||
Lerp // Smoothly blend toward a value
|
||||
};
|
||||
|
||||
struct VariantOpRequest {
|
||||
EngineVariant* target; // The variable we want to change
|
||||
OpType operation; // How we want to change it
|
||||
EngineVariant operand; // The value we are using to make the change
|
||||
float alpha; // Used ONLY for Lerp (0.0 to 1.0)
|
||||
EngineVariant *target; // The variable we want to change
|
||||
OpType operation; // How we want to change it
|
||||
EngineVariant operand; // The value we are using to make the change
|
||||
float alpha; // Used ONLY for Lerp (0.0 to 1.0)
|
||||
};
|
||||
|
||||
class VariantOpManager{
|
||||
class VariantOpManager {
|
||||
|
||||
public:
|
||||
VariantOpManager(){}
|
||||
void ExecuteOperation(const VariantOpRequest& req) {
|
||||
public:
|
||||
VariantOpManager() {}
|
||||
void ExecuteOperation(const VariantOpRequest &req) {
|
||||
|
||||
if (req.target == nullptr) return;
|
||||
if (req.target == nullptr)
|
||||
return;
|
||||
|
||||
switch (req.operation){
|
||||
switch (req.operation) {
|
||||
|
||||
case OpType::Assign:
|
||||
req.target->data = req.operand.data;
|
||||
req.target->type = req.operand.type;
|
||||
break;
|
||||
case OpType::Add:
|
||||
req.target->data = std::visit(
|
||||
overloaded{
|
||||
[](float p, float n) -> EngineVariant::InternalVariant {return p + n;},
|
||||
[](Vec2 p, Vec2 n) -> EngineVariant::InternalVariant {return p + n;},
|
||||
[](Vec3 p, Vec3 n) -> EngineVariant::InternalVariant {return p + n;},
|
||||
[](Vec4 p, Vec4 n) -> EngineVariant::InternalVariant {return p + n;},
|
||||
case OpType::Assign:
|
||||
req.target->data = req.operand.data;
|
||||
break;
|
||||
case OpType::Add:
|
||||
req.target->data = std::visit(
|
||||
overloaded{
|
||||
[](float p, float n) -> EngineVariant::InternalVariant {
|
||||
return p + n;
|
||||
},
|
||||
[](Vec2 p, Vec2 n) -> EngineVariant::InternalVariant {
|
||||
return p + n;
|
||||
},
|
||||
[](Vec3 p, Vec3 n) -> EngineVariant::InternalVariant {
|
||||
return p + n;
|
||||
},
|
||||
[](Vec4 p, Vec4 n) -> EngineVariant::InternalVariant {
|
||||
return p + n;
|
||||
},
|
||||
|
||||
// Fallback
|
||||
[](auto p, auto n) -> EngineVariant::InternalVariant {return p;},
|
||||
},req.target->data,req.operand.data);
|
||||
break;
|
||||
// Fallback
|
||||
[](auto p, auto n) -> EngineVariant::InternalVariant {
|
||||
return p;
|
||||
},
|
||||
},
|
||||
req.target->data, req.operand.data);
|
||||
break;
|
||||
|
||||
case OpType::Lerp:
|
||||
req.target->data = std::visit(overloaded{
|
||||
[&](float p, float n) -> EngineVariant::InternalVariant { return p + (n - p) * req.alpha; },
|
||||
[&](Vec2 p, Vec2 n) -> EngineVariant::InternalVariant { return Vec2Lerp(p, n, req.alpha); },
|
||||
[&](Vec3 p, Vec3 n) -> EngineVariant::InternalVariant { return Vec3Lerp(p, n, req.alpha); },
|
||||
[&](Vec4 p, Vec4 n) -> EngineVariant::InternalVariant { return Vec4Lerp(p, n, req.alpha); },
|
||||
[&](const std::string& p, const std::string& n) -> EngineVariant::InternalVariant {
|
||||
return req.alpha >= 0.5f ? n : p; // Discrete switch for strings
|
||||
},
|
||||
case OpType::Lerp:
|
||||
req.target->data = std::visit(
|
||||
overloaded{
|
||||
[&](float p, float n) -> EngineVariant::InternalVariant {
|
||||
return p + (n - p) * req.alpha;
|
||||
},
|
||||
[&](Vec2 p, Vec2 n) -> EngineVariant::InternalVariant {
|
||||
return Vec2Lerp(p, n, req.alpha);
|
||||
},
|
||||
[&](Vec3 p, Vec3 n) -> EngineVariant::InternalVariant {
|
||||
return Vec3Lerp(p, n, req.alpha);
|
||||
},
|
||||
[&](Vec4 p, Vec4 n) -> EngineVariant::InternalVariant {
|
||||
return Vec4Lerp(p, n, req.alpha);
|
||||
},
|
||||
[&](const std::string &p, const std::string &n)
|
||||
-> EngineVariant::InternalVariant {
|
||||
return req.alpha >= 0.5f
|
||||
? n
|
||||
: p; // Discrete switch for strings
|
||||
},
|
||||
|
||||
// Fallback
|
||||
[](auto p, auto n) -> EngineVariant::InternalVariant { return p; }
|
||||
}, req.target->data, req.operand.data);
|
||||
break;
|
||||
|
||||
case OpType::Multiply:
|
||||
req.target->data = std::visit(overloaded{
|
||||
[](float p, float n) -> EngineVariant::InternalVariant { return p * n; },
|
||||
[](Vec2 p, float n) -> EngineVariant::InternalVariant { return p * n; }, // Scale a vector!
|
||||
[](auto p, auto n) -> EngineVariant::InternalVariant { return p; }
|
||||
}, req.target->data, req.operand.data);
|
||||
break;
|
||||
}
|
||||
// Fallback
|
||||
[](auto p, auto n) -> EngineVariant::InternalVariant {
|
||||
return p;
|
||||
}},
|
||||
req.target->data, req.operand.data);
|
||||
break;
|
||||
|
||||
case OpType::Multiply:
|
||||
req.target->data = std::visit(
|
||||
overloaded{
|
||||
[](float p, float n) -> EngineVariant::InternalVariant {
|
||||
return p * n;
|
||||
},
|
||||
[](Vec2 p, float n) -> EngineVariant::InternalVariant {
|
||||
return p * n;
|
||||
}, // Scale a vector!
|
||||
[](auto p, auto n) -> EngineVariant::InternalVariant {
|
||||
return p;
|
||||
}},
|
||||
req.target->data, req.operand.data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace varicle
|
||||
|
||||
@@ -6,10 +6,12 @@
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace varicle {
|
||||
|
||||
using VariantGetter =
|
||||
std::function<EngineVariant(const entt::registry &, entt::entity)>;
|
||||
using VariantSetter = std::function<void(entt::registry &, entt::entity,
|
||||
const EngineVariant &)>;
|
||||
using VariantSetter =
|
||||
std::function<void(entt::registry &, entt::entity, const EngineVariant &)>;
|
||||
|
||||
struct PropertyRef {
|
||||
uint32_t property_id;
|
||||
@@ -47,8 +49,7 @@ class PropertyRegistry {
|
||||
return EngineVariant((*component).*member);
|
||||
}
|
||||
return EngineVariant();
|
||||
}
|
||||
};
|
||||
}};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -70,8 +71,8 @@ class PropertyDatabase {
|
||||
return EngineVariant();
|
||||
}
|
||||
|
||||
bool set_value(entt::registry ®, entt::entity e,
|
||||
uint32_t prop_hash, const EngineVariant &value) const {
|
||||
bool set_value(entt::registry ®, entt::entity e, uint32_t prop_hash,
|
||||
const EngineVariant &value) const {
|
||||
auto it = properites.find(prop_hash);
|
||||
if (it != properites.end()) {
|
||||
it->second.set(reg, e, value);
|
||||
@@ -80,3 +81,4 @@ class PropertyDatabase {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
} // namespace varicle
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
namespace varicle {
|
||||
// Overload helper pattern
|
||||
template <class... Ts> struct Overloaded : Ts... {
|
||||
using Ts::operator()...;
|
||||
@@ -75,8 +76,8 @@ enum class VariantType { Null, Float, Vector2, Vector3, Vector4, String };
|
||||
class EngineVariant {
|
||||
|
||||
public:
|
||||
using InternalVariant =
|
||||
std::variant<std::monostate, float, Vec2, Vec3, Vec4, std::string, bool>;
|
||||
using InternalVariant = std::variant<std::monostate, float, Vec2, Vec3,
|
||||
Vec4, std::string, bool>;
|
||||
|
||||
EngineVariant() : data(std::monostate{}) {}
|
||||
EngineVariant(float v) : data(v) {}
|
||||
@@ -109,7 +110,9 @@ class EngineVariant {
|
||||
std::to_string(v.w) + ")";
|
||||
},
|
||||
[](const std::string &s) { return s; },
|
||||
[](const bool &b) { return std::string(b ? "true" : "false"); }},
|
||||
[](const bool &b) {
|
||||
return std::string(b ? "true" : "false");
|
||||
}},
|
||||
data);
|
||||
}
|
||||
|
||||
@@ -147,3 +150,4 @@ class EngineVariant {
|
||||
|
||||
friend class VariantOpManager;
|
||||
};
|
||||
} // namespace varicle
|
||||
|
||||
@@ -3,55 +3,50 @@
|
||||
#include "engine/core/service-locator.hpp"
|
||||
#include "engine/ecs/components.hpp"
|
||||
|
||||
#include<raylib.h>
|
||||
#include <entt/entt.hpp>
|
||||
#include <raylib.h>
|
||||
|
||||
namespace varicle {
|
||||
|
||||
void update_render_system(entt::registry ®istry){
|
||||
auto view = registry.view<const Sprite, const GlobalTransform2D>();
|
||||
void update_render_system(entt::registry ®istry) {
|
||||
auto view = registry.view<const Sprite, const GlobalTransform2D>();
|
||||
|
||||
for (entt::entity entity : view){
|
||||
const Sprite &sprite = view.get<Sprite>(entity);
|
||||
auto [global_position,global_scale,global_rotation] = view.get<GlobalTransform2D>(entity);
|
||||
auto texture = ServiceLocator::get<RaylibAssetLoader>().get_texture(sprite.texture_path);
|
||||
for (entt::entity entity : view) {
|
||||
const Sprite &sprite = view.get<Sprite>(entity);
|
||||
auto [global_position, global_scale, global_rotation] =
|
||||
view.get<GlobalTransform2D>(entity);
|
||||
auto texture = ServiceLocator::get<RaylibAssetLoader>().get_texture(
|
||||
sprite.texture_path);
|
||||
|
||||
float width = sprite.width * global_scale;
|
||||
float height = sprite.height * global_scale;
|
||||
float rotation = sprite.rotation + global_rotation;
|
||||
float width = sprite.width * global_scale;
|
||||
float height = sprite.height * global_scale;
|
||||
float rotation = sprite.rotation + global_rotation;
|
||||
|
||||
if (texture){
|
||||
DrawTexturePro(
|
||||
*(texture),
|
||||
Rectangle{
|
||||
0,
|
||||
0,
|
||||
(float)texture->width,
|
||||
(float)texture->height,
|
||||
},
|
||||
Rectangle {
|
||||
sprite.offset_x + global_position.x,
|
||||
sprite.offset_y + global_position.y,
|
||||
width,
|
||||
height,
|
||||
},
|
||||
Vector2 {width * 0.5f, height*0.5f},
|
||||
rotation,
|
||||
WHITE);
|
||||
if (texture) {
|
||||
DrawTexturePro(
|
||||
*(texture),
|
||||
Rectangle{
|
||||
0.0f,
|
||||
0.0f,
|
||||
static_cast<float>(sprite.flip_h ? -texture->width
|
||||
: texture->width),
|
||||
static_cast<float>(sprite.flip_v ? -texture->height
|
||||
: texture->height),
|
||||
},
|
||||
Rectangle{
|
||||
sprite.offset_x + global_position.x,
|
||||
sprite.offset_y + global_position.y,
|
||||
width,
|
||||
height,
|
||||
},
|
||||
Vector2{width * 0.5f, height * 0.5f}, rotation, WHITE);
|
||||
|
||||
}else{
|
||||
DrawRectanglePro(Rectangle {
|
||||
sprite.offset_x + global_position.x,
|
||||
sprite.offset_y + global_position.y,
|
||||
width,
|
||||
height
|
||||
},
|
||||
Vector2 {width * 0.5f, height*0.5f},
|
||||
rotation,
|
||||
PURPLE
|
||||
);
|
||||
}
|
||||
} else {
|
||||
DrawRectanglePro(
|
||||
Rectangle{sprite.offset_x + global_position.x,
|
||||
sprite.offset_y + global_position.y, width, height},
|
||||
Vector2{width * 0.5f, height * 0.5f}, rotation, PURPLE);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} // namespace varicle
|
||||
|
||||
Reference in New Issue
Block a user