Scripting work : improve object-kart collision callbacks

This commit is contained in:
Marianne Gagnon
2015-05-28 19:57:36 -04:00
parent 1ada542fa5
commit 40ea7f94f0
3 changed files with 23 additions and 9 deletions

View File

@@ -63,6 +63,7 @@ PhysicalObject::Settings::Settings(const XMLNode &xml_node)
xml_node.get("reset", &m_crash_reset );
xml_node.get("explode", &m_knock_kart );
xml_node.get("flatten", &m_flatten_kart);
xml_node.get("on-kart-collision", &m_on_kart_collision);
m_reset_when_too_low =
xml_node.get("reset-when-below", &m_reset_height) == 1;
@@ -140,6 +141,7 @@ PhysicalObject::PhysicalObject(bool is_dynamic,
m_flatten_kart = settings.m_flatten_kart;
m_reset_when_too_low = settings.m_reset_when_too_low;
m_reset_height = settings.m_reset_height;
m_on_kart_collision = settings.m_on_kart_collision;
m_init_pos.setIdentity();
Vec3 radHpr(m_init_hpr);

View File

@@ -67,6 +67,10 @@ public:
/** If the item is below that height, it is reset (when
* m_reset_when_too_low is true). */
float m_reset_height;
/** If non-empty, the name of the scripting function to call
* when a kart collides with this object
*/
std::string m_on_kart_collision;
private:
void init();
public:
@@ -140,6 +144,10 @@ private:
/** If m_reset_when_too_low this object is set back to its start
* position if its height is below this value. */
float m_reset_height;
/** If non-empty, the name of the scripting function to call
* when a kart collides with this object
*/
std::string m_on_kart_collision;
/** If this body is a bullet dynamic body, i.e. affected by physics
* or not (static (not moving) or kinematic (animated outside
@@ -194,6 +202,8 @@ public:
/** Add body to dynamic world */
void addBody();
// ------------------------------------------------------------------------
const std::string& getOnKartCollisionFunction() const { return m_on_kart_collision; }
// ------------------------------------------------------------------------
// Methods usable by scripts
/**

View File

@@ -186,15 +186,17 @@ void Physics::update(float dt)
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
AbstractKart *kart = p->getUserPointer(1)->getPointerKart();
int kartId = kart->getWorldKartId();
std::string obj_id = p->getUserPointer(0)->getPointerPhysicalObject()->getID();
// TODO: pass obj_id as arguent
script_engine->runFunction("void onKartObjectCollision(int)",
[=](asIScriptContext* ctx) {
ctx->SetArgDWord(0, kartId);
});
PhysicalObject *obj = p->getUserPointer(0)
->getPointerPhysicalObject();
PhysicalObject* obj = p->getUserPointer(0)->getPointerPhysicalObject();
std::string obj_id = obj->getID();
std::string scripting_function = obj->getOnKartCollisionFunction();
if (scripting_function.size() > 0)
{
script_engine->runFunction("void " + scripting_function + "(int, const string)",
[&](asIScriptContext* ctx) {
ctx->SetArgDWord(0, kartId);
ctx->SetArgObject(1, &obj_id);
});
}
if (obj->isCrashReset())
{
new RescueAnimation(kart);