2
0

Component.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #pragma once
  9. #include "Math.h"
  10. #include <rapidjson/document.h>
  11. class Component
  12. {
  13. public:
  14. enum TypeID
  15. {
  16. TComponent = 0,
  17. TAudioComponent,
  18. TBallMove,
  19. TBoxComponent,
  20. TCameraComponent,
  21. TFollowCamera,
  22. TMeshComponent,
  23. TMoveComponent,
  24. TSkeletalMeshComponent,
  25. TSpriteComponent,
  26. TMirrorCamera,
  27. TPointLightComponent,
  28. TTargetComponent,
  29. NUM_COMPONENT_TYPES
  30. };
  31. static const char* TypeNames[NUM_COMPONENT_TYPES];
  32. // Constructor
  33. // (the lower the update order, the earlier the component updates)
  34. Component(class Actor* owner, int updateOrder = 100);
  35. // Destructor
  36. virtual ~Component();
  37. // Update this component by delta time
  38. virtual void Update(float deltaTime);
  39. // Process input for this component
  40. virtual void ProcessInput(const uint8_t* keyState) {}
  41. // Called when world transform changes
  42. virtual void OnUpdateWorldTransform();
  43. class Actor* GetOwner() { return mOwner; }
  44. int GetUpdateOrder() const { return mUpdateOrder; }
  45. virtual TypeID GetType() const = 0;
  46. // Load/Save
  47. virtual void LoadProperties(const rapidjson::Value& inObj);
  48. virtual void SaveProperties(rapidjson::Document::AllocatorType& alloc,
  49. rapidjson::Value& inObj) const;
  50. // Create a component with specified properties
  51. template <typename T>
  52. static Component* Create(class Actor* actor, const rapidjson::Value& inObj)
  53. {
  54. // Dynamically allocate component of type T
  55. T* t = new T(actor);
  56. // Call LoadProperties on new component
  57. t->LoadProperties(inObj);
  58. return t;
  59. }
  60. protected:
  61. // Owning actor
  62. class Actor* mOwner;
  63. // Update order of component
  64. int mUpdateOrder;
  65. };