Actor.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <algorithm>
  12. Actor::Actor(Game* game)
  13. :mState(EActive)
  14. ,mPosition(Vector3::Zero)
  15. ,mRotation(Quaternion::Identity)
  16. ,mScale(1.0f)
  17. ,mGame(game)
  18. ,mRecomputeWorldTransform(true)
  19. {
  20. mGame->AddActor(this);
  21. }
  22. Actor::~Actor()
  23. {
  24. mGame->RemoveActor(this);
  25. // Need to delete components
  26. // Because ~Component calls RemoveComponent, need a different style loop
  27. while (!mComponents.empty())
  28. {
  29. delete mComponents.back();
  30. }
  31. }
  32. void Actor::Update(float deltaTime)
  33. {
  34. if (mState == EActive)
  35. {
  36. ComputeWorldTransform();
  37. UpdateComponents(deltaTime);
  38. UpdateActor(deltaTime);
  39. ComputeWorldTransform();
  40. }
  41. }
  42. void Actor::UpdateComponents(float deltaTime)
  43. {
  44. for (auto comp : mComponents)
  45. {
  46. comp->Update(deltaTime);
  47. }
  48. }
  49. void Actor::UpdateActor(float deltaTime)
  50. {
  51. }
  52. void Actor::ProcessInput(const uint8_t* keyState)
  53. {
  54. if (mState == EActive)
  55. {
  56. // First process input for components
  57. for (auto comp : mComponents)
  58. {
  59. comp->ProcessInput(keyState);
  60. }
  61. ActorInput(keyState);
  62. }
  63. }
  64. void Actor::ActorInput(const uint8_t* keyState)
  65. {
  66. }
  67. void Actor::RotateToNewForward(const Vector3& forward)
  68. {
  69. // Figure out difference between original (unit x) and new
  70. float dot = Vector3::Dot(Vector3::UnitX, forward);
  71. float angle = Math::Acos(dot);
  72. // Facing down X
  73. if (dot > 0.9999f)
  74. {
  75. SetRotation(Quaternion::Identity);
  76. }
  77. // Facing down -X
  78. else if (dot < -0.9999f)
  79. {
  80. SetRotation(Quaternion(Vector3::UnitZ, Math::Pi));
  81. }
  82. else
  83. {
  84. // Rotate about axis from cross product
  85. Vector3 axis = Vector3::Cross(Vector3::UnitX, forward);
  86. axis.Normalize();
  87. SetRotation(Quaternion(axis, angle));
  88. }
  89. }
  90. void Actor::ComputeWorldTransform()
  91. {
  92. if (mRecomputeWorldTransform)
  93. {
  94. mRecomputeWorldTransform = false;
  95. // Scale, then rotate, then translate
  96. mWorldTransform = Matrix4::CreateScale(mScale);
  97. mWorldTransform *= Matrix4::CreateFromQuaternion(mRotation);
  98. mWorldTransform *= Matrix4::CreateTranslation(mPosition);
  99. // Inform components world transform updated
  100. for (auto comp : mComponents)
  101. {
  102. comp->OnUpdateWorldTransform();
  103. }
  104. }
  105. }
  106. void Actor::AddComponent(Component* component)
  107. {
  108. // Find the insertion point in the sorted vector
  109. // (The first element with a order higher than me)
  110. int myOrder = component->GetUpdateOrder();
  111. auto iter = mComponents.begin();
  112. for (;
  113. iter != mComponents.end();
  114. ++iter)
  115. {
  116. if (myOrder < (*iter)->GetUpdateOrder())
  117. {
  118. break;
  119. }
  120. }
  121. // Inserts element before position of iterator
  122. mComponents.insert(iter, component);
  123. }
  124. void Actor::RemoveComponent(Component* component)
  125. {
  126. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  127. if (iter != mComponents.end())
  128. {
  129. mComponents.erase(iter);
  130. }
  131. }