Scripting cleanup, remove hardcoded stuff in favor of more generic approaches
This commit is contained in:
parent
85cf5b181e
commit
585d1c5016
@ -171,9 +171,9 @@ void Physics::update(float dt)
|
||||
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
|
||||
int kartid1 = p->getUserPointer(0)->getPointerKart()->getWorldKartId();
|
||||
int kartid2 = p->getUserPointer(1)->getPointerKart()->getWorldKartId();
|
||||
// TODO: do not use globals this way, pass directly as function paramters
|
||||
Scripting::Physics::setCollision(kartid1,kartid2);
|
||||
Scripting::Physics::setCollisionType("KartKart");
|
||||
script_engine->runScript("collisions");
|
||||
script_engine->runScript("onKartKartCollision");
|
||||
continue;
|
||||
} // if kart-kart collision
|
||||
|
||||
@ -183,16 +183,15 @@ void Physics::update(float dt)
|
||||
// -------------------------
|
||||
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
|
||||
Scripting::Physics::setCollision(0, 0);
|
||||
Scripting::Physics::setCollisionType("KartObject"); //object as in physical object
|
||||
Scripting::Physics::setCollision
|
||||
(
|
||||
//Scripting::Physics::setCollisionType("KartObject"); //object as in physical object
|
||||
Scripting::Physics::setCollision(
|
||||
p->getUserPointer(0)->getPointerPhysicalObject()->getID(),
|
||||
"kart"
|
||||
);
|
||||
script_engine->runScript("collisions");
|
||||
);
|
||||
script_engine->runScript("onKartObjectCollision");
|
||||
PhysicalObject *obj = p->getUserPointer(0)
|
||||
->getPointerPhysicalObject();
|
||||
if(obj->isCrashReset())
|
||||
if (obj->isCrashReset())
|
||||
{
|
||||
AbstractKart *kart = p->getUserPointer(1)->getPointerKart();
|
||||
new RescueAnimation(kart);
|
||||
@ -219,7 +218,7 @@ void Physics::update(float dt)
|
||||
continue;
|
||||
}
|
||||
|
||||
if(p->getUserPointer(0)->is(UserPointer::UP_ANIMATION))
|
||||
if (p->getUserPointer(0)->is(UserPointer::UP_ANIMATION))
|
||||
{
|
||||
// Kart hits animation
|
||||
ThreeDAnimation *anim=p->getUserPointer(0)->getPointerAnimation();
|
||||
@ -257,13 +256,12 @@ void Physics::update(float dt)
|
||||
// -------------------------------
|
||||
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
|
||||
Scripting::Physics::setCollision(0,0); //TODO : support item types etc
|
||||
Scripting::Physics::setCollisionType("ItemObject");
|
||||
Scripting::Physics::setCollision
|
||||
(
|
||||
//Scripting::Physics::setCollisionType("ItemObject");
|
||||
Scripting::Physics::setCollision(
|
||||
p->getUserPointer(1)->getPointerPhysicalObject()->getID(),
|
||||
"item"
|
||||
);
|
||||
script_engine->runScript("collisions");
|
||||
);
|
||||
script_engine->runScript("onItemObjectCollision");
|
||||
p->getUserPointer(0)->getPointerFlyable()
|
||||
->hit(NULL, p->getUserPointer(1)->getPointerPhysicalObject());
|
||||
PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject();
|
||||
|
@ -85,10 +85,9 @@ std::string getScript(std::string scriptName)
|
||||
{
|
||||
std::string script_dir = file_manager->getAsset(FileManager::SCRIPT, "");
|
||||
script_dir += World::getWorld()->getTrack()->getIdent() + "/";
|
||||
if (scriptName != "update" && scriptName != "collisions" && scriptName != "start")
|
||||
scriptName = "triggers";
|
||||
|
||||
script_dir += scriptName + ".as";
|
||||
// TODO: allow splitting in multiple files
|
||||
script_dir += "scripting.as";
|
||||
FILE *f = fopen(script_dir.c_str(), "rb");
|
||||
if (f == NULL)
|
||||
{
|
||||
@ -134,7 +133,8 @@ void ScriptEngine::runScript(std::string scriptName)
|
||||
|
||||
|
||||
asIScriptFunction *func;
|
||||
if (m_script_cache.find(scriptName) == m_script_cache.end())
|
||||
auto cached_script = m_script_cache.find(scriptName);
|
||||
if (cached_script == m_script_cache.end())
|
||||
{
|
||||
// Compile the script code
|
||||
Log::debug("Scripting", "Compiling script '%s' (was not in cache)", scriptName.c_str());
|
||||
@ -142,7 +142,7 @@ void ScriptEngine::runScript(std::string scriptName)
|
||||
if (r < 0)
|
||||
{
|
||||
Log::debug("Scripting", "Script '%s' is not available", scriptName.c_str());
|
||||
m_script_cache[scriptName] = NULL; // remember that this script is unavaiable
|
||||
m_script_cache[scriptName] = NULL; // remember that this script is unavailable
|
||||
ctx->Release();
|
||||
return;
|
||||
}
|
||||
@ -150,27 +150,12 @@ void ScriptEngine::runScript(std::string scriptName)
|
||||
// Find the function for the function we want to execute.
|
||||
//This is how you call a normal function with arguments
|
||||
//asIScriptFunction *func = engine->GetModule(0)->GetFunctionByDecl("void func(arg1Type, arg2Type)");
|
||||
if (scriptName == "collisions")
|
||||
{
|
||||
func = Scripting::Physics::registerScriptCallbacks(m_engine);
|
||||
}
|
||||
else if (scriptName == "update")
|
||||
{
|
||||
func = Scripting::Track::registerUpdateScriptCallbacks(m_engine);
|
||||
}
|
||||
else if (scriptName == "start")
|
||||
{
|
||||
func = Scripting::Track::registerStartScriptCallbacks(m_engine);
|
||||
}
|
||||
else
|
||||
{
|
||||
//trigger type can have different names
|
||||
func = Scripting::Track::registerScriptCallbacks(m_engine, scriptName);
|
||||
}
|
||||
|
||||
func = Scripting::Track::registerScriptCallbacks(m_engine, scriptName);
|
||||
|
||||
if (func == NULL)
|
||||
{
|
||||
Log::warn("Scripting", "The required function was not found : %s", scriptName.c_str());
|
||||
Log::debug("Scripting", "Scripting function was not found : %s", scriptName.c_str());
|
||||
m_script_cache[scriptName] = NULL; // remember that this script is unavailable
|
||||
ctx->Release();
|
||||
return;
|
||||
}
|
||||
@ -181,9 +166,8 @@ void ScriptEngine::runScript(std::string scriptName)
|
||||
}
|
||||
else
|
||||
{
|
||||
//Script present in cache
|
||||
// TODO: clear when done
|
||||
func = m_script_cache[scriptName];
|
||||
// Script present in cache
|
||||
func = cached_script->second;
|
||||
}
|
||||
|
||||
if (func == NULL)
|
||||
|
@ -30,10 +30,7 @@ namespace Scripting
|
||||
namespace Physics
|
||||
{
|
||||
void registerScriptFunctions(asIScriptEngine *engine);
|
||||
asIScriptFunction*
|
||||
registerScriptCallbacks(asIScriptEngine *engine);
|
||||
void setCollision(int collider1,int collider2);
|
||||
void setCollisionType(std::string collisionType);
|
||||
void setCollision(std::string collider1, std::string collider2);
|
||||
}
|
||||
|
||||
@ -50,12 +47,6 @@ namespace Scripting
|
||||
|
||||
asIScriptFunction*
|
||||
registerScriptCallbacks(asIScriptEngine *engine, std::string scriptName);
|
||||
|
||||
asIScriptFunction*
|
||||
registerUpdateScriptCallbacks(asIScriptEngine *engine);
|
||||
|
||||
asIScriptFunction*
|
||||
registerStartScriptCallbacks(asIScriptEngine *engine);
|
||||
}
|
||||
|
||||
class ScriptEngine
|
||||
|
@ -174,6 +174,24 @@ namespace Scripting
|
||||
new(gen->GetAddressOfReturnLocation()) std::string(StringUtils::wide_to_utf8(out.c_str()));
|
||||
}
|
||||
|
||||
void scriptLogInfo(asIScriptGeneric *gen)
|
||||
{
|
||||
std::string input = *(std::string*)gen->GetArgAddress(0);
|
||||
Log::info("Script", "%s", input.c_str());
|
||||
}
|
||||
|
||||
void scriptLogWarning(asIScriptGeneric *gen)
|
||||
{
|
||||
std::string input = *(std::string*)gen->GetArgAddress(0);
|
||||
Log::warn("Script", "%s", input.c_str());
|
||||
}
|
||||
|
||||
void scriptLogError(asIScriptGeneric *gen)
|
||||
{
|
||||
std::string input = *(std::string*)gen->GetArgAddress(0);
|
||||
Log::error("Script", "%s", input.c_str());
|
||||
}
|
||||
|
||||
void registerScriptFunctions(asIScriptEngine *engine)
|
||||
{
|
||||
int r; // of type asERetCodes
|
||||
@ -185,13 +203,20 @@ namespace Scripting
|
||||
r = engine->RegisterGlobalFunction("string translate(const string &in, const string &in)", asFUNCTION(translateAndInsertValues1), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterGlobalFunction("string translate(const string &in, const string &in, const string &in)", asFUNCTION(translateAndInsertValues2), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterGlobalFunction("string translate(const string &in, const string &in, const string &in, const string &in)", asFUNCTION(translateAndInsertValues3), asCALL_GENERIC); assert(r >= 0);
|
||||
|
||||
engine->SetDefaultNamespace("Utils");
|
||||
r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in)", asFUNCTION(insertValues1), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in, const string &in)", asFUNCTION(insertValues2), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in, const string &in, const string &in)", asFUNCTION(insertValues3), asCALL_GENERIC); assert(r >= 0);
|
||||
|
||||
r = engine->RegisterGlobalFunction("void logInfo(const string &in)", asFUNCTION(scriptLogInfo), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterGlobalFunction("void logWarning(const string &in)", asFUNCTION(scriptLogWarning), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterGlobalFunction("void logError(const string &in)", asFUNCTION(scriptLogError), asCALL_GENERIC); assert(r >= 0);
|
||||
}
|
||||
|
||||
void registerScriptEnums(asIScriptEngine *engine)
|
||||
{
|
||||
engine->SetDefaultNamespace("GUI");
|
||||
engine->RegisterEnum("PA");
|
||||
engine->RegisterEnumValue("PA", "STEER_LEFT", PA_STEER_LEFT);
|
||||
engine->RegisterEnumValue("PA", "STEER_RIGHT", PA_STEER_RIGHT);
|
||||
|
@ -51,11 +51,6 @@ namespace Scripting
|
||||
void *pointer = &m_collider1;
|
||||
gen->SetReturnObject(pointer);
|
||||
}
|
||||
void getCollisionType(asIScriptGeneric *gen)
|
||||
{
|
||||
void *pointer = &m_collisionType;
|
||||
gen->SetReturnObject(pointer);
|
||||
}
|
||||
|
||||
//Callbacks from Physics Engine, for collisions
|
||||
void setCollision(int collider1,int collider2)
|
||||
@ -68,24 +63,12 @@ namespace Scripting
|
||||
m_collider1 = collider1;
|
||||
m_collider2 = collider2;
|
||||
}
|
||||
void setCollisionType(std::string collisionType)
|
||||
{
|
||||
m_collisionType = collisionType;
|
||||
}
|
||||
asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine)
|
||||
{
|
||||
asIScriptFunction *func;
|
||||
std::string function_name = "void on" + m_collisionType + "Collision()";
|
||||
func = engine->GetModule(0)->GetFunctionByDecl(function_name.c_str());
|
||||
return func;
|
||||
}
|
||||
void registerScriptFunctions(asIScriptEngine *engine)
|
||||
{
|
||||
int r;
|
||||
engine->SetDefaultNamespace("Physics");
|
||||
r = engine->RegisterGlobalFunction("uint getCollidingKart1()", asFUNCTION(getCollidingKart1), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterGlobalFunction("uint getCollidingKart2()", asFUNCTION(getCollidingKart2), asCALL_GENERIC); assert( r >= 0 );
|
||||
r = engine->RegisterGlobalFunction("string getCollisionType()", asFUNCTION(getCollisionType), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterGlobalFunction("string getCollidingID()", asFUNCTION(getCollidingID), asCALL_GENERIC); assert(r >= 0);
|
||||
r = engine->RegisterGlobalFunction("string createExplosion(Vec3 &in)", asFUNCTION(createExplosion), asCALL_GENERIC); assert(r >= 0);
|
||||
}
|
||||
|
@ -46,18 +46,6 @@ namespace Scripting
|
||||
func = engine->GetModule(0)->GetFunctionByDecl(function_name.c_str());
|
||||
return func;
|
||||
}
|
||||
asIScriptFunction* registerStartScriptCallbacks(asIScriptEngine *engine)
|
||||
{
|
||||
asIScriptFunction *func;
|
||||
func = engine->GetModule(0)->GetFunctionByDecl("void onStart()");
|
||||
return func;
|
||||
}
|
||||
asIScriptFunction* registerUpdateScriptCallbacks(asIScriptEngine *engine)
|
||||
{
|
||||
asIScriptFunction *func;
|
||||
func = engine->GetModule(0)->GetFunctionByDecl("void onUpdate()");
|
||||
return func;
|
||||
}
|
||||
/*
|
||||
void disableAnimation(std::string *name, void *memory)
|
||||
{
|
||||
|
@ -1486,7 +1486,7 @@ void Track::update(float dt)
|
||||
if (!m_startup_run) // first time running update = good point to run startup script
|
||||
{
|
||||
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
|
||||
script_engine->runScript("start");
|
||||
script_engine->runScript("onStart");
|
||||
m_startup_run = true;
|
||||
}
|
||||
m_track_object_manager->update(dt);
|
||||
@ -1498,7 +1498,7 @@ void Track::update(float dt)
|
||||
CheckManager::get()->update(dt);
|
||||
ItemManager::get()->update(dt);
|
||||
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
|
||||
script_engine->runScript("update");
|
||||
script_engine->runScript("onUpdate");
|
||||
} // update
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user