CSComponent.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Core/Context.h>
  26. #include <Atomic/Resource/ResourceCache.h>
  27. #ifdef ATOMIC_PHYSICS
  28. #include <Atomic/Physics/PhysicsEvents.h>
  29. #include <Atomic/Physics/PhysicsWorld.h>
  30. #endif
  31. #include <Atomic/Scene/Scene.h>
  32. #include <Atomic/Scene/SceneEvents.h>
  33. #include "CSComponent.h"
  34. #include "AtomicSharpAPI.h"
  35. namespace Atomic
  36. {
  37. extern const char* LOGIC_CATEGORY;
  38. class CSComponentFactory : public ObjectFactory
  39. {
  40. public:
  41. /// Construct.
  42. CSComponentFactory(Context* context) :
  43. ObjectFactory(context)
  44. {
  45. type_ = CSComponent::GetTypeStatic();
  46. baseType_ = CSComponent::GetBaseTypeStatic();
  47. typeName_ = CSComponent::GetTypeNameStatic();
  48. }
  49. /// Create an object of the specific type.
  50. SharedPtr<Object> CreateObject(const XMLElement& source = XMLElement::EMPTY)
  51. {
  52. // if in editor, just create the CSComponent
  53. if (context_->GetEditorContext())
  54. {
  55. return SharedPtr<Object>(new CSComponent(context_));
  56. }
  57. // At runtime, a XML CSComponent may refer to a managed component
  58. String managedClass;
  59. if (source != XMLElement::EMPTY)
  60. {
  61. XMLElement attrElem = source.GetChild("attribute");
  62. while (attrElem)
  63. {
  64. if (attrElem.GetAttribute("name") == "ManagedClass")
  65. {
  66. managedClass = attrElem.GetAttribute("value");
  67. break;
  68. }
  69. attrElem = attrElem.GetNext("attribute");
  70. }
  71. }
  72. SharedPtr<Object> ptr;
  73. if (managedClass.Length())
  74. {
  75. ptr = CSComponentCreate(managedClass);
  76. }
  77. if (ptr.Null())
  78. {
  79. ptr = new CSComponent(context_);
  80. }
  81. return ptr; }
  82. };
  83. CSComponent::CSComponent(Context* context) :
  84. Component(context),
  85. updateEventMask_(USE_UPDATE | USE_POSTUPDATE | USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE),
  86. currentEventMask_(0),
  87. instanceInitialized_(false),
  88. started_(false),
  89. destroyed_(false),
  90. scriptClassInstance_(false),
  91. delayedStartCalled_(false),
  92. loading_(false)
  93. {
  94. }
  95. CSComponent::~CSComponent()
  96. {
  97. }
  98. void CSComponent::RegisterObject(Context* context)
  99. {
  100. context->RegisterFactory(new CSComponentFactory(context), LOGIC_CATEGORY);
  101. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  102. }
  103. void CSComponent::OnSetEnabled()
  104. {
  105. UpdateEventSubscription();
  106. }
  107. void CSComponent::SetUpdateEventMask(unsigned char mask)
  108. {
  109. if (updateEventMask_ != mask)
  110. {
  111. updateEventMask_ = mask;
  112. UpdateEventSubscription();
  113. }
  114. }
  115. void CSComponent::ApplyAttributes()
  116. {
  117. }
  118. void CSComponent::CallScriptMethod(CSComponentMethod method, float value)
  119. {
  120. if (destroyed_ || !node_ || !node_->GetScene())
  121. return;
  122. CSComponentCallMethod(GetRefID(), method, value);
  123. }
  124. void CSComponent::Start()
  125. {
  126. CallScriptMethod(CSComponentMethod_Start);
  127. }
  128. void CSComponent::DelayedStart()
  129. {
  130. CallScriptMethod(CSComponentMethod_DelayedStart);
  131. }
  132. void CSComponent::Update(float timeStep)
  133. {
  134. //if (!instanceInitialized_)
  135. // InitInstance();
  136. if (!started_)
  137. {
  138. started_ = true;
  139. Start();
  140. }
  141. CallScriptMethod(CSComponentMethod_Update, timeStep);
  142. }
  143. void CSComponent::PostUpdate(float timeStep)
  144. {
  145. CallScriptMethod(CSComponentMethod_PostUpdate, timeStep);
  146. }
  147. void CSComponent::FixedUpdate(float timeStep)
  148. {
  149. CallScriptMethod(CSComponentMethod_FixedUpdate, timeStep);
  150. }
  151. void CSComponent::FixedPostUpdate(float timeStep)
  152. {
  153. CallScriptMethod(CSComponentMethod_PostFixedUpdate, timeStep);
  154. }
  155. void CSComponent::OnNodeSet(Node* node)
  156. {
  157. if (node)
  158. {
  159. //UpdateReferences();
  160. }
  161. else
  162. {
  163. // We are being detached from a node: execute user-defined stop function and prepare for destruction
  164. //UpdateReferences(true);
  165. Stop();
  166. }
  167. }
  168. void CSComponent::OnSceneSet(Scene* scene)
  169. {
  170. if (scene)
  171. UpdateEventSubscription();
  172. else
  173. {
  174. UnsubscribeFromEvent(E_SCENEUPDATE);
  175. UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
  176. #ifdef ATOMIC_PHYSICS
  177. UnsubscribeFromEvent(E_PHYSICSPRESTEP);
  178. UnsubscribeFromEvent(E_PHYSICSPOSTSTEP);
  179. #endif
  180. currentEventMask_ = 0;
  181. }
  182. }
  183. void CSComponent::UpdateEventSubscription()
  184. {
  185. Scene* scene = GetScene();
  186. if (!scene)
  187. return;
  188. bool enabled = IsEnabledEffective();
  189. bool needUpdate = enabled && ((updateEventMask_ & USE_UPDATE) || !delayedStartCalled_);
  190. if (needUpdate && !(currentEventMask_ & USE_UPDATE))
  191. {
  192. SubscribeToEvent(scene, E_SCENEUPDATE, HANDLER(CSComponent, HandleSceneUpdate));
  193. currentEventMask_ |= USE_UPDATE;
  194. }
  195. else if (!needUpdate && (currentEventMask_ & USE_UPDATE))
  196. {
  197. UnsubscribeFromEvent(scene, E_SCENEUPDATE);
  198. currentEventMask_ &= ~USE_UPDATE;
  199. }
  200. bool needPostUpdate = enabled && (updateEventMask_ & USE_POSTUPDATE);
  201. if (needPostUpdate && !(currentEventMask_ & USE_POSTUPDATE))
  202. {
  203. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(CSComponent, HandleScenePostUpdate));
  204. currentEventMask_ |= USE_POSTUPDATE;
  205. }
  206. else if (!needUpdate && (currentEventMask_ & USE_POSTUPDATE))
  207. {
  208. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  209. currentEventMask_ &= ~USE_POSTUPDATE;
  210. }
  211. #ifdef ATOMIC_PHYSICS
  212. PhysicsWorld* world = scene->GetComponent<PhysicsWorld>();
  213. if (!world)
  214. return;
  215. bool needFixedUpdate = enabled && (updateEventMask_ & USE_FIXEDUPDATE);
  216. if (needFixedUpdate && !(currentEventMask_ & USE_FIXEDUPDATE))
  217. {
  218. SubscribeToEvent(world, E_PHYSICSPRESTEP, HANDLER(CSComponent, HandlePhysicsPreStep));
  219. currentEventMask_ |= USE_FIXEDUPDATE;
  220. }
  221. else if (!needFixedUpdate && (currentEventMask_ & USE_FIXEDUPDATE))
  222. {
  223. UnsubscribeFromEvent(world, E_PHYSICSPRESTEP);
  224. currentEventMask_ &= ~USE_FIXEDUPDATE;
  225. }
  226. bool needFixedPostUpdate = enabled && (updateEventMask_ & USE_FIXEDPOSTUPDATE);
  227. if (needFixedPostUpdate && !(currentEventMask_ & USE_FIXEDPOSTUPDATE))
  228. {
  229. SubscribeToEvent(world, E_PHYSICSPOSTSTEP, HANDLER(CSComponent, HandlePhysicsPostStep));
  230. currentEventMask_ |= USE_FIXEDPOSTUPDATE;
  231. }
  232. else if (!needFixedPostUpdate && (currentEventMask_ & USE_FIXEDPOSTUPDATE))
  233. {
  234. UnsubscribeFromEvent(world, E_PHYSICSPOSTSTEP);
  235. currentEventMask_ &= ~USE_FIXEDPOSTUPDATE;
  236. }
  237. #endif
  238. }
  239. void CSComponent::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  240. {
  241. using namespace SceneUpdate;
  242. assert(!destroyed_);
  243. // Execute user-defined delayed start function before first update
  244. if (!delayedStartCalled_)
  245. {
  246. DelayedStart();
  247. delayedStartCalled_ = true;
  248. // If did not need actual update events, unsubscribe now
  249. if (!(updateEventMask_ & USE_UPDATE))
  250. {
  251. UnsubscribeFromEvent(GetScene(), E_SCENEUPDATE);
  252. currentEventMask_ &= ~USE_UPDATE;
  253. return;
  254. }
  255. }
  256. // Then execute user-defined update function
  257. Update(eventData[P_TIMESTEP].GetFloat());
  258. }
  259. void CSComponent::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  260. {
  261. using namespace ScenePostUpdate;
  262. // Execute user-defined post-update function
  263. PostUpdate(eventData[P_TIMESTEP].GetFloat());
  264. }
  265. #ifdef ATOMIC_PHYSICS
  266. void CSComponent::HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
  267. {
  268. using namespace PhysicsPreStep;
  269. // Execute user-defined fixed update function
  270. FixedUpdate(eventData[P_TIMESTEP].GetFloat());
  271. }
  272. void CSComponent::HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData)
  273. {
  274. using namespace PhysicsPostStep;
  275. // Execute user-defined fixed post-update function
  276. FixedPostUpdate(eventData[P_TIMESTEP].GetFloat());
  277. }
  278. #endif
  279. bool CSComponent::Load(Deserializer& source, bool setInstanceDefault)
  280. {
  281. loading_ = true;
  282. bool success = Component::Load(source, setInstanceDefault);
  283. loading_ = false;
  284. return success;
  285. }
  286. bool CSComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  287. {
  288. loading_ = true;
  289. bool success = Component::LoadXML(source, setInstanceDefault);
  290. loading_ = false;
  291. return success;
  292. }
  293. }