Actor.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(Vector2::Zero)
  15. ,mScale(1.0f)
  16. ,mRotation(0.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::ComputeWorldTransform()
  68. {
  69. if (mRecomputeWorldTransform)
  70. {
  71. mRecomputeWorldTransform = false;
  72. // Scale, then rotate, then translate
  73. mWorldTransform = Matrix4::CreateScale(mScale);
  74. mWorldTransform *= Matrix4::CreateRotationZ(mRotation);
  75. mWorldTransform *= Matrix4::CreateTranslation(Vector3(mPosition.x, mPosition.y, 0.0f));
  76. // Inform components world transform updated
  77. for (auto comp : mComponents)
  78. {
  79. comp->OnUpdateWorldTransform();
  80. }
  81. }
  82. }
  83. void Actor::AddComponent(Component* component)
  84. {
  85. // Find the insertion point in the sorted vector
  86. // (The first element with a order higher than me)
  87. int myOrder = component->GetUpdateOrder();
  88. auto iter = mComponents.begin();
  89. for (;
  90. iter != mComponents.end();
  91. ++iter)
  92. {
  93. if (myOrder < (*iter)->GetUpdateOrder())
  94. {
  95. break;
  96. }
  97. }
  98. // Inserts element before position of iterator
  99. mComponents.insert(iter, component);
  100. }
  101. void Actor::RemoveComponent(Component* component)
  102. {
  103. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  104. if (iter != mComponents.end())
  105. {
  106. mComponents.erase(iter);
  107. }
  108. }