mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 15:49:24 +02:00
script interface for creating an action now takes an action-type name rather than having a different call for each action
This commit is contained in:
parent
9cae11cc3c
commit
14026667f5
9 changed files with 112 additions and 42 deletions
39
libraries/entities/src/EntityActionInterface.cpp
Normal file
39
libraries/entities/src/EntityActionInterface.cpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
//
|
||||||
|
// EntityItem.h
|
||||||
|
// libraries/entities/src
|
||||||
|
//
|
||||||
|
// Created by Seth Alves on 2015-6-4
|
||||||
|
// Copyright 2015 High Fidelity, Inc.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "EntityItem.h"
|
||||||
|
|
||||||
|
#include "EntityActionInterface.h"
|
||||||
|
|
||||||
|
|
||||||
|
EntityActionType EntityActionInterface::actionTypeFromString(QString actionTypeString) {
|
||||||
|
QString normalizedActionTypeString = actionTypeString.toLower().remove('-').remove('_');
|
||||||
|
if (normalizedActionTypeString == "none") {
|
||||||
|
return ACTION_TYPE_NONE;
|
||||||
|
}
|
||||||
|
if (normalizedActionTypeString == "pulltopoint") {
|
||||||
|
return ACTION_TYPE_PULL_TO_POINT;
|
||||||
|
}
|
||||||
|
|
||||||
|
qDebug() << "Warning -- EntityActionInterface::actionTypeFromString got unknown action-type name" << actionTypeString;
|
||||||
|
return ACTION_TYPE_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString EntityActionInterface::actionTypeToString(EntityActionType actionType) {
|
||||||
|
switch(actionType) {
|
||||||
|
case ACTION_TYPE_NONE:
|
||||||
|
return "none";
|
||||||
|
case ACTION_TYPE_PULL_TO_POINT:
|
||||||
|
return "pullToPoint";
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
|
return "none";
|
||||||
|
}
|
|
@ -12,9 +12,12 @@
|
||||||
#ifndef hifi_EntityActionInterface_h
|
#ifndef hifi_EntityActionInterface_h
|
||||||
#define hifi_EntityActionInterface_h
|
#define hifi_EntityActionInterface_h
|
||||||
|
|
||||||
|
#include <QUuid>
|
||||||
|
|
||||||
class EntitySimulation;
|
class EntitySimulation;
|
||||||
|
|
||||||
enum EntityActionType {
|
enum EntityActionType {
|
||||||
|
// keep these synchronized with actionTypeFromString and actionTypeToString
|
||||||
ACTION_TYPE_NONE,
|
ACTION_TYPE_NONE,
|
||||||
ACTION_TYPE_PULL_TO_POINT
|
ACTION_TYPE_PULL_TO_POINT
|
||||||
};
|
};
|
||||||
|
@ -30,6 +33,9 @@ class EntityActionInterface {
|
||||||
virtual void setOwnerEntity(const EntityItemPointer ownerEntity) = 0;
|
virtual void setOwnerEntity(const EntityItemPointer ownerEntity) = 0;
|
||||||
// virtual QByteArray serialize() = 0;
|
// virtual QByteArray serialize() = 0;
|
||||||
// static EntityActionInterface* deserialize(EntityItemPointer ownerEntity, QByteArray data);
|
// static EntityActionInterface* deserialize(EntityItemPointer ownerEntity, QByteArray data);
|
||||||
|
|
||||||
|
static EntityActionType actionTypeFromString(QString actionTypeString);
|
||||||
|
static QString actionTypeToString(EntityActionType actionType);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_EntityActionInterface_h
|
#endif // hifi_EntityActionInterface_h
|
||||||
|
|
|
@ -1338,7 +1338,7 @@ void EntityItem::updateSimulatorID(const QUuid& value) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EntityItem::addAction(EntityActionInterface* action) {
|
bool EntityItem::addAction(EntitySimulation* simulation, EntityActionInterface* action) {
|
||||||
assert(action);
|
assert(action);
|
||||||
const QUuid& actionID = action->getID();
|
const QUuid& actionID = action->getID();
|
||||||
assert(!_objectActions.contains(actionID) || _objectActions[actionID] == action);
|
assert(!_objectActions.contains(actionID) || _objectActions[actionID] == action);
|
||||||
|
@ -1347,16 +1347,7 @@ bool EntityItem::addAction(EntityActionInterface* action) {
|
||||||
|
|
||||||
assert(action->getOwnerEntity().get() == this);
|
assert(action->getOwnerEntity().get() == this);
|
||||||
|
|
||||||
if (_element) {
|
|
||||||
const EntityTree* tree = _element->getTree();
|
|
||||||
if (tree) {
|
|
||||||
EntitySimulation* simulation = tree->getSimulation();
|
|
||||||
if (simulation) {
|
|
||||||
simulation->addAction(action);
|
simulation->addAction(action);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -354,7 +354,7 @@ public:
|
||||||
|
|
||||||
void getAllTerseUpdateProperties(EntityItemProperties& properties) const;
|
void getAllTerseUpdateProperties(EntityItemProperties& properties) const;
|
||||||
|
|
||||||
bool addAction(EntityActionInterface* actionID);
|
bool addAction(EntitySimulation* simulation, EntityActionInterface* actionID);
|
||||||
void removeAction(EntitySimulation* simulation, const QUuid actionID);
|
void removeAction(EntitySimulation* simulation, const QUuid actionID);
|
||||||
void clearActions(EntitySimulation* simulation);
|
void clearActions(EntitySimulation* simulation);
|
||||||
|
|
||||||
|
|
|
@ -450,7 +450,7 @@ bool EntityScriptingInterface::setAllVoxels(QUuid entityID, int value) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
QUuid EntityScriptingInterface::addActionPullToPoint(QUuid entityID, const glm::vec3& target, float speed) {
|
QUuid EntityScriptingInterface::addAction(QString actionTypeString, QUuid entityID, QVariantMap arguments) {
|
||||||
if (!_entityTree) {
|
if (!_entityTree) {
|
||||||
return QUuid();
|
return QUuid();
|
||||||
}
|
}
|
||||||
|
@ -466,15 +466,24 @@ QUuid EntityScriptingInterface::addActionPullToPoint(QUuid entityID, const glm::
|
||||||
return QUuid();
|
return QUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap arguments;
|
if (!simulation) {
|
||||||
QVariantList targetList;
|
qDebug() << "addAction -- no simulation" << entityID;
|
||||||
targetList << QVariant(target[0]) << QVariant(target[1]) << QVariant(target[2]);
|
_entityTree->unlock();
|
||||||
arguments["target"] = targetList;
|
return QUuid();
|
||||||
arguments["speed"] = QVariant(speed);
|
}
|
||||||
EntityActionInterface* action = simulation->actionFactory(ACTION_TYPE_PULL_TO_POINT, actionID, entity, arguments);
|
|
||||||
entity->addAction(action);
|
|
||||||
|
|
||||||
|
EntityActionType actionType = EntityActionInterface::actionTypeFromString(actionTypeString);
|
||||||
|
|
||||||
|
if (actionType == ACTION_TYPE_NONE) {
|
||||||
|
_entityTree->unlock();
|
||||||
|
return QUuid();
|
||||||
|
}
|
||||||
|
|
||||||
|
EntityActionInterface* action = simulation->actionFactory(actionType, actionID, entity, arguments);
|
||||||
_entityTree->unlock();
|
_entityTree->unlock();
|
||||||
|
|
||||||
|
if (action) {
|
||||||
return actionID;
|
return actionID;
|
||||||
|
}
|
||||||
|
return QUuid();
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,7 +124,7 @@ public slots:
|
||||||
|
|
||||||
Q_INVOKABLE void dumpTree() const;
|
Q_INVOKABLE void dumpTree() const;
|
||||||
|
|
||||||
Q_INVOKABLE QUuid addActionPullToPoint(QUuid entityID, const glm::vec3& target, float velocity);
|
Q_INVOKABLE QUuid addAction(QString actionTypeString, QUuid entityID, QVariantMap arguments);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void entityCollisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision);
|
void entityCollisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision);
|
||||||
|
|
|
@ -152,7 +152,6 @@ public:
|
||||||
bool hasEntities() const { return _entityItems ? _entityItems->size() > 0 : false; }
|
bool hasEntities() const { return _entityItems ? _entityItems->size() > 0 : false; }
|
||||||
|
|
||||||
void setTree(EntityTree* tree) { _myTree = tree; }
|
void setTree(EntityTree* tree) { _myTree = tree; }
|
||||||
const EntityTree* getTree() const { return _myTree; }
|
|
||||||
|
|
||||||
bool updateEntity(const EntityItem& entity);
|
bool updateEntity(const EntityItem& entity);
|
||||||
void addEntityItem(EntityItemPointer entity);
|
void addEntityItem(EntityItemPointer entity);
|
||||||
|
|
|
@ -238,19 +238,45 @@ EntityActionInterface* PhysicalEntitySimulation::actionFactory(EntityActionType
|
||||||
QUuid id,
|
QUuid id,
|
||||||
EntityItemPointer ownerEntity,
|
EntityItemPointer ownerEntity,
|
||||||
QVariantMap arguments) {
|
QVariantMap arguments) {
|
||||||
|
EntityActionInterface* action = nullptr;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ACTION_TYPE_NONE:
|
case ACTION_TYPE_NONE:
|
||||||
assert(false);
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
case ACTION_TYPE_PULL_TO_POINT:
|
case ACTION_TYPE_PULL_TO_POINT:
|
||||||
QVariantList target = arguments["target"].toList();
|
if (!arguments.contains("target")) {
|
||||||
glm::vec3 glmTarget(target[0].toFloat(), target[1].toFloat(), target[2].toFloat());
|
qDebug() << "PullToPoint action requires a `target' argument";
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
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();
|
float speed = arguments["speed"].toFloat();
|
||||||
return (EntityActionInterface*) new ObjectActionPullToPoint(id, ownerEntity, glmTarget, speed);
|
action = (EntityActionInterface*) new ObjectActionPullToPoint(id, ownerEntity, glmTarget, speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(false);
|
assert(action);
|
||||||
return nullptr;
|
ownerEntity->addAction(this, action);
|
||||||
|
return action;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicalEntitySimulation::applyActionChanges() {
|
void PhysicalEntitySimulation::applyActionChanges() {
|
||||||
|
|
Loading…
Reference in a new issue