MovementController.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. //========================================================================
  3. // File: MovementController.h
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. #include "../GameCode4/interfaces.h"
  40. #include "Geometry.h"
  41. // Forward declarations
  42. class SceneNode;
  43. ////////////////////////////////////////////////////
  44. //
  45. // MovementController Description
  46. //
  47. // Implements a WASD style movement controller
  48. //
  49. // class MovementController - Chapter 10, page 281
  50. //
  51. ////////////////////////////////////////////////////
  52. class MovementController : public IPointerHandler, public IKeyboardHandler
  53. {
  54. protected:
  55. glm::mat4 m_matFromWorld;
  56. glm::mat4 m_matToWorld;
  57. glm::mat4 m_matPosition;
  58. Point m_lastMousePos;
  59. bool m_bKey[256]; // Which keys are up and down
  60. // Orientation Controls
  61. float m_fTargetYaw;
  62. float m_fTargetPitch;
  63. float m_fYaw;
  64. float m_fPitch;
  65. float m_fPitchOnDown;
  66. float m_fYawOnDown;
  67. float m_maxSpeed;
  68. float m_currentSpeed;
  69. // Added for Ch19/20 refactor
  70. bool m_mouseLButtonDown;
  71. bool m_bRotateWhenLButtonDown;
  72. std::shared_ptr<SceneNode> m_object;
  73. public:
  74. MovementController(std::shared_ptr<SceneNode> object, float initialYaw, float initialPitch, bool rotateWhenLButtonDown);
  75. void SetObject(std::shared_ptr<SceneNode> newObject);
  76. void OnUpdate(uint64_t const elapsedMs);
  77. public:
  78. virtual bool VOnPointerMove(const Point &pos, const int radius);
  79. virtual bool VOnPointerButtonDown(const Point &pos, const int radius, const std::string &buttonName);
  80. virtual bool VOnPointerButtonUp(const Point &pos, const int radius, const std::string &buttonName);
  81. virtual bool VOnKeyDown(const unsigned char c) { m_bKey[c] = true; return true; }
  82. virtual bool VOnKeyUp(const unsigned char c) { m_bKey[c] = false; return true; }
  83. const glm::mat4 GetToWorld() { return m_matToWorld; }
  84. const glm::mat4 GetFromWorld() { return m_matFromWorld; }
  85. };