JSComponent.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  23. // Please see LICENSE.md in repository root for license information
  24. // https://github.com/AtomicGameEngine/AtomicGameEngine
  25. #pragma once
  26. #include <Atomic/Scene/Component.h>
  27. #include "JSComponentFile.h"
  28. namespace Atomic
  29. {
  30. class JSVM;
  31. /// Helper base class for user-defined game logic components that hooks up to update events and forwards them to virtual functions similar to ScriptInstance class.
  32. class ATOMIC_API JSComponent : public Component
  33. {
  34. friend class JSComponentFactory;
  35. friend class JSComponentFile;
  36. enum EventFlags
  37. {
  38. USE_UPDATE = 0x1,
  39. USE_POSTUPDATE = 0x2,
  40. USE_FIXEDUPDATE = 0x4,
  41. USE_FIXEDPOSTUPDATE = 0x8
  42. };
  43. public:
  44. OBJECT(JSComponent);
  45. /// Construct.
  46. JSComponent(Context* context);
  47. /// Destruct.
  48. virtual ~JSComponent();
  49. /// Register object factory.
  50. static void RegisterObject(Context* context);
  51. bool Load(Deserializer& source, bool setInstanceDefault);
  52. bool LoadXML(const XMLElement& source, bool setInstanceDefault);
  53. void ApplyAttributes();
  54. /// Get script attribute
  55. VariantMap& GetFieldValues() { return fieldValues_; }
  56. ResourceRef GetScriptAttr() const;
  57. JSComponentFile* GetComponentFile() { return componentFile_; }
  58. /// Handle enabled/disabled state change. Changes update event subscription.
  59. virtual void OnSetEnabled();
  60. /// Set what update events should be subscribed to. Use this for optimization: by default all are in use. Note that this is not an attribute and is not saved or network-serialized, therefore it should always be called eg. in the subclass constructor.
  61. void SetUpdateEventMask(unsigned char mask);
  62. /// Return what update events are subscribed to.
  63. unsigned char GetUpdateEventMask() const { return updateEventMask_; }
  64. /// Return whether the DelayedStart() function has been called.
  65. bool IsDelayedStartCalled() const { return delayedStartCalled_; }
  66. /// Set script attribute.
  67. void SetScriptAttr(const ResourceRef& value);
  68. void SetComponentFile(JSComponentFile* cfile);
  69. void SetDestroyed() { destroyed_ = true; }
  70. void InitInstance(bool hasArgs = false, int argIdx = 0);
  71. protected:
  72. /// Handle scene node being assigned at creation.
  73. virtual void OnNodeSet(Node* node);
  74. /// Handle scene being assigned.
  75. virtual void OnSceneSet(Scene* scene);
  76. private:
  77. /// Subscribe/unsubscribe to update events based on current enabled state and update event mask.
  78. void UpdateEventSubscription();
  79. /// Handle scene update event.
  80. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  81. /// Handle scene post-update event.
  82. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  83. #ifdef ATOMIC_PHYSICS
  84. /// Handle physics pre-step event.
  85. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  86. /// Handle physics post-step event.
  87. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  88. #endif
  89. void CallScriptMethod(const String& name, bool passValue = false, float value = 0.0f);
  90. /// Called when the component is added to a scene node. Other components may not yet exist.
  91. virtual void Start();
  92. /// Called before the first update. At this point all other components of the node should exist. Will also be called if update events are not wanted; in that case the event is immediately unsubscribed afterward.
  93. virtual void DelayedStart();
  94. /// Called when the component is detached from a scene node, usually on destruction. Note that you will no longer have access to the node and scene at that point.
  95. virtual void Stop() {}
  96. /// Called on scene update, variable timestep.
  97. virtual void Update(float timeStep);
  98. /// Called on scene post-update, variable timestep.
  99. virtual void PostUpdate(float timeStep);
  100. /// Called on physics update, fixed timestep.
  101. virtual void FixedUpdate(float timeStep);
  102. /// Called on physics post-update, fixed timestep.
  103. virtual void FixedPostUpdate(float timeStep);
  104. void UpdateReferences(bool remove = false);
  105. /// Requested event subscription mask.
  106. unsigned char updateEventMask_;
  107. /// Current event subscription mask.
  108. unsigned char currentEventMask_;
  109. bool started_;
  110. bool destroyed_;
  111. bool scriptClassInstance_;
  112. /// Flag for delayed start.
  113. bool delayedStartCalled_;
  114. bool loading_;
  115. WeakPtr<JSVM> vm_;
  116. SharedPtr<JSComponentFile> componentFile_;
  117. VariantMap fieldValues_;
  118. };
  119. }