diff --git a/src/objects/part.cpp b/src/objects/part.cpp index 5e41f24..1303b9d 100644 --- a/src/objects/part.cpp +++ b/src/objects/part.cpp @@ -8,6 +8,42 @@ #include #include "physics/simulation.h" +// template +// constexpr FieldCodec fieldCodecOf() { +// return FieldCodec { +// .write = [](Data::Variant source, void* destination) { +// *(U*)destination = (U)source.get(); +// }, +// .read = [](void* source) -> Data::Variant { +// return T(*(U*)source); +// }, +// }; +// } + +constexpr FieldCodec cframePositionCodec() { + return FieldCodec { + .write = [](Data::Variant source, void* destination) { + Data::CFrame* cframe = static_cast(destination); + *cframe = cframe->Rotation() + source.get(); + }, + .read = [](void* source) -> Data::Variant { + return *static_cast(source); + }, + }; +} + +constexpr FieldCodec cframeRotationCodec() { + return FieldCodec { + .write = [](Data::Variant source, void* destination) { + Data::CFrame* cframe = static_cast(destination); + *cframe = Data::CFrame::FromEulerAnglesXYZ(source.get()) + cframe->Position(); + }, + .read = [](void* source) -> Data::Variant { + return static_cast(source)->ToEulerAnglesXYZ(); + }, + }; +} + const InstanceType Part::TYPE = { .super = &Instance::TYPE, .className = "Part", @@ -28,7 +64,8 @@ Part::Part(PartConstructParams params): Instance(&TYPE), cframe(Data::CFrame(par .super = std::move(this->memberMap), .members = { { "Anchored", { .backingField = &anchored, .type = &Data::Bool::TYPE, .codec = fieldCodecOf(), .updateCallback = memberFunctionOf(&Part::onUpdated, this) } }, - // { "Position", { .backingField = &position, .type = &Data::Vector3::TYPE, .codec = fieldCodecOf(), .updateCallback = memberFunctionOf(&Part::onUpdated, this) } } + { "Position", { .backingField = &cframe, .type = &Data::Vector3::TYPE, .codec = cframePositionCodec(), .updateCallback = memberFunctionOf(&Part::onUpdated, this) } }, + { "Rotation", { .backingField = &cframe, .type = &Data::Vector3::TYPE, .codec = cframeRotationCodec(), .updateCallback = memberFunctionOf(&Part::onUpdated, this) } } } }); }