ScriptInstance.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 asIScriptContext;
  36. class asIScriptFunction;
  37. class asIScriptObject;
  38. //! Inbuilt scripted component methods
  39. enum ScriptInstanceMethod
  40. {
  41. METHOD_START = 0,
  42. METHOD_STOP,
  43. METHOD_UPDATE,
  44. METHOD_POSTUPDATE,
  45. METHOD_UPDATEFIXED,
  46. METHOD_POSTUPDATEFIXED,
  47. METHOD_SAVE,
  48. METHOD_LOAD,
  49. METHOD_POSTLOAD,
  50. METHOD_SAVEXML,
  51. METHOD_LOADXML,
  52. METHOD_WRITENETUPDATE,
  53. METHOD_READNETUPDATE,
  54. METHOD_POSTNETUPDATE,
  55. METHOD_INTERPOLATE,
  56. METHOD_GETCOMPONENTREFS,
  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. //! Add an event handler. Called by script exposed version of subscribeToEvent()
  89. virtual void addEventHandler(StringHash eventType, const std::string& handlerName);
  90. //! Set script file and class
  91. bool setScriptClass(ScriptFile* scriptFile, const std::string& className);
  92. //! Enable or disable scripted updates and event handlers
  93. void setEnabled(bool enable);
  94. //! Query for a method by declaration and execute if found
  95. bool execute(const std::string& declaration, const std::vector<Variant>& parameters = std::vector<Variant>());
  96. //! Execute a method
  97. bool execute(asIScriptFunction* method, const std::vector<Variant>& parameters = std::vector<Variant>());
  98. //! Return script engine
  99. ScriptEngine* getScriptEngine() const { return mScriptEngine; }
  100. //! Return script file
  101. ScriptFile* getScriptFile() const { return mScriptFile; }
  102. //! Return script context
  103. asIScriptContext* getScriptContext() const { return mScriptContext; }
  104. //! Return script object
  105. asIScriptObject* getScriptObject() const { return mScriptObject; }
  106. //! Return class name
  107. const std::string& getClassName() const { return mClassName; }
  108. //! Return whether object created and running
  109. bool isRunning() const { return mScriptObject != 0; }
  110. //! Return whether scripted updates and event handlers are enabled
  111. bool isEnabled() const { return mEnabled; }
  112. private:
  113. //! Release object
  114. void releaseObject();
  115. //! Clear supported methods
  116. void clearMethods();
  117. //! Check for supported methods
  118. void getSupportedMethods();
  119. //! Handle scene update event
  120. void handleSceneUpdate(StringHash eventType, VariantMap& eventData);
  121. //! Handle scene post-update event
  122. void handleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  123. //! Handle physics pre-step event
  124. void handlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  125. //! Handle physics post-step event
  126. void handlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  127. //! Handle an event with a handler in script
  128. void handleScriptEvent(StringHash eventType, VariantMap& eventData);
  129. //! Script engine
  130. SharedPtr<ScriptEngine> mScriptEngine;
  131. //! Script file
  132. SharedPtr<ScriptFile> mScriptFile;
  133. //! Script context
  134. asIScriptContext* mScriptContext;
  135. //! Script object
  136. asIScriptObject* mScriptObject;
  137. //! Class name
  138. std::string mClassName;
  139. //! Pointers to supported inbuilt methods
  140. asIScriptFunction* mMethods[MAX_SCRIPT_METHODS];
  141. //! Cache of executed functions
  142. std::map<std::string, asIScriptFunction*> mExecuteCache;
  143. //! Enabled flag
  144. bool mEnabled;
  145. };
  146. ScriptInstance* getScriptContextComponent();
  147. Entity* getScriptContextEntity();
  148. Scene* getScriptContextScene();
  149. #endif // SCRIPT_SCRIPTINSTANCE_H