2
0

Actor.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <cstdint>
  12. class Actor
  13. {
  14. public:
  15. enum State
  16. {
  17. EActive,
  18. EPaused,
  19. EDead
  20. };
  21. Actor(class Game* game);
  22. virtual ~Actor();
  23. // Update function called from Game (not overridable)
  24. void Update(float deltaTime);
  25. // Updates all the components attached to the actor (not overridable)
  26. void UpdateComponents(float deltaTime);
  27. // Any actor-specific update code (overridable)
  28. virtual void UpdateActor(float deltaTime);
  29. // ProcessInput function called from Game (not overridable)
  30. void ProcessInput(const uint8_t* keyState);
  31. // Any actor-specific input code (overridable)
  32. virtual void ActorInput(const uint8_t* keyState);
  33. // Getters/setters
  34. const Vector3& GetPosition() const { return mPosition; }
  35. void SetPosition(const Vector3& pos) { mPosition = pos; mRecomputeWorldTransform = true; }
  36. float GetScale() const { return mScale; }
  37. void SetScale(float scale) { mScale = scale; mRecomputeWorldTransform = true; }
  38. const Quaternion& GetRotation() const { return mRotation; }
  39. void SetRotation(const Quaternion& rotation) { mRotation = rotation; mRecomputeWorldTransform = true; }
  40. void ComputeWorldTransform();
  41. const Matrix4& GetWorldTransform() const { return mWorldTransform; }
  42. Vector3 GetForward() const { return Vector3::Transform(Vector3::UnitX, mRotation); }
  43. Vector3 GetRight() const { return Vector3::Transform(Vector3::UnitY, mRotation); }
  44. State GetState() const { return mState; }
  45. void SetState(State state) { mState = state; }
  46. class Game* GetGame() { return mGame; }
  47. // Add/remove components
  48. void AddComponent(class Component* component);
  49. void RemoveComponent(class Component* component);
  50. private:
  51. // Actor's state
  52. State mState;
  53. // Transform
  54. Matrix4 mWorldTransform;
  55. Vector3 mPosition;
  56. Quaternion mRotation;
  57. float mScale;
  58. bool mRecomputeWorldTransform;
  59. std::vector<class Component*> mComponents;
  60. class Game* mGame;
  61. };