ScriptInstance.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. //! A scripted component
  60. class ScriptInstance : public Component, public ScriptEventListener
  61. {
  62. DEFINE_TYPE(ScriptInstance);
  63. public:
  64. //! Construct with script engine pointer and name
  65. ScriptInstance(ScriptEngine* scriptEngine, const std::string& name = std::string());
  66. //! Destruct
  67. virtual ~ScriptInstance();
  68. //! Write component state to a stream
  69. virtual void save(Serializer& dest);
  70. //! Read component state from a stream
  71. virtual void load(Deserializer& source, ResourceCache* cache);
  72. //! Write component state to an XML element
  73. virtual void saveXML(XMLElement& dest);
  74. //! Read component state from an XML element
  75. virtual void loadXML(const XMLElement& source, ResourceCache* cache);
  76. //! Resolve component references after loading
  77. virtual void postLoad(ResourceCache* cache);
  78. //! Write a network update
  79. virtual bool writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info);
  80. //! Read a network update
  81. virtual void readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info);
  82. //! Resolve component references after a network update
  83. virtual void postNetUpdate(ResourceCache* cache);
  84. //! Perform client-side visual smoothing
  85. virtual void interpolate(bool snapToEnd);
  86. //! Return component references
  87. virtual void getComponentRefs(std::vector<ComponentRef>& dest);
  88. //! Return resource references
  89. virtual void getResourceRefs(std::vector<Resource*>& dest);
  90. //! Add an event handler. Called by script exposed version of subscribeToEvent()
  91. virtual void addEventHandler(StringHash eventType, const std::string& handlerName);
  92. //! Set script file and class
  93. bool setScriptClass(ScriptFile* scriptFile, const std::string& className);
  94. //! Enable or disable scripted updates and event handlers
  95. void setEnabled(bool enable);
  96. //! Set fixed updates per second. 0 (default) uses the physics frame rate
  97. void setFixedUpdateFps(int fps);
  98. //! Query for a method by declaration and execute if found
  99. bool execute(const std::string& declaration, const std::vector<Variant>& parameters = std::vector<Variant>());
  100. //! Execute a method
  101. bool execute(asIScriptFunction* method, const std::vector<Variant>& parameters = std::vector<Variant>());
  102. //! Return script engine
  103. ScriptEngine* getScriptEngine() const { return mScriptEngine; }
  104. //! Return script file
  105. ScriptFile* getScriptFile() const { return mScriptFile; }
  106. //! Return script object
  107. asIScriptObject* getScriptObject() const { return mScriptObject; }
  108. //! Return class name
  109. const std::string& getClassName() const { return mClassName; }
  110. //! Return whether object created and running
  111. bool isRunning() const { return mScriptObject != 0; }
  112. //! Return whether scripted updates and event handlers are enabled
  113. bool isEnabled() const { return mEnabled; }
  114. //! Return fixed updates per second
  115. int getFixedUpdateFps() const { return mFixedUpdateFps; }
  116. //! Create the script object. Check for supported methods and register self to the ScriptFile if successful
  117. bool createObject();
  118. //! Release the script object and unregister self from the ScriptFile
  119. void releaseObject();
  120. private:
  121. //! Clear supported methods
  122. void clearMethods();
  123. //! Check for supported methods
  124. void getSupportedMethods();
  125. //! Handle scene update event
  126. void handleSceneUpdate(StringHash eventType, VariantMap& eventData);
  127. //! Handle scene post-update event
  128. void handleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  129. //! Handle physics pre-step event
  130. void handlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  131. //! Handle physics post-step event
  132. void handlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  133. //! Handle an event with a handler in script
  134. void handleScriptEvent(StringHash eventType, VariantMap& eventData);
  135. //! Script engine
  136. SharedPtr<ScriptEngine> mScriptEngine;
  137. //! Script file
  138. WeakPtr<ScriptFile> mScriptFile;
  139. //! Script object
  140. asIScriptObject* mScriptObject;
  141. //! Class name
  142. std::string mClassName;
  143. //! Pointers to supported inbuilt methods
  144. asIScriptFunction* mMethods[MAX_SCRIPT_METHODS];
  145. //! Enabled flag
  146. bool mEnabled;
  147. //! Fixed update FPS
  148. int mFixedUpdateFps;
  149. //! Fixed update time interval
  150. float mFixedUpdateInterval;
  151. //! Fixed update time accumulator
  152. float mFixedUpdateTimer;
  153. //! Fixed post update time accumulator
  154. float mFixedPostUpdateTimer;
  155. };
  156. //! Return the ScriptInstance of the active context
  157. ScriptInstance* getScriptContextComponent();
  158. //! Return the entity of the active context
  159. Entity* getScriptContextEntity();
  160. #endif // SCRIPT_SCRIPTINSTANCE_H