openblocks/src/objects/part.cpp

49 lines
1.6 KiB
C++
Raw Normal View History

2025-01-12 17:38:23 +00:00
#include "part.h"
2025-01-17 08:24:18 +00:00
#include "base/instance.h"
2025-01-29 21:41:24 +00:00
#include "datatypes/base.h"
#include "objects/base/member.h"
#include <memory>
#include <optional>
2025-01-12 17:38:23 +00:00
static InstanceType TYPE_ {
.super = Instance::TYPE,
.className = "Part",
.constructor = &Part::CreateGeneric,
.explorerIcon = "part",
2025-01-12 17:38:23 +00:00
};
InstanceType* Part::TYPE = &TYPE_;
InstanceType* Part::GetClass() {
return &TYPE_;
}
2025-01-29 21:41:24 +00:00
Part::Part(): Part(PartConstructParams {}) {
2025-01-12 17:38:23 +00:00
}
2025-01-17 08:24:18 +00:00
Part::Part(PartConstructParams params): Instance(&TYPE_), position(params.position), rotation(params.rotation),
scale(params.scale), material(params.material), anchored(params.anchored) {
2025-01-29 21:41:24 +00:00
this->memberMap = std::make_unique<MemberMap>(MemberMap {
.super = std::move(this->memberMap),
.members = {
{ "Anchored", { .backingField = &anchored, .type = &Data::Bool::TYPE, .codec = fieldCodecOf<Data::Bool, bool>() } }
2025-01-29 21:41:24 +00:00
}
});
2025-01-23 10:29:50 +00:00
}
// This feels wrong. Get access to PhysicsWorld somehow else? Part will need access to this often though, most likely...
extern rp::PhysicsWorld* world;
Part::~Part() {
// This relies on physicsCommon still existing. Be very careful.
if (this->rigidBody)
world->destroyRigidBody(rigidBody);
2025-01-23 10:29:50 +00:00
}
void Part::OnParentUpdated(std::optional<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent) {
if (this->rigidBody)
this->rigidBody->setIsActive(newParent.has_value());
// TODO: Sleeping bodies that touch this one also need to be updated
2025-01-17 08:24:18 +00:00
}