| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Log.h"
- #include "PhysicsEvents.h"
- #include "PhysicsWorld.h"
- #include "RegisterArray.h"
- #include "ReplicationUtils.h"
- #include "ResourceCache.h"
- #include "Scene.h"
- #include "SceneEvents.h"
- #include "ScriptEngine.h"
- #include "ScriptFile.h"
- #include "ScriptInstance.h"
- #include "StringUtils.h"
- #include <angelscript.h>
- #include "DebugNew.h"
- static const std::string methodDeclarations[] = {
- "void start()",
- "void stop()",
- "void update(float)",
- "void postUpdate(float)",
- "void updateFixed(float)",
- "void postUpdateFixed(float)",
- "void save(Serializer&)",
- "void load(Deserializer&)",
- "void postLoad()",
- "void saveXML(XMLElement&)",
- "void loadXML(const XMLElement&)",
- "void writeNetUpdate(Serializer&, const NetUpdateInfo&)",
- "void readNetUpdate(Deserializer&, const NetUpdateInfo&)",
- "void postNetUpdate()",
- "void interpolate(bool)",
- "array<ComponentRef> getComponentRefs()",
- "array<Resource@> getResourceRefs()"
- };
- std::map<void*, ScriptInstance*> objectToInstance;
- ScriptInstance::ScriptInstance(ScriptEngine* scriptEngine, const std::string& name) :
- Component(name),
- mScriptEngine(scriptEngine),
- mScriptObject(0),
- mEnabled(true),
- mFixedUpdateFps(0),
- mFixedUpdateInterval(0.0f),
- mFixedUpdateTimer(0.0f),
- mFixedPostUpdateTimer(0.0f)
- {
- if (!mScriptEngine)
- EXCEPTION("Null script engine for ScriptInstance");
-
- clearMethods();
- }
- ScriptInstance::~ScriptInstance()
- {
- releaseObject();
- }
- void ScriptInstance::save(Serializer& dest)
- {
- Component::save(dest);
-
- dest.writeBool(mEnabled);
- dest.writeStringHash(getResourceHash(mScriptFile));
- dest.writeString(mClassName);
- dest.writeInt(mFixedUpdateFps);
- dest.writeFloat(mFixedUpdateTimer);
-
- dest.writeVLE(mDelayedMethodCalls.size());
- for (unsigned i = 0; i < mDelayedMethodCalls.size(); ++i)
- {
- const DelayedMethodCall& call = mDelayedMethodCalls[i];
- dest.writeFloat(call.mDelay);
- dest.writeString(call.mDeclaration);
- dest.writeVLE(call.mParameters.size());
- for (unsigned j = 0; j < call.mParameters.size(); ++j)
- dest.writeVariant(call.mParameters[j]);
- }
-
- // Save script's data into a separate buffer for safety
- static VectorBuffer scriptBuffer;
- scriptBuffer.clear();
- if (mMethods[METHOD_SAVE])
- {
- std::vector<Variant> parameters;
- parameters.push_back(Variant((void*)static_cast<Serializer*>(&scriptBuffer)));
- mScriptFile->execute(mScriptObject, mMethods[METHOD_SAVE], parameters);
- }
- dest.writeVLE(scriptBuffer.getSize());
- dest.write(scriptBuffer.getData(), scriptBuffer.getSize());
- }
- void ScriptInstance::load(Deserializer& source, ResourceCache* cache)
- {
- Component::load(source, cache);
-
- mEnabled = source.readBool();
- StringHash scriptFile = source.readStringHash();
- std::string className = source.readString();
- setScriptClass(cache->getResource<ScriptFile>(scriptFile), className);
- mFixedUpdateFps = source.readInt();
- mFixedUpdateTimer = mFixedPostUpdateTimer = source.readFloat();
- mFixedUpdateInterval = mFixedUpdateFps ? (1.0f / mFixedUpdateFps) : 0.0f;
-
- clearDelayedExecute();
- unsigned numCalls = source.readVLE();
- for (unsigned i = 0; i < numCalls; ++i)
- {
- DelayedMethodCall call;
- call.mDelay = source.readFloat();
- call.mDeclaration = source.readString();
- unsigned numParameters = source.readVLE();
- for (unsigned j = 0; j < numParameters; ++j)
- call.mParameters.push_back(source.readVariant());
- mDelayedMethodCalls.push_back(call);
- }
-
- static VectorBuffer scriptBuffer;
- unsigned scriptDataSize = source.readVLE();
- scriptBuffer.setData(source, scriptDataSize);
- if (mMethods[METHOD_LOAD])
- {
- std::vector<Variant> parameters;
- parameters.push_back(Variant((void*)static_cast<Deserializer*>(&scriptBuffer)));
- mScriptFile->execute(mScriptObject, mMethods[METHOD_LOAD], parameters);
- }
- }
- void ScriptInstance::postLoad(ResourceCache* cache)
- {
- if (mMethods[METHOD_POSTLOAD])
- mScriptFile->execute(mScriptObject, mMethods[METHOD_POSTLOAD]);
- }
- void ScriptInstance::saveXML(XMLElement& dest)
- {
- Component::saveXML(dest);
-
- XMLElement scriptElem = dest.createChildElement("script");
- scriptElem.setBool("enabled", mEnabled);
- scriptElem.setString("name", getResourceName(mScriptFile));
- scriptElem.setString("class", mClassName);
- scriptElem.setInt("fps", mFixedUpdateFps);
- scriptElem.setFloat("timeacc", mFixedUpdateTimer);
-
- for (unsigned i = 0; i < mDelayedMethodCalls.size(); ++i)
- {
- const DelayedMethodCall& call = mDelayedMethodCalls[i];
- XMLElement callElem = dest.createChildElement("call");
- callElem.setFloat("delay", call.mDelay);
- callElem.setString("method", call.mDeclaration);
- for (unsigned j = 0; j < call.mParameters.size(); ++j)
- {
- XMLElement paramElem = callElem.createChildElement("parameter");
- paramElem.setVariant(call.mParameters[j]);
- }
- }
-
- if (mMethods[METHOD_SAVEXML])
- {
- XMLElement dataElem = dest.createChildElement("data");
- std::vector<Variant> parameters;
- parameters.push_back(Variant((void*)&dataElem));
- mScriptFile->execute(mScriptObject, mMethods[METHOD_SAVEXML], parameters);
- }
- }
- void ScriptInstance::loadXML(const XMLElement& source, ResourceCache* cache)
- {
- Component::loadXML(source, cache);
-
- XMLElement scriptElem = source.getChildElement("script");
- mEnabled = scriptElem.getBool("enabled");
- setScriptClass(cache->getResource<ScriptFile>(scriptElem.getString("name")), scriptElem.getString("class"));
- mFixedUpdateFps = scriptElem.getInt("fps");
- mFixedUpdateTimer = mFixedPostUpdateTimer = scriptElem.getFloat("timeacc");
- mFixedUpdateInterval = mFixedUpdateFps ? (1.0f / mFixedUpdateFps) : 0.0f;
-
- clearDelayedExecute();
- XMLElement callElem = source.getChildElement("call");
- while (callElem)
- {
- DelayedMethodCall call;
- call.mDelay = callElem.getFloat("delay");
- call.mDeclaration = callElem.getString("method");
- XMLElement paramElem = callElem.getChildElement("parameter");
- while (paramElem)
- {
- call.mParameters.push_back(paramElem.getVariant());
- paramElem = paramElem.getNextElement("parameter");
- }
- mDelayedMethodCalls.push_back(call);
- callElem = callElem.getNextElement("call");
- }
-
- if (mMethods[METHOD_LOADXML])
- {
- XMLElement dataElem = source.getChildElement("data");
- if (dataElem)
- {
- std::vector<Variant> parameters;
- parameters.push_back(Variant((void*)&dataElem));
- mScriptFile->execute(mScriptObject, mMethods[METHOD_LOADXML], parameters);
- }
- }
- }
- bool ScriptInstance::writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info)
- {
- unsigned char bits = 0;
- StringHash scriptFileHash = getResourceHash(mScriptFile);
- checkBool(mEnabled, true, baseRevision, bits, 1);
- checkStringHash(scriptFileHash, StringHash(), baseRevision, bits, 2);
- checkString(mClassName, std::string(), baseRevision, bits, 2);
- checkInt(mFixedUpdateFps, 0, baseRevision, bits, 4);
- checkFloat(mFixedUpdateTimer, 0.0f, baseRevision, bits, 8);
-
- // Save script's data into a separate buffer for safety
- static VectorBuffer scriptBuffer;
- scriptBuffer.clear();
- // Write update only if enabled
- if ((mEnabled) && (mMethods[METHOD_WRITENETUPDATE]))
- {
- std::vector<Variant> parameters;
- parameters.push_back(Variant((void*)static_cast<Serializer*>(&scriptBuffer)));
- parameters.push_back(Variant((void*)&info));
- mScriptFile->execute(mScriptObject, mMethods[METHOD_WRITENETUPDATE], parameters);
- }
- // Compare buffer to previous revision if available
- unsigned scriptDataSize = scriptBuffer.getSize();
- if (scriptDataSize)
- checkBuffer(scriptBuffer, baseRevision, bits, 16);
-
- dest.writeUByte(bits);
- writeBoolDelta(mEnabled, dest, destRevision, bits & 1);
- writeStringHashDelta(scriptFileHash, dest, destRevision, bits & 2);
- writeStringDelta(mClassName, dest, destRevision, bits & 2);
- writeIntDelta(mFixedUpdateFps, dest, destRevision, bits & 4);
- writeFloatDelta(mFixedUpdateTimer, dest, destRevision, bits & 8);
- writeBufferDelta(scriptBuffer, dest, destRevision, bits & 16);
-
- return bits != 0;
- }
- void ScriptInstance::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
- {
- unsigned char bits = source.readUByte();
- if (bits & 1)
- mEnabled = source.readBool();
- if (bits & 2)
- {
- StringHash scriptFile = source.readStringHash();
- std::string className = source.readString();
- setScriptClass(cache->getResource<ScriptFile>(scriptFile), className);
- }
- if (bits & 4)
- {
- mFixedUpdateFps = source.readInt();
- mFixedUpdateInterval = mFixedUpdateFps ? (1.0f / mFixedUpdateFps) : 0.0f;
- }
- if (bits & 8)
- mFixedUpdateTimer = mFixedPostUpdateTimer = source.readFloat();
- if (bits & 16)
- {
- static VectorBuffer scriptBuffer;
- unsigned scriptDataSize = source.readVLE();
- scriptBuffer.setData(source, scriptDataSize);
- if (mMethods[METHOD_READNETUPDATE])
- {
- std::vector<Variant> parameters;
- parameters.push_back(Variant((void*)static_cast<Deserializer*>(&scriptBuffer)));
- parameters.push_back(Variant((void*)&info));
- mScriptFile->execute(mScriptObject, mMethods[METHOD_READNETUPDATE], parameters);
- }
- }
- }
- void ScriptInstance::postNetUpdate(ResourceCache* cache)
- {
- if (mMethods[METHOD_POSTNETUPDATE])
- mScriptFile->execute(mScriptObject, mMethods[METHOD_POSTNETUPDATE]);
- }
- void ScriptInstance::interpolate(bool snapToEnd)
- {
- if ((mEnabled) && (mMethods[METHOD_INTERPOLATE]))
- {
- std::vector<Variant> parameters;
- parameters.push_back(Variant(snapToEnd));
- mScriptFile->execute(mScriptObject, mMethods[METHOD_INTERPOLATE], parameters);
- }
- }
- void ScriptInstance::getComponentRefs(std::vector<ComponentRef>& dest)
- {
- if ((mEnabled) && (mMethods[METHOD_GETCOMPONENTREFS]))
- {
- asIScriptContext* context = mScriptEngine->getScriptFileContext(getScriptNestingLevel());
- if (!context)
- return;
- mScriptFile->execute(mScriptObject, mMethods[METHOD_GETCOMPONENTREFS]);
- CScriptArray* arr = static_cast<CScriptArray*>(context->GetAddressOfReturnValue());
- if (arr)
- {
- for (unsigned i = 0; i < arr->GetSize(); ++i)
- dest.push_back(*(static_cast<ComponentRef*>(arr->At(i))));
- }
- }
- }
- void ScriptInstance::getResourceRefs(std::vector<Resource*>& dest)
- {
- if (mMethods[METHOD_GETRESOURCEREFS])
- {
- asIScriptContext* context = mScriptEngine->getScriptFileContext(getScriptNestingLevel());
- if (!context)
- return;
- mScriptFile->execute(mScriptObject, mMethods[METHOD_GETRESOURCEREFS]);
- CScriptArray* arr = static_cast<CScriptArray*>(context->GetAddressOfReturnValue());
- if (arr)
- {
- for (unsigned i = 0; i < arr->GetSize(); ++i)
- dest.push_back(*(static_cast<Resource**>(arr->At(i))));
- }
- }
- }
- bool ScriptInstance::setScriptClass(ScriptFile* scriptFile, const std::string& className)
- {
- if ((scriptFile == mScriptFile) && (className == mClassName))
- return true;
-
- releaseObject();
-
- mScriptFile = scriptFile;
- mClassName = className;
-
- if ((!mScriptFile) && (mClassName.empty()))
- return true;
-
- return createObject();
- }
- void ScriptInstance::setEnabled(bool enable)
- {
- mEnabled = enable;
- }
- void ScriptInstance::setFixedUpdateFps(int fps)
- {
- mFixedUpdateFps = max(fps, 0);
- mFixedUpdateInterval = mFixedUpdateFps ? (1.0f / mFixedUpdateFps) : 0.0f;
- mFixedUpdateTimer = 0.0f;
- mFixedPostUpdateTimer = 0.0f;
- }
- bool ScriptInstance::execute(const std::string& declaration, const std::vector<Variant>& parameters)
- {
- if (!mScriptObject)
- return false;
-
- asIScriptFunction* method = mScriptFile->getMethod(mScriptObject, declaration);
- return mScriptFile->execute(mScriptObject, method, parameters);
- }
- bool ScriptInstance::execute(asIScriptFunction* method, const std::vector<Variant>& parameters)
- {
- if ((!method) || (!mScriptObject))
- return false;
-
- return mScriptFile->execute(mScriptObject, method, parameters);
- }
- void ScriptInstance::delayedExecute(float delay, const std::string& declaration, const std::vector<Variant>& parameters)
- {
- if (!mScriptObject)
- return;
-
- DelayedMethodCall call;
- call.mDelay = max(delay, 0.0f);
- call.mDeclaration = declaration;
- call.mParameters = parameters;
- mDelayedMethodCalls.push_back(call);
-
- // Make sure we are registered to the scene update event, because delayed calls are executed there
- if ((!mMethods[METHOD_UPDATE]) && (!hasSubscribedToEvent(EVENT_SCENEUPDATE)))
- subscribeToEvent(EVENT_SCENEUPDATE, EVENT_HANDLER(ScriptInstance, handleSceneUpdate));
- }
- void ScriptInstance::clearDelayedExecute()
- {
- mDelayedMethodCalls.clear();
- }
- void ScriptInstance::addEventHandler(StringHash eventType, const std::string& handlerName)
- {
- if (!mScriptObject)
- return;
-
- std::string declaration = "void " + handlerName + "(StringHash, VariantMap&)";
- asIScriptFunction* method = mScriptFile->getMethod(mScriptObject, declaration);
- if (!method)
- {
- LOGERROR("Event handler method " + declaration + " not found in " + mScriptFile->getName());
- return;
- }
-
- subscribeToEvent(eventType, EVENT_HANDLER(ScriptInstance, handleScriptEvent));
- mEventHandlers[eventType] = method;
- }
- bool ScriptInstance::createObject()
- {
- if (!mScriptFile)
- {
- LOGERROR("Null script file for ScriptInstance");
- return false;
- }
- if (mClassName.empty())
- {
- LOGERROR("Empty script class name");
- return false;
- }
-
- mScriptObject = mScriptFile->createObject(mClassName);
- if (mScriptObject)
- {
- mScriptFile->addScriptInstance(this);
- objectToInstance[(void*)mScriptObject] = this;
- getSupportedMethods();
- if (mMethods[METHOD_START])
- mScriptFile->execute(mScriptObject, mMethods[METHOD_START]);
- return true;
- }
- else
- {
- LOGERROR("Failed to create object of class " + mClassName + " from " + mScriptFile->getName());
- return false;
- }
- }
- void ScriptInstance::releaseObject()
- {
- if (mScriptObject)
- {
- if (mMethods[METHOD_STOP])
- mScriptFile->execute(mScriptObject, mMethods[METHOD_STOP]);
-
- removeAllEventHandlers();
- unsubscribeFromAllEvents();
- clearMethods();
- clearDelayedExecute();
-
- mScriptObject->Release();
- mScriptObject = 0;
-
- objectToInstance.erase((void*)mScriptObject);
-
- mScriptFile->removeScriptInstance(this);
- }
- }
- void ScriptInstance::clearMethods()
- {
- for (unsigned i = 0; i < MAX_SCRIPT_METHODS; ++i)
- mMethods[i] = 0;
-
- mDelayedMethodCalls.clear();
- }
- void ScriptInstance::getSupportedMethods()
- {
- for (unsigned i = 0; i < MAX_SCRIPT_METHODS; ++i)
- mMethods[i] = mScriptFile->getMethod(mScriptObject, methodDeclarations[i]);
-
- // Subscribe to the update events as supported
- if (mMethods[METHOD_UPDATE])
- subscribeToEvent(EVENT_SCENEUPDATE, EVENT_HANDLER(ScriptInstance, handleSceneUpdate));
- if (mMethods[METHOD_POSTUPDATE])
- subscribeToEvent(EVENT_SCENEPOSTUPDATE, EVENT_HANDLER(ScriptInstance, handleScenePostUpdate));
- if (mMethods[METHOD_UPDATEFIXED])
- subscribeToEvent(EVENT_PHYSICSPRESTEP, EVENT_HANDLER(ScriptInstance, handlePhysicsPreStep));
- if (mMethods[METHOD_POSTUPDATEFIXED])
- subscribeToEvent(EVENT_PHYSICSPOSTSTEP, EVENT_HANDLER(ScriptInstance, handlePhysicsPostStep));
- }
- void ScriptInstance::handleSceneUpdate(StringHash eventType, VariantMap& eventData)
- {
- if ((!mEnabled) || (!mScriptObject))
- return;
-
- using namespace SceneUpdate;
-
- float timeStep = eventData[P_TIMESTEP].getFloat();
-
- // Check that the scene matches
- Scene* scene = mEntity ? mEntity->getScene() : 0;
- if (eventData[P_SCENE].getPtr() == (void*)scene)
- {
- // Execute delayed method calls
- for (std::vector<DelayedMethodCall>::iterator i = mDelayedMethodCalls.begin(); i != mDelayedMethodCalls.end();)
- {
- i->mDelay -= timeStep;
- if (i->mDelay <= 0.0f)
- {
- execute(i->mDeclaration, i->mParameters);
- i = mDelayedMethodCalls.erase(i);
- }
- else
- ++i;
- }
-
- if (mMethods[METHOD_UPDATE])
- {
- std::vector<Variant> parameters;
- parameters.push_back(timeStep);
- mScriptFile->execute(mScriptObject, mMethods[METHOD_UPDATE], parameters);
- }
- }
- }
- void ScriptInstance::handleScenePostUpdate(StringHash eventType, VariantMap& eventData)
- {
- if ((!mEnabled) || (!mScriptObject))
- return;
-
- using namespace ScenePostUpdate;
-
- // Check that the scene matches
- Scene* scene = mEntity ? mEntity->getScene() : 0;
- if (eventData[P_SCENE].getPtr() == (void*)scene)
- {
- std::vector<Variant> parameters;
- parameters.push_back(eventData[P_TIMESTEP]);
- mScriptFile->execute(mScriptObject, mMethods[METHOD_POSTUPDATE], parameters);
- }
- }
- void ScriptInstance::handlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
- {
- if ((!mEnabled) || (!mScriptObject))
- return;
-
- using namespace PhysicsPreStep;
-
- // Check that the scene matches
- Scene* scene = mEntity ? mEntity->getScene() : 0;
- if (eventData[P_SCENE].getPtr() == (void*)scene)
- {
- if (!mFixedUpdateFps)
- {
- std::vector<Variant> parameters;
- parameters.push_back(eventData[P_TIMESTEP]);
- mScriptFile->execute(mScriptObject, mMethods[METHOD_UPDATEFIXED], parameters);
- }
- else
- {
- float timeStep = eventData[P_TIMESTEP].getFloat();
- mFixedUpdateTimer += timeStep;
- if (mFixedUpdateTimer >= mFixedUpdateInterval)
- {
- mFixedUpdateTimer = fmodf(mFixedUpdateTimer, mFixedUpdateInterval);
- std::vector<Variant> parameters;
- parameters.push_back(mFixedUpdateInterval);
- mScriptFile->execute(mScriptObject, mMethods[METHOD_UPDATEFIXED], parameters);
- }
- }
- }
- }
- void ScriptInstance::handlePhysicsPostStep(StringHash eventType, VariantMap& eventData)
- {
- if ((!mEnabled) || (!mScriptObject))
- return;
-
- using namespace PhysicsPostStep;
-
- // Check that the scene matches
- Scene* scene = mEntity ? mEntity->getScene() : 0;
- if (eventData[P_SCENE].getPtr() == (void*)scene)
- {
- if (!mFixedUpdateFps)
- {
- std::vector<Variant> parameters;
- parameters.push_back(eventData[P_TIMESTEP]);
- mScriptFile->execute(mScriptObject, mMethods[METHOD_UPDATEFIXED], parameters);
- }
- else
- {
- float timeStep = eventData[P_TIMESTEP].getFloat();
- mFixedPostUpdateTimer += timeStep;
- if (mFixedPostUpdateTimer >= mFixedUpdateInterval)
- {
- mFixedPostUpdateTimer = fmodf(mFixedPostUpdateTimer, mFixedUpdateInterval);
- std::vector<Variant> parameters;
- parameters.push_back(mFixedUpdateInterval);
- mScriptFile->execute(mScriptObject, mMethods[METHOD_UPDATEFIXED], parameters);
- }
- }
- }
- }
- void ScriptInstance::handleScriptEvent(StringHash eventType, VariantMap& eventData)
- {
- if ((!mEnabled) || (!mScriptFile) || (!mScriptObject))
- return;
-
- std::map<StringHash, asIScriptFunction*>::iterator i = mEventHandlers.find(eventType);
- if (i == mEventHandlers.end())
- return;
-
- std::vector<Variant> parameters;
- parameters.push_back(Variant((void*)&eventType));
- parameters.push_back(Variant((void*)&eventData));
- mScriptFile->execute(mScriptObject, i->second, parameters);
- }
- ScriptInstance* getScriptContextInstance()
- {
- void* object = asGetActiveContext()->GetThisPointer();
- std::map<void*, ScriptInstance*>::const_iterator i = objectToInstance.find(object);
- if (i != objectToInstance.end())
- return i->second;
- else
- return 0;
- }
- Entity* getScriptContextEntity()
- {
- ScriptInstance* instance = getScriptContextInstance();
- return instance ? instance->getEntity() : 0;
- }
- ScriptEventListener* getScriptContextEventListener()
- {
- // First try to get the script instance. If not found, get the script file for procedural event handling
- ScriptInstance* instance = getScriptContextInstance();
- if (instance)
- return instance;
- ScriptFile* file = getScriptContextFile();
- return file;
- }
|