pull argument extracting code into EntityActionInterface

This commit is contained in:
Seth Alves 2015-06-04 11:39:15 -07:00
parent 14026667f5
commit 8f19bad97d
3 changed files with 74 additions and 30 deletions

View file

@ -37,3 +37,65 @@ QString EntityActionInterface::actionTypeToString(EntityActionType actionType) {
assert(false);
return "none";
}
glm::vec3 EntityActionInterface::extractVec3Argument(QString objectName, QVariantMap arguments,
QString argumentName, bool& ok) {
if (!arguments.contains(argumentName)) {
qDebug() << objectName << "requires argument:" << argumentName;
ok = false;
return vec3();
}
QVariant resultV = arguments[argumentName];
if (resultV.type() != (QVariant::Type) QMetaType::QVariantMap) {
qDebug() << objectName << "argument" << argumentName << "must be a map";
ok = false;
return vec3();
}
QVariantMap resultVM = resultV.toMap();
if (!resultVM.contains("x") || !resultVM.contains("y") || !resultVM.contains("z")) {
qDebug() << objectName << "argument" << argumentName << "must be a map with keys of x, y, z";
ok = false;
return vec3();
}
QVariant xV = resultVM["x"];
QVariant yV = resultVM["y"];
QVariant zV = resultVM["z"];
bool xOk = true;
bool yOk = true;
bool zOk = true;
float x = xV.toFloat(&xOk);
float y = yV.toFloat(&yOk);
float z = zV.toFloat(&zOk);
if (!xOk || !yOk || !zOk) {
qDebug() << objectName << "argument" << argumentName << "must be a map with keys of x, y, z and values of type float.";
ok = false;
return vec3();
}
return vec3(x, y, z);
}
float EntityActionInterface::extractFloatArgument(QString objectName, QVariantMap arguments,
QString argumentName, bool& ok) {
if (!arguments.contains(argumentName)) {
qDebug() << objectName << "requires argument:" << argumentName;
ok = false;
return 0.0f;
}
QVariant vV = arguments[argumentName];
bool vOk = true;
float v = vV.toFloat(&vOk);
if (!vOk) {
ok = false;
return 0.0f;
}
return v;
}

View file

@ -36,6 +36,9 @@ class EntityActionInterface {
static EntityActionType actionTypeFromString(QString actionTypeString);
static QString actionTypeToString(EntityActionType actionType);
static glm::vec3 extractVec3Argument(QString objectName, QVariantMap arguments, QString argumentName, bool& ok);
static float extractFloatArgument(QString objectName, QVariantMap arguments, QString argumentName, bool& ok);
};
#endif // hifi_EntityActionInterface_h

View file

@ -243,39 +243,18 @@ EntityActionInterface* PhysicalEntitySimulation::actionFactory(EntityActionType
case ACTION_TYPE_NONE:
return nullptr;
case ACTION_TYPE_PULL_TO_POINT:
if (!arguments.contains("target")) {
qDebug() << "PullToPoint action requires a `target' argument";
return nullptr;
bool ok = true;
glm::vec3 target = EntityActionInterface::extractVec3Argument("pull-to-point action", arguments, "target", ok);
float speed = EntityActionInterface::extractFloatArgument("pull-to-point action", arguments, "speed", ok);
if (ok) {
action = (EntityActionInterface*) new ObjectActionPullToPoint(id, ownerEntity, target, speed);
}
QVariant targetV = arguments["target"];
if (targetV.type() != (QVariant::Type) QMetaType::QVariantMap) {
qDebug() << "PullToPoint argument `target' must be a map";
return nullptr;
}
QVariantMap targetVM = targetV.toMap();
if (!targetVM.contains("x") || !targetVM.contains("y") || !targetVM.contains("z")) {
qDebug() << "PullToPoint argument `target' must be a map with keys of x, y, z";
return nullptr;
}
QVariant xV = targetVM["x"];
QVariant yV = targetVM["y"];
QVariant zV = targetVM["z"];
bool xOk = true;
bool yOk = true;
bool zOk = true;
float x = xV.toFloat(&xOk);
float y = yV.toFloat(&yOk);
float z = zV.toFloat(&zOk);
if (!xOk || !yOk || !zOk) {
qDebug() << "PullToPoint argument `target' must be a map with keys of x, y, z and values of type float.";
}
glm::vec3 glmTarget(x, y, z);
float speed = arguments["speed"].toFloat();
action = (EntityActionInterface*) new ObjectActionPullToPoint(id, ownerEntity, glmTarget, speed);
break;
}
assert(action);
ownerEntity->addAction(this, action);
if (action) {
ownerEntity->addAction(this, action);
}
return action;
}