ScriptInstance.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef SCRIPT_SCRIPTINSTANCE_H
  24. #define SCRIPT_SCRIPTINSTANCE_H
  25. #include "Component.h"
  26. #include "EventListener.h"
  27. #include "Quaternion.h"
  28. #include "ScriptEventListener.h"
  29. #include "SharedPtr.h"
  30. class Entity;
  31. class PhysicsWorld;
  32. class Scene;
  33. class ScriptEngine;
  34. class ScriptFile;
  35. class asIScriptFunction;
  36. class asIScriptObject;
  37. //! Inbuilt scripted component methods
  38. enum ScriptInstanceMethod
  39. {
  40. METHOD_START = 0,
  41. METHOD_STOP,
  42. METHOD_UPDATE,
  43. METHOD_POSTUPDATE,
  44. METHOD_UPDATEFIXED,
  45. METHOD_POSTUPDATEFIXED,
  46. METHOD_SAVE,
  47. METHOD_LOAD,
  48. METHOD_POSTLOAD,
  49. METHOD_SAVEXML,
  50. METHOD_LOADXML,
  51. METHOD_WRITENETUPDATE,
  52. METHOD_READNETUPDATE,
  53. METHOD_POSTNETUPDATE,
  54. METHOD_INTERPOLATE,
  55. METHOD_GETCOMPONENTREFS,
  56. METHOD_GETRESOURCEREFS,
  57. MAX_SCRIPT_METHODS
  58. };
  59. //! Delay-executed method call
  60. struct DelayedMethodCall
  61. {
  62. //! Delay time remaining until execution
  63. float mDelay;
  64. //! Method declaration
  65. std::string mDeclaration;
  66. //! Parameters
  67. std::vector<Variant> mParameters;
  68. };
  69. //! A scripted component
  70. class ScriptInstance : public Component, public ScriptEventListener
  71. {
  72. DEFINE_TYPE(ScriptInstance);
  73. public:
  74. //! Construct with script engine pointer and name
  75. ScriptInstance(ScriptEngine* scriptEngine, const std::string& name = std::string());
  76. //! Destruct
  77. virtual ~ScriptInstance();
  78. //! Write component state to a stream
  79. virtual void save(Serializer& dest);
  80. //! Read component state from a stream
  81. virtual void load(Deserializer& source, ResourceCache* cache);
  82. //! Write component state to an XML element
  83. virtual void saveXML(XMLElement& dest);
  84. //! Read component state from an XML element
  85. virtual void loadXML(const XMLElement& source, ResourceCache* cache);
  86. //! Resolve component references after loading
  87. virtual void postLoad(ResourceCache* cache);
  88. //! Write a network update
  89. virtual bool writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info);
  90. //! Read a network update
  91. virtual void readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info);
  92. //! Resolve component references after a network update
  93. virtual void postNetUpdate(ResourceCache* cache);
  94. //! Perform client-side visual smoothing
  95. virtual void interpolate(bool snapToEnd);
  96. //! Return component references
  97. virtual void getComponentRefs(std::vector<ComponentRef>& dest);
  98. //! Return resource references
  99. virtual void getResourceRefs(std::vector<Resource*>& dest);
  100. //! Add an event handler. Called by script exposed version of subscribeToEvent()
  101. virtual void addEventHandler(StringHash eventType, const std::string& handlerName);
  102. //! Set script file and class
  103. bool setScriptClass(ScriptFile* scriptFile, const std::string& className);
  104. //! Enable or disable scripted updates and event handlers
  105. void setEnabled(bool enable);
  106. //! Set fixed updates per second. 0 (default) uses the physics frame rate
  107. void setFixedUpdateFps(int fps);
  108. //! Query for a method by declaration and execute if found
  109. bool execute(const std::string& declaration, const std::vector<Variant>& parameters = std::vector<Variant>());
  110. //! Execute a method
  111. bool execute(asIScriptFunction* method, const std::vector<Variant>& parameters = std::vector<Variant>());
  112. //! Add a delay-executed method call
  113. void delayedExecute(float delay, const std::string& declaration, const std::vector<Variant>& parameters = std::vector<Variant>());
  114. //! Clear pending delay-executed method calls
  115. void clearDelayedExecute();
  116. //! Return script engine
  117. ScriptEngine* getScriptEngine() const { return mScriptEngine; }
  118. //! Return script file
  119. ScriptFile* getScriptFile() const { return mScriptFile; }
  120. //! Return script object
  121. asIScriptObject* getScriptObject() const { return mScriptObject; }
  122. //! Return class name
  123. const std::string& getClassName() const { return mClassName; }
  124. //! Return whether scripted updates and event handlers are enabled
  125. bool isEnabled() const { return mEnabled; }
  126. //! Return fixed updates per second
  127. int getFixedUpdateFps() const { return mFixedUpdateFps; }
  128. //! Create the script object. Check for supported methods and register self to the ScriptFile if successful
  129. bool createObject();
  130. //! Release the script object and unregister self from the ScriptFile
  131. void releaseObject();
  132. private:
  133. //! Clear supported methods
  134. void clearMethods();
  135. //! Check for supported methods
  136. void getSupportedMethods();
  137. //! Handle scene update event
  138. void handleSceneUpdate(StringHash eventType, VariantMap& eventData);
  139. //! Handle scene post-update event
  140. void handleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  141. //! Handle physics pre-step event
  142. void handlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  143. //! Handle physics post-step event
  144. void handlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  145. //! Handle an event with a handler in script
  146. void handleScriptEvent(StringHash eventType, VariantMap& eventData);
  147. //! Script engine
  148. SharedPtr<ScriptEngine> mScriptEngine;
  149. //! Script file
  150. WeakPtr<ScriptFile> mScriptFile;
  151. //! Script object
  152. asIScriptObject* mScriptObject;
  153. //! Class name
  154. std::string mClassName;
  155. //! Pointers to supported inbuilt methods
  156. asIScriptFunction* mMethods[MAX_SCRIPT_METHODS];
  157. //! Enabled flag
  158. bool mEnabled;
  159. //! Fixed update FPS
  160. int mFixedUpdateFps;
  161. //! Fixed update time interval
  162. float mFixedUpdateInterval;
  163. //! Fixed update time accumulator
  164. float mFixedUpdateTimer;
  165. //! Fixed post update time accumulator
  166. float mFixedPostUpdateTimer;
  167. //! Delayed method calls
  168. std::vector<DelayedMethodCall> mDelayedMethodCalls;
  169. };
  170. //! Return the ScriptInstance of the active context
  171. ScriptInstance* getScriptContextInstance();
  172. //! Return the entity of the active context
  173. Entity* getScriptContextEntity();
  174. //! Return the event listener of the active context
  175. ScriptEventListener* getScriptContextEventListener();
  176. #endif // SCRIPT_SCRIPTINSTANCE_H