Actor.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <vector>
  10. #include "Math.h"
  11. #include <rapidjson/document.h>
  12. #include "Component.h"
  13. class Actor
  14. {
  15. public:
  16. enum TypeID
  17. {
  18. TActor = 0,
  19. TBallActor,
  20. TFollowActor,
  21. TPlaneActor,
  22. TTargetActor,
  23. NUM_ACTOR_TYPES
  24. };
  25. static const char* TypeNames[NUM_ACTOR_TYPES];
  26. enum State
  27. {
  28. EActive,
  29. EPaused,
  30. EDead
  31. };
  32. Actor(class Game* game);
  33. virtual ~Actor();
  34. // Update function called from Game (not overridable)
  35. void Update(float deltaTime);
  36. // Updates all the components attached to the actor (not overridable)
  37. void UpdateComponents(float deltaTime);
  38. // Any actor-specific update code (overridable)
  39. virtual void UpdateActor(float deltaTime);
  40. // ProcessInput function called from Game (not overridable)
  41. void ProcessInput(const uint8_t* keyState);
  42. // Any actor-specific input code (overridable)
  43. virtual void ActorInput(const uint8_t* keyState);
  44. // Getters/setters
  45. const Vector3& GetPosition() const { return mPosition; }
  46. void SetPosition(const Vector3& pos) { mPosition = pos; mRecomputeTransform = true; }
  47. float GetScale() const { return mScale; }
  48. void SetScale(float scale) { mScale = scale; mRecomputeTransform = true; }
  49. const Quaternion& GetRotation() const { return mRotation; }
  50. void SetRotation(const Quaternion& rotation) { mRotation = rotation; mRecomputeTransform = true; }
  51. void ComputeWorldTransform();
  52. const Matrix4& GetWorldTransform() const { return mWorldTransform; }
  53. Vector3 GetForward() const { return Vector3::Transform(Vector3::UnitX, mRotation); }
  54. Vector3 GetRight() const { return Vector3::Transform(Vector3::UnitY, mRotation); }
  55. void RotateToNewForward(const Vector3& forward);
  56. State GetState() const { return mState; }
  57. void SetState(State state) { mState = state; }
  58. class Game* GetGame() { return mGame; }
  59. // Add/remove components
  60. void AddComponent(class Component* component);
  61. void RemoveComponent(class Component* component);
  62. // Load/Save
  63. virtual void LoadProperties(const rapidjson::Value& inObj);
  64. virtual void SaveProperties(rapidjson::Document::AllocatorType& alloc,
  65. rapidjson::Value& inObj) const;
  66. // Create an actor with specified properties
  67. template <typename T>
  68. static Actor* Create(class Game* game, const rapidjson::Value& inObj)
  69. {
  70. // Dynamically allocate actor of type T
  71. T* t = new T(game);
  72. // Call LoadProperties on new actor
  73. t->LoadProperties(inObj);
  74. return t;
  75. }
  76. // Search through component vector for one of type
  77. Component* GetComponentOfType(Component::TypeID type)
  78. {
  79. Component* comp = nullptr;
  80. for (Component* c : mComponents)
  81. {
  82. if (c->GetType() == type)
  83. {
  84. comp = c;
  85. break;
  86. }
  87. }
  88. return comp;
  89. }
  90. virtual TypeID GetType() const { return TActor; }
  91. const std::vector<Component*>& GetComponents() const { return mComponents; }
  92. private:
  93. // Actor's state
  94. State mState;
  95. // Transform
  96. Matrix4 mWorldTransform;
  97. Vector3 mPosition;
  98. Quaternion mRotation;
  99. float mScale;
  100. bool mRecomputeTransform;
  101. std::vector<Component*> mComponents;
  102. class Game* mGame;
  103. };