RoboidControl-cpp/Thing.cpp
2024-11-07 13:59:24 +01:00

96 lines
2.8 KiB
C++

#include "Thing.h"
using namespace Passer::RoboidControl;
Thing::Thing(unsigned char id) : id(id) {
// this->position = SphericalOf<signed short>::zero;
this->type = (unsigned int)Type::Undetermined;
this->childCount = 0;
this->parent = nullptr;
this->children = nullptr;
}
const unsigned int Thing::SwitchType = SensorType | (unsigned int)Type::Switch;
const unsigned int Thing::DistanceSensorType =
SensorType | (unsigned int)Type::DistanceSensor;
const unsigned int Thing::TemperatureSensorType =
SensorType | (unsigned int)Type::TemperatureSensor;
const unsigned int Thing::ControlledMotorType =
MotorType | (unsigned int)Type::ControlledMotor;
const unsigned int Thing::UncontrolledMotorType =
MotorType | (unsigned int)Type::UncontrolledMotor;
const unsigned int Thing::ServoType = (unsigned int)Type::Servo;
const unsigned int Thing::HumanoidType = (unsigned int)Type::Humanoid;
bool Thing::IsMotor() { return (type & Thing::MotorType) != 0; }
bool Thing::IsSensor() { return (type & Thing::SensorType) != 0; }
bool Thing::IsRoboid() { return (type & Thing::RoboidType) != 0; }
void Thing::SetModel(const char *url) { this->modelUrl = url; }
void Thing::SetParent(Thing *parent) {
if (parent == nullptr)
return;
parent->AddChild(this);
}
Thing *Thing::GetParent() { return this->parent; }
void Thing::AddChild(Thing *child) {
Thing **newChildren = new Thing *[this->childCount + 1];
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
newChildren[childIx] = this->children[childIx];
if (this->children[childIx] == child) {
// child is already present, stop copying do not update the children
delete[] newChildren;
return;
}
}
newChildren[this->childCount] = child;
child->parent = this;
if (this->children != nullptr)
delete[] this->children;
this->children = newChildren;
this->childCount++;
}
Thing *Thing::GetChild(unsigned char childIx) {
if (childIx >= 0 && childIx < this->childCount) {
return this->children[childIx];
} else
return nullptr;
}
Thing *Passer::RoboidControl::Thing::RemoveChild(Thing *child) {
Thing **newChildren = new Thing *[this->childCount - 1];
unsigned char newChildIx = 0;
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
if (this->children[childIx] != child) {
if (newChildIx == this->childCount - 1) { // We did not find the child
// stop copying and return nothing
delete[] newChildren;
return nullptr;
} else
newChildren[newChildIx++] = this->children[childIx];
}
}
child->parent = nullptr;
delete[] this->children;
this->children = newChildren;
this->childCount--;
return child;
}
Spherical16 Thing::GetLinearVelocity() { return this->linearVelocity; }
Spherical16 Thing::GetAngularVelocity() { return this->angularVelocity; }