CSComponent.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. CSComponent* csc = new CSComponent(context_);
  76. CSComponentCreate(managedClass, csc);
  77. assert(csc->managedID_);
  78. ptr = csc;
  79. }
  80. if (ptr.Null())
  81. {
  82. ptr = new CSComponent(context_);
  83. }
  84. return ptr; }
  85. };
  86. CSComponent::CSComponent(Context* context) :
  87. Component(context),
  88. updateEventMask_(USE_UPDATE | USE_POSTUPDATE | USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE),
  89. currentEventMask_(0),
  90. instanceInitialized_(false),
  91. started_(false),
  92. destroyed_(false),
  93. scriptClassInstance_(false),
  94. delayedStartCalled_(false),
  95. loading_(false),
  96. managedID_(0)
  97. {
  98. }
  99. CSComponent::~CSComponent()
  100. {
  101. }
  102. void CSComponent::RegisterObject(Context* context)
  103. {
  104. context->RegisterFactory(new CSComponentFactory(context), LOGIC_CATEGORY);
  105. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  106. }
  107. void CSComponent::OnSetEnabled()
  108. {
  109. UpdateEventSubscription();
  110. }
  111. void CSComponent::SetUpdateEventMask(unsigned char mask)
  112. {
  113. if (updateEventMask_ != mask)
  114. {
  115. updateEventMask_ = mask;
  116. UpdateEventSubscription();
  117. }
  118. }
  119. void CSComponent::ApplyAttributes()
  120. {
  121. }
  122. void CSComponent::CallScriptMethod(CSComponentMethod method, float value)
  123. {
  124. if (!managedID_ || destroyed_ || !node_ || !node_->GetScene())
  125. return;
  126. CSComponentCallMethod(managedID_, method, value);
  127. }
  128. void CSComponent::Start()
  129. {
  130. CallScriptMethod(CSComponentMethod_Start);
  131. }
  132. void CSComponent::DelayedStart()
  133. {
  134. CallScriptMethod(CSComponentMethod_DelayedStart);
  135. }
  136. void CSComponent::Update(float timeStep)
  137. {
  138. //if (!instanceInitialized_)
  139. // InitInstance();
  140. if (!started_)
  141. {
  142. started_ = true;
  143. Start();
  144. }
  145. CallScriptMethod(CSComponentMethod_Update, timeStep);
  146. }
  147. void CSComponent::PostUpdate(float timeStep)
  148. {
  149. CallScriptMethod(CSComponentMethod_PostUpdate, timeStep);
  150. }
  151. void CSComponent::FixedUpdate(float timeStep)
  152. {
  153. CallScriptMethod(CSComponentMethod_FixedUpdate, timeStep);
  154. }
  155. void CSComponent::FixedPostUpdate(float timeStep)
  156. {
  157. CallScriptMethod(CSComponentMethod_PostFixedUpdate, timeStep);
  158. }
  159. void CSComponent::OnNodeSet(Node* node)
  160. {
  161. if (node)
  162. {
  163. //UpdateReferences();
  164. }
  165. else
  166. {
  167. // We are being detached from a node: execute user-defined stop function and prepare for destruction
  168. //UpdateReferences(true);
  169. Stop();
  170. }
  171. }
  172. void CSComponent::OnSceneSet(Scene* scene)
  173. {
  174. if (scene)
  175. UpdateEventSubscription();
  176. else
  177. {
  178. UnsubscribeFromEvent(E_SCENEUPDATE);
  179. UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
  180. #ifdef ATOMIC_PHYSICS
  181. UnsubscribeFromEvent(E_PHYSICSPRESTEP);
  182. UnsubscribeFromEvent(E_PHYSICSPOSTSTEP);
  183. #endif
  184. currentEventMask_ = 0;
  185. }
  186. }
  187. void CSComponent::UpdateEventSubscription()
  188. {
  189. Scene* scene = GetScene();
  190. if (!scene)
  191. return;
  192. bool enabled = IsEnabledEffective();
  193. bool needUpdate = enabled && ((updateEventMask_ & USE_UPDATE) || !delayedStartCalled_);
  194. if (needUpdate && !(currentEventMask_ & USE_UPDATE))
  195. {
  196. SubscribeToEvent(scene, E_SCENEUPDATE, HANDLER(CSComponent, HandleSceneUpdate));
  197. currentEventMask_ |= USE_UPDATE;
  198. }
  199. else if (!needUpdate && (currentEventMask_ & USE_UPDATE))
  200. {
  201. UnsubscribeFromEvent(scene, E_SCENEUPDATE);
  202. currentEventMask_ &= ~USE_UPDATE;
  203. }
  204. bool needPostUpdate = enabled && (updateEventMask_ & USE_POSTUPDATE);
  205. if (needPostUpdate && !(currentEventMask_ & USE_POSTUPDATE))
  206. {
  207. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(CSComponent, HandleScenePostUpdate));
  208. currentEventMask_ |= USE_POSTUPDATE;
  209. }
  210. else if (!needUpdate && (currentEventMask_ & USE_POSTUPDATE))
  211. {
  212. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  213. currentEventMask_ &= ~USE_POSTUPDATE;
  214. }
  215. #ifdef ATOMIC_PHYSICS
  216. PhysicsWorld* world = scene->GetComponent<PhysicsWorld>();
  217. if (!world)
  218. return;
  219. bool needFixedUpdate = enabled && (updateEventMask_ & USE_FIXEDUPDATE);
  220. if (needFixedUpdate && !(currentEventMask_ & USE_FIXEDUPDATE))
  221. {
  222. SubscribeToEvent(world, E_PHYSICSPRESTEP, HANDLER(CSComponent, HandlePhysicsPreStep));
  223. currentEventMask_ |= USE_FIXEDUPDATE;
  224. }
  225. else if (!needFixedUpdate && (currentEventMask_ & USE_FIXEDUPDATE))
  226. {
  227. UnsubscribeFromEvent(world, E_PHYSICSPRESTEP);
  228. currentEventMask_ &= ~USE_FIXEDUPDATE;
  229. }
  230. bool needFixedPostUpdate = enabled && (updateEventMask_ & USE_FIXEDPOSTUPDATE);
  231. if (needFixedPostUpdate && !(currentEventMask_ & USE_FIXEDPOSTUPDATE))
  232. {
  233. SubscribeToEvent(world, E_PHYSICSPOSTSTEP, HANDLER(CSComponent, HandlePhysicsPostStep));
  234. currentEventMask_ |= USE_FIXEDPOSTUPDATE;
  235. }
  236. else if (!needFixedPostUpdate && (currentEventMask_ & USE_FIXEDPOSTUPDATE))
  237. {
  238. UnsubscribeFromEvent(world, E_PHYSICSPOSTSTEP);
  239. currentEventMask_ &= ~USE_FIXEDPOSTUPDATE;
  240. }
  241. #endif
  242. }
  243. void CSComponent::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  244. {
  245. using namespace SceneUpdate;
  246. assert(!destroyed_);
  247. // Execute user-defined delayed start function before first update
  248. if (!delayedStartCalled_)
  249. {
  250. DelayedStart();
  251. delayedStartCalled_ = true;
  252. // If did not need actual update events, unsubscribe now
  253. if (!(updateEventMask_ & USE_UPDATE))
  254. {
  255. UnsubscribeFromEvent(GetScene(), E_SCENEUPDATE);
  256. currentEventMask_ &= ~USE_UPDATE;
  257. return;
  258. }
  259. }
  260. // Then execute user-defined update function
  261. Update(eventData[P_TIMESTEP].GetFloat());
  262. }
  263. void CSComponent::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  264. {
  265. using namespace ScenePostUpdate;
  266. // Execute user-defined post-update function
  267. PostUpdate(eventData[P_TIMESTEP].GetFloat());
  268. }
  269. #ifdef ATOMIC_PHYSICS
  270. void CSComponent::HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
  271. {
  272. using namespace PhysicsPreStep;
  273. // Execute user-defined fixed update function
  274. FixedUpdate(eventData[P_TIMESTEP].GetFloat());
  275. }
  276. void CSComponent::HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData)
  277. {
  278. using namespace PhysicsPostStep;
  279. // Execute user-defined fixed post-update function
  280. FixedPostUpdate(eventData[P_TIMESTEP].GetFloat());
  281. }
  282. #endif
  283. bool CSComponent::Load(Deserializer& source, bool setInstanceDefault)
  284. {
  285. loading_ = true;
  286. bool success = Component::Load(source, setInstanceDefault);
  287. loading_ = false;
  288. return success;
  289. }
  290. bool CSComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  291. {
  292. loading_ = true;
  293. bool success = Component::LoadXML(source, setInstanceDefault);
  294. loading_ = false;
  295. return success;
  296. }
  297. }