2
0

Actor.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include "Actor.h"
  9. #include "Game.h"
  10. #include "Component.h"
  11. #include "LevelLoader.h"
  12. const char* Actor::TypeNames[NUM_ACTOR_TYPES] = {
  13. "Actor",
  14. "BallActor",
  15. "FollowActor",
  16. "PlaneActor",
  17. "TargetActor",
  18. };
  19. Actor::Actor(Game* game)
  20. :mState(EActive)
  21. ,mPosition(Vector3::Zero)
  22. ,mRotation(Quaternion::Identity)
  23. ,mScale(1.0f)
  24. ,mGame(game)
  25. ,mRecomputeTransform(true)
  26. {
  27. mGame->AddActor(this);
  28. }
  29. Actor::~Actor()
  30. {
  31. mGame->RemoveActor(this);
  32. // Need to delete components
  33. // Because ~Component calls RemoveComponent, need a different style loop
  34. while (!mComponents.empty())
  35. {
  36. delete mComponents.back();
  37. }
  38. }
  39. void Actor::Update(float deltaTime)
  40. {
  41. if (mState == EActive)
  42. {
  43. if (mRecomputeTransform)
  44. {
  45. ComputeWorldTransform();
  46. }
  47. UpdateComponents(deltaTime);
  48. UpdateActor(deltaTime);
  49. }
  50. }
  51. void Actor::UpdateComponents(float deltaTime)
  52. {
  53. for (auto comp : mComponents)
  54. {
  55. comp->Update(deltaTime);
  56. }
  57. }
  58. void Actor::UpdateActor(float deltaTime)
  59. {
  60. }
  61. void Actor::ProcessInput(const uint8_t* keyState)
  62. {
  63. if (mState == EActive)
  64. {
  65. // First process input for components
  66. for (auto comp : mComponents)
  67. {
  68. comp->ProcessInput(keyState);
  69. }
  70. ActorInput(keyState);
  71. }
  72. }
  73. void Actor::ActorInput(const uint8_t* keyState)
  74. {
  75. }
  76. void Actor::ComputeWorldTransform()
  77. {
  78. mRecomputeTransform = false;
  79. // Scale, then rotate, then translate
  80. mWorldTransform = Matrix4::CreateScale(mScale);
  81. mWorldTransform *= Matrix4::CreateFromQuaternion(mRotation);
  82. mWorldTransform *= Matrix4::CreateTranslation(mPosition);
  83. // Inform components world transform updated
  84. for (auto comp : mComponents)
  85. {
  86. comp->OnUpdateWorldTransform();
  87. }
  88. }
  89. void Actor::RotateToNewForward(const Vector3& forward)
  90. {
  91. // Figure out difference between original (unit x) and new
  92. float dot = Vector3::Dot(Vector3::UnitX, forward);
  93. float angle = Math::Acos(dot);
  94. // Facing down X
  95. if (dot > 0.9999f)
  96. {
  97. SetRotation(Quaternion::Identity);
  98. }
  99. // Facing down -X
  100. else if (dot < -0.9999f)
  101. {
  102. SetRotation(Quaternion(Vector3::UnitZ, Math::Pi));
  103. }
  104. else
  105. {
  106. // Rotate about axis from cross product
  107. Vector3 axis = Vector3::Cross(Vector3::UnitX, forward);
  108. axis.Normalize();
  109. SetRotation(Quaternion(axis, angle));
  110. }
  111. }
  112. void Actor::AddComponent(Component* component)
  113. {
  114. // Find the insertion point in the sorted vector
  115. // (The first element with a order higher than me)
  116. int myOrder = component->GetUpdateOrder();
  117. auto iter = mComponents.begin();
  118. for (;
  119. iter != mComponents.end();
  120. ++iter)
  121. {
  122. if (myOrder < (*iter)->GetUpdateOrder())
  123. {
  124. break;
  125. }
  126. }
  127. // Inserts element before position of iterator
  128. mComponents.insert(iter, component);
  129. }
  130. void Actor::RemoveComponent(Component* component)
  131. {
  132. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  133. if (iter != mComponents.end())
  134. {
  135. mComponents.erase(iter);
  136. }
  137. }
  138. void Actor::LoadProperties(const rapidjson::Value& inObj)
  139. {
  140. // Use strings for different states
  141. std::string state;
  142. if (JsonHelper::GetString(inObj, "state", state))
  143. {
  144. if (state == "active")
  145. {
  146. SetState(EActive);
  147. }
  148. else if (state == "paused")
  149. {
  150. SetState(EPaused);
  151. }
  152. else if (state == "dead")
  153. {
  154. SetState(EDead);
  155. }
  156. }
  157. // Load position, rotation, and scale, and compute transform
  158. JsonHelper::GetVector3(inObj, "position", mPosition);
  159. JsonHelper::GetQuaternion(inObj, "rotation", mRotation);
  160. JsonHelper::GetFloat(inObj, "scale", mScale);
  161. ComputeWorldTransform();
  162. }
  163. void Actor::SaveProperties(rapidjson::Document::AllocatorType& alloc, rapidjson::Value& inObj) const
  164. {
  165. std::string state = "active";
  166. if (mState == EPaused)
  167. {
  168. state = "paused";
  169. }
  170. else if (mState == EDead)
  171. {
  172. state = "dead";
  173. }
  174. JsonHelper::AddString(alloc, inObj, "state", state);
  175. JsonHelper::AddVector3(alloc, inObj, "position", mPosition);
  176. JsonHelper::AddQuaternion(alloc, inObj, "rotation", mRotation);
  177. JsonHelper::AddFloat(alloc, inObj, "scale", mScale);
  178. }