More scripting work on item-object collisions

This commit is contained in:
Marianne Gagnon 2015-05-31 18:20:52 -04:00
parent 8ef16c146e
commit 501dd14bb6
3 changed files with 22 additions and 8 deletions

View File

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

View File

@ -71,6 +71,10 @@ public:
* when a kart collides with this object * when a kart collides with this object
*/ */
std::string m_on_kart_collision; std::string m_on_kart_collision;
/** If non-empty, the name of the scripting function to call
* when a (flyable) item collides with this object
*/
std::string m_on_item_collision;
private: private:
void init(); void init();
public: public:
@ -148,7 +152,10 @@ private:
* when a kart collides with this object * when a kart collides with this object
*/ */
std::string m_on_kart_collision; std::string m_on_kart_collision;
/** If non-empty, the name of the scripting function to call
* when a (flyable) item collides with this object
*/
std::string m_on_item_collision;
/** If this body is a bullet dynamic body, i.e. affected by physics /** If this body is a bullet dynamic body, i.e. affected by physics
* or not (static (not moving) or kinematic (animated outside * or not (static (not moving) or kinematic (animated outside
* of physics). */ * of physics). */
@ -204,6 +211,8 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
const std::string& getOnKartCollisionFunction() const { return m_on_kart_collision; } const std::string& getOnKartCollisionFunction() const { return m_on_kart_collision; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
const std::string& getOnItemCollisionFunction() const { return m_on_item_collision; }
// ------------------------------------------------------------------------
// Methods usable by scripts // Methods usable by scripts
/** /**

View File

@ -260,13 +260,16 @@ void Physics::update(float dt)
Flyable* flyable = p->getUserPointer(0)->getPointerFlyable(); Flyable* flyable = p->getUserPointer(0)->getPointerFlyable();
PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject(); PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject();
std::string obj_id = obj->getID(); std::string obj_id = obj->getID();
// TODO: attach this callback directly to a specific track object std::string scripting_function = obj->getOnItemCollisionFunction();
script_engine->runFunction("void onItemObjectCollision(int, int, const string)", if (scripting_function.size() > 0)
[&](asIScriptContext* ctx) { {
ctx->SetArgDWord(0, (int)flyable->getType()); script_engine->runFunction("void " + scripting_function + "(int, int, const string)",
ctx->SetArgDWord(1, flyable->getOwnerId()); [&](asIScriptContext* ctx) {
ctx->SetArgObject(2, &obj_id); ctx->SetArgDWord(0, (int)flyable->getType());
}); ctx->SetArgDWord(1, flyable->getOwnerId());
ctx->SetArgObject(2, &obj_id);
});
}
flyable->hit(NULL, obj); flyable->hit(NULL, obj);
if (obj->isSoccerBall() && if (obj->isSoccerBall() &&