2
0

Actor.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. State GetState() const { return mState; }
  44. void SetState(State state) { mState = state; }
  45. class Game* GetGame() { return mGame; }
  46. // Add/remove components
  47. void AddComponent(class Component* component);
  48. void RemoveComponent(class Component* component);
  49. private:
  50. // Actor's state
  51. State mState;
  52. // Transform
  53. Matrix4 mWorldTransform;
  54. Vector3 mPosition;
  55. Quaternion mRotation;
  56. float mScale;
  57. bool mRecomputeWorldTransform;
  58. std::vector<class Component*> mComponents;
  59. class Game* mGame;
  60. };