openblocks/src/objects/part.cpp

51 lines
1.7 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>
#include "physics/simulation.h"
2025-01-12 17:38:23 +00:00
2025-02-04 19:18:58 +00:00
const InstanceType Part::TYPE = {
.super = &Instance::TYPE,
2025-01-12 17:38:23 +00:00
.className = "Part",
.constructor = &Part::CreateGeneric,
.explorerIcon = "part",
2025-01-12 17:38:23 +00:00
};
2025-02-04 19:18:58 +00:00
const InstanceType* Part::GetClass() {
return &TYPE;
2025-01-12 17:38:23 +00:00
}
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
2025-02-04 19:18:58 +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>(), .updateCallback = memberFunctionOf(&Part::onUpdated, this) } }
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
}
void Part::onUpdated(std::string property) {
syncPartPhysics(std::dynamic_pointer_cast<Part>(this->shared_from_this()));
2025-01-17 08:24:18 +00:00
}